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