1<?php 2 3/** 4 * webtrees: online genealogy 5 * Copyright (C) 2022 webtrees development team 6 * This program is free software: you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation, either version 3 of the License, or 9 * (at your option) any later version. 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * You should have received a copy of the GNU General Public License 15 * along with this program. If not, see <https://www.gnu.org/licenses/>. 16 */ 17 18declare(strict_types=1); 19 20namespace Fisharebest\Webtrees\Module; 21 22use Fisharebest\Webtrees\Auth; 23use Fisharebest\Webtrees\I18N; 24use Fisharebest\Webtrees\Individual; 25use Fisharebest\Webtrees\Services\ModuleService; 26use Fisharebest\Webtrees\Statistics; 27use Fisharebest\Webtrees\Tree; 28use Illuminate\Database\Capsule\Manager as DB; 29use Illuminate\Database\Query\Expression; 30use Illuminate\Support\Str; 31use Psr\Http\Message\ServerRequestInterface; 32 33use function app; 34use function extract; 35use function uksort; 36use function view; 37 38use const EXTR_OVERWRITE; 39 40/** 41 * Class FamilyTreeStatisticsModule 42 */ 43class FamilyTreeStatisticsModule extends AbstractModule implements ModuleBlockInterface 44{ 45 use ModuleBlockTrait; 46 47 /** Show this number of surnames by default */ 48 private const DEFAULT_NUMBER_OF_SURNAMES = '10'; 49 50 private ModuleService $module_service; 51 52 /** 53 * @param ModuleService $module_service 54 */ 55 public function __construct(ModuleService $module_service) 56 { 57 $this->module_service = $module_service; 58 } 59 60 /** 61 * How should this module be identified in the control panel, etc.? 62 * 63 * @return string 64 */ 65 public function title(): string 66 { 67 /* I18N: Name of a module */ 68 return I18N::translate('Statistics'); 69 } 70 71 /** 72 * A sentence describing what this module does. 73 * 74 * @return string 75 */ 76 public function description(): string 77 { 78 /* I18N: Description of “Statistics” module */ 79 return I18N::translate('The size of the family tree, earliest and latest events, common names, etc.'); 80 } 81 82 /** 83 * Generate the HTML content of this block. 84 * 85 * @param Tree $tree 86 * @param int $block_id 87 * @param string $context 88 * @param array<string,string> $config 89 * 90 * @return string 91 */ 92 public function getBlock(Tree $tree, int $block_id, string $context, array $config = []): string 93 { 94 $statistics = app(Statistics::class); 95 96 $show_last_update = $this->getBlockSetting($block_id, 'show_last_update', '1'); 97 $show_common_surnames = $this->getBlockSetting($block_id, 'show_common_surnames', '1'); 98 $number_of_surnames = (int) $this->getBlockSetting($block_id, 'number_of_surnames', self::DEFAULT_NUMBER_OF_SURNAMES); 99 $stat_indi = $this->getBlockSetting($block_id, 'stat_indi', '1'); 100 $stat_fam = $this->getBlockSetting($block_id, 'stat_fam', '1'); 101 $stat_sour = $this->getBlockSetting($block_id, 'stat_sour', '1'); 102 $stat_media = $this->getBlockSetting($block_id, 'stat_media', '1'); 103 $stat_repo = $this->getBlockSetting($block_id, 'stat_repo', '1'); 104 $stat_surname = $this->getBlockSetting($block_id, 'stat_surname', '1'); 105 $stat_events = $this->getBlockSetting($block_id, 'stat_events', '1'); 106 $stat_users = $this->getBlockSetting($block_id, 'stat_users', '1'); 107 $stat_first_birth = $this->getBlockSetting($block_id, 'stat_first_birth', '1'); 108 $stat_last_birth = $this->getBlockSetting($block_id, 'stat_last_birth', '1'); 109 $stat_first_death = $this->getBlockSetting($block_id, 'stat_first_death', '1'); 110 $stat_last_death = $this->getBlockSetting($block_id, 'stat_last_death', '1'); 111 $stat_long_life = $this->getBlockSetting($block_id, 'stat_long_life', '1'); 112 $stat_avg_life = $this->getBlockSetting($block_id, 'stat_avg_life', '1'); 113 $stat_most_chil = $this->getBlockSetting($block_id, 'stat_most_chil', '1'); 114 $stat_avg_chil = $this->getBlockSetting($block_id, 'stat_avg_chil', '1'); 115 116 extract($config, EXTR_OVERWRITE); 117 118 if ($show_common_surnames) { 119 // Use the count of base surnames. 120 $top_surnames = DB::table('name') 121 ->where('n_file', '=', $tree->id()) 122 ->where('n_type', '<>', '_MARNM') 123 ->whereNotIn('n_surn', [Individual::NOMEN_NESCIO, '']) 124 ->groupBy(['n_surn']) 125 ->orderByDesc(new Expression('COUNT(n_surn)')) 126 ->take($number_of_surnames) 127 ->pluck('n_surn'); 128 129 $all_surnames = []; 130 131 foreach ($top_surnames as $top_surname) { 132 $variants = DB::table('name') 133 ->where('n_file', '=', $tree->id()) 134 ->where(new Expression('n_surn /*! COLLATE utf8_bin */'), '=', $top_surname) 135 ->groupBy(['surname']) 136 ->select([new Expression('n_surname /*! COLLATE utf8_bin */ AS surname'), new Expression('count(*) AS total')]) 137 ->pluck('total', 'surname') 138 ->all(); 139 140 $all_surnames[$top_surname] = $variants; 141 } 142 143 uksort($all_surnames, I18N::comparator()); 144 145 // Find a module providing individual lists 146 $module = $this->module_service 147 ->findByComponent(ModuleListInterface::class, $tree, Auth::user()) 148 ->first(static fn (ModuleInterface $module): bool => $module instanceof IndividualListModule); 149 150 $surnames = view('lists/surnames-compact-list', [ 151 'module' => $module, 152 'totals' => false, 153 'surnames' => $all_surnames, 154 'tree' => $tree, 155 ]); 156 } else { 157 $surnames = ''; 158 } 159 160 $content = view('modules/gedcom_stats/statistics', [ 161 'show_last_update' => $show_last_update, 162 'show_common_surnames' => $show_common_surnames, 163 'number_of_surnames' => $number_of_surnames, 164 'stat_indi' => $stat_indi, 165 'stat_fam' => $stat_fam, 166 'stat_sour' => $stat_sour, 167 'stat_media' => $stat_media, 168 'stat_repo' => $stat_repo, 169 'stat_surname' => $stat_surname, 170 'stat_events' => $stat_events, 171 'stat_users' => $stat_users, 172 'stat_first_birth' => $stat_first_birth, 173 'stat_last_birth' => $stat_last_birth, 174 'stat_first_death' => $stat_first_death, 175 'stat_last_death' => $stat_last_death, 176 'stat_long_life' => $stat_long_life, 177 'stat_avg_life' => $stat_avg_life, 178 'stat_most_chil' => $stat_most_chil, 179 'stat_avg_chil' => $stat_avg_chil, 180 'surnames' => $surnames, 181 ]); 182 183 $content = $statistics->embedTags($content); 184 185 if ($context !== self::CONTEXT_EMBED) { 186 return view('modules/block-template', [ 187 'block' => Str::kebab($this->name()), 188 'id' => $block_id, 189 'config_url' => $this->configUrl($tree, $context, $block_id), 190 'title' => $this->title(), 191 'content' => $content, 192 ]); 193 } 194 195 return $content; 196 } 197 198 /** 199 * Should this block load asynchronously using AJAX? 200 * 201 * Simple blocks are faster in-line, more complex ones can be loaded later. 202 * 203 * @return bool 204 */ 205 public function loadAjax(): bool 206 { 207 return true; 208 } 209 210 /** 211 * Can this block be shown on the user’s home page? 212 * 213 * @return bool 214 */ 215 public function isUserBlock(): bool 216 { 217 return true; 218 } 219 220 /** 221 * Can this block be shown on the tree’s home page? 222 * 223 * @return bool 224 */ 225 public function isTreeBlock(): bool 226 { 227 return true; 228 } 229 230 /** 231 * Update the configuration for a block. 232 * 233 * @param ServerRequestInterface $request 234 * @param int $block_id 235 * 236 * @return void 237 */ 238 public function saveBlockConfiguration(ServerRequestInterface $request, int $block_id): void 239 { 240 $params = (array) $request->getParsedBody(); 241 242 $this->setBlockSetting($block_id, 'show_last_update', $params['show_last_update'] ?? ''); 243 $this->setBlockSetting($block_id, 'show_common_surnames', $params['show_common_surnames'] ?? ''); 244 $this->setBlockSetting($block_id, 'number_of_surnames', $params['number_of_surnames']); 245 $this->setBlockSetting($block_id, 'stat_indi', $params['stat_indi'] ?? ''); 246 $this->setBlockSetting($block_id, 'stat_fam', $params['stat_fam'] ?? ''); 247 $this->setBlockSetting($block_id, 'stat_sour', $params['stat_sour'] ?? ''); 248 $this->setBlockSetting($block_id, 'stat_other', $params['stat_other'] ?? ''); 249 $this->setBlockSetting($block_id, 'stat_media', $params['stat_media'] ?? ''); 250 $this->setBlockSetting($block_id, 'stat_repo', $params['stat_repo'] ?? ''); 251 $this->setBlockSetting($block_id, 'stat_surname', $params['stat_surname'] ?? ''); 252 $this->setBlockSetting($block_id, 'stat_events', $params['stat_events'] ?? ''); 253 $this->setBlockSetting($block_id, 'stat_users', $params['stat_users'] ?? ''); 254 $this->setBlockSetting($block_id, 'stat_first_birth', $params['stat_first_birth'] ?? ''); 255 $this->setBlockSetting($block_id, 'stat_last_birth', $params['stat_last_birth'] ?? ''); 256 $this->setBlockSetting($block_id, 'stat_first_death', $params['stat_first_death'] ?? ''); 257 $this->setBlockSetting($block_id, 'stat_last_death', $params['stat_last_death'] ?? ''); 258 $this->setBlockSetting($block_id, 'stat_long_life', $params['stat_long_life'] ?? ''); 259 $this->setBlockSetting($block_id, 'stat_avg_life', $params['stat_avg_life'] ?? ''); 260 $this->setBlockSetting($block_id, 'stat_most_chil', $params['stat_most_chil'] ?? ''); 261 $this->setBlockSetting($block_id, 'stat_avg_chil', $params['stat_avg_chil'] ?? ''); 262 } 263 264 /** 265 * An HTML form to edit block settings 266 * 267 * @param Tree $tree 268 * @param int $block_id 269 * 270 * @return string 271 */ 272 public function editBlockConfiguration(Tree $tree, int $block_id): string 273 { 274 $show_last_update = $this->getBlockSetting($block_id, 'show_last_update', '1'); 275 $show_common_surnames = $this->getBlockSetting($block_id, 'show_common_surnames', '1'); 276 $number_of_surnames = $this->getBlockSetting($block_id, 'number_of_surnames', self::DEFAULT_NUMBER_OF_SURNAMES); 277 $stat_indi = $this->getBlockSetting($block_id, 'stat_indi', '1'); 278 $stat_fam = $this->getBlockSetting($block_id, 'stat_fam', '1'); 279 $stat_sour = $this->getBlockSetting($block_id, 'stat_sour', '1'); 280 $stat_media = $this->getBlockSetting($block_id, 'stat_media', '1'); 281 $stat_repo = $this->getBlockSetting($block_id, 'stat_repo', '1'); 282 $stat_surname = $this->getBlockSetting($block_id, 'stat_surname', '1'); 283 $stat_events = $this->getBlockSetting($block_id, 'stat_events', '1'); 284 $stat_users = $this->getBlockSetting($block_id, 'stat_users', '1'); 285 $stat_first_birth = $this->getBlockSetting($block_id, 'stat_first_birth', '1'); 286 $stat_last_birth = $this->getBlockSetting($block_id, 'stat_last_birth', '1'); 287 $stat_first_death = $this->getBlockSetting($block_id, 'stat_first_death', '1'); 288 $stat_last_death = $this->getBlockSetting($block_id, 'stat_last_death', '1'); 289 $stat_long_life = $this->getBlockSetting($block_id, 'stat_long_life', '1'); 290 $stat_avg_life = $this->getBlockSetting($block_id, 'stat_avg_life', '1'); 291 $stat_most_chil = $this->getBlockSetting($block_id, 'stat_most_chil', '1'); 292 $stat_avg_chil = $this->getBlockSetting($block_id, 'stat_avg_chil', '1'); 293 294 return view('modules/gedcom_stats/config', [ 295 'show_last_update' => $show_last_update, 296 'show_common_surnames' => $show_common_surnames, 297 'number_of_surnames' => $number_of_surnames, 298 'stat_indi' => $stat_indi, 299 'stat_fam' => $stat_fam, 300 'stat_sour' => $stat_sour, 301 'stat_media' => $stat_media, 302 'stat_repo' => $stat_repo, 303 'stat_surname' => $stat_surname, 304 'stat_events' => $stat_events, 305 'stat_users' => $stat_users, 306 'stat_first_birth' => $stat_first_birth, 307 'stat_last_birth' => $stat_last_birth, 308 'stat_first_death' => $stat_first_death, 309 'stat_last_death' => $stat_last_death, 310 'stat_long_life' => $stat_long_life, 311 'stat_avg_life' => $stat_avg_life, 312 'stat_most_chil' => $stat_most_chil, 313 'stat_avg_chil' => $stat_avg_chil, 314 ]); 315 } 316} 317