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