xref: /webtrees/app/Module/FamilyTreeStatisticsModule.php (revision a9430be8fd65a5fa4bf77ffdf439873a82f51e13)
18c2e8227SGreg Roach<?php
28c2e8227SGreg Roach/**
38c2e8227SGreg Roach * webtrees: online genealogy
46bdf7674SGreg Roach * Copyright (C) 2017 webtrees development team
58c2e8227SGreg Roach * This program is free software: you can redistribute it and/or modify
68c2e8227SGreg Roach * it under the terms of the GNU General Public License as published by
78c2e8227SGreg Roach * the Free Software Foundation, either version 3 of the License, or
88c2e8227SGreg Roach * (at your option) any later version.
98c2e8227SGreg Roach * This program is distributed in the hope that it will be useful,
108c2e8227SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
118c2e8227SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
128c2e8227SGreg Roach * GNU General Public License for more details.
138c2e8227SGreg Roach * You should have received a copy of the GNU General Public License
148c2e8227SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
158c2e8227SGreg Roach */
1676692c8bSGreg Roachnamespace Fisharebest\Webtrees\Module;
1776692c8bSGreg Roach
180e62c4b8SGreg Roachuse Fisharebest\Webtrees\Auth;
1915d603e7SGreg Roachuse Fisharebest\Webtrees\Bootstrap4;
200e62c4b8SGreg Roachuse Fisharebest\Webtrees\Filter;
213d7a8a4cSGreg Roachuse Fisharebest\Webtrees\Functions\FunctionsDb;
22f36626dbSGreg Roachuse Fisharebest\Webtrees\Functions\FunctionsPrintLists;
23047f239bSGreg Roachuse Fisharebest\Webtrees\Html;
240e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N;
25f36626dbSGreg Roachuse Fisharebest\Webtrees\Query\QueryName;
260e62c4b8SGreg Roachuse Fisharebest\Webtrees\Stats;
279c6524dcSGreg Roachuse Fisharebest\Webtrees\View;
288c2e8227SGreg Roach
298c2e8227SGreg Roach/**
308c2e8227SGreg Roach * Class FamilyTreeStatisticsModule
318c2e8227SGreg Roach */
32e2a378d3SGreg Roachclass FamilyTreeStatisticsModule extends AbstractModule implements ModuleBlockInterface {
33f36626dbSGreg Roach	/** Show this number of surnames by default */
34f36626dbSGreg Roach	const DEFAULT_NUMBER_OF_SURNAMES = 10;
35f36626dbSGreg Roach
368c2e8227SGreg Roach	/** {@inheritdoc} */
378c2e8227SGreg Roach	public function getTitle() {
381d3c0c1aSGreg Roach		return /* I18N: Name of a module */
391d3c0c1aSGreg Roach			I18N::translate('Statistics');
408c2e8227SGreg Roach	}
418c2e8227SGreg Roach
428c2e8227SGreg Roach	/** {@inheritdoc} */
438c2e8227SGreg Roach	public function getDescription() {
441d3c0c1aSGreg Roach		return /* I18N: Description of “Statistics” module */
451d3c0c1aSGreg Roach			I18N::translate('The size of the family tree, earliest and latest events, common names, etc.');
468c2e8227SGreg Roach	}
478c2e8227SGreg Roach
4876692c8bSGreg Roach	/**
4976692c8bSGreg Roach	 * Generate the HTML content of this block.
5076692c8bSGreg Roach	 *
5176692c8bSGreg Roach	 * @param int      $block_id
5276692c8bSGreg Roach	 * @param bool     $template
53727f238cSGreg Roach	 * @param string[] $cfg
5476692c8bSGreg Roach	 *
5576692c8bSGreg Roach	 * @return string
5676692c8bSGreg Roach	 */
57*a9430be8SGreg Roach	public function getBlock($block_id, $template = true, $cfg = []): string {
588c2e8227SGreg Roach		global $WT_TREE, $ctype;
598c2e8227SGreg Roach
60e2a378d3SGreg Roach		$show_last_update     = $this->getBlockSetting($block_id, 'show_last_update', '1');
61e2a378d3SGreg Roach		$show_common_surnames = $this->getBlockSetting($block_id, 'show_common_surnames', '1');
62f36626dbSGreg Roach		$number_of_surnames   = $this->getBlockSetting($block_id, 'number_of_surnames', self::DEFAULT_NUMBER_OF_SURNAMES);
63e2a378d3SGreg Roach		$stat_indi            = $this->getBlockSetting($block_id, 'stat_indi', '1');
64e2a378d3SGreg Roach		$stat_fam             = $this->getBlockSetting($block_id, 'stat_fam', '1');
65e2a378d3SGreg Roach		$stat_sour            = $this->getBlockSetting($block_id, 'stat_sour', '1');
66e2a378d3SGreg Roach		$stat_media           = $this->getBlockSetting($block_id, 'stat_media', '1');
67e2a378d3SGreg Roach		$stat_repo            = $this->getBlockSetting($block_id, 'stat_repo', '1');
68e2a378d3SGreg Roach		$stat_surname         = $this->getBlockSetting($block_id, 'stat_surname', '1');
69e2a378d3SGreg Roach		$stat_events          = $this->getBlockSetting($block_id, 'stat_events', '1');
70e2a378d3SGreg Roach		$stat_users           = $this->getBlockSetting($block_id, 'stat_users', '1');
71e2a378d3SGreg Roach		$stat_first_birth     = $this->getBlockSetting($block_id, 'stat_first_birth', '1');
72e2a378d3SGreg Roach		$stat_last_birth      = $this->getBlockSetting($block_id, 'stat_last_birth', '1');
73e2a378d3SGreg Roach		$stat_first_death     = $this->getBlockSetting($block_id, 'stat_first_death', '1');
74e2a378d3SGreg Roach		$stat_last_death      = $this->getBlockSetting($block_id, 'stat_last_death', '1');
75e2a378d3SGreg Roach		$stat_long_life       = $this->getBlockSetting($block_id, 'stat_long_life', '1');
76e2a378d3SGreg Roach		$stat_avg_life        = $this->getBlockSetting($block_id, 'stat_avg_life', '1');
77e2a378d3SGreg Roach		$stat_most_chil       = $this->getBlockSetting($block_id, 'stat_most_chil', '1');
78e2a378d3SGreg Roach		$stat_avg_chil        = $this->getBlockSetting($block_id, 'stat_avg_chil', '1');
798c2e8227SGreg Roach
8015d603e7SGreg Roach		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) {
818c2e8227SGreg Roach			if (array_key_exists($name, $cfg)) {
828c2e8227SGreg Roach				$$name = $cfg[$name];
838c2e8227SGreg Roach			}
848c2e8227SGreg Roach		}
858c2e8227SGreg Roach
868c2e8227SGreg Roach		if ($show_common_surnames) {
87f36626dbSGreg Roach			$surnames = FunctionsDb::getTopSurnames($WT_TREE->getTreeId(), 0, (int) $number_of_surnames);
88e0275e5bSGreg Roach
8913abd6f3SGreg Roach			$all_surnames = [];
90f36626dbSGreg Roach			foreach (array_keys($surnames) as $surname) {
91f36626dbSGreg Roach				$all_surnames = array_merge($all_surnames, QueryName::surnames($WT_TREE, $surname, '', false, false));
92f36626dbSGreg Roach			}
93f36626dbSGreg Roach			ksort($all_surnames);
941d3c0c1aSGreg Roach
951d3c0c1aSGreg Roach			$surnames = FunctionsPrintLists::surnameList($all_surnames, 2, false, 'indilist.php', $WT_TREE);
961d3c0c1aSGreg Roach		} else {
971d3c0c1aSGreg Roach			$surnames = '';
988c2e8227SGreg Roach		}
991d3c0c1aSGreg Roach
1001d3c0c1aSGreg Roach		$content = View::make('blocks/family-tree-statistics', [
1011d3c0c1aSGreg Roach			'show_last_update'     => $show_last_update,
1021d3c0c1aSGreg Roach			'show_common_surnames' => $show_common_surnames,
1031d3c0c1aSGreg Roach			'number_of_surnames'   => $number_of_surnames,
1041d3c0c1aSGreg Roach			'stat_indi'            => $stat_indi,
1051d3c0c1aSGreg Roach			'stat_fam'             => $stat_fam,
1061d3c0c1aSGreg Roach			'stat_sour'            => $stat_sour,
1071d3c0c1aSGreg Roach			'stat_media'           => $stat_media,
1081d3c0c1aSGreg Roach			'stat_repo'            => $stat_repo,
1091d3c0c1aSGreg Roach			'stat_surname'         => $stat_surname,
1101d3c0c1aSGreg Roach			'stat_events'          => $stat_events,
1111d3c0c1aSGreg Roach			'stat_users'           => $stat_users,
1121d3c0c1aSGreg Roach			'stat_first_birth'     => $stat_first_birth,
1131d3c0c1aSGreg Roach			'stat_last_birth'      => $stat_last_birth,
1141d3c0c1aSGreg Roach			'stat_first_death'     => $stat_first_death,
1151d3c0c1aSGreg Roach			'stat_last_death'      => $stat_last_death,
1161d3c0c1aSGreg Roach			'stat_long_life'       => $stat_long_life,
1171d3c0c1aSGreg Roach			'stat_avg_life'        => $stat_avg_life,
1181d3c0c1aSGreg Roach			'stat_most_chil'       => $stat_most_chil,
1191d3c0c1aSGreg Roach			'stat_avg_chil'        => $stat_avg_chil,
1201d3c0c1aSGreg Roach			'stats'                => new Stats($WT_TREE),
1211d3c0c1aSGreg Roach			'surnames'             => $surnames,
1221d3c0c1aSGreg Roach		]);
12328941e3cSGreg Roach
1248c2e8227SGreg Roach		if ($template) {
1259c6524dcSGreg Roach			if ($ctype === 'gedcom' && Auth::isManager($WT_TREE) || $ctype === 'user' && Auth::check()) {
1269c6524dcSGreg Roach				$config_url = Html::url('block_edit.php', ['block_id' => $block_id, 'ged' => $WT_TREE->getName()]);
1279c6524dcSGreg Roach			} else {
1289c6524dcSGreg Roach				$config_url = '';
1299c6524dcSGreg Roach			}
1309c6524dcSGreg Roach
1319c6524dcSGreg Roach			return View::make('blocks/template', [
1329c6524dcSGreg Roach				'block'      => str_replace('_', '-', $this->getName()),
1339c6524dcSGreg Roach				'id'         => $block_id,
1349c6524dcSGreg Roach				'config_url' => $config_url,
1359c6524dcSGreg Roach				'title'      => $this->getTitle(),
1369c6524dcSGreg Roach				'content'    => $content,
1379c6524dcSGreg Roach			]);
1388c2e8227SGreg Roach		} else {
1398c2e8227SGreg Roach			return $content;
1408c2e8227SGreg Roach		}
1418c2e8227SGreg Roach	}
1428c2e8227SGreg Roach
1438c2e8227SGreg Roach	/** {@inheritdoc} */
144*a9430be8SGreg Roach	public function loadAjax(): bool {
1458c2e8227SGreg Roach		return true;
1468c2e8227SGreg Roach	}
1478c2e8227SGreg Roach
1488c2e8227SGreg Roach	/** {@inheritdoc} */
149*a9430be8SGreg Roach	public function isUserBlock(): bool {
1508c2e8227SGreg Roach		return true;
1518c2e8227SGreg Roach	}
1528c2e8227SGreg Roach
1538c2e8227SGreg Roach	/** {@inheritdoc} */
154*a9430be8SGreg Roach	public function isGedcomBlock(): bool {
1558c2e8227SGreg Roach		return true;
1568c2e8227SGreg Roach	}
1578c2e8227SGreg Roach
15876692c8bSGreg Roach	/**
15976692c8bSGreg Roach	 * An HTML form to edit block settings
16076692c8bSGreg Roach	 *
16176692c8bSGreg Roach	 * @param int $block_id
162*a9430be8SGreg Roach	 *
163*a9430be8SGreg Roach	 * @return void
16476692c8bSGreg Roach	 */
165*a9430be8SGreg Roach	public function configureBlock($block_id): void {
1668c2e8227SGreg Roach		if (Filter::postBool('save') && Filter::checkCsrf()) {
167e2a378d3SGreg Roach			$this->setBlockSetting($block_id, 'show_last_update', Filter::postBool('show_last_update'));
168e2a378d3SGreg Roach			$this->setBlockSetting($block_id, 'show_common_surnames', Filter::postBool('show_common_surnames'));
169f36626dbSGreg Roach			$this->setBlockSetting($block_id, 'number_of_surnames', Filter::postInteger('number_of_surnames'));
170e2a378d3SGreg Roach			$this->setBlockSetting($block_id, 'stat_indi', Filter::postBool('stat_indi'));
171e2a378d3SGreg Roach			$this->setBlockSetting($block_id, 'stat_fam', Filter::postBool('stat_fam'));
172e2a378d3SGreg Roach			$this->setBlockSetting($block_id, 'stat_sour', Filter::postBool('stat_sour'));
173e2a378d3SGreg Roach			$this->setBlockSetting($block_id, 'stat_other', Filter::postBool('stat_other'));
174e2a378d3SGreg Roach			$this->setBlockSetting($block_id, 'stat_media', Filter::postBool('stat_media'));
175e2a378d3SGreg Roach			$this->setBlockSetting($block_id, 'stat_repo', Filter::postBool('stat_repo'));
176e2a378d3SGreg Roach			$this->setBlockSetting($block_id, 'stat_surname', Filter::postBool('stat_surname'));
177e2a378d3SGreg Roach			$this->setBlockSetting($block_id, 'stat_events', Filter::postBool('stat_events'));
178e2a378d3SGreg Roach			$this->setBlockSetting($block_id, 'stat_users', Filter::postBool('stat_users'));
179e2a378d3SGreg Roach			$this->setBlockSetting($block_id, 'stat_first_birth', Filter::postBool('stat_first_birth'));
180e2a378d3SGreg Roach			$this->setBlockSetting($block_id, 'stat_last_birth', Filter::postBool('stat_last_birth'));
181e2a378d3SGreg Roach			$this->setBlockSetting($block_id, 'stat_first_death', Filter::postBool('stat_first_death'));
182e2a378d3SGreg Roach			$this->setBlockSetting($block_id, 'stat_last_death', Filter::postBool('stat_last_death'));
183e2a378d3SGreg Roach			$this->setBlockSetting($block_id, 'stat_long_life', Filter::postBool('stat_long_life'));
184e2a378d3SGreg Roach			$this->setBlockSetting($block_id, 'stat_avg_life', Filter::postBool('stat_avg_life'));
185e2a378d3SGreg Roach			$this->setBlockSetting($block_id, 'stat_most_chil', Filter::postBool('stat_most_chil'));
186e2a378d3SGreg Roach			$this->setBlockSetting($block_id, 'stat_avg_chil', Filter::postBool('stat_avg_chil'));
1878c2e8227SGreg Roach		}
1888c2e8227SGreg Roach
189e2a378d3SGreg Roach		$show_last_update     = $this->getBlockSetting($block_id, 'show_last_update', '1');
190e2a378d3SGreg Roach		$show_common_surnames = $this->getBlockSetting($block_id, 'show_common_surnames', '1');
191f36626dbSGreg Roach		$number_of_surnames   = $this->getBlockSetting($block_id, 'number_of_surnames', self::DEFAULT_NUMBER_OF_SURNAMES);
192e2a378d3SGreg Roach		$stat_indi            = $this->getBlockSetting($block_id, 'stat_indi', '1');
193e2a378d3SGreg Roach		$stat_fam             = $this->getBlockSetting($block_id, 'stat_fam', '1');
194e2a378d3SGreg Roach		$stat_sour            = $this->getBlockSetting($block_id, 'stat_sour', '1');
195e2a378d3SGreg Roach		$stat_media           = $this->getBlockSetting($block_id, 'stat_media', '1');
196e2a378d3SGreg Roach		$stat_repo            = $this->getBlockSetting($block_id, 'stat_repo', '1');
197e2a378d3SGreg Roach		$stat_surname         = $this->getBlockSetting($block_id, 'stat_surname', '1');
198e2a378d3SGreg Roach		$stat_events          = $this->getBlockSetting($block_id, 'stat_events', '1');
199e2a378d3SGreg Roach		$stat_users           = $this->getBlockSetting($block_id, 'stat_users', '1');
200e2a378d3SGreg Roach		$stat_first_birth     = $this->getBlockSetting($block_id, 'stat_first_birth', '1');
201e2a378d3SGreg Roach		$stat_last_birth      = $this->getBlockSetting($block_id, 'stat_last_birth', '1');
202e2a378d3SGreg Roach		$stat_first_death     = $this->getBlockSetting($block_id, 'stat_first_death', '1');
203e2a378d3SGreg Roach		$stat_last_death      = $this->getBlockSetting($block_id, 'stat_last_death', '1');
204e2a378d3SGreg Roach		$stat_long_life       = $this->getBlockSetting($block_id, 'stat_long_life', '1');
205e2a378d3SGreg Roach		$stat_avg_life        = $this->getBlockSetting($block_id, 'stat_avg_life', '1');
206e2a378d3SGreg Roach		$stat_most_chil       = $this->getBlockSetting($block_id, 'stat_most_chil', '1');
207e2a378d3SGreg Roach		$stat_avg_chil        = $this->getBlockSetting($block_id, 'stat_avg_chil', '1');
2088c2e8227SGreg Roach
2098c2e8227SGreg Roach		?>
21015d603e7SGreg Roach		<fieldset class="form-group">
21115d603e7SGreg Roach			<div class="row">
21215d603e7SGreg Roach				<legend class="col-form-legend col-sm-3">
21315d603e7SGreg Roach					<?= I18N::translate('Last change') ?>
21415d603e7SGreg Roach				</legend>
21515d603e7SGreg Roach				<div class="col-sm-9">
2161d3c0c1aSGreg Roach					<?= Bootstrap4::checkbox(/* I18N: label for yes/no option */
2171d3c0c1aSGreg Roach						I18N::translate('Show date of last update'), false, ['name' => 'show_last_update', 'checked' => (bool) $show_last_update]) ?>
21815d603e7SGreg Roach				</div>
21915d603e7SGreg Roach			</div>
22015d603e7SGreg Roach		</fieldset>
22115d603e7SGreg Roach
22215d603e7SGreg Roach		<fieldset class="form-group">
22315d603e7SGreg Roach			<div class="row">
22415d603e7SGreg Roach				<legend class="col-form-legend col-sm-3">
225564ae2d7SGreg Roach					<?= I18N::translate('Statistics') ?>
22615d603e7SGreg Roach				</legend>
22715d603e7SGreg Roach				<div class="col-sm-9">
22815d603e7SGreg Roach					<?= Bootstrap4::checkbox(I18N::translate('Individuals'), false, ['name' => 'stat_indi', 'checked' => (bool) $stat_indi]) ?>
22915d603e7SGreg Roach					<?= Bootstrap4::checkbox(I18N::translate('Total surnames'), false, ['name' => 'stat_surname', 'checked' => (bool) $stat_surname]) ?>
23015d603e7SGreg Roach					<?= Bootstrap4::checkbox(I18N::translate('Families'), false, ['name' => 'stat_fam', 'checked' => (bool) $stat_fam]) ?>
23115d603e7SGreg Roach					<?= Bootstrap4::checkbox(I18N::translate('Sources'), false, ['name' => 'stat_sour', 'checked' => (bool) $stat_sour]) ?>
23215d603e7SGreg Roach					<?= Bootstrap4::checkbox(I18N::translate('Media objects'), false, ['name' => 'stat_media', 'checked' => (bool) $stat_media]) ?>
23315d603e7SGreg Roach					<?= Bootstrap4::checkbox(I18N::translate('Repositories'), false, ['name' => 'stat_repo', 'checked' => (bool) $stat_repo]) ?>
23415d603e7SGreg Roach					<?= Bootstrap4::checkbox(I18N::translate('Total events'), false, ['name' => 'stat_events', 'checked' => (bool) $stat_events]) ?>
23515d603e7SGreg Roach					<?= Bootstrap4::checkbox(I18N::translate('Total users'), false, ['name' => 'stat_users', 'checked' => (bool) $stat_users]) ?>
2361d3c0c1aSGreg Roach					<?= Bootstrap4::checkbox(I18N::translate('Earliest birth'), false, ['name' => 'stat_first_birth', 'checked' => (bool) $stat_first_birth]) ?>
2371d3c0c1aSGreg Roach					<?= Bootstrap4::checkbox(I18N::translate('Latest birth'), false, ['name' => 'stat_last_birth', 'checked' => (bool) $stat_last_birth]) ?>
2381d3c0c1aSGreg Roach					<?= Bootstrap4::checkbox(I18N::translate('Earliest death'), false, ['name' => 'stat_first_death', 'checked' => (bool) $stat_first_death]) ?>
2391d3c0c1aSGreg Roach					<?= Bootstrap4::checkbox(I18N::translate('Latest death'), false, ['name' => 'stat_last_death', 'checked' => (bool) $stat_last_death]) ?>
24015d603e7SGreg Roach					<?= Bootstrap4::checkbox(I18N::translate('Individual who lived the longest'), false, ['name' => 'stat_long_life', 'checked' => (bool) $stat_long_life]) ?>
24115d603e7SGreg Roach					<?= Bootstrap4::checkbox(I18N::translate('Average age at death'), false, ['name' => 'stat_avg_life', 'checked' => (bool) $stat_avg_life]) ?>
24215d603e7SGreg Roach					<?= Bootstrap4::checkbox(I18N::translate('Family with the most children'), false, ['name' => 'stat_most_chil', 'checked' => (bool) $stat_most_chil]) ?>
24315d603e7SGreg Roach					<?= Bootstrap4::checkbox(I18N::translate('Average number of children per family'), false, ['name' => 'stat_avg_chil', 'checked' => (bool) $stat_avg_chil]) ?>
24415d603e7SGreg Roach				</div>
24515d603e7SGreg Roach			</div>
24615d603e7SGreg Roach		</fieldset>
24715d603e7SGreg Roach
24815d603e7SGreg Roach		<fieldset class="form-group">
24915d603e7SGreg Roach			<div class="row">
25015d603e7SGreg Roach				<legend class="col-form-legend col-sm-3">
25115d603e7SGreg Roach					<label for="show_common_surnames">
25215d603e7SGreg Roach						<?= I18N::translate('Surnames') ?>
2538c2e8227SGreg Roach					</label>
25415d603e7SGreg Roach				</legend>
25515d603e7SGreg Roach				<div class="col-sm-9">
25615d603e7SGreg Roach					<?= Bootstrap4::checkbox(I18N::translate('Most common surnames'), false, ['name' => 'show_common_surnames', 'checked' => (bool) $show_common_surnames]) ?>
25715d603e7SGreg Roach					<label for="number_of_surnames">
2581d3c0c1aSGreg Roach						<?= /* I18N: ... to show in a list */
2591d3c0c1aSGreg Roach						I18N::translate('Number of surnames') ?>
260f36626dbSGreg Roach						<input
26115d603e7SGreg Roach							class="form-control"
26215d603e7SGreg Roach							id="number_of_surnames"
263f36626dbSGreg Roach							maxlength="5"
264f36626dbSGreg Roach							name="number_of_surnames"
265f36626dbSGreg Roach							pattern="[1-9][0-9]*"
266f36626dbSGreg Roach							required
267f36626dbSGreg Roach							type="text"
268cc5ab399SGreg Roach							value="<?= Html::escape($number_of_surnames) ?>"
269f36626dbSGreg Roach						>
27015d603e7SGreg Roach					</label>
27115d603e7SGreg Roach				</div>
27215d603e7SGreg Roach			</div>
27315d603e7SGreg Roach		</fieldset>
2748c2e8227SGreg Roach		<?php
2758c2e8227SGreg Roach	}
2768c2e8227SGreg Roach}
277