xref: /webtrees/app/Module/FamilyTreeStatisticsModule.php (revision 225e381f36d59b49c8cdac0060465fa5af2fc308)
18c2e8227SGreg Roach<?php
28c2e8227SGreg Roach/**
38c2e8227SGreg Roach * webtrees: online genealogy
41062a142SGreg Roach * Copyright (C) 2018 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;
278c2e8227SGreg Roach
288c2e8227SGreg Roach/**
298c2e8227SGreg Roach * Class FamilyTreeStatisticsModule
308c2e8227SGreg Roach */
31e2a378d3SGreg Roachclass FamilyTreeStatisticsModule extends AbstractModule implements ModuleBlockInterface {
32f36626dbSGreg Roach	/** Show this number of surnames by default */
33f36626dbSGreg Roach	const DEFAULT_NUMBER_OF_SURNAMES = 10;
34f36626dbSGreg Roach
358c2e8227SGreg Roach	/** {@inheritdoc} */
368c2e8227SGreg Roach	public function getTitle() {
371d3c0c1aSGreg Roach		return /* I18N: Name of a module */
381d3c0c1aSGreg Roach			I18N::translate('Statistics');
398c2e8227SGreg Roach	}
408c2e8227SGreg Roach
418c2e8227SGreg Roach	/** {@inheritdoc} */
428c2e8227SGreg Roach	public function getDescription() {
431d3c0c1aSGreg Roach		return /* I18N: Description of “Statistics” module */
441d3c0c1aSGreg Roach			I18N::translate('The size of the family tree, earliest and latest events, common names, etc.');
458c2e8227SGreg Roach	}
468c2e8227SGreg Roach
4776692c8bSGreg Roach	/**
4876692c8bSGreg Roach	 * Generate the HTML content of this block.
4976692c8bSGreg Roach	 *
5076692c8bSGreg Roach	 * @param int      $block_id
5176692c8bSGreg Roach	 * @param bool     $template
52727f238cSGreg Roach	 * @param string[] $cfg
5376692c8bSGreg Roach	 *
5476692c8bSGreg Roach	 * @return string
5576692c8bSGreg Roach	 */
56a9430be8SGreg Roach	public function getBlock($block_id, $template = true, $cfg = []): string {
578c2e8227SGreg Roach		global $WT_TREE, $ctype;
588c2e8227SGreg Roach
59e2a378d3SGreg Roach		$show_last_update     = $this->getBlockSetting($block_id, 'show_last_update', '1');
60e2a378d3SGreg Roach		$show_common_surnames = $this->getBlockSetting($block_id, 'show_common_surnames', '1');
61f36626dbSGreg Roach		$number_of_surnames   = $this->getBlockSetting($block_id, 'number_of_surnames', self::DEFAULT_NUMBER_OF_SURNAMES);
62e2a378d3SGreg Roach		$stat_indi            = $this->getBlockSetting($block_id, 'stat_indi', '1');
63e2a378d3SGreg Roach		$stat_fam             = $this->getBlockSetting($block_id, 'stat_fam', '1');
64e2a378d3SGreg Roach		$stat_sour            = $this->getBlockSetting($block_id, 'stat_sour', '1');
65e2a378d3SGreg Roach		$stat_media           = $this->getBlockSetting($block_id, 'stat_media', '1');
66e2a378d3SGreg Roach		$stat_repo            = $this->getBlockSetting($block_id, 'stat_repo', '1');
67e2a378d3SGreg Roach		$stat_surname         = $this->getBlockSetting($block_id, 'stat_surname', '1');
68e2a378d3SGreg Roach		$stat_events          = $this->getBlockSetting($block_id, 'stat_events', '1');
69e2a378d3SGreg Roach		$stat_users           = $this->getBlockSetting($block_id, 'stat_users', '1');
70e2a378d3SGreg Roach		$stat_first_birth     = $this->getBlockSetting($block_id, 'stat_first_birth', '1');
71e2a378d3SGreg Roach		$stat_last_birth      = $this->getBlockSetting($block_id, 'stat_last_birth', '1');
72e2a378d3SGreg Roach		$stat_first_death     = $this->getBlockSetting($block_id, 'stat_first_death', '1');
73e2a378d3SGreg Roach		$stat_last_death      = $this->getBlockSetting($block_id, 'stat_last_death', '1');
74e2a378d3SGreg Roach		$stat_long_life       = $this->getBlockSetting($block_id, 'stat_long_life', '1');
75e2a378d3SGreg Roach		$stat_avg_life        = $this->getBlockSetting($block_id, 'stat_avg_life', '1');
76e2a378d3SGreg Roach		$stat_most_chil       = $this->getBlockSetting($block_id, 'stat_most_chil', '1');
77e2a378d3SGreg Roach		$stat_avg_chil        = $this->getBlockSetting($block_id, 'stat_avg_chil', '1');
788c2e8227SGreg Roach
7915d603e7SGreg 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) {
808c2e8227SGreg Roach			if (array_key_exists($name, $cfg)) {
818c2e8227SGreg Roach				$$name = $cfg[$name];
828c2e8227SGreg Roach			}
838c2e8227SGreg Roach		}
848c2e8227SGreg Roach
858c2e8227SGreg Roach		if ($show_common_surnames) {
86f36626dbSGreg Roach			$surnames = FunctionsDb::getTopSurnames($WT_TREE->getTreeId(), 0, (int) $number_of_surnames);
87e0275e5bSGreg Roach
8813abd6f3SGreg Roach			$all_surnames = [];
89f36626dbSGreg Roach			foreach (array_keys($surnames) as $surname) {
90f36626dbSGreg Roach				$all_surnames = array_merge($all_surnames, QueryName::surnames($WT_TREE, $surname, '', false, false));
91f36626dbSGreg Roach			}
92f36626dbSGreg Roach			ksort($all_surnames);
931d3c0c1aSGreg Roach
941d3c0c1aSGreg Roach			$surnames = FunctionsPrintLists::surnameList($all_surnames, 2, false, 'indilist.php', $WT_TREE);
951d3c0c1aSGreg Roach		} else {
961d3c0c1aSGreg Roach			$surnames = '';
978c2e8227SGreg Roach		}
981d3c0c1aSGreg Roach
99*225e381fSGreg Roach		$content = view('blocks/family-tree-statistics', [
1001d3c0c1aSGreg Roach			'show_last_update'     => $show_last_update,
1011d3c0c1aSGreg Roach			'show_common_surnames' => $show_common_surnames,
1021d3c0c1aSGreg Roach			'number_of_surnames'   => $number_of_surnames,
1031d3c0c1aSGreg Roach			'stat_indi'            => $stat_indi,
1041d3c0c1aSGreg Roach			'stat_fam'             => $stat_fam,
1051d3c0c1aSGreg Roach			'stat_sour'            => $stat_sour,
1061d3c0c1aSGreg Roach			'stat_media'           => $stat_media,
1071d3c0c1aSGreg Roach			'stat_repo'            => $stat_repo,
1081d3c0c1aSGreg Roach			'stat_surname'         => $stat_surname,
1091d3c0c1aSGreg Roach			'stat_events'          => $stat_events,
1101d3c0c1aSGreg Roach			'stat_users'           => $stat_users,
1111d3c0c1aSGreg Roach			'stat_first_birth'     => $stat_first_birth,
1121d3c0c1aSGreg Roach			'stat_last_birth'      => $stat_last_birth,
1131d3c0c1aSGreg Roach			'stat_first_death'     => $stat_first_death,
1141d3c0c1aSGreg Roach			'stat_last_death'      => $stat_last_death,
1151d3c0c1aSGreg Roach			'stat_long_life'       => $stat_long_life,
1161d3c0c1aSGreg Roach			'stat_avg_life'        => $stat_avg_life,
1171d3c0c1aSGreg Roach			'stat_most_chil'       => $stat_most_chil,
1181d3c0c1aSGreg Roach			'stat_avg_chil'        => $stat_avg_chil,
1191d3c0c1aSGreg Roach			'stats'                => new Stats($WT_TREE),
1201d3c0c1aSGreg Roach			'surnames'             => $surnames,
1211d3c0c1aSGreg Roach		]);
12228941e3cSGreg Roach
1238c2e8227SGreg Roach		if ($template) {
1249c6524dcSGreg Roach			if ($ctype === 'gedcom' && Auth::isManager($WT_TREE) || $ctype === 'user' && Auth::check()) {
1259c6524dcSGreg Roach				$config_url = Html::url('block_edit.php', ['block_id' => $block_id, 'ged' => $WT_TREE->getName()]);
1269c6524dcSGreg Roach			} else {
1279c6524dcSGreg Roach				$config_url = '';
1289c6524dcSGreg Roach			}
1299c6524dcSGreg Roach
130*225e381fSGreg Roach			return view('blocks/template', [
1319c6524dcSGreg Roach				'block'      => str_replace('_', '-', $this->getName()),
1329c6524dcSGreg Roach				'id'         => $block_id,
1339c6524dcSGreg Roach				'config_url' => $config_url,
1349c6524dcSGreg Roach				'title'      => $this->getTitle(),
1359c6524dcSGreg Roach				'content'    => $content,
1369c6524dcSGreg Roach			]);
1378c2e8227SGreg Roach		} else {
1388c2e8227SGreg Roach			return $content;
1398c2e8227SGreg Roach		}
1408c2e8227SGreg Roach	}
1418c2e8227SGreg Roach
1428c2e8227SGreg Roach	/** {@inheritdoc} */
143a9430be8SGreg Roach	public function loadAjax(): bool {
1448c2e8227SGreg Roach		return true;
1458c2e8227SGreg Roach	}
1468c2e8227SGreg Roach
1478c2e8227SGreg Roach	/** {@inheritdoc} */
148a9430be8SGreg Roach	public function isUserBlock(): bool {
1498c2e8227SGreg Roach		return true;
1508c2e8227SGreg Roach	}
1518c2e8227SGreg Roach
1528c2e8227SGreg Roach	/** {@inheritdoc} */
153a9430be8SGreg Roach	public function isGedcomBlock(): bool {
1548c2e8227SGreg Roach		return true;
1558c2e8227SGreg Roach	}
1568c2e8227SGreg Roach
15776692c8bSGreg Roach	/**
15876692c8bSGreg Roach	 * An HTML form to edit block settings
15976692c8bSGreg Roach	 *
16076692c8bSGreg Roach	 * @param int $block_id
161a9430be8SGreg Roach	 *
162a9430be8SGreg Roach	 * @return void
16376692c8bSGreg Roach	 */
164be9a728cSGreg Roach	public function configureBlock($block_id) {
1658c2e8227SGreg Roach		if (Filter::postBool('save') && Filter::checkCsrf()) {
166e2a378d3SGreg Roach			$this->setBlockSetting($block_id, 'show_last_update', Filter::postBool('show_last_update'));
167e2a378d3SGreg Roach			$this->setBlockSetting($block_id, 'show_common_surnames', Filter::postBool('show_common_surnames'));
168f36626dbSGreg Roach			$this->setBlockSetting($block_id, 'number_of_surnames', Filter::postInteger('number_of_surnames'));
169e2a378d3SGreg Roach			$this->setBlockSetting($block_id, 'stat_indi', Filter::postBool('stat_indi'));
170e2a378d3SGreg Roach			$this->setBlockSetting($block_id, 'stat_fam', Filter::postBool('stat_fam'));
171e2a378d3SGreg Roach			$this->setBlockSetting($block_id, 'stat_sour', Filter::postBool('stat_sour'));
172e2a378d3SGreg Roach			$this->setBlockSetting($block_id, 'stat_other', Filter::postBool('stat_other'));
173e2a378d3SGreg Roach			$this->setBlockSetting($block_id, 'stat_media', Filter::postBool('stat_media'));
174e2a378d3SGreg Roach			$this->setBlockSetting($block_id, 'stat_repo', Filter::postBool('stat_repo'));
175e2a378d3SGreg Roach			$this->setBlockSetting($block_id, 'stat_surname', Filter::postBool('stat_surname'));
176e2a378d3SGreg Roach			$this->setBlockSetting($block_id, 'stat_events', Filter::postBool('stat_events'));
177e2a378d3SGreg Roach			$this->setBlockSetting($block_id, 'stat_users', Filter::postBool('stat_users'));
178e2a378d3SGreg Roach			$this->setBlockSetting($block_id, 'stat_first_birth', Filter::postBool('stat_first_birth'));
179e2a378d3SGreg Roach			$this->setBlockSetting($block_id, 'stat_last_birth', Filter::postBool('stat_last_birth'));
180e2a378d3SGreg Roach			$this->setBlockSetting($block_id, 'stat_first_death', Filter::postBool('stat_first_death'));
181e2a378d3SGreg Roach			$this->setBlockSetting($block_id, 'stat_last_death', Filter::postBool('stat_last_death'));
182e2a378d3SGreg Roach			$this->setBlockSetting($block_id, 'stat_long_life', Filter::postBool('stat_long_life'));
183e2a378d3SGreg Roach			$this->setBlockSetting($block_id, 'stat_avg_life', Filter::postBool('stat_avg_life'));
184e2a378d3SGreg Roach			$this->setBlockSetting($block_id, 'stat_most_chil', Filter::postBool('stat_most_chil'));
185e2a378d3SGreg Roach			$this->setBlockSetting($block_id, 'stat_avg_chil', Filter::postBool('stat_avg_chil'));
1868c2e8227SGreg Roach		}
1878c2e8227SGreg Roach
188e2a378d3SGreg Roach		$show_last_update     = $this->getBlockSetting($block_id, 'show_last_update', '1');
189e2a378d3SGreg Roach		$show_common_surnames = $this->getBlockSetting($block_id, 'show_common_surnames', '1');
190f36626dbSGreg Roach		$number_of_surnames   = $this->getBlockSetting($block_id, 'number_of_surnames', self::DEFAULT_NUMBER_OF_SURNAMES);
191e2a378d3SGreg Roach		$stat_indi            = $this->getBlockSetting($block_id, 'stat_indi', '1');
192e2a378d3SGreg Roach		$stat_fam             = $this->getBlockSetting($block_id, 'stat_fam', '1');
193e2a378d3SGreg Roach		$stat_sour            = $this->getBlockSetting($block_id, 'stat_sour', '1');
194e2a378d3SGreg Roach		$stat_media           = $this->getBlockSetting($block_id, 'stat_media', '1');
195e2a378d3SGreg Roach		$stat_repo            = $this->getBlockSetting($block_id, 'stat_repo', '1');
196e2a378d3SGreg Roach		$stat_surname         = $this->getBlockSetting($block_id, 'stat_surname', '1');
197e2a378d3SGreg Roach		$stat_events          = $this->getBlockSetting($block_id, 'stat_events', '1');
198e2a378d3SGreg Roach		$stat_users           = $this->getBlockSetting($block_id, 'stat_users', '1');
199e2a378d3SGreg Roach		$stat_first_birth     = $this->getBlockSetting($block_id, 'stat_first_birth', '1');
200e2a378d3SGreg Roach		$stat_last_birth      = $this->getBlockSetting($block_id, 'stat_last_birth', '1');
201e2a378d3SGreg Roach		$stat_first_death     = $this->getBlockSetting($block_id, 'stat_first_death', '1');
202e2a378d3SGreg Roach		$stat_last_death      = $this->getBlockSetting($block_id, 'stat_last_death', '1');
203e2a378d3SGreg Roach		$stat_long_life       = $this->getBlockSetting($block_id, 'stat_long_life', '1');
204e2a378d3SGreg Roach		$stat_avg_life        = $this->getBlockSetting($block_id, 'stat_avg_life', '1');
205e2a378d3SGreg Roach		$stat_most_chil       = $this->getBlockSetting($block_id, 'stat_most_chil', '1');
206e2a378d3SGreg Roach		$stat_avg_chil        = $this->getBlockSetting($block_id, 'stat_avg_chil', '1');
2078c2e8227SGreg Roach
2088c2e8227SGreg Roach		?>
20915d603e7SGreg Roach		<fieldset class="form-group">
21015d603e7SGreg Roach			<div class="row">
211fff20713SGreg Roach				<legend class="col-form-label col-sm-3">
21215d603e7SGreg Roach					<?= I18N::translate('Last change') ?>
21315d603e7SGreg Roach				</legend>
21415d603e7SGreg Roach				<div class="col-sm-9">
2151d3c0c1aSGreg Roach					<?= Bootstrap4::checkbox(/* I18N: label for yes/no option */
2161d3c0c1aSGreg Roach						I18N::translate('Show date of last update'), false, ['name' => 'show_last_update', 'checked' => (bool) $show_last_update]) ?>
21715d603e7SGreg Roach				</div>
21815d603e7SGreg Roach			</div>
21915d603e7SGreg Roach		</fieldset>
22015d603e7SGreg Roach
22115d603e7SGreg Roach		<fieldset class="form-group">
22215d603e7SGreg Roach			<div class="row">
223fff20713SGreg Roach				<legend class="col-form-label col-sm-3">
224564ae2d7SGreg Roach					<?= I18N::translate('Statistics') ?>
22515d603e7SGreg Roach				</legend>
22615d603e7SGreg Roach				<div class="col-sm-9">
22715d603e7SGreg Roach					<?= Bootstrap4::checkbox(I18N::translate('Individuals'), false, ['name' => 'stat_indi', 'checked' => (bool) $stat_indi]) ?>
22815d603e7SGreg Roach					<?= Bootstrap4::checkbox(I18N::translate('Total surnames'), false, ['name' => 'stat_surname', 'checked' => (bool) $stat_surname]) ?>
22915d603e7SGreg Roach					<?= Bootstrap4::checkbox(I18N::translate('Families'), false, ['name' => 'stat_fam', 'checked' => (bool) $stat_fam]) ?>
23015d603e7SGreg Roach					<?= Bootstrap4::checkbox(I18N::translate('Sources'), false, ['name' => 'stat_sour', 'checked' => (bool) $stat_sour]) ?>
23115d603e7SGreg Roach					<?= Bootstrap4::checkbox(I18N::translate('Media objects'), false, ['name' => 'stat_media', 'checked' => (bool) $stat_media]) ?>
23215d603e7SGreg Roach					<?= Bootstrap4::checkbox(I18N::translate('Repositories'), false, ['name' => 'stat_repo', 'checked' => (bool) $stat_repo]) ?>
23315d603e7SGreg Roach					<?= Bootstrap4::checkbox(I18N::translate('Total events'), false, ['name' => 'stat_events', 'checked' => (bool) $stat_events]) ?>
23415d603e7SGreg Roach					<?= Bootstrap4::checkbox(I18N::translate('Total users'), false, ['name' => 'stat_users', 'checked' => (bool) $stat_users]) ?>
2351d3c0c1aSGreg Roach					<?= Bootstrap4::checkbox(I18N::translate('Earliest birth'), false, ['name' => 'stat_first_birth', 'checked' => (bool) $stat_first_birth]) ?>
2361d3c0c1aSGreg Roach					<?= Bootstrap4::checkbox(I18N::translate('Latest birth'), false, ['name' => 'stat_last_birth', 'checked' => (bool) $stat_last_birth]) ?>
2371d3c0c1aSGreg Roach					<?= Bootstrap4::checkbox(I18N::translate('Earliest death'), false, ['name' => 'stat_first_death', 'checked' => (bool) $stat_first_death]) ?>
2381d3c0c1aSGreg Roach					<?= Bootstrap4::checkbox(I18N::translate('Latest death'), false, ['name' => 'stat_last_death', 'checked' => (bool) $stat_last_death]) ?>
23915d603e7SGreg Roach					<?= Bootstrap4::checkbox(I18N::translate('Individual who lived the longest'), false, ['name' => 'stat_long_life', 'checked' => (bool) $stat_long_life]) ?>
24015d603e7SGreg Roach					<?= Bootstrap4::checkbox(I18N::translate('Average age at death'), false, ['name' => 'stat_avg_life', 'checked' => (bool) $stat_avg_life]) ?>
24115d603e7SGreg Roach					<?= Bootstrap4::checkbox(I18N::translate('Family with the most children'), false, ['name' => 'stat_most_chil', 'checked' => (bool) $stat_most_chil]) ?>
24215d603e7SGreg Roach					<?= Bootstrap4::checkbox(I18N::translate('Average number of children per family'), false, ['name' => 'stat_avg_chil', 'checked' => (bool) $stat_avg_chil]) ?>
24315d603e7SGreg Roach				</div>
24415d603e7SGreg Roach			</div>
24515d603e7SGreg Roach		</fieldset>
24615d603e7SGreg Roach
24715d603e7SGreg Roach		<fieldset class="form-group">
24815d603e7SGreg Roach			<div class="row">
249fff20713SGreg Roach				<legend class="col-form-label col-sm-3">
25015d603e7SGreg Roach					<label for="show_common_surnames">
25115d603e7SGreg Roach						<?= I18N::translate('Surnames') ?>
2528c2e8227SGreg Roach					</label>
25315d603e7SGreg Roach				</legend>
25415d603e7SGreg Roach				<div class="col-sm-9">
25515d603e7SGreg Roach					<?= Bootstrap4::checkbox(I18N::translate('Most common surnames'), false, ['name' => 'show_common_surnames', 'checked' => (bool) $show_common_surnames]) ?>
25615d603e7SGreg Roach					<label for="number_of_surnames">
2571d3c0c1aSGreg Roach						<?= /* I18N: ... to show in a list */
2581d3c0c1aSGreg Roach						I18N::translate('Number of surnames') ?>
259f36626dbSGreg Roach						<input
26015d603e7SGreg Roach							class="form-control"
26115d603e7SGreg Roach							id="number_of_surnames"
262f36626dbSGreg Roach							maxlength="5"
263f36626dbSGreg Roach							name="number_of_surnames"
264f36626dbSGreg Roach							pattern="[1-9][0-9]*"
265f36626dbSGreg Roach							required
266f36626dbSGreg Roach							type="text"
267d53324c9SGreg Roach							value="<?= e($number_of_surnames) ?>"
268f36626dbSGreg Roach						>
26915d603e7SGreg Roach					</label>
27015d603e7SGreg Roach				</div>
27115d603e7SGreg Roach			</div>
27215d603e7SGreg Roach		</fieldset>
2738c2e8227SGreg Roach		<?php
2748c2e8227SGreg Roach	}
2758c2e8227SGreg Roach}
276