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 33*cd1ec0d0SGreg Roachuse function app; 34*cd1ec0d0SGreg Roachuse function extract; 35*cd1ec0d0SGreg Roachuse function uksort; 36*cd1ec0d0SGreg Roachuse function view; 37*cd1ec0d0SGreg Roach 38*cd1ec0d0SGreg Roachuse const EXTR_OVERWRITE; 39*cd1ec0d0SGreg 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 50961ec755SGreg Roach /** 510cfd6963SGreg Roach * How should this module be identified in the control panel, etc.? 52961ec755SGreg Roach * 53961ec755SGreg Roach * @return string 54961ec755SGreg Roach */ 5549a243cbSGreg Roach public function title(): string 56c1010edaSGreg Roach { 57bbb76c12SGreg Roach /* I18N: Name of a module */ 58bbb76c12SGreg Roach return I18N::translate('Statistics'); 598c2e8227SGreg Roach } 608c2e8227SGreg Roach 61961ec755SGreg Roach /** 62961ec755SGreg Roach * A sentence describing what this module does. 63961ec755SGreg Roach * 64961ec755SGreg Roach * @return string 65961ec755SGreg Roach */ 6649a243cbSGreg Roach public function description(): string 67c1010edaSGreg Roach { 68bbb76c12SGreg Roach /* I18N: Description of “Statistics” module */ 69bbb76c12SGreg Roach return I18N::translate('The size of the family tree, earliest and latest events, common names, etc.'); 708c2e8227SGreg Roach } 718c2e8227SGreg Roach 7276692c8bSGreg Roach /** 7376692c8bSGreg Roach * Generate the HTML content of this block. 7476692c8bSGreg Roach * 75e490cd80SGreg Roach * @param Tree $tree 7676692c8bSGreg Roach * @param int $block_id 773caaa4d2SGreg Roach * @param string $context 7809482a55SGreg Roach * @param array<string> $config 7976692c8bSGreg Roach * 8076692c8bSGreg Roach * @return string 8176692c8bSGreg Roach */ 823caaa4d2SGreg Roach public function getBlock(Tree $tree, int $block_id, string $context, array $config = []): string 83c1010edaSGreg Roach { 84cab242e7SGreg Roach $statistics = app(Statistics::class); 85bf57b580SGreg Roach 86e2a378d3SGreg Roach $show_last_update = $this->getBlockSetting($block_id, 'show_last_update', '1'); 87e2a378d3SGreg Roach $show_common_surnames = $this->getBlockSetting($block_id, 'show_common_surnames', '1'); 8880536c2dSGreg Roach $number_of_surnames = (int) $this->getBlockSetting($block_id, 'number_of_surnames', self::DEFAULT_NUMBER_OF_SURNAMES); 89e2a378d3SGreg Roach $stat_indi = $this->getBlockSetting($block_id, 'stat_indi', '1'); 90e2a378d3SGreg Roach $stat_fam = $this->getBlockSetting($block_id, 'stat_fam', '1'); 91e2a378d3SGreg Roach $stat_sour = $this->getBlockSetting($block_id, 'stat_sour', '1'); 92e2a378d3SGreg Roach $stat_media = $this->getBlockSetting($block_id, 'stat_media', '1'); 93e2a378d3SGreg Roach $stat_repo = $this->getBlockSetting($block_id, 'stat_repo', '1'); 94e2a378d3SGreg Roach $stat_surname = $this->getBlockSetting($block_id, 'stat_surname', '1'); 95e2a378d3SGreg Roach $stat_events = $this->getBlockSetting($block_id, 'stat_events', '1'); 96e2a378d3SGreg Roach $stat_users = $this->getBlockSetting($block_id, 'stat_users', '1'); 97e2a378d3SGreg Roach $stat_first_birth = $this->getBlockSetting($block_id, 'stat_first_birth', '1'); 98e2a378d3SGreg Roach $stat_last_birth = $this->getBlockSetting($block_id, 'stat_last_birth', '1'); 99e2a378d3SGreg Roach $stat_first_death = $this->getBlockSetting($block_id, 'stat_first_death', '1'); 100e2a378d3SGreg Roach $stat_last_death = $this->getBlockSetting($block_id, 'stat_last_death', '1'); 101e2a378d3SGreg Roach $stat_long_life = $this->getBlockSetting($block_id, 'stat_long_life', '1'); 102e2a378d3SGreg Roach $stat_avg_life = $this->getBlockSetting($block_id, 'stat_avg_life', '1'); 103e2a378d3SGreg Roach $stat_most_chil = $this->getBlockSetting($block_id, 'stat_most_chil', '1'); 104e2a378d3SGreg Roach $stat_avg_chil = $this->getBlockSetting($block_id, 'stat_avg_chil', '1'); 1058c2e8227SGreg Roach 1063caaa4d2SGreg Roach extract($config, EXTR_OVERWRITE); 1078c2e8227SGreg Roach 1088c2e8227SGreg Roach if ($show_common_surnames) { 10980536c2dSGreg Roach // Use the count of base surnames. 1101ae92905SGreg Roach $top_surnames = DB::table('name') 1111ae92905SGreg Roach ->where('n_file', '=', $tree->id()) 1121ae92905SGreg Roach ->where('n_type', '<>', '_MARNM') 1138fb4e87cSGreg Roach ->whereNotIn('n_surn', [Individual::NOMEN_NESCIO, '']) 1147f5c2944SGreg Roach ->groupBy(['n_surn']) 115a69f5655SGreg Roach ->orderByDesc(new Expression('COUNT(n_surn)')) 1161ae92905SGreg Roach ->take($number_of_surnames) 1171ae92905SGreg Roach ->pluck('n_surn'); 118e0275e5bSGreg Roach 11913abd6f3SGreg Roach $all_surnames = []; 1201ae92905SGreg Roach 12180536c2dSGreg Roach foreach ($top_surnames as $top_surname) { 1221ae92905SGreg Roach $variants = DB::table('name') 1231ae92905SGreg Roach ->where('n_file', '=', $tree->id()) 124a69f5655SGreg Roach ->where(new Expression('n_surn /*! COLLATE utf8_bin */'), '=', $top_surname) 1257f5c2944SGreg Roach ->groupBy(['surname']) 126a69f5655SGreg Roach ->select([new Expression('n_surname /*! COLLATE utf8_bin */ AS surname'), new Expression('count(*) AS total')]) 1271ae92905SGreg Roach ->pluck('total', 'surname') 1281ae92905SGreg Roach ->all(); 12980536c2dSGreg Roach 13080536c2dSGreg Roach $all_surnames[$top_surname] = $variants; 131f36626dbSGreg Roach } 13280536c2dSGreg Roach 13337646143SGreg Roach uksort($all_surnames, I18N::comparator()); 1341d3c0c1aSGreg Roach 13567992b6aSRichard Cissee //find a module providing individual lists 1360b5fd0a6SGreg Roach $module = app(ModuleService::class)->findByComponent(ModuleListInterface::class, $tree, Auth::user())->first(static function (ModuleInterface $module) { 13767992b6aSRichard Cissee return $module instanceof IndividualListModule; 13867992b6aSRichard Cissee }); 13967992b6aSRichard Cissee 140*cd1ec0d0SGreg Roach $surnames = view('lists/surnames-compact-list', [ 141*cd1ec0d0SGreg Roach 'module' => $module, 142*cd1ec0d0SGreg Roach 'totals' => false, 143*cd1ec0d0SGreg Roach 'surnames' => $all_surnames, 144*cd1ec0d0SGreg Roach 'tree' => $tree, 145*cd1ec0d0SGreg Roach ]); 1461d3c0c1aSGreg Roach } else { 1471d3c0c1aSGreg Roach $surnames = ''; 1488c2e8227SGreg Roach } 1491d3c0c1aSGreg Roach 150147e99aaSGreg Roach $content = view('modules/gedcom_stats/statistics', [ 1511d3c0c1aSGreg Roach 'show_last_update' => $show_last_update, 1521d3c0c1aSGreg Roach 'show_common_surnames' => $show_common_surnames, 1531d3c0c1aSGreg Roach 'number_of_surnames' => $number_of_surnames, 1541d3c0c1aSGreg Roach 'stat_indi' => $stat_indi, 1551d3c0c1aSGreg Roach 'stat_fam' => $stat_fam, 1561d3c0c1aSGreg Roach 'stat_sour' => $stat_sour, 1571d3c0c1aSGreg Roach 'stat_media' => $stat_media, 1581d3c0c1aSGreg Roach 'stat_repo' => $stat_repo, 1591d3c0c1aSGreg Roach 'stat_surname' => $stat_surname, 1601d3c0c1aSGreg Roach 'stat_events' => $stat_events, 1611d3c0c1aSGreg Roach 'stat_users' => $stat_users, 1621d3c0c1aSGreg Roach 'stat_first_birth' => $stat_first_birth, 1631d3c0c1aSGreg Roach 'stat_last_birth' => $stat_last_birth, 1641d3c0c1aSGreg Roach 'stat_first_death' => $stat_first_death, 1651d3c0c1aSGreg Roach 'stat_last_death' => $stat_last_death, 1661d3c0c1aSGreg Roach 'stat_long_life' => $stat_long_life, 1671d3c0c1aSGreg Roach 'stat_avg_life' => $stat_avg_life, 1681d3c0c1aSGreg Roach 'stat_most_chil' => $stat_most_chil, 1691d3c0c1aSGreg Roach 'stat_avg_chil' => $stat_avg_chil, 170bf57b580SGreg Roach 'stats' => $statistics, 1711d3c0c1aSGreg Roach 'surnames' => $surnames, 1721d3c0c1aSGreg Roach ]); 17328941e3cSGreg Roach 1743caaa4d2SGreg Roach if ($context !== self::CONTEXT_EMBED) { 175147e99aaSGreg Roach return view('modules/block-template', [ 1761e7a7a28SGreg Roach 'block' => Str::kebab($this->name()), 1779c6524dcSGreg Roach 'id' => $block_id, 1783caaa4d2SGreg Roach 'config_url' => $this->configUrl($tree, $context, $block_id), 17949a243cbSGreg Roach 'title' => $this->title(), 1809c6524dcSGreg Roach 'content' => $content, 1819c6524dcSGreg Roach ]); 1828c2e8227SGreg Roach } 183b2ce94c6SRico Sonntag 184b2ce94c6SRico Sonntag return $content; 1858c2e8227SGreg Roach } 1868c2e8227SGreg Roach 1873caaa4d2SGreg Roach /** 1883caaa4d2SGreg Roach * Should this block load asynchronously using AJAX? 1893caaa4d2SGreg Roach * 1903caaa4d2SGreg Roach * Simple blocks are faster in-line, more complex ones can be loaded later. 1913caaa4d2SGreg Roach * 1923caaa4d2SGreg Roach * @return bool 1933caaa4d2SGreg Roach */ 194c1010edaSGreg Roach public function loadAjax(): bool 195c1010edaSGreg Roach { 1968c2e8227SGreg Roach return true; 1978c2e8227SGreg Roach } 1988c2e8227SGreg Roach 1993caaa4d2SGreg Roach /** 2003caaa4d2SGreg Roach * Can this block be shown on the user’s home page? 2013caaa4d2SGreg Roach * 2023caaa4d2SGreg Roach * @return bool 2033caaa4d2SGreg Roach */ 204c1010edaSGreg Roach public function isUserBlock(): bool 205c1010edaSGreg Roach { 2068c2e8227SGreg Roach return true; 2078c2e8227SGreg Roach } 2088c2e8227SGreg Roach 2093caaa4d2SGreg Roach /** 2103caaa4d2SGreg Roach * Can this block be shown on the tree’s home page? 2113caaa4d2SGreg Roach * 2123caaa4d2SGreg Roach * @return bool 2133caaa4d2SGreg Roach */ 21463276d8fSGreg Roach public function isTreeBlock(): bool 215c1010edaSGreg Roach { 2168c2e8227SGreg Roach return true; 2178c2e8227SGreg Roach } 2188c2e8227SGreg Roach 21976692c8bSGreg Roach /** 220a45f9889SGreg Roach * Update the configuration for a block. 221a45f9889SGreg Roach * 2226ccdf4f0SGreg Roach * @param ServerRequestInterface $request 223a45f9889SGreg Roach * @param int $block_id 224a45f9889SGreg Roach * 225a45f9889SGreg Roach * @return void 226a45f9889SGreg Roach */ 2276ccdf4f0SGreg Roach public function saveBlockConfiguration(ServerRequestInterface $request, int $block_id): void 228a45f9889SGreg Roach { 229b46c87bdSGreg Roach $params = (array) $request->getParsedBody(); 230ac5f8ed1SGreg Roach 231b46c87bdSGreg Roach $this->setBlockSetting($block_id, 'show_last_update', $params['show_last_update'] ?? ''); 232b46c87bdSGreg Roach $this->setBlockSetting($block_id, 'show_common_surnames', $params['show_common_surnames'] ?? ''); 233b46c87bdSGreg Roach $this->setBlockSetting($block_id, 'number_of_surnames', $params['number_of_surnames']); 234b46c87bdSGreg Roach $this->setBlockSetting($block_id, 'stat_indi', $params['stat_indi'] ?? ''); 235b46c87bdSGreg Roach $this->setBlockSetting($block_id, 'stat_fam', $params['stat_fam'] ?? ''); 236b46c87bdSGreg Roach $this->setBlockSetting($block_id, 'stat_sour', $params['stat_sour'] ?? ''); 237b46c87bdSGreg Roach $this->setBlockSetting($block_id, 'stat_other', $params['stat_other'] ?? ''); 238b46c87bdSGreg Roach $this->setBlockSetting($block_id, 'stat_media', $params['stat_media'] ?? ''); 239b46c87bdSGreg Roach $this->setBlockSetting($block_id, 'stat_repo', $params['stat_repo'] ?? ''); 240b46c87bdSGreg Roach $this->setBlockSetting($block_id, 'stat_surname', $params['stat_surname'] ?? ''); 241b46c87bdSGreg Roach $this->setBlockSetting($block_id, 'stat_events', $params['stat_events'] ?? ''); 242b46c87bdSGreg Roach $this->setBlockSetting($block_id, 'stat_users', $params['stat_users'] ?? ''); 243b46c87bdSGreg Roach $this->setBlockSetting($block_id, 'stat_first_birth', $params['stat_first_birth'] ?? ''); 244b46c87bdSGreg Roach $this->setBlockSetting($block_id, 'stat_last_birth', $params['stat_last_birth'] ?? ''); 245b46c87bdSGreg Roach $this->setBlockSetting($block_id, 'stat_first_death', $params['stat_first_death'] ?? ''); 246b46c87bdSGreg Roach $this->setBlockSetting($block_id, 'stat_last_death', $params['stat_last_death'] ?? ''); 247b46c87bdSGreg Roach $this->setBlockSetting($block_id, 'stat_long_life', $params['stat_long_life'] ?? ''); 248b46c87bdSGreg Roach $this->setBlockSetting($block_id, 'stat_avg_life', $params['stat_avg_life'] ?? ''); 249b46c87bdSGreg Roach $this->setBlockSetting($block_id, 'stat_most_chil', $params['stat_most_chil'] ?? ''); 250b46c87bdSGreg Roach $this->setBlockSetting($block_id, 'stat_avg_chil', $params['stat_avg_chil'] ?? ''); 251a45f9889SGreg Roach } 252a45f9889SGreg Roach 253a45f9889SGreg Roach /** 25476692c8bSGreg Roach * An HTML form to edit block settings 25576692c8bSGreg Roach * 256e490cd80SGreg Roach * @param Tree $tree 25776692c8bSGreg Roach * @param int $block_id 258a9430be8SGreg Roach * 2593caaa4d2SGreg Roach * @return string 26076692c8bSGreg Roach */ 2613caaa4d2SGreg Roach public function editBlockConfiguration(Tree $tree, int $block_id): string 262c1010edaSGreg Roach { 263e2a378d3SGreg Roach $show_last_update = $this->getBlockSetting($block_id, 'show_last_update', '1'); 264e2a378d3SGreg Roach $show_common_surnames = $this->getBlockSetting($block_id, 'show_common_surnames', '1'); 265f36626dbSGreg Roach $number_of_surnames = $this->getBlockSetting($block_id, 'number_of_surnames', self::DEFAULT_NUMBER_OF_SURNAMES); 266e2a378d3SGreg Roach $stat_indi = $this->getBlockSetting($block_id, 'stat_indi', '1'); 267e2a378d3SGreg Roach $stat_fam = $this->getBlockSetting($block_id, 'stat_fam', '1'); 268e2a378d3SGreg Roach $stat_sour = $this->getBlockSetting($block_id, 'stat_sour', '1'); 269e2a378d3SGreg Roach $stat_media = $this->getBlockSetting($block_id, 'stat_media', '1'); 270e2a378d3SGreg Roach $stat_repo = $this->getBlockSetting($block_id, 'stat_repo', '1'); 271e2a378d3SGreg Roach $stat_surname = $this->getBlockSetting($block_id, 'stat_surname', '1'); 272e2a378d3SGreg Roach $stat_events = $this->getBlockSetting($block_id, 'stat_events', '1'); 273e2a378d3SGreg Roach $stat_users = $this->getBlockSetting($block_id, 'stat_users', '1'); 274e2a378d3SGreg Roach $stat_first_birth = $this->getBlockSetting($block_id, 'stat_first_birth', '1'); 275e2a378d3SGreg Roach $stat_last_birth = $this->getBlockSetting($block_id, 'stat_last_birth', '1'); 276e2a378d3SGreg Roach $stat_first_death = $this->getBlockSetting($block_id, 'stat_first_death', '1'); 277e2a378d3SGreg Roach $stat_last_death = $this->getBlockSetting($block_id, 'stat_last_death', '1'); 278e2a378d3SGreg Roach $stat_long_life = $this->getBlockSetting($block_id, 'stat_long_life', '1'); 279e2a378d3SGreg Roach $stat_avg_life = $this->getBlockSetting($block_id, 'stat_avg_life', '1'); 280e2a378d3SGreg Roach $stat_most_chil = $this->getBlockSetting($block_id, 'stat_most_chil', '1'); 281e2a378d3SGreg Roach $stat_avg_chil = $this->getBlockSetting($block_id, 'stat_avg_chil', '1'); 2828c2e8227SGreg Roach 2833caaa4d2SGreg Roach return view('modules/gedcom_stats/config', [ 284c385536dSGreg Roach 'show_last_update' => $show_last_update, 285c385536dSGreg Roach 'show_common_surnames' => $show_common_surnames, 286c385536dSGreg Roach 'number_of_surnames' => $number_of_surnames, 287c385536dSGreg Roach 'stat_indi' => $stat_indi, 288c385536dSGreg Roach 'stat_fam' => $stat_fam, 289c385536dSGreg Roach 'stat_sour' => $stat_sour, 290c385536dSGreg Roach 'stat_media' => $stat_media, 291c385536dSGreg Roach 'stat_repo' => $stat_repo, 292c385536dSGreg Roach 'stat_surname' => $stat_surname, 293c385536dSGreg Roach 'stat_events' => $stat_events, 294c385536dSGreg Roach 'stat_users' => $stat_users, 295c385536dSGreg Roach 'stat_first_birth' => $stat_first_birth, 296c385536dSGreg Roach 'stat_last_birth' => $stat_last_birth, 297c385536dSGreg Roach 'stat_first_death' => $stat_first_death, 298c385536dSGreg Roach 'stat_last_death' => $stat_last_death, 299c385536dSGreg Roach 'stat_long_life' => $stat_long_life, 300c385536dSGreg Roach 'stat_avg_life' => $stat_avg_life, 301c385536dSGreg Roach 'stat_most_chil' => $stat_most_chil, 302c385536dSGreg Roach 'stat_avg_chil' => $stat_avg_chil, 303c385536dSGreg Roach ]); 3048c2e8227SGreg Roach } 3058c2e8227SGreg Roach} 306