18c2e8227SGreg Roach<?php 23976b470SGreg Roach 38c2e8227SGreg Roach/** 48c2e8227SGreg Roach * webtrees: online genealogy 589f7189bSGreg Roach * Copyright (C) 2021 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 1589f7189bSGreg Roach * along with this program. If not, see <https://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; 230e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N; 248fb4e87cSGreg Roachuse Fisharebest\Webtrees\Individual; 2567992b6aSRichard Cisseeuse Fisharebest\Webtrees\Services\ModuleService; 269219296aSGreg Roachuse Fisharebest\Webtrees\Statistics; 27e490cd80SGreg Roachuse Fisharebest\Webtrees\Tree; 281ae92905SGreg Roachuse Illuminate\Database\Capsule\Manager as DB; 29a69f5655SGreg Roachuse Illuminate\Database\Query\Expression; 301e7a7a28SGreg Roachuse Illuminate\Support\Str; 316ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface; 328c2e8227SGreg Roach 33cd1ec0d0SGreg Roachuse function app; 34cd1ec0d0SGreg Roachuse function extract; 35cd1ec0d0SGreg Roachuse function uksort; 36cd1ec0d0SGreg Roachuse function view; 37cd1ec0d0SGreg Roach 38cd1ec0d0SGreg Roachuse const EXTR_OVERWRITE; 39cd1ec0d0SGreg Roach 408c2e8227SGreg Roach/** 418c2e8227SGreg Roach * Class FamilyTreeStatisticsModule 428c2e8227SGreg Roach */ 4337eb8894SGreg Roachclass FamilyTreeStatisticsModule extends AbstractModule implements ModuleBlockInterface 44c1010edaSGreg Roach{ 4549a243cbSGreg Roach use ModuleBlockTrait; 4649a243cbSGreg Roach 47f36626dbSGreg Roach /** Show this number of surnames by default */ 4816d6367aSGreg Roach private const DEFAULT_NUMBER_OF_SURNAMES = '10'; 49f36626dbSGreg Roach 50*b55cbc6bSGreg Roach private ModuleService $module_service; 51*b55cbc6bSGreg Roach 52*b55cbc6bSGreg Roach /** 53*b55cbc6bSGreg Roach * @param ModuleService $module_service 54*b55cbc6bSGreg Roach */ 55*b55cbc6bSGreg Roach public function __construct(ModuleService $module_service) 56*b55cbc6bSGreg Roach { 57*b55cbc6bSGreg Roach $this->module_service = $module_service; 58*b55cbc6bSGreg Roach } 59*b55cbc6bSGreg Roach 60961ec755SGreg Roach /** 610cfd6963SGreg Roach * How should this module be identified in the control panel, etc.? 62961ec755SGreg Roach * 63961ec755SGreg Roach * @return string 64961ec755SGreg Roach */ 6549a243cbSGreg Roach public function title(): string 66c1010edaSGreg Roach { 67bbb76c12SGreg Roach /* I18N: Name of a module */ 68bbb76c12SGreg Roach return I18N::translate('Statistics'); 698c2e8227SGreg Roach } 708c2e8227SGreg Roach 71961ec755SGreg Roach /** 72961ec755SGreg Roach * A sentence describing what this module does. 73961ec755SGreg Roach * 74961ec755SGreg Roach * @return string 75961ec755SGreg Roach */ 7649a243cbSGreg Roach public function description(): string 77c1010edaSGreg Roach { 78bbb76c12SGreg Roach /* I18N: Description of “Statistics” module */ 79bbb76c12SGreg Roach return I18N::translate('The size of the family tree, earliest and latest events, common names, etc.'); 808c2e8227SGreg Roach } 818c2e8227SGreg Roach 8276692c8bSGreg Roach /** 8376692c8bSGreg Roach * Generate the HTML content of this block. 8476692c8bSGreg Roach * 85e490cd80SGreg Roach * @param Tree $tree 8676692c8bSGreg Roach * @param int $block_id 873caaa4d2SGreg Roach * @param string $context 8876d39c55SGreg Roach * @param array<string,string> $config 8976692c8bSGreg Roach * 9076692c8bSGreg Roach * @return string 9176692c8bSGreg Roach */ 923caaa4d2SGreg Roach public function getBlock(Tree $tree, int $block_id, string $context, array $config = []): string 93c1010edaSGreg Roach { 94cab242e7SGreg Roach $statistics = app(Statistics::class); 95bf57b580SGreg Roach 96e2a378d3SGreg Roach $show_last_update = $this->getBlockSetting($block_id, 'show_last_update', '1'); 97e2a378d3SGreg Roach $show_common_surnames = $this->getBlockSetting($block_id, 'show_common_surnames', '1'); 9880536c2dSGreg Roach $number_of_surnames = (int) $this->getBlockSetting($block_id, 'number_of_surnames', self::DEFAULT_NUMBER_OF_SURNAMES); 99e2a378d3SGreg Roach $stat_indi = $this->getBlockSetting($block_id, 'stat_indi', '1'); 100e2a378d3SGreg Roach $stat_fam = $this->getBlockSetting($block_id, 'stat_fam', '1'); 101e2a378d3SGreg Roach $stat_sour = $this->getBlockSetting($block_id, 'stat_sour', '1'); 102e2a378d3SGreg Roach $stat_media = $this->getBlockSetting($block_id, 'stat_media', '1'); 103e2a378d3SGreg Roach $stat_repo = $this->getBlockSetting($block_id, 'stat_repo', '1'); 104e2a378d3SGreg Roach $stat_surname = $this->getBlockSetting($block_id, 'stat_surname', '1'); 105e2a378d3SGreg Roach $stat_events = $this->getBlockSetting($block_id, 'stat_events', '1'); 106e2a378d3SGreg Roach $stat_users = $this->getBlockSetting($block_id, 'stat_users', '1'); 107e2a378d3SGreg Roach $stat_first_birth = $this->getBlockSetting($block_id, 'stat_first_birth', '1'); 108e2a378d3SGreg Roach $stat_last_birth = $this->getBlockSetting($block_id, 'stat_last_birth', '1'); 109e2a378d3SGreg Roach $stat_first_death = $this->getBlockSetting($block_id, 'stat_first_death', '1'); 110e2a378d3SGreg Roach $stat_last_death = $this->getBlockSetting($block_id, 'stat_last_death', '1'); 111e2a378d3SGreg Roach $stat_long_life = $this->getBlockSetting($block_id, 'stat_long_life', '1'); 112e2a378d3SGreg Roach $stat_avg_life = $this->getBlockSetting($block_id, 'stat_avg_life', '1'); 113e2a378d3SGreg Roach $stat_most_chil = $this->getBlockSetting($block_id, 'stat_most_chil', '1'); 114e2a378d3SGreg Roach $stat_avg_chil = $this->getBlockSetting($block_id, 'stat_avg_chil', '1'); 1158c2e8227SGreg Roach 1163caaa4d2SGreg Roach extract($config, EXTR_OVERWRITE); 1178c2e8227SGreg Roach 1188c2e8227SGreg Roach if ($show_common_surnames) { 11980536c2dSGreg Roach // Use the count of base surnames. 1201ae92905SGreg Roach $top_surnames = DB::table('name') 1211ae92905SGreg Roach ->where('n_file', '=', $tree->id()) 1221ae92905SGreg Roach ->where('n_type', '<>', '_MARNM') 1238fb4e87cSGreg Roach ->whereNotIn('n_surn', [Individual::NOMEN_NESCIO, '']) 1247f5c2944SGreg Roach ->groupBy(['n_surn']) 125a69f5655SGreg Roach ->orderByDesc(new Expression('COUNT(n_surn)')) 1261ae92905SGreg Roach ->take($number_of_surnames) 1271ae92905SGreg Roach ->pluck('n_surn'); 128e0275e5bSGreg Roach 12913abd6f3SGreg Roach $all_surnames = []; 1301ae92905SGreg Roach 13180536c2dSGreg Roach foreach ($top_surnames as $top_surname) { 1321ae92905SGreg Roach $variants = DB::table('name') 1331ae92905SGreg Roach ->where('n_file', '=', $tree->id()) 134a69f5655SGreg Roach ->where(new Expression('n_surn /*! COLLATE utf8_bin */'), '=', $top_surname) 1357f5c2944SGreg Roach ->groupBy(['surname']) 136a69f5655SGreg Roach ->select([new Expression('n_surname /*! COLLATE utf8_bin */ AS surname'), new Expression('count(*) AS total')]) 1371ae92905SGreg Roach ->pluck('total', 'surname') 1381ae92905SGreg Roach ->all(); 13980536c2dSGreg Roach 14080536c2dSGreg Roach $all_surnames[$top_surname] = $variants; 141f36626dbSGreg Roach } 14280536c2dSGreg Roach 14337646143SGreg Roach uksort($all_surnames, I18N::comparator()); 1441d3c0c1aSGreg Roach 145*b55cbc6bSGreg Roach // Find a module providing individual lists 146*b55cbc6bSGreg Roach $module = $this->module_service 147*b55cbc6bSGreg Roach ->findByComponent(ModuleListInterface::class, $tree, Auth::user()) 148*b55cbc6bSGreg Roach ->first(static fn (ModuleInterface $module): bool => $module instanceof IndividualListModule); 14967992b6aSRichard Cissee 150cd1ec0d0SGreg Roach $surnames = view('lists/surnames-compact-list', [ 151cd1ec0d0SGreg Roach 'module' => $module, 152cd1ec0d0SGreg Roach 'totals' => false, 153cd1ec0d0SGreg Roach 'surnames' => $all_surnames, 154cd1ec0d0SGreg Roach 'tree' => $tree, 155cd1ec0d0SGreg Roach ]); 1561d3c0c1aSGreg Roach } else { 1571d3c0c1aSGreg Roach $surnames = ''; 1588c2e8227SGreg Roach } 1591d3c0c1aSGreg Roach 160147e99aaSGreg Roach $content = view('modules/gedcom_stats/statistics', [ 1611d3c0c1aSGreg Roach 'show_last_update' => $show_last_update, 1621d3c0c1aSGreg Roach 'show_common_surnames' => $show_common_surnames, 1631d3c0c1aSGreg Roach 'number_of_surnames' => $number_of_surnames, 1641d3c0c1aSGreg Roach 'stat_indi' => $stat_indi, 1651d3c0c1aSGreg Roach 'stat_fam' => $stat_fam, 1661d3c0c1aSGreg Roach 'stat_sour' => $stat_sour, 1671d3c0c1aSGreg Roach 'stat_media' => $stat_media, 1681d3c0c1aSGreg Roach 'stat_repo' => $stat_repo, 1691d3c0c1aSGreg Roach 'stat_surname' => $stat_surname, 1701d3c0c1aSGreg Roach 'stat_events' => $stat_events, 1711d3c0c1aSGreg Roach 'stat_users' => $stat_users, 1721d3c0c1aSGreg Roach 'stat_first_birth' => $stat_first_birth, 1731d3c0c1aSGreg Roach 'stat_last_birth' => $stat_last_birth, 1741d3c0c1aSGreg Roach 'stat_first_death' => $stat_first_death, 1751d3c0c1aSGreg Roach 'stat_last_death' => $stat_last_death, 1761d3c0c1aSGreg Roach 'stat_long_life' => $stat_long_life, 1771d3c0c1aSGreg Roach 'stat_avg_life' => $stat_avg_life, 1781d3c0c1aSGreg Roach 'stat_most_chil' => $stat_most_chil, 1791d3c0c1aSGreg Roach 'stat_avg_chil' => $stat_avg_chil, 180bf57b580SGreg Roach 'stats' => $statistics, 1811d3c0c1aSGreg Roach 'surnames' => $surnames, 1821d3c0c1aSGreg Roach ]); 18328941e3cSGreg Roach 1843caaa4d2SGreg Roach if ($context !== self::CONTEXT_EMBED) { 185147e99aaSGreg Roach return view('modules/block-template', [ 1861e7a7a28SGreg Roach 'block' => Str::kebab($this->name()), 1879c6524dcSGreg Roach 'id' => $block_id, 1883caaa4d2SGreg Roach 'config_url' => $this->configUrl($tree, $context, $block_id), 18949a243cbSGreg Roach 'title' => $this->title(), 1909c6524dcSGreg Roach 'content' => $content, 1919c6524dcSGreg Roach ]); 1928c2e8227SGreg Roach } 193b2ce94c6SRico Sonntag 194b2ce94c6SRico Sonntag return $content; 1958c2e8227SGreg Roach } 1968c2e8227SGreg Roach 1973caaa4d2SGreg Roach /** 1983caaa4d2SGreg Roach * Should this block load asynchronously using AJAX? 1993caaa4d2SGreg Roach * 2003caaa4d2SGreg Roach * Simple blocks are faster in-line, more complex ones can be loaded later. 2013caaa4d2SGreg Roach * 2023caaa4d2SGreg Roach * @return bool 2033caaa4d2SGreg Roach */ 204c1010edaSGreg Roach public function loadAjax(): bool 205c1010edaSGreg Roach { 2068c2e8227SGreg Roach return true; 2078c2e8227SGreg Roach } 2088c2e8227SGreg Roach 2093caaa4d2SGreg Roach /** 2103caaa4d2SGreg Roach * Can this block be shown on the user’s home page? 2113caaa4d2SGreg Roach * 2123caaa4d2SGreg Roach * @return bool 2133caaa4d2SGreg Roach */ 214c1010edaSGreg Roach public function isUserBlock(): bool 215c1010edaSGreg Roach { 2168c2e8227SGreg Roach return true; 2178c2e8227SGreg Roach } 2188c2e8227SGreg Roach 2193caaa4d2SGreg Roach /** 2203caaa4d2SGreg Roach * Can this block be shown on the tree’s home page? 2213caaa4d2SGreg Roach * 2223caaa4d2SGreg Roach * @return bool 2233caaa4d2SGreg Roach */ 22463276d8fSGreg Roach public function isTreeBlock(): bool 225c1010edaSGreg Roach { 2268c2e8227SGreg Roach return true; 2278c2e8227SGreg Roach } 2288c2e8227SGreg Roach 22976692c8bSGreg Roach /** 230a45f9889SGreg Roach * Update the configuration for a block. 231a45f9889SGreg Roach * 2326ccdf4f0SGreg Roach * @param ServerRequestInterface $request 233a45f9889SGreg Roach * @param int $block_id 234a45f9889SGreg Roach * 235a45f9889SGreg Roach * @return void 236a45f9889SGreg Roach */ 2376ccdf4f0SGreg Roach public function saveBlockConfiguration(ServerRequestInterface $request, int $block_id): void 238a45f9889SGreg Roach { 239b46c87bdSGreg Roach $params = (array) $request->getParsedBody(); 240ac5f8ed1SGreg Roach 241b46c87bdSGreg Roach $this->setBlockSetting($block_id, 'show_last_update', $params['show_last_update'] ?? ''); 242b46c87bdSGreg Roach $this->setBlockSetting($block_id, 'show_common_surnames', $params['show_common_surnames'] ?? ''); 243b46c87bdSGreg Roach $this->setBlockSetting($block_id, 'number_of_surnames', $params['number_of_surnames']); 244b46c87bdSGreg Roach $this->setBlockSetting($block_id, 'stat_indi', $params['stat_indi'] ?? ''); 245b46c87bdSGreg Roach $this->setBlockSetting($block_id, 'stat_fam', $params['stat_fam'] ?? ''); 246b46c87bdSGreg Roach $this->setBlockSetting($block_id, 'stat_sour', $params['stat_sour'] ?? ''); 247b46c87bdSGreg Roach $this->setBlockSetting($block_id, 'stat_other', $params['stat_other'] ?? ''); 248b46c87bdSGreg Roach $this->setBlockSetting($block_id, 'stat_media', $params['stat_media'] ?? ''); 249b46c87bdSGreg Roach $this->setBlockSetting($block_id, 'stat_repo', $params['stat_repo'] ?? ''); 250b46c87bdSGreg Roach $this->setBlockSetting($block_id, 'stat_surname', $params['stat_surname'] ?? ''); 251b46c87bdSGreg Roach $this->setBlockSetting($block_id, 'stat_events', $params['stat_events'] ?? ''); 252b46c87bdSGreg Roach $this->setBlockSetting($block_id, 'stat_users', $params['stat_users'] ?? ''); 253b46c87bdSGreg Roach $this->setBlockSetting($block_id, 'stat_first_birth', $params['stat_first_birth'] ?? ''); 254b46c87bdSGreg Roach $this->setBlockSetting($block_id, 'stat_last_birth', $params['stat_last_birth'] ?? ''); 255b46c87bdSGreg Roach $this->setBlockSetting($block_id, 'stat_first_death', $params['stat_first_death'] ?? ''); 256b46c87bdSGreg Roach $this->setBlockSetting($block_id, 'stat_last_death', $params['stat_last_death'] ?? ''); 257b46c87bdSGreg Roach $this->setBlockSetting($block_id, 'stat_long_life', $params['stat_long_life'] ?? ''); 258b46c87bdSGreg Roach $this->setBlockSetting($block_id, 'stat_avg_life', $params['stat_avg_life'] ?? ''); 259b46c87bdSGreg Roach $this->setBlockSetting($block_id, 'stat_most_chil', $params['stat_most_chil'] ?? ''); 260b46c87bdSGreg Roach $this->setBlockSetting($block_id, 'stat_avg_chil', $params['stat_avg_chil'] ?? ''); 261a45f9889SGreg Roach } 262a45f9889SGreg Roach 263a45f9889SGreg Roach /** 26476692c8bSGreg Roach * An HTML form to edit block settings 26576692c8bSGreg Roach * 266e490cd80SGreg Roach * @param Tree $tree 26776692c8bSGreg Roach * @param int $block_id 268a9430be8SGreg Roach * 2693caaa4d2SGreg Roach * @return string 27076692c8bSGreg Roach */ 2713caaa4d2SGreg Roach public function editBlockConfiguration(Tree $tree, int $block_id): string 272c1010edaSGreg Roach { 273e2a378d3SGreg Roach $show_last_update = $this->getBlockSetting($block_id, 'show_last_update', '1'); 274e2a378d3SGreg Roach $show_common_surnames = $this->getBlockSetting($block_id, 'show_common_surnames', '1'); 275f36626dbSGreg Roach $number_of_surnames = $this->getBlockSetting($block_id, 'number_of_surnames', self::DEFAULT_NUMBER_OF_SURNAMES); 276e2a378d3SGreg Roach $stat_indi = $this->getBlockSetting($block_id, 'stat_indi', '1'); 277e2a378d3SGreg Roach $stat_fam = $this->getBlockSetting($block_id, 'stat_fam', '1'); 278e2a378d3SGreg Roach $stat_sour = $this->getBlockSetting($block_id, 'stat_sour', '1'); 279e2a378d3SGreg Roach $stat_media = $this->getBlockSetting($block_id, 'stat_media', '1'); 280e2a378d3SGreg Roach $stat_repo = $this->getBlockSetting($block_id, 'stat_repo', '1'); 281e2a378d3SGreg Roach $stat_surname = $this->getBlockSetting($block_id, 'stat_surname', '1'); 282e2a378d3SGreg Roach $stat_events = $this->getBlockSetting($block_id, 'stat_events', '1'); 283e2a378d3SGreg Roach $stat_users = $this->getBlockSetting($block_id, 'stat_users', '1'); 284e2a378d3SGreg Roach $stat_first_birth = $this->getBlockSetting($block_id, 'stat_first_birth', '1'); 285e2a378d3SGreg Roach $stat_last_birth = $this->getBlockSetting($block_id, 'stat_last_birth', '1'); 286e2a378d3SGreg Roach $stat_first_death = $this->getBlockSetting($block_id, 'stat_first_death', '1'); 287e2a378d3SGreg Roach $stat_last_death = $this->getBlockSetting($block_id, 'stat_last_death', '1'); 288e2a378d3SGreg Roach $stat_long_life = $this->getBlockSetting($block_id, 'stat_long_life', '1'); 289e2a378d3SGreg Roach $stat_avg_life = $this->getBlockSetting($block_id, 'stat_avg_life', '1'); 290e2a378d3SGreg Roach $stat_most_chil = $this->getBlockSetting($block_id, 'stat_most_chil', '1'); 291e2a378d3SGreg Roach $stat_avg_chil = $this->getBlockSetting($block_id, 'stat_avg_chil', '1'); 2928c2e8227SGreg Roach 2933caaa4d2SGreg Roach return view('modules/gedcom_stats/config', [ 294c385536dSGreg Roach 'show_last_update' => $show_last_update, 295c385536dSGreg Roach 'show_common_surnames' => $show_common_surnames, 296c385536dSGreg Roach 'number_of_surnames' => $number_of_surnames, 297c385536dSGreg Roach 'stat_indi' => $stat_indi, 298c385536dSGreg Roach 'stat_fam' => $stat_fam, 299c385536dSGreg Roach 'stat_sour' => $stat_sour, 300c385536dSGreg Roach 'stat_media' => $stat_media, 301c385536dSGreg Roach 'stat_repo' => $stat_repo, 302c385536dSGreg Roach 'stat_surname' => $stat_surname, 303c385536dSGreg Roach 'stat_events' => $stat_events, 304c385536dSGreg Roach 'stat_users' => $stat_users, 305c385536dSGreg Roach 'stat_first_birth' => $stat_first_birth, 306c385536dSGreg Roach 'stat_last_birth' => $stat_last_birth, 307c385536dSGreg Roach 'stat_first_death' => $stat_first_death, 308c385536dSGreg Roach 'stat_last_death' => $stat_last_death, 309c385536dSGreg Roach 'stat_long_life' => $stat_long_life, 310c385536dSGreg Roach 'stat_avg_life' => $stat_avg_life, 311c385536dSGreg Roach 'stat_most_chil' => $stat_most_chil, 312c385536dSGreg Roach 'stat_avg_chil' => $stat_avg_chil, 313c385536dSGreg Roach ]); 3148c2e8227SGreg Roach } 3158c2e8227SGreg Roach} 316