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