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