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