xref: /webtrees/app/Module/FamilyTreeStatisticsModule.php (revision f36626dbc06faf6e61af35fd18604d82d86c966f)
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\Database;
20use Fisharebest\Webtrees\Filter;
21use Fisharebest\Webtrees\Functions\FunctionsDb;
22use Fisharebest\Webtrees\Functions\FunctionsEdit;
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;
29use Fisharebest\Webtrees\Tree;
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 = array()) {
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		// This can be overriden when embedding in an HTML block
81		$block     = '0';
82
83		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) {
84			if (array_key_exists($name, $cfg)) {
85				$$name = $cfg[$name];
86			}
87		}
88
89		$id    = $this->getName() . $block_id;
90		$class = $this->getName() . '_block';
91		if ($ctype === 'gedcom' && Auth::isManager($WT_TREE) || $ctype === 'user' && Auth::check()) {
92			$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>';
93		} else {
94			$title = '';
95		}
96		$title .= $this->getTitle() . ' — ' . $WT_TREE->getTitleHtml();
97
98		$stats = new Stats($WT_TREE);
99
100		$content = '';
101
102		if ($show_last_update) {
103			$content .= '<p>' . /* I18N: %s is a date */
104				I18N::translate('This family tree was last updated on %s.', strip_tags($stats->gedcomUpdated())) . '</p>';
105		}
106
107		/** Responsive Design */
108		$content .= '<div class="stat-table1">';
109		if ($stat_indi) {
110			$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>';
111			$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>';
112			$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>';
113		}
114		if ($stat_surname) {
115			$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>';
116		}
117		if ($stat_fam) {
118			$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>';
119		}
120		if ($stat_sour) {
121			$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>';
122		}
123		if ($stat_media) {
124			$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>';
125		}
126		if ($stat_repo) {
127			$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>';
128		}
129		if ($stat_events) {
130			$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>';
131		}
132		if ($stat_users) {
133			$content .= '<div class="stat-row"><div class="facts_label stat-cell">' . I18N::translate('Total users') . '</div><div class="facts_value stats_value stat-cell">';
134			if (Auth::isManager($WT_TREE)) {
135				$content .= '<a href="admin_users.php">' . $stats->totalUsers() . '</a>';
136			} else {
137				$content .= $stats->totalUsers();
138			}
139			$content .= '</div></div>';
140		}
141		if (!$block) {
142			$content .= '</div><div class="facts_table stat-table2">';
143		}
144		if ($stat_first_birth) {
145			$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>';
146			if (!$block) {
147				$content .= '<div class="facts_value stat-cell left">' . $stats->firstBirth() . '</div>';
148			}
149			$content .= '</div>';
150		}
151		if ($stat_last_birth) {
152			$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>';
153			if (!$block) {
154				$content .= '<div class="facts_value stat-cell left">' . $stats->lastBirth() . '</div>';
155			}
156			$content .= '</div>';
157		}
158		if ($stat_first_death) {
159			$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>';
160			if (!$block) {
161				$content .= '<div class="facts_value stat-cell left">' . $stats->firstDeath() . '</div>';
162			}
163			$content .= '</div>';
164		}
165		if ($stat_last_death) {
166			$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>';
167			if (!$block) {
168				$content .= '<div class="facts_value stat-cell left">' . $stats->lastDeath() . '</div>';
169			}
170			$content .= '</div>';
171		}
172		if ($stat_long_life) {
173			$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>';
174			if (!$block) {
175				$content .= '<div class="facts_value stat-cell left">' . $stats->longestLife() . '</div>';
176			}
177			$content .= '</div>';
178		}
179		if ($stat_avg_life) {
180			$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>';
181			if (!$block) {
182				$content .= '<div class="facts_value stat-cell left">' . I18N::translate('Males') . ':&nbsp;' . $stats->averageLifespanMale();
183				$content .= '&nbsp;&nbsp;&nbsp;' . I18N::translate('Females') . ':&nbsp;' . $stats->averageLifespanFemale() . '</div>';
184			}
185			$content .= '</div>';
186		}
187
188		if ($stat_most_chil && !$block) {
189			$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>';
190			if (!$block) {
191				$content .= '<div class="facts_value stat-cell left">' . $stats->largestFamily() . '</div>';
192			}
193			$content .= '</div>';
194		}
195		if ($stat_avg_chil) {
196			$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>';
197			if (!$block) {
198				$content .= '<div class="facts_value stat-cell left"></div>';
199			}
200			$content .= '</div>';
201		}
202		$content .= '</div>';
203
204		if ($show_common_surnames) {
205			$surnames = FunctionsDb::getTopSurnames($WT_TREE->getTreeId(), 0, (int) $number_of_surnames);
206
207			$all_surnames = array();
208			foreach (array_keys($surnames) as $surname) {
209				$all_surnames = array_merge($all_surnames, QueryName::surnames($WT_TREE, $surname, '', false, false));
210			}
211
212			if (!empty($surnames)) {
213				ksort($all_surnames);
214				$content .= '<div class="clearfloat">';
215				$content .= '<p>';
216				$content .= '<strong>' . I18N::translate('Most common surnames') . '</strong>';
217				$content .= '<br>';
218				$content .= '<span class="common_surnames">' . FunctionsPrintLists::surnameList($all_surnames, 2, false, 'indilist.php', $WT_TREE) . '</span>';
219				$content .= '</p>';
220				$content .= '</div>';
221			}
222		}
223
224		if ($template) {
225			return Theme::theme()->formatBlock($id, $title, $class, $content);
226		} else {
227			return $content;
228		}
229	}
230
231	/** {@inheritdoc} */
232	public function loadAjax() {
233		return true;
234	}
235
236	/** {@inheritdoc} */
237	public function isUserBlock() {
238		return true;
239	}
240
241	/** {@inheritdoc} */
242	public function isGedcomBlock() {
243		return true;
244	}
245
246	/**
247	 * An HTML form to edit block settings
248	 *
249	 * @param int $block_id
250	 */
251	public function configureBlock($block_id) {
252		if (Filter::postBool('save') && Filter::checkCsrf()) {
253			$this->setBlockSetting($block_id, 'show_last_update', Filter::postBool('show_last_update'));
254			$this->setBlockSetting($block_id, 'show_common_surnames', Filter::postBool('show_common_surnames'));
255			$this->setBlockSetting($block_id, 'number_of_surnames', Filter::postInteger('number_of_surnames'));
256			$this->setBlockSetting($block_id, 'stat_indi', Filter::postBool('stat_indi'));
257			$this->setBlockSetting($block_id, 'stat_fam', Filter::postBool('stat_fam'));
258			$this->setBlockSetting($block_id, 'stat_sour', Filter::postBool('stat_sour'));
259			$this->setBlockSetting($block_id, 'stat_other', Filter::postBool('stat_other'));
260			$this->setBlockSetting($block_id, 'stat_media', Filter::postBool('stat_media'));
261			$this->setBlockSetting($block_id, 'stat_repo', Filter::postBool('stat_repo'));
262			$this->setBlockSetting($block_id, 'stat_surname', Filter::postBool('stat_surname'));
263			$this->setBlockSetting($block_id, 'stat_events', Filter::postBool('stat_events'));
264			$this->setBlockSetting($block_id, 'stat_users', Filter::postBool('stat_users'));
265			$this->setBlockSetting($block_id, 'stat_first_birth', Filter::postBool('stat_first_birth'));
266			$this->setBlockSetting($block_id, 'stat_last_birth', Filter::postBool('stat_last_birth'));
267			$this->setBlockSetting($block_id, 'stat_first_death', Filter::postBool('stat_first_death'));
268			$this->setBlockSetting($block_id, 'stat_last_death', Filter::postBool('stat_last_death'));
269			$this->setBlockSetting($block_id, 'stat_long_life', Filter::postBool('stat_long_life'));
270			$this->setBlockSetting($block_id, 'stat_avg_life', Filter::postBool('stat_avg_life'));
271			$this->setBlockSetting($block_id, 'stat_most_chil', Filter::postBool('stat_most_chil'));
272			$this->setBlockSetting($block_id, 'stat_avg_chil', Filter::postBool('stat_avg_chil'));
273		}
274
275		$show_last_update     = $this->getBlockSetting($block_id, 'show_last_update', '1');
276		$show_common_surnames = $this->getBlockSetting($block_id, 'show_common_surnames', '1');
277		$number_of_surnames   = $this->getBlockSetting($block_id, 'number_of_surnames', self::DEFAULT_NUMBER_OF_SURNAMES);
278		$stat_indi            = $this->getBlockSetting($block_id, 'stat_indi', '1');
279		$stat_fam             = $this->getBlockSetting($block_id, 'stat_fam', '1');
280		$stat_sour            = $this->getBlockSetting($block_id, 'stat_sour', '1');
281		$stat_media           = $this->getBlockSetting($block_id, 'stat_media', '1');
282		$stat_repo            = $this->getBlockSetting($block_id, 'stat_repo', '1');
283		$stat_surname         = $this->getBlockSetting($block_id, 'stat_surname', '1');
284		$stat_events          = $this->getBlockSetting($block_id, 'stat_events', '1');
285		$stat_users           = $this->getBlockSetting($block_id, 'stat_users', '1');
286		$stat_first_birth     = $this->getBlockSetting($block_id, 'stat_first_birth', '1');
287		$stat_last_birth      = $this->getBlockSetting($block_id, 'stat_last_birth', '1');
288		$stat_first_death     = $this->getBlockSetting($block_id, 'stat_first_death', '1');
289		$stat_last_death      = $this->getBlockSetting($block_id, 'stat_last_death', '1');
290		$stat_long_life       = $this->getBlockSetting($block_id, 'stat_long_life', '1');
291		$stat_avg_life        = $this->getBlockSetting($block_id, 'stat_avg_life', '1');
292		$stat_most_chil       = $this->getBlockSetting($block_id, 'stat_most_chil', '1');
293		$stat_avg_chil        = $this->getBlockSetting($block_id, 'stat_avg_chil', '1');
294
295		?>
296		<tr>
297			<td class="descriptionbox wrap width33">
298				<label for="show-last-update">
299					<?php echo /* I18N: label for yes/no option */ I18N::translate('Show date of last update') ?>
300				</label>
301			</td>
302			<td class="optionbox">
303				<input type="checkbox" value="yes" id="show-last-update" name="show_last_update" <?php echo $show_last_update ? 'checked' : ''; ?>>
304			</td>
305		</tr>
306		<tr>
307			<td class="descriptionbox wrap width33">
308				<?php echo I18N::translate('Statistics'); ?>
309			</td>
310			<td class="optionbox">
311				<table>
312					<tbody>
313						<tr>
314							<td>
315								<label>
316									<input type="checkbox" value="yes" name="stat_indi" <?php echo $stat_indi ? 'checked' : ''; ?>>
317									<?php echo I18N::translate('Individuals'); ?>
318								</label>
319							</td>
320							<td>
321								<label>
322									<input type="checkbox" value="yes" name="stat_first_birth" <?php echo $stat_first_birth ? 'checked' : ''; ?>>
323									<?php echo I18N::translate('Earliest birth year'); ?>
324								</label>
325							</td>
326						</tr>
327						<tr>
328							<td>
329								<label>
330									<input type="checkbox" value="yes" name="stat_surname" <?php echo $stat_surname ? 'checked' : ''; ?>>
331									<?php echo I18N::translate('Total surnames'); ?>
332								</label>
333							</td>
334							<td>
335								<label>
336									<input type="checkbox" value="yes" name="stat_last_birth" <?php echo $stat_last_birth ? 'checked' : ''; ?>>
337									<?php echo I18N::translate('Latest birth year'); ?>
338								</label>
339							</td>
340						</tr>
341						<tr>
342							<td>
343								<label>
344									<input type="checkbox" value="yes" name="stat_fam" <?php echo $stat_fam ? 'checked' : ''; ?>>
345									<?php echo I18N::translate('Families'); ?>
346								</label>
347							</td>
348							<td>
349								<label>
350									<input type="checkbox" value="yes" name="stat_first_death" <?php echo $stat_first_death ? 'checked' : ''; ?>>
351									<?php echo I18N::translate('Earliest death year'); ?>
352								</label>
353							</td>
354						</tr>
355						<tr>
356							<td>
357								<label>
358									<input type="checkbox" value="yes" name="stat_sour" <?php echo $stat_sour ? 'checked' : ''; ?>>
359									<?php echo I18N::translate('Sources'); ?>
360								</label>
361							</td>
362							<td>
363								<label>
364									<input type="checkbox" value="yes" name="stat_last_death" <?php echo $stat_last_death ? 'checked' : ''; ?>>
365									<?php echo I18N::translate('Latest death year'); ?>
366								</label>
367							</td>
368						</tr>
369						<tr>
370							<td>
371								<label>
372									<input type="checkbox" value="yes" name="stat_media" <?php echo $stat_media ? 'checked' : ''; ?>>
373									<?php echo I18N::translate('Media objects'); ?>
374								</label>
375							</td>
376							<td>
377								<label>
378									<input type="checkbox" value="yes" name="stat_long_life" <?php echo $stat_long_life ? 'checked' : ''; ?>>
379									<?php echo I18N::translate('Individual who lived the longest'); ?>
380								</label>
381							</td>
382						</tr>
383						<tr>
384							<td>
385								<label>
386									<input type="checkbox" value="yes" name="stat_repo" <?php echo $stat_repo ? 'checked' : ''; ?>>
387									<?php echo I18N::translate('Repositories'); ?>
388								</label>
389							</td>
390							<td>
391								<label>
392									<input type="checkbox" value="yes" name="stat_avg_life" <?php echo $stat_avg_life ? 'checked' : ''; ?>>
393									<?php echo I18N::translate('Average age at death'); ?>
394								</label>
395							</td>
396						</tr>
397						<tr>
398							<td>
399								<label>
400									<input type="checkbox" value="yes" name="stat_events" <?php echo $stat_events ? 'checked' : ''; ?>>
401									<?php echo I18N::translate('Total events'); ?>
402								</label>
403							</td>
404							<td>
405								<label>
406									<input type="checkbox" value="yes" name="stat_most_chil" <?php echo $stat_most_chil ? 'checked' : ''; ?>>
407									<?php echo I18N::translate('Family with the most children'); ?>
408								</label>
409							</td>
410						</tr>
411						<tr>
412							<td>
413								<label>
414									<input type="checkbox" value="yes" name="stat_users" <?php echo $stat_users ? 'checked' : ''; ?>>
415									<?php echo I18N::translate('Total users'); ?>
416								</label>
417							</td>
418							<td>
419								<label>
420									<input type="checkbox" value="yes" name="stat_avg_chil" <?php echo $stat_avg_chil ? 'checked' : ''; ?>>
421									<?php echo I18N::translate('Average number of children per family'); ?>
422								</label>
423							</td>
424						</tr>
425					</tbody>
426				</table>
427			</td>
428		</tr>
429		<tr>
430			<td class="descriptionbox wrap width33">
431				<label for="show-common-surnames">
432					<?php echo I18N::translate('Most common surnames') ?>
433				</label>
434			</td>
435			<td class="optionbox">
436				<input type="checkbox" value="yes" id="show-common-surnames" name="show_common_surnames" <?php echo $show_common_surnames ? 'checked' : ''; ?>>
437			</td>
438		</tr>
439		<tr>
440			<td class="descriptionbox wrap width33">
441				<label for="number-of-surnames">
442					<?php echo I18N::translate('Number of surnames') ?>
443				</label>
444			</td>
445			<td class="optionbox">
446				<input
447					id="number-of-surnames"
448					maxlength="5"
449					name="number_of_surnames"
450					pattern="[1-9][0-9]*"
451					required
452					type="text"
453					value="<?php echo Filter::escapeHtml($number_of_surnames); ?>"
454				>
455			</td>
456		</tr>
457		<?php
458	}
459}
460