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