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