xref: /webtrees/app/Module/FamilyTreeStatisticsModule.php (revision 6664b4a34cf6b2d1fc123cfb8f05bb5dda4a7f25)
1<?php
2/**
3 * webtrees: online genealogy
4 * Copyright (C) 2016 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\Filter;
20use Fisharebest\Webtrees\Functions\FunctionsDb;
21use Fisharebest\Webtrees\Functions\FunctionsPrintLists;
22use Fisharebest\Webtrees\I18N;
23use Fisharebest\Webtrees\Module;
24use Fisharebest\Webtrees\Query\QueryName;
25use Fisharebest\Webtrees\Stats;
26use Fisharebest\Webtrees\Theme;
27
28/**
29 * Class FamilyTreeStatisticsModule
30 */
31class FamilyTreeStatisticsModule extends AbstractModule implements ModuleBlockInterface {
32	/** Show this number of surnames by default */
33	const DEFAULT_NUMBER_OF_SURNAMES = 10;
34
35	/** {@inheritdoc} */
36	public function getTitle() {
37		return /* I18N: Name of a module */ I18N::translate('Statistics');
38	}
39
40	/** {@inheritdoc} */
41	public function getDescription() {
42		return /* I18N: Description of “Statistics” module */ I18N::translate('The size of the family tree, earliest and latest events, common names, etc.');
43	}
44
45	/**
46	 * Generate the HTML content of this block.
47	 *
48	 * @param int      $block_id
49	 * @param bool     $template
50	 * @param string[] $cfg
51	 *
52	 * @return string
53	 */
54	public function getBlock($block_id, $template = true, $cfg = array()) {
55		global $WT_TREE, $ctype;
56
57		$show_last_update     = $this->getBlockSetting($block_id, 'show_last_update', '1');
58		$show_common_surnames = $this->getBlockSetting($block_id, 'show_common_surnames', '1');
59		$number_of_surnames   = $this->getBlockSetting($block_id, 'number_of_surnames', self::DEFAULT_NUMBER_OF_SURNAMES);
60		$stat_indi            = $this->getBlockSetting($block_id, 'stat_indi', '1');
61		$stat_fam             = $this->getBlockSetting($block_id, 'stat_fam', '1');
62		$stat_sour            = $this->getBlockSetting($block_id, 'stat_sour', '1');
63		$stat_media           = $this->getBlockSetting($block_id, 'stat_media', '1');
64		$stat_repo            = $this->getBlockSetting($block_id, 'stat_repo', '1');
65		$stat_surname         = $this->getBlockSetting($block_id, 'stat_surname', '1');
66		$stat_events          = $this->getBlockSetting($block_id, 'stat_events', '1');
67		$stat_users           = $this->getBlockSetting($block_id, 'stat_users', '1');
68		$stat_first_birth     = $this->getBlockSetting($block_id, 'stat_first_birth', '1');
69		$stat_last_birth      = $this->getBlockSetting($block_id, 'stat_last_birth', '1');
70		$stat_first_death     = $this->getBlockSetting($block_id, 'stat_first_death', '1');
71		$stat_last_death      = $this->getBlockSetting($block_id, 'stat_last_death', '1');
72		$stat_long_life       = $this->getBlockSetting($block_id, 'stat_long_life', '1');
73		$stat_avg_life        = $this->getBlockSetting($block_id, 'stat_avg_life', '1');
74		$stat_most_chil       = $this->getBlockSetting($block_id, 'stat_most_chil', '1');
75		$stat_avg_chil        = $this->getBlockSetting($block_id, 'stat_avg_chil', '1');
76
77		// This can be overriden when embedding in an HTML block
78		$block = '0';
79
80		foreach (array('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', 'block') 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 = '<a class="icon-admin" title="' . I18N::translate('Configure') . '" href="block_edit.php?block_id=' . $block_id . '&amp;ged=' . $WT_TREE->getNameHtml() . '&amp;ctype=' . $ctype . '"></a>';
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		if (!$block) {
139			$content .= '</div><div class="facts_table stat-table2">';
140		}
141		if ($stat_first_birth) {
142			$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>';
143			if (!$block) {
144				$content .= '<div class="facts_value stat-cell left">' . $stats->firstBirth() . '</div>';
145			}
146			$content .= '</div>';
147		}
148		if ($stat_last_birth) {
149			$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>';
150			if (!$block) {
151				$content .= '<div class="facts_value stat-cell left">' . $stats->lastBirth() . '</div>';
152			}
153			$content .= '</div>';
154		}
155		if ($stat_first_death) {
156			$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>';
157			if (!$block) {
158				$content .= '<div class="facts_value stat-cell left">' . $stats->firstDeath() . '</div>';
159			}
160			$content .= '</div>';
161		}
162		if ($stat_last_death) {
163			$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>';
164			if (!$block) {
165				$content .= '<div class="facts_value stat-cell left">' . $stats->lastDeath() . '</div>';
166			}
167			$content .= '</div>';
168		}
169		if ($stat_long_life) {
170			$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>';
171			if (!$block) {
172				$content .= '<div class="facts_value stat-cell left">' . $stats->longestLife() . '</div>';
173			}
174			$content .= '</div>';
175		}
176		if ($stat_avg_life) {
177			$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>';
178			if (!$block) {
179				$content .= '<div class="facts_value stat-cell left">' . I18N::translate('Males') . ':&nbsp;' . $stats->averageLifespanMale();
180				$content .= '&nbsp;&nbsp;&nbsp;' . I18N::translate('Females') . ':&nbsp;' . $stats->averageLifespanFemale() . '</div>';
181			}
182			$content .= '</div>';
183		}
184
185		if ($stat_most_chil && !$block) {
186			$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>';
187			if (!$block) {
188				$content .= '<div class="facts_value stat-cell left">' . $stats->largestFamily() . '</div>';
189			}
190			$content .= '</div>';
191		}
192		if ($stat_avg_chil) {
193			$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>';
194			if (!$block) {
195				$content .= '<div class="facts_value stat-cell left"></div>';
196			}
197			$content .= '</div>';
198		}
199		$content .= '</div>';
200
201		if ($show_common_surnames) {
202			$surnames = FunctionsDb::getTopSurnames($WT_TREE->getTreeId(), 0, (int) $number_of_surnames);
203
204			$all_surnames = array();
205			foreach (array_keys($surnames) as $surname) {
206				$all_surnames = array_merge($all_surnames, QueryName::surnames($WT_TREE, $surname, '', false, false));
207			}
208
209			if (!empty($surnames)) {
210				ksort($all_surnames);
211				$content .= '<div class="clearfloat">';
212				$content .= '<p>';
213				$content .= '<strong>' . I18N::translate('Most common surnames') . '</strong>';
214				$content .= '<br>';
215				$content .= '<span class="common_surnames">' . FunctionsPrintLists::surnameList($all_surnames, 2, false, 'indilist.php', $WT_TREE) . '</span>';
216				$content .= '</p>';
217				$content .= '</div>';
218			}
219		}
220
221		if ($template) {
222			return Theme::theme()->formatBlock($id, $title, $class, $content);
223		} else {
224			return $content;
225		}
226	}
227
228	/** {@inheritdoc} */
229	public function loadAjax() {
230		return true;
231	}
232
233	/** {@inheritdoc} */
234	public function isUserBlock() {
235		return true;
236	}
237
238	/** {@inheritdoc} */
239	public function isGedcomBlock() {
240		return true;
241	}
242
243	/**
244	 * An HTML form to edit block settings
245	 *
246	 * @param int $block_id
247	 */
248	public function configureBlock($block_id) {
249		if (Filter::postBool('save') && Filter::checkCsrf()) {
250			$this->setBlockSetting($block_id, 'show_last_update', Filter::postBool('show_last_update'));
251			$this->setBlockSetting($block_id, 'show_common_surnames', Filter::postBool('show_common_surnames'));
252			$this->setBlockSetting($block_id, 'number_of_surnames', Filter::postInteger('number_of_surnames'));
253			$this->setBlockSetting($block_id, 'stat_indi', Filter::postBool('stat_indi'));
254			$this->setBlockSetting($block_id, 'stat_fam', Filter::postBool('stat_fam'));
255			$this->setBlockSetting($block_id, 'stat_sour', Filter::postBool('stat_sour'));
256			$this->setBlockSetting($block_id, 'stat_other', Filter::postBool('stat_other'));
257			$this->setBlockSetting($block_id, 'stat_media', Filter::postBool('stat_media'));
258			$this->setBlockSetting($block_id, 'stat_repo', Filter::postBool('stat_repo'));
259			$this->setBlockSetting($block_id, 'stat_surname', Filter::postBool('stat_surname'));
260			$this->setBlockSetting($block_id, 'stat_events', Filter::postBool('stat_events'));
261			$this->setBlockSetting($block_id, 'stat_users', Filter::postBool('stat_users'));
262			$this->setBlockSetting($block_id, 'stat_first_birth', Filter::postBool('stat_first_birth'));
263			$this->setBlockSetting($block_id, 'stat_last_birth', Filter::postBool('stat_last_birth'));
264			$this->setBlockSetting($block_id, 'stat_first_death', Filter::postBool('stat_first_death'));
265			$this->setBlockSetting($block_id, 'stat_last_death', Filter::postBool('stat_last_death'));
266			$this->setBlockSetting($block_id, 'stat_long_life', Filter::postBool('stat_long_life'));
267			$this->setBlockSetting($block_id, 'stat_avg_life', Filter::postBool('stat_avg_life'));
268			$this->setBlockSetting($block_id, 'stat_most_chil', Filter::postBool('stat_most_chil'));
269			$this->setBlockSetting($block_id, 'stat_avg_chil', Filter::postBool('stat_avg_chil'));
270		}
271
272		$show_last_update     = $this->getBlockSetting($block_id, 'show_last_update', '1');
273		$show_common_surnames = $this->getBlockSetting($block_id, 'show_common_surnames', '1');
274		$number_of_surnames   = $this->getBlockSetting($block_id, 'number_of_surnames', self::DEFAULT_NUMBER_OF_SURNAMES);
275		$stat_indi            = $this->getBlockSetting($block_id, 'stat_indi', '1');
276		$stat_fam             = $this->getBlockSetting($block_id, 'stat_fam', '1');
277		$stat_sour            = $this->getBlockSetting($block_id, 'stat_sour', '1');
278		$stat_media           = $this->getBlockSetting($block_id, 'stat_media', '1');
279		$stat_repo            = $this->getBlockSetting($block_id, 'stat_repo', '1');
280		$stat_surname         = $this->getBlockSetting($block_id, 'stat_surname', '1');
281		$stat_events          = $this->getBlockSetting($block_id, 'stat_events', '1');
282		$stat_users           = $this->getBlockSetting($block_id, 'stat_users', '1');
283		$stat_first_birth     = $this->getBlockSetting($block_id, 'stat_first_birth', '1');
284		$stat_last_birth      = $this->getBlockSetting($block_id, 'stat_last_birth', '1');
285		$stat_first_death     = $this->getBlockSetting($block_id, 'stat_first_death', '1');
286		$stat_last_death      = $this->getBlockSetting($block_id, 'stat_last_death', '1');
287		$stat_long_life       = $this->getBlockSetting($block_id, 'stat_long_life', '1');
288		$stat_avg_life        = $this->getBlockSetting($block_id, 'stat_avg_life', '1');
289		$stat_most_chil       = $this->getBlockSetting($block_id, 'stat_most_chil', '1');
290		$stat_avg_chil        = $this->getBlockSetting($block_id, 'stat_avg_chil', '1');
291
292		?>
293		<tr>
294			<td class="descriptionbox wrap width33">
295				<label for="show-last-update">
296					<?php echo /* I18N: label for yes/no option */ I18N::translate('Show date of last update') ?>
297				</label>
298			</td>
299			<td class="optionbox">
300				<input type="checkbox" value="yes" id="show-last-update" name="show_last_update" <?php echo $show_last_update ? 'checked' : ''; ?>>
301			</td>
302		</tr>
303		<tr>
304			<td class="descriptionbox wrap width33">
305				<?php echo I18N::translate('Statistics'); ?>
306			</td>
307			<td class="optionbox">
308				<table>
309					<tbody>
310						<tr>
311							<td>
312								<label>
313									<input type="checkbox" value="yes" name="stat_indi" <?php echo $stat_indi ? 'checked' : ''; ?>>
314									<?php echo I18N::translate('Individuals'); ?>
315								</label>
316							</td>
317							<td>
318								<label>
319									<input type="checkbox" value="yes" name="stat_first_birth" <?php echo $stat_first_birth ? 'checked' : ''; ?>>
320									<?php echo I18N::translate('Earliest birth year'); ?>
321								</label>
322							</td>
323						</tr>
324						<tr>
325							<td>
326								<label>
327									<input type="checkbox" value="yes" name="stat_surname" <?php echo $stat_surname ? 'checked' : ''; ?>>
328									<?php echo I18N::translate('Total surnames'); ?>
329								</label>
330							</td>
331							<td>
332								<label>
333									<input type="checkbox" value="yes" name="stat_last_birth" <?php echo $stat_last_birth ? 'checked' : ''; ?>>
334									<?php echo I18N::translate('Latest birth year'); ?>
335								</label>
336							</td>
337						</tr>
338						<tr>
339							<td>
340								<label>
341									<input type="checkbox" value="yes" name="stat_fam" <?php echo $stat_fam ? 'checked' : ''; ?>>
342									<?php echo I18N::translate('Families'); ?>
343								</label>
344							</td>
345							<td>
346								<label>
347									<input type="checkbox" value="yes" name="stat_first_death" <?php echo $stat_first_death ? 'checked' : ''; ?>>
348									<?php echo I18N::translate('Earliest death year'); ?>
349								</label>
350							</td>
351						</tr>
352						<tr>
353							<td>
354								<label>
355									<input type="checkbox" value="yes" name="stat_sour" <?php echo $stat_sour ? 'checked' : ''; ?>>
356									<?php echo I18N::translate('Sources'); ?>
357								</label>
358							</td>
359							<td>
360								<label>
361									<input type="checkbox" value="yes" name="stat_last_death" <?php echo $stat_last_death ? 'checked' : ''; ?>>
362									<?php echo I18N::translate('Latest death year'); ?>
363								</label>
364							</td>
365						</tr>
366						<tr>
367							<td>
368								<label>
369									<input type="checkbox" value="yes" name="stat_media" <?php echo $stat_media ? 'checked' : ''; ?>>
370									<?php echo I18N::translate('Media objects'); ?>
371								</label>
372							</td>
373							<td>
374								<label>
375									<input type="checkbox" value="yes" name="stat_long_life" <?php echo $stat_long_life ? 'checked' : ''; ?>>
376									<?php echo I18N::translate('Individual who lived the longest'); ?>
377								</label>
378							</td>
379						</tr>
380						<tr>
381							<td>
382								<label>
383									<input type="checkbox" value="yes" name="stat_repo" <?php echo $stat_repo ? 'checked' : ''; ?>>
384									<?php echo I18N::translate('Repositories'); ?>
385								</label>
386							</td>
387							<td>
388								<label>
389									<input type="checkbox" value="yes" name="stat_avg_life" <?php echo $stat_avg_life ? 'checked' : ''; ?>>
390									<?php echo I18N::translate('Average age at death'); ?>
391								</label>
392							</td>
393						</tr>
394						<tr>
395							<td>
396								<label>
397									<input type="checkbox" value="yes" name="stat_events" <?php echo $stat_events ? 'checked' : ''; ?>>
398									<?php echo I18N::translate('Total events'); ?>
399								</label>
400							</td>
401							<td>
402								<label>
403									<input type="checkbox" value="yes" name="stat_most_chil" <?php echo $stat_most_chil ? 'checked' : ''; ?>>
404									<?php echo I18N::translate('Family with the most children'); ?>
405								</label>
406							</td>
407						</tr>
408						<tr>
409							<td>
410								<label>
411									<input type="checkbox" value="yes" name="stat_users" <?php echo $stat_users ? 'checked' : ''; ?>>
412									<?php echo I18N::translate('Total users'); ?>
413								</label>
414							</td>
415							<td>
416								<label>
417									<input type="checkbox" value="yes" name="stat_avg_chil" <?php echo $stat_avg_chil ? 'checked' : ''; ?>>
418									<?php echo I18N::translate('Average number of children per family'); ?>
419								</label>
420							</td>
421						</tr>
422					</tbody>
423				</table>
424			</td>
425		</tr>
426		<tr>
427			<td class="descriptionbox wrap width33">
428				<label for="show-common-surnames">
429					<?php echo I18N::translate('Most common surnames') ?>
430				</label>
431			</td>
432			<td class="optionbox">
433				<input type="checkbox" value="yes" id="show-common-surnames" name="show_common_surnames" <?php echo $show_common_surnames ? 'checked' : ''; ?>>
434			</td>
435		</tr>
436		<tr>
437			<td class="descriptionbox wrap width33">
438				<label for="number-of-surnames">
439					<?php echo I18N::translate('Number of surnames') ?>
440				</label>
441			</td>
442			<td class="optionbox">
443				<input
444					id="number-of-surnames"
445					maxlength="5"
446					name="number_of_surnames"
447					pattern="[1-9][0-9]*"
448					required
449					type="text"
450					value="<?php echo Filter::escapeHtml($number_of_surnames); ?>"
451				>
452			</td>
453		</tr>
454		<?php
455	}
456}
457