18c2e8227SGreg Roach<?php 23976b470SGreg Roach 38c2e8227SGreg Roach/** 48c2e8227SGreg Roach * webtrees: online genealogy 5*8fb4e87cSGreg Roach * Copyright (C) 2020 webtrees development team 68c2e8227SGreg Roach * This program is free software: you can redistribute it and/or modify 78c2e8227SGreg Roach * it under the terms of the GNU General Public License as published by 88c2e8227SGreg Roach * the Free Software Foundation, either version 3 of the License, or 98c2e8227SGreg Roach * (at your option) any later version. 108c2e8227SGreg Roach * This program is distributed in the hope that it will be useful, 118c2e8227SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of 128c2e8227SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 138c2e8227SGreg Roach * GNU General Public License for more details. 148c2e8227SGreg Roach * You should have received a copy of the GNU General Public License 158c2e8227SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>. 168c2e8227SGreg Roach */ 17fcfa147eSGreg Roach 18e7f56f2aSGreg Roachdeclare(strict_types=1); 19e7f56f2aSGreg Roach 2076692c8bSGreg Roachnamespace Fisharebest\Webtrees\Module; 2176692c8bSGreg Roach 220e62c4b8SGreg Roachuse Fisharebest\Webtrees\Auth; 23f36626dbSGreg Roachuse Fisharebest\Webtrees\Functions\FunctionsPrintLists; 240e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N; 25*8fb4e87cSGreg Roachuse Fisharebest\Webtrees\Individual; 2667992b6aSRichard Cisseeuse Fisharebest\Webtrees\Services\ModuleService; 279219296aSGreg Roachuse Fisharebest\Webtrees\Statistics; 28e490cd80SGreg Roachuse Fisharebest\Webtrees\Tree; 291ae92905SGreg Roachuse Illuminate\Database\Capsule\Manager as DB; 30a69f5655SGreg Roachuse Illuminate\Database\Query\Expression; 311e7a7a28SGreg Roachuse Illuminate\Support\Str; 326ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface; 338c2e8227SGreg Roach 348c2e8227SGreg Roach/** 358c2e8227SGreg Roach * Class FamilyTreeStatisticsModule 368c2e8227SGreg Roach */ 3737eb8894SGreg Roachclass FamilyTreeStatisticsModule extends AbstractModule implements ModuleBlockInterface 38c1010edaSGreg Roach{ 3949a243cbSGreg Roach use ModuleBlockTrait; 4049a243cbSGreg Roach 41f36626dbSGreg Roach /** Show this number of surnames by default */ 4216d6367aSGreg Roach private const DEFAULT_NUMBER_OF_SURNAMES = '10'; 43f36626dbSGreg Roach 44961ec755SGreg Roach /** 450cfd6963SGreg Roach * How should this module be identified in the control panel, etc.? 46961ec755SGreg Roach * 47961ec755SGreg Roach * @return string 48961ec755SGreg Roach */ 4949a243cbSGreg Roach public function title(): string 50c1010edaSGreg Roach { 51bbb76c12SGreg Roach /* I18N: Name of a module */ 52bbb76c12SGreg Roach return I18N::translate('Statistics'); 538c2e8227SGreg Roach } 548c2e8227SGreg Roach 55961ec755SGreg Roach /** 56961ec755SGreg Roach * A sentence describing what this module does. 57961ec755SGreg Roach * 58961ec755SGreg Roach * @return string 59961ec755SGreg Roach */ 6049a243cbSGreg Roach public function description(): string 61c1010edaSGreg Roach { 62bbb76c12SGreg Roach /* I18N: Description of “Statistics” module */ 63bbb76c12SGreg Roach return I18N::translate('The size of the family tree, earliest and latest events, common names, etc.'); 648c2e8227SGreg Roach } 658c2e8227SGreg Roach 6676692c8bSGreg Roach /** 6776692c8bSGreg Roach * Generate the HTML content of this block. 6876692c8bSGreg Roach * 69e490cd80SGreg Roach * @param Tree $tree 7076692c8bSGreg Roach * @param int $block_id 713caaa4d2SGreg Roach * @param string $context 723caaa4d2SGreg Roach * @param string[] $config 7376692c8bSGreg Roach * 7476692c8bSGreg Roach * @return string 7576692c8bSGreg Roach */ 763caaa4d2SGreg Roach public function getBlock(Tree $tree, int $block_id, string $context, array $config = []): string 77c1010edaSGreg Roach { 78cab242e7SGreg Roach $statistics = app(Statistics::class); 79bf57b580SGreg Roach 80e2a378d3SGreg Roach $show_last_update = $this->getBlockSetting($block_id, 'show_last_update', '1'); 81e2a378d3SGreg Roach $show_common_surnames = $this->getBlockSetting($block_id, 'show_common_surnames', '1'); 8280536c2dSGreg Roach $number_of_surnames = (int) $this->getBlockSetting($block_id, 'number_of_surnames', self::DEFAULT_NUMBER_OF_SURNAMES); 83e2a378d3SGreg Roach $stat_indi = $this->getBlockSetting($block_id, 'stat_indi', '1'); 84e2a378d3SGreg Roach $stat_fam = $this->getBlockSetting($block_id, 'stat_fam', '1'); 85e2a378d3SGreg Roach $stat_sour = $this->getBlockSetting($block_id, 'stat_sour', '1'); 86e2a378d3SGreg Roach $stat_media = $this->getBlockSetting($block_id, 'stat_media', '1'); 87e2a378d3SGreg Roach $stat_repo = $this->getBlockSetting($block_id, 'stat_repo', '1'); 88e2a378d3SGreg Roach $stat_surname = $this->getBlockSetting($block_id, 'stat_surname', '1'); 89e2a378d3SGreg Roach $stat_events = $this->getBlockSetting($block_id, 'stat_events', '1'); 90e2a378d3SGreg Roach $stat_users = $this->getBlockSetting($block_id, 'stat_users', '1'); 91e2a378d3SGreg Roach $stat_first_birth = $this->getBlockSetting($block_id, 'stat_first_birth', '1'); 92e2a378d3SGreg Roach $stat_last_birth = $this->getBlockSetting($block_id, 'stat_last_birth', '1'); 93e2a378d3SGreg Roach $stat_first_death = $this->getBlockSetting($block_id, 'stat_first_death', '1'); 94e2a378d3SGreg Roach $stat_last_death = $this->getBlockSetting($block_id, 'stat_last_death', '1'); 95e2a378d3SGreg Roach $stat_long_life = $this->getBlockSetting($block_id, 'stat_long_life', '1'); 96e2a378d3SGreg Roach $stat_avg_life = $this->getBlockSetting($block_id, 'stat_avg_life', '1'); 97e2a378d3SGreg Roach $stat_most_chil = $this->getBlockSetting($block_id, 'stat_most_chil', '1'); 98e2a378d3SGreg Roach $stat_avg_chil = $this->getBlockSetting($block_id, 'stat_avg_chil', '1'); 998c2e8227SGreg Roach 1003caaa4d2SGreg Roach extract($config, EXTR_OVERWRITE); 1018c2e8227SGreg Roach 1028c2e8227SGreg Roach if ($show_common_surnames) { 10380536c2dSGreg Roach // Use the count of base surnames. 1041ae92905SGreg Roach $top_surnames = DB::table('name') 1051ae92905SGreg Roach ->where('n_file', '=', $tree->id()) 1061ae92905SGreg Roach ->where('n_type', '<>', '_MARNM') 107*8fb4e87cSGreg Roach ->whereNotIn('n_surn', [Individual::NOMEN_NESCIO, '']) 1087f5c2944SGreg Roach ->groupBy(['n_surn']) 109a69f5655SGreg Roach ->orderByDesc(new Expression('COUNT(n_surn)')) 1101ae92905SGreg Roach ->take($number_of_surnames) 1111ae92905SGreg Roach ->pluck('n_surn'); 112e0275e5bSGreg Roach 11313abd6f3SGreg Roach $all_surnames = []; 1141ae92905SGreg Roach 11580536c2dSGreg Roach foreach ($top_surnames as $top_surname) { 1161ae92905SGreg Roach $variants = DB::table('name') 1171ae92905SGreg Roach ->where('n_file', '=', $tree->id()) 118a69f5655SGreg Roach ->where(new Expression('n_surn /*! COLLATE utf8_bin */'), '=', $top_surname) 1197f5c2944SGreg Roach ->groupBy(['surname']) 120a69f5655SGreg Roach ->select([new Expression('n_surname /*! COLLATE utf8_bin */ AS surname'), new Expression('count(*) AS total')]) 1211ae92905SGreg Roach ->pluck('total', 'surname') 1221ae92905SGreg Roach ->all(); 12380536c2dSGreg Roach 12480536c2dSGreg Roach $all_surnames[$top_surname] = $variants; 125f36626dbSGreg Roach } 12680536c2dSGreg Roach 12780536c2dSGreg Roach uksort($all_surnames, [I18N::class, 'strcasecmp']); 1281d3c0c1aSGreg Roach 12967992b6aSRichard Cissee //find a module providing individual lists 1300b5fd0a6SGreg Roach $module = app(ModuleService::class)->findByComponent(ModuleListInterface::class, $tree, Auth::user())->first(static function (ModuleInterface $module) { 13167992b6aSRichard Cissee return $module instanceof IndividualListModule; 13267992b6aSRichard Cissee }); 13367992b6aSRichard Cissee 13467992b6aSRichard Cissee $surnames = FunctionsPrintLists::surnameList($all_surnames, 2, false, $module, $tree); 1351d3c0c1aSGreg Roach } else { 1361d3c0c1aSGreg Roach $surnames = ''; 1378c2e8227SGreg Roach } 1381d3c0c1aSGreg Roach 139147e99aaSGreg Roach $content = view('modules/gedcom_stats/statistics', [ 1401d3c0c1aSGreg Roach 'show_last_update' => $show_last_update, 1411d3c0c1aSGreg Roach 'show_common_surnames' => $show_common_surnames, 1421d3c0c1aSGreg Roach 'number_of_surnames' => $number_of_surnames, 1431d3c0c1aSGreg Roach 'stat_indi' => $stat_indi, 1441d3c0c1aSGreg Roach 'stat_fam' => $stat_fam, 1451d3c0c1aSGreg Roach 'stat_sour' => $stat_sour, 1461d3c0c1aSGreg Roach 'stat_media' => $stat_media, 1471d3c0c1aSGreg Roach 'stat_repo' => $stat_repo, 1481d3c0c1aSGreg Roach 'stat_surname' => $stat_surname, 1491d3c0c1aSGreg Roach 'stat_events' => $stat_events, 1501d3c0c1aSGreg Roach 'stat_users' => $stat_users, 1511d3c0c1aSGreg Roach 'stat_first_birth' => $stat_first_birth, 1521d3c0c1aSGreg Roach 'stat_last_birth' => $stat_last_birth, 1531d3c0c1aSGreg Roach 'stat_first_death' => $stat_first_death, 1541d3c0c1aSGreg Roach 'stat_last_death' => $stat_last_death, 1551d3c0c1aSGreg Roach 'stat_long_life' => $stat_long_life, 1561d3c0c1aSGreg Roach 'stat_avg_life' => $stat_avg_life, 1571d3c0c1aSGreg Roach 'stat_most_chil' => $stat_most_chil, 1581d3c0c1aSGreg Roach 'stat_avg_chil' => $stat_avg_chil, 159bf57b580SGreg Roach 'stats' => $statistics, 1601d3c0c1aSGreg Roach 'surnames' => $surnames, 1611d3c0c1aSGreg Roach ]); 16228941e3cSGreg Roach 1633caaa4d2SGreg Roach if ($context !== self::CONTEXT_EMBED) { 164147e99aaSGreg Roach return view('modules/block-template', [ 1651e7a7a28SGreg Roach 'block' => Str::kebab($this->name()), 1669c6524dcSGreg Roach 'id' => $block_id, 1673caaa4d2SGreg Roach 'config_url' => $this->configUrl($tree, $context, $block_id), 16849a243cbSGreg Roach 'title' => $this->title(), 1699c6524dcSGreg Roach 'content' => $content, 1709c6524dcSGreg Roach ]); 1718c2e8227SGreg Roach } 172b2ce94c6SRico Sonntag 173b2ce94c6SRico Sonntag return $content; 1748c2e8227SGreg Roach } 1758c2e8227SGreg Roach 1763caaa4d2SGreg Roach /** 1773caaa4d2SGreg Roach * Should this block load asynchronously using AJAX? 1783caaa4d2SGreg Roach * 1793caaa4d2SGreg Roach * Simple blocks are faster in-line, more complex ones can be loaded later. 1803caaa4d2SGreg Roach * 1813caaa4d2SGreg Roach * @return bool 1823caaa4d2SGreg Roach */ 183c1010edaSGreg Roach public function loadAjax(): bool 184c1010edaSGreg Roach { 1858c2e8227SGreg Roach return true; 1868c2e8227SGreg Roach } 1878c2e8227SGreg Roach 1883caaa4d2SGreg Roach /** 1893caaa4d2SGreg Roach * Can this block be shown on the user’s home page? 1903caaa4d2SGreg Roach * 1913caaa4d2SGreg Roach * @return bool 1923caaa4d2SGreg Roach */ 193c1010edaSGreg Roach public function isUserBlock(): bool 194c1010edaSGreg Roach { 1958c2e8227SGreg Roach return true; 1968c2e8227SGreg Roach } 1978c2e8227SGreg Roach 1983caaa4d2SGreg Roach /** 1993caaa4d2SGreg Roach * Can this block be shown on the tree’s home page? 2003caaa4d2SGreg Roach * 2013caaa4d2SGreg Roach * @return bool 2023caaa4d2SGreg Roach */ 20363276d8fSGreg Roach public function isTreeBlock(): bool 204c1010edaSGreg Roach { 2058c2e8227SGreg Roach return true; 2068c2e8227SGreg Roach } 2078c2e8227SGreg Roach 20876692c8bSGreg Roach /** 209a45f9889SGreg Roach * Update the configuration for a block. 210a45f9889SGreg Roach * 2116ccdf4f0SGreg Roach * @param ServerRequestInterface $request 212a45f9889SGreg Roach * @param int $block_id 213a45f9889SGreg Roach * 214a45f9889SGreg Roach * @return void 215a45f9889SGreg Roach */ 2166ccdf4f0SGreg Roach public function saveBlockConfiguration(ServerRequestInterface $request, int $block_id): void 217a45f9889SGreg Roach { 218b46c87bdSGreg Roach $params = (array) $request->getParsedBody(); 219ac5f8ed1SGreg Roach 220b46c87bdSGreg Roach $this->setBlockSetting($block_id, 'show_last_update', $params['show_last_update'] ?? ''); 221b46c87bdSGreg Roach $this->setBlockSetting($block_id, 'show_common_surnames', $params['show_common_surnames'] ?? ''); 222b46c87bdSGreg Roach $this->setBlockSetting($block_id, 'number_of_surnames', $params['number_of_surnames']); 223b46c87bdSGreg Roach $this->setBlockSetting($block_id, 'stat_indi', $params['stat_indi'] ?? ''); 224b46c87bdSGreg Roach $this->setBlockSetting($block_id, 'stat_fam', $params['stat_fam'] ?? ''); 225b46c87bdSGreg Roach $this->setBlockSetting($block_id, 'stat_sour', $params['stat_sour'] ?? ''); 226b46c87bdSGreg Roach $this->setBlockSetting($block_id, 'stat_other', $params['stat_other'] ?? ''); 227b46c87bdSGreg Roach $this->setBlockSetting($block_id, 'stat_media', $params['stat_media'] ?? ''); 228b46c87bdSGreg Roach $this->setBlockSetting($block_id, 'stat_repo', $params['stat_repo'] ?? ''); 229b46c87bdSGreg Roach $this->setBlockSetting($block_id, 'stat_surname', $params['stat_surname'] ?? ''); 230b46c87bdSGreg Roach $this->setBlockSetting($block_id, 'stat_events', $params['stat_events'] ?? ''); 231b46c87bdSGreg Roach $this->setBlockSetting($block_id, 'stat_users', $params['stat_users'] ?? ''); 232b46c87bdSGreg Roach $this->setBlockSetting($block_id, 'stat_first_birth', $params['stat_first_birth'] ?? ''); 233b46c87bdSGreg Roach $this->setBlockSetting($block_id, 'stat_last_birth', $params['stat_last_birth'] ?? ''); 234b46c87bdSGreg Roach $this->setBlockSetting($block_id, 'stat_first_death', $params['stat_first_death'] ?? ''); 235b46c87bdSGreg Roach $this->setBlockSetting($block_id, 'stat_last_death', $params['stat_last_death'] ?? ''); 236b46c87bdSGreg Roach $this->setBlockSetting($block_id, 'stat_long_life', $params['stat_long_life'] ?? ''); 237b46c87bdSGreg Roach $this->setBlockSetting($block_id, 'stat_avg_life', $params['stat_avg_life'] ?? ''); 238b46c87bdSGreg Roach $this->setBlockSetting($block_id, 'stat_most_chil', $params['stat_most_chil'] ?? ''); 239b46c87bdSGreg Roach $this->setBlockSetting($block_id, 'stat_avg_chil', $params['stat_avg_chil'] ?? ''); 240a45f9889SGreg Roach } 241a45f9889SGreg Roach 242a45f9889SGreg Roach /** 24376692c8bSGreg Roach * An HTML form to edit block settings 24476692c8bSGreg Roach * 245e490cd80SGreg Roach * @param Tree $tree 24676692c8bSGreg Roach * @param int $block_id 247a9430be8SGreg Roach * 2483caaa4d2SGreg Roach * @return string 24976692c8bSGreg Roach */ 2503caaa4d2SGreg Roach public function editBlockConfiguration(Tree $tree, int $block_id): string 251c1010edaSGreg Roach { 252e2a378d3SGreg Roach $show_last_update = $this->getBlockSetting($block_id, 'show_last_update', '1'); 253e2a378d3SGreg Roach $show_common_surnames = $this->getBlockSetting($block_id, 'show_common_surnames', '1'); 254f36626dbSGreg Roach $number_of_surnames = $this->getBlockSetting($block_id, 'number_of_surnames', self::DEFAULT_NUMBER_OF_SURNAMES); 255e2a378d3SGreg Roach $stat_indi = $this->getBlockSetting($block_id, 'stat_indi', '1'); 256e2a378d3SGreg Roach $stat_fam = $this->getBlockSetting($block_id, 'stat_fam', '1'); 257e2a378d3SGreg Roach $stat_sour = $this->getBlockSetting($block_id, 'stat_sour', '1'); 258e2a378d3SGreg Roach $stat_media = $this->getBlockSetting($block_id, 'stat_media', '1'); 259e2a378d3SGreg Roach $stat_repo = $this->getBlockSetting($block_id, 'stat_repo', '1'); 260e2a378d3SGreg Roach $stat_surname = $this->getBlockSetting($block_id, 'stat_surname', '1'); 261e2a378d3SGreg Roach $stat_events = $this->getBlockSetting($block_id, 'stat_events', '1'); 262e2a378d3SGreg Roach $stat_users = $this->getBlockSetting($block_id, 'stat_users', '1'); 263e2a378d3SGreg Roach $stat_first_birth = $this->getBlockSetting($block_id, 'stat_first_birth', '1'); 264e2a378d3SGreg Roach $stat_last_birth = $this->getBlockSetting($block_id, 'stat_last_birth', '1'); 265e2a378d3SGreg Roach $stat_first_death = $this->getBlockSetting($block_id, 'stat_first_death', '1'); 266e2a378d3SGreg Roach $stat_last_death = $this->getBlockSetting($block_id, 'stat_last_death', '1'); 267e2a378d3SGreg Roach $stat_long_life = $this->getBlockSetting($block_id, 'stat_long_life', '1'); 268e2a378d3SGreg Roach $stat_avg_life = $this->getBlockSetting($block_id, 'stat_avg_life', '1'); 269e2a378d3SGreg Roach $stat_most_chil = $this->getBlockSetting($block_id, 'stat_most_chil', '1'); 270e2a378d3SGreg Roach $stat_avg_chil = $this->getBlockSetting($block_id, 'stat_avg_chil', '1'); 2718c2e8227SGreg Roach 2723caaa4d2SGreg Roach return view('modules/gedcom_stats/config', [ 273c385536dSGreg Roach 'show_last_update' => $show_last_update, 274c385536dSGreg Roach 'show_common_surnames' => $show_common_surnames, 275c385536dSGreg Roach 'number_of_surnames' => $number_of_surnames, 276c385536dSGreg Roach 'stat_indi' => $stat_indi, 277c385536dSGreg Roach 'stat_fam' => $stat_fam, 278c385536dSGreg Roach 'stat_sour' => $stat_sour, 279c385536dSGreg Roach 'stat_media' => $stat_media, 280c385536dSGreg Roach 'stat_repo' => $stat_repo, 281c385536dSGreg Roach 'stat_surname' => $stat_surname, 282c385536dSGreg Roach 'stat_events' => $stat_events, 283c385536dSGreg Roach 'stat_users' => $stat_users, 284c385536dSGreg Roach 'stat_first_birth' => $stat_first_birth, 285c385536dSGreg Roach 'stat_last_birth' => $stat_last_birth, 286c385536dSGreg Roach 'stat_first_death' => $stat_first_death, 287c385536dSGreg Roach 'stat_last_death' => $stat_last_death, 288c385536dSGreg Roach 'stat_long_life' => $stat_long_life, 289c385536dSGreg Roach 'stat_avg_life' => $stat_avg_life, 290c385536dSGreg Roach 'stat_most_chil' => $stat_most_chil, 291c385536dSGreg Roach 'stat_avg_chil' => $stat_avg_chil, 292c385536dSGreg Roach ]); 2938c2e8227SGreg Roach } 2948c2e8227SGreg Roach} 295