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