1<?php 2/** 3 * webtrees: online genealogy 4 * Copyright (C) 2017 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 */ 16namespace Fisharebest\Webtrees\Module; 17 18use Fisharebest\Webtrees\Auth; 19use Fisharebest\Webtrees\Bootstrap4; 20use Fisharebest\Webtrees\Filter; 21use Fisharebest\Webtrees\Functions\FunctionsDb; 22use Fisharebest\Webtrees\Functions\FunctionsPrintLists; 23use Fisharebest\Webtrees\Html; 24use Fisharebest\Webtrees\I18N; 25use Fisharebest\Webtrees\Query\QueryName; 26use Fisharebest\Webtrees\Stats; 27use Fisharebest\Webtrees\View; 28 29/** 30 * Class FamilyTreeStatisticsModule 31 */ 32class FamilyTreeStatisticsModule extends AbstractModule implements ModuleBlockInterface { 33 /** Show this number of surnames by default */ 34 const DEFAULT_NUMBER_OF_SURNAMES = 10; 35 36 /** {@inheritdoc} */ 37 public function getTitle() { 38 return /* I18N: Name of a module */ I18N::translate('Statistics'); 39 } 40 41 /** {@inheritdoc} */ 42 public function getDescription() { 43 return /* I18N: Description of “Statistics” module */ I18N::translate('The size of the family tree, earliest and latest events, common names, etc.'); 44 } 45 46 /** 47 * Generate the HTML content of this block. 48 * 49 * @param int $block_id 50 * @param bool $template 51 * @param string[] $cfg 52 * 53 * @return string 54 */ 55 public function getBlock($block_id, $template = true, $cfg = []) { 56 global $WT_TREE, $ctype; 57 58 $show_last_update = $this->getBlockSetting($block_id, 'show_last_update', '1'); 59 $show_common_surnames = $this->getBlockSetting($block_id, 'show_common_surnames', '1'); 60 $number_of_surnames = $this->getBlockSetting($block_id, 'number_of_surnames', self::DEFAULT_NUMBER_OF_SURNAMES); 61 $stat_indi = $this->getBlockSetting($block_id, 'stat_indi', '1'); 62 $stat_fam = $this->getBlockSetting($block_id, 'stat_fam', '1'); 63 $stat_sour = $this->getBlockSetting($block_id, 'stat_sour', '1'); 64 $stat_media = $this->getBlockSetting($block_id, 'stat_media', '1'); 65 $stat_repo = $this->getBlockSetting($block_id, 'stat_repo', '1'); 66 $stat_surname = $this->getBlockSetting($block_id, 'stat_surname', '1'); 67 $stat_events = $this->getBlockSetting($block_id, 'stat_events', '1'); 68 $stat_users = $this->getBlockSetting($block_id, 'stat_users', '1'); 69 $stat_first_birth = $this->getBlockSetting($block_id, 'stat_first_birth', '1'); 70 $stat_last_birth = $this->getBlockSetting($block_id, 'stat_last_birth', '1'); 71 $stat_first_death = $this->getBlockSetting($block_id, 'stat_first_death', '1'); 72 $stat_last_death = $this->getBlockSetting($block_id, 'stat_last_death', '1'); 73 $stat_long_life = $this->getBlockSetting($block_id, 'stat_long_life', '1'); 74 $stat_avg_life = $this->getBlockSetting($block_id, 'stat_avg_life', '1'); 75 $stat_most_chil = $this->getBlockSetting($block_id, 'stat_most_chil', '1'); 76 $stat_avg_chil = $this->getBlockSetting($block_id, 'stat_avg_chil', '1'); 77 78 foreach (['show_common_surnames', 'number_common_surnames', 'stat_indi', 'stat_fam', 'stat_sour', 'stat_media', 'stat_surname', 'stat_events', 'stat_users', 'stat_first_birth', 'stat_last_birth', 'stat_first_death', 'stat_last_death', 'stat_long_life', 'stat_avg_life', 'stat_most_chil', 'stat_avg_chil'] as $name) { 79 if (array_key_exists($name, $cfg)) { 80 $$name = $cfg[$name]; 81 } 82 } 83 84 $stats = new Stats($WT_TREE); 85 86 $content = ''; 87 88 if ($show_last_update) { 89 $content .= '<p>' . /* I18N: %s is a date */ 90 I18N::translate('This family tree was last updated on %s.', strip_tags($stats->gedcomUpdated())) . '</p>'; 91 } 92 93 /** Responsive Design */ 94 $content .= '<div class="stat-table1">'; 95 if ($stat_indi) { 96 $content .= '<div class="stat-row"><div class="facts_label stat-cell">' . I18N::translate('Individuals') . '</div><div class="facts_value stats_value stat-cell"><a href="' . 'indilist.php?surname_sublist=no&ged=' . $WT_TREE->getNameUrl() . '">' . $stats->totalIndividuals() . '</a></div></div>'; 97 $content .= '<div class="stat-row"><div class="facts_label stat-cell">' . I18N::translate('Males') . '</div><div class="facts_value stats_value stat-cell">' . $stats->totalSexMales() . '<br>' . $stats->totalSexMalesPercentage() . '</div></div>'; 98 $content .= '<div class="stat-row"><div class="facts_label stat-cell">' . I18N::translate('Females') . '</div><div class="facts_value stats_value stat-cell">' . $stats->totalSexFemales() . '<br>' . $stats->totalSexFemalesPercentage() . '</div></div>'; 99 } 100 if ($stat_surname) { 101 $content .= '<div class="stat-row"><div class="facts_label stat-cell">' . I18N::translate('Total surnames') . '</div><div class="facts_value stats_value stat-cell"><a href="indilist.php?show_all=yes&surname_sublist=yes&ged=' . $WT_TREE->getNameUrl() . '">' . $stats->totalSurnames() . '</a></div></div>'; 102 } 103 if ($stat_fam) { 104 $content .= '<div class="stat-row"><div class="facts_label stat-cell">' . I18N::translate('Families') . '</div><div class="facts_value stats_value stat-cell"><a href="famlist.php?ged=' . $WT_TREE->getNameUrl() . '">' . $stats->totalFamilies() . '</a></div></div>'; 105 } 106 if ($stat_sour) { 107 $content .= '<div class="stat-row"><div class="facts_label stat-cell">' . I18N::translate('Sources') . '</div><div class="facts_value stats_value stat-cell"><a href="sourcelist.php?ged=' . $WT_TREE->getNameUrl() . '">' . $stats->totalSources() . '</a></div></div>'; 108 } 109 if ($stat_media) { 110 $content .= '<div class="stat-row"><div class="facts_label stat-cell">' . I18N::translate('Media objects') . '</div><div class="facts_value stats_value stat-cell"><a href="medialist.php?ged=' . $WT_TREE->getNameUrl() . '">' . $stats->totalMedia() . '</a></div></div>'; 111 } 112 if ($stat_repo) { 113 $content .= '<div class="stat-row"><div class="facts_label stat-cell">' . I18N::translate('Repositories') . '</div><div class="facts_value stats_value stat-cell"><a href="repolist.php?ged=' . $WT_TREE->getNameUrl() . '">' . $stats->totalRepositories() . '</a></div></div>'; 114 } 115 if ($stat_events) { 116 $content .= '<div class="stat-row"><div class="facts_label stat-cell">' . I18N::translate('Total events') . '</div><div class="facts_value stats_value stat-cell">' . $stats->totalEvents() . '</div></div>'; 117 } 118 if ($stat_users) { 119 $content .= '<div class="stat-row"><div class="facts_label stat-cell">' . I18N::translate('Total users') . '</div><div class="facts_value stats_value stat-cell">'; 120 if (Auth::isManager($WT_TREE)) { 121 $content .= '<a href="admin_users.php">' . $stats->totalUsers() . '</a>'; 122 } else { 123 $content .= $stats->totalUsers(); 124 } 125 $content .= '</div></div>'; 126 } 127 $content .= '</div><div class="facts_table stat-table2">'; 128 if ($stat_first_birth) { 129 $content .= '<div class="stat-row"><div class="facts_label stat-cell">' . I18N::translate('Earliest birth year') . '</div><div class="facts_value stats_value stat-cell">' . $stats->firstBirthYear() . '</div>'; 130 $content .= '<div class="facts_value stat-cell left">' . $stats->firstBirth() . '</div>'; 131 $content .= '</div>'; 132 } 133 if ($stat_last_birth) { 134 $content .= '<div class="stat-row"><div class="facts_label stat-cell">' . I18N::translate('Latest birth year') . '</div><div class="facts_value stats_value stat-cell">' . $stats->lastBirthYear() . '</div>'; 135 $content .= '<div class="facts_value stat-cell left">' . $stats->lastBirth() . '</div>'; 136 $content .= '</div>'; 137 } 138 if ($stat_first_death) { 139 $content .= '<div class="stat-row"><div class="facts_label stat-cell">' . I18N::translate('Earliest death year') . '</div><div class="facts_value stats_value stat-cell">' . $stats->firstDeathYear() . '</div>'; 140 $content .= '<div class="facts_value stat-cell left">' . $stats->firstDeath() . '</div>'; 141 $content .= '</div>'; 142 } 143 if ($stat_last_death) { 144 $content .= '<div class="stat-row"><div class="facts_label stat-cell">' . I18N::translate('Latest death year') . '</div><div class="facts_value stats_value stat-cell">' . $stats->lastDeathYear() . '</div>'; 145 $content .= '<div class="facts_value stat-cell left">' . $stats->lastDeath() . '</div>'; 146 $content .= '</div>'; 147 } 148 if ($stat_long_life) { 149 $content .= '<div class="stat-row"><div class="facts_label stat-cell">' . I18N::translate('Individual who lived the longest') . '</div><div class="facts_value stats_value stat-cell">' . $stats->longestLifeAge() . '</div>'; 150 $content .= '<div class="facts_value stat-cell left">' . $stats->longestLife() . '</div>'; 151 $content .= '</div>'; 152 } 153 if ($stat_avg_life) { 154 $content .= '<div class="stat-row"><div class="facts_label stat-cell">' . I18N::translate('Average age at death') . '</div><div class="facts_value stats_value stat-cell">' . $stats->averageLifespan() . '</div>'; 155 $content .= '<div class="facts_value stat-cell left">' . I18N::translate('Males') . ': ' . $stats->averageLifespanMale(); 156 $content .= ' ' . I18N::translate('Females') . ': ' . $stats->averageLifespanFemale() . '</div>'; 157 $content .= '</div>'; 158 } 159 160 if ($stat_most_chil) { 161 $content .= '<div class="stat-row"><div class="facts_label stat-cell">' . I18N::translate('Family with the most children') . '</div><div class="facts_value stats_value stat-cell">' . $stats->largestFamilySize() . '</div>'; 162 $content .= '<div class="facts_value stat-cell left">' . $stats->largestFamily() . '</div>'; 163 $content .= '</div>'; 164 } 165 if ($stat_avg_chil) { 166 $content .= '<div class="stat-row"><div class="facts_label stat-cell">' . I18N::translate('Average number of children per family') . '</div><div class="facts_value stats_value stat-cell">' . $stats->averageChildren() . '</div>'; 167 $content .= '<div class="facts_value stat-cell left"></div>'; 168 $content .= '</div>'; 169 } 170 $content .= '</div>'; 171 172 if ($show_common_surnames) { 173 $surnames = FunctionsDb::getTopSurnames($WT_TREE->getTreeId(), 0, (int) $number_of_surnames); 174 175 $all_surnames = []; 176 foreach (array_keys($surnames) as $surname) { 177 $all_surnames = array_merge($all_surnames, QueryName::surnames($WT_TREE, $surname, '', false, false)); 178 } 179 180 if (!empty($surnames)) { 181 ksort($all_surnames); 182 $content .= '<div class="clearfloat">'; 183 $content .= '<p>'; 184 $content .= '<strong>' . I18N::translate('Most common surnames') . '</strong>'; 185 $content .= '<br>'; 186 $content .= '<span class="common_surnames">' . FunctionsPrintLists::surnameList($all_surnames, 2, false, 'indilist.php', $WT_TREE) . '</span>'; 187 $content .= '</p>'; 188 $content .= '</div>'; 189 } 190 } 191 192 if ($template) { 193 if ($ctype === 'gedcom' && Auth::isManager($WT_TREE) || $ctype === 'user' && Auth::check()) { 194 $config_url = Html::url('block_edit.php', ['block_id' => $block_id, 'ged' => $WT_TREE->getName()]); 195 } else { 196 $config_url = ''; 197 } 198 199 return View::make('blocks/template', [ 200 'block' => str_replace('_', '-', $this->getName()), 201 'id' => $block_id, 202 'config_url' => $config_url, 203 'title' => $this->getTitle(), 204 'content' => $content, 205 ]); 206 } else { 207 return $content; 208 } 209 } 210 211 /** {@inheritdoc} */ 212 public function loadAjax() { 213 return true; 214 } 215 216 /** {@inheritdoc} */ 217 public function isUserBlock() { 218 return true; 219 } 220 221 /** {@inheritdoc} */ 222 public function isGedcomBlock() { 223 return true; 224 } 225 226 /** 227 * An HTML form to edit block settings 228 * 229 * @param int $block_id 230 */ 231 public function configureBlock($block_id) { 232 if (Filter::postBool('save') && Filter::checkCsrf()) { 233 $this->setBlockSetting($block_id, 'show_last_update', Filter::postBool('show_last_update')); 234 $this->setBlockSetting($block_id, 'show_common_surnames', Filter::postBool('show_common_surnames')); 235 $this->setBlockSetting($block_id, 'number_of_surnames', Filter::postInteger('number_of_surnames')); 236 $this->setBlockSetting($block_id, 'stat_indi', Filter::postBool('stat_indi')); 237 $this->setBlockSetting($block_id, 'stat_fam', Filter::postBool('stat_fam')); 238 $this->setBlockSetting($block_id, 'stat_sour', Filter::postBool('stat_sour')); 239 $this->setBlockSetting($block_id, 'stat_other', Filter::postBool('stat_other')); 240 $this->setBlockSetting($block_id, 'stat_media', Filter::postBool('stat_media')); 241 $this->setBlockSetting($block_id, 'stat_repo', Filter::postBool('stat_repo')); 242 $this->setBlockSetting($block_id, 'stat_surname', Filter::postBool('stat_surname')); 243 $this->setBlockSetting($block_id, 'stat_events', Filter::postBool('stat_events')); 244 $this->setBlockSetting($block_id, 'stat_users', Filter::postBool('stat_users')); 245 $this->setBlockSetting($block_id, 'stat_first_birth', Filter::postBool('stat_first_birth')); 246 $this->setBlockSetting($block_id, 'stat_last_birth', Filter::postBool('stat_last_birth')); 247 $this->setBlockSetting($block_id, 'stat_first_death', Filter::postBool('stat_first_death')); 248 $this->setBlockSetting($block_id, 'stat_last_death', Filter::postBool('stat_last_death')); 249 $this->setBlockSetting($block_id, 'stat_long_life', Filter::postBool('stat_long_life')); 250 $this->setBlockSetting($block_id, 'stat_avg_life', Filter::postBool('stat_avg_life')); 251 $this->setBlockSetting($block_id, 'stat_most_chil', Filter::postBool('stat_most_chil')); 252 $this->setBlockSetting($block_id, 'stat_avg_chil', Filter::postBool('stat_avg_chil')); 253 } 254 255 $show_last_update = $this->getBlockSetting($block_id, 'show_last_update', '1'); 256 $show_common_surnames = $this->getBlockSetting($block_id, 'show_common_surnames', '1'); 257 $number_of_surnames = $this->getBlockSetting($block_id, 'number_of_surnames', self::DEFAULT_NUMBER_OF_SURNAMES); 258 $stat_indi = $this->getBlockSetting($block_id, 'stat_indi', '1'); 259 $stat_fam = $this->getBlockSetting($block_id, 'stat_fam', '1'); 260 $stat_sour = $this->getBlockSetting($block_id, 'stat_sour', '1'); 261 $stat_media = $this->getBlockSetting($block_id, 'stat_media', '1'); 262 $stat_repo = $this->getBlockSetting($block_id, 'stat_repo', '1'); 263 $stat_surname = $this->getBlockSetting($block_id, 'stat_surname', '1'); 264 $stat_events = $this->getBlockSetting($block_id, 'stat_events', '1'); 265 $stat_users = $this->getBlockSetting($block_id, 'stat_users', '1'); 266 $stat_first_birth = $this->getBlockSetting($block_id, 'stat_first_birth', '1'); 267 $stat_last_birth = $this->getBlockSetting($block_id, 'stat_last_birth', '1'); 268 $stat_first_death = $this->getBlockSetting($block_id, 'stat_first_death', '1'); 269 $stat_last_death = $this->getBlockSetting($block_id, 'stat_last_death', '1'); 270 $stat_long_life = $this->getBlockSetting($block_id, 'stat_long_life', '1'); 271 $stat_avg_life = $this->getBlockSetting($block_id, 'stat_avg_life', '1'); 272 $stat_most_chil = $this->getBlockSetting($block_id, 'stat_most_chil', '1'); 273 $stat_avg_chil = $this->getBlockSetting($block_id, 'stat_avg_chil', '1'); 274 275 ?> 276 <fieldset class="form-group"> 277 <div class="row"> 278 <legend class="col-form-legend col-sm-3"> 279 <?= I18N::translate('Last change') ?> 280 </legend> 281 <div class="col-sm-9"> 282 <?= Bootstrap4::checkbox(/* I18N: label for yes/no option */ I18N::translate('Show date of last update'), false, ['name' => 'show_last_update', 'checked' => (bool) $show_last_update]) ?> 283 </div> 284 </div> 285 </fieldset> 286 287 <fieldset class="form-group"> 288 <div class="row"> 289 <legend class="col-form-legend col-sm-3"> 290 <?= I18N::translate('Statistics') ?> 291 </legend> 292 <div class="col-sm-9"> 293 <?= Bootstrap4::checkbox(I18N::translate('Individuals'), false, ['name' => 'stat_indi', 'checked' => (bool) $stat_indi]) ?> 294 <?= Bootstrap4::checkbox(I18N::translate('Total surnames'), false, ['name' => 'stat_surname', 'checked' => (bool) $stat_surname]) ?> 295 <?= Bootstrap4::checkbox(I18N::translate('Families'), false, ['name' => 'stat_fam', 'checked' => (bool) $stat_fam]) ?> 296 <?= Bootstrap4::checkbox(I18N::translate('Sources'), false, ['name' => 'stat_sour', 'checked' => (bool) $stat_sour]) ?> 297 <?= Bootstrap4::checkbox(I18N::translate('Media objects'), false, ['name' => 'stat_media', 'checked' => (bool) $stat_media]) ?> 298 <?= Bootstrap4::checkbox(I18N::translate('Repositories'), false, ['name' => 'stat_repo', 'checked' => (bool) $stat_repo]) ?> 299 <?= Bootstrap4::checkbox(I18N::translate('Total events'), false, ['name' => 'stat_events', 'checked' => (bool) $stat_events]) ?> 300 <?= Bootstrap4::checkbox(I18N::translate('Total users'), false, ['name' => 'stat_users', 'checked' => (bool) $stat_users]) ?> 301 <?= Bootstrap4::checkbox(I18N::translate('Earliest birth year'), false, ['name' => 'stat_first_birth', 'checked' => (bool) $stat_first_birth]) ?> 302 <?= Bootstrap4::checkbox(I18N::translate('Latest birth year'), false, ['name' => 'stat_last_birth', 'checked' => (bool) $stat_last_birth]) ?> 303 <?= Bootstrap4::checkbox(I18N::translate('Earliest death year'), false, ['name' => 'stat_first_death', 'checked' => (bool) $stat_first_death]) ?> 304 <?= Bootstrap4::checkbox(I18N::translate('Latest death year'), false, ['name' => 'stat_last_death', 'checked' => (bool) $stat_last_death]) ?> 305 <?= Bootstrap4::checkbox(I18N::translate('Individual who lived the longest'), false, ['name' => 'stat_long_life', 'checked' => (bool) $stat_long_life]) ?> 306 <?= Bootstrap4::checkbox(I18N::translate('Average age at death'), false, ['name' => 'stat_avg_life', 'checked' => (bool) $stat_avg_life]) ?> 307 <?= Bootstrap4::checkbox(I18N::translate('Family with the most children'), false, ['name' => 'stat_most_chil', 'checked' => (bool) $stat_most_chil]) ?> 308 <?= Bootstrap4::checkbox(I18N::translate('Average number of children per family'), false, ['name' => 'stat_avg_chil', 'checked' => (bool) $stat_avg_chil]) ?> 309 </div> 310 </div> 311 </fieldset> 312 313 <fieldset class="form-group"> 314 <div class="row"> 315 <legend class="col-form-legend col-sm-3"> 316 <label for="show_common_surnames"> 317 <?= I18N::translate('Surnames') ?> 318 </label> 319 </legend> 320 <div class="col-sm-9"> 321 <?= Bootstrap4::checkbox(I18N::translate('Most common surnames'), false, ['name' => 'show_common_surnames', 'checked' => (bool) $show_common_surnames]) ?> 322 <label for="number_of_surnames"> 323 <?= /* I18N: ... to show in a list */ I18N::translate('Number of surnames') ?> 324 <input 325 class="form-control" 326 id="number_of_surnames" 327 maxlength="5" 328 name="number_of_surnames" 329 pattern="[1-9][0-9]*" 330 required 331 type="text" 332 value="<?= Html::escape($number_of_surnames) ?>" 333 > 334 </label> 335 </div> 336 </div> 337 </fieldset> 338 <?php 339 } 340} 341