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