xref: /webtrees/app/Module/FamilyTreeStatisticsModule.php (revision 2cc41105854089b2b62d3c6f69fc0540e703f361)
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\FunctionsEdit;
22use Fisharebest\Webtrees\I18N;
23use Fisharebest\Webtrees\Stats;
24use Fisharebest\Webtrees\Theme;
25
26/**
27 * Class FamilyTreeStatisticsModule
28 */
29class FamilyTreeStatisticsModule extends AbstractModule implements ModuleBlockInterface {
30	/** {@inheritdoc} */
31	public function getTitle() {
32		return /* I18N: Name of a module */ I18N::translate('Statistics');
33	}
34
35	/** {@inheritdoc} */
36	public function getDescription() {
37		return /* I18N: Description of “Statistics” module */ I18N::translate('The size of the family tree, earliest and latest events, common names, etc.');
38	}
39
40	/**
41	 * Generate the HTML content of this block.
42	 *
43	 * @param int      $block_id
44	 * @param bool     $template
45	 * @param string[] $cfg
46	 *
47	 * @return string
48	 */
49	public function getBlock($block_id, $template = true, $cfg = array()) {
50		global $WT_TREE, $ctype;
51
52		$show_last_update     = $this->getBlockSetting($block_id, 'show_last_update', '1');
53		$show_common_surnames = $this->getBlockSetting($block_id, 'show_common_surnames', '1');
54		$stat_indi            = $this->getBlockSetting($block_id, 'stat_indi', '1');
55		$stat_fam             = $this->getBlockSetting($block_id, 'stat_fam', '1');
56		$stat_sour            = $this->getBlockSetting($block_id, 'stat_sour', '1');
57		$stat_media           = $this->getBlockSetting($block_id, 'stat_media', '1');
58		$stat_repo            = $this->getBlockSetting($block_id, 'stat_repo', '1');
59		$stat_surname         = $this->getBlockSetting($block_id, 'stat_surname', '1');
60		$stat_events          = $this->getBlockSetting($block_id, 'stat_events', '1');
61		$stat_users           = $this->getBlockSetting($block_id, 'stat_users', '1');
62		$stat_first_birth     = $this->getBlockSetting($block_id, 'stat_first_birth', '1');
63		$stat_last_birth      = $this->getBlockSetting($block_id, 'stat_last_birth', '1');
64		$stat_first_death     = $this->getBlockSetting($block_id, 'stat_first_death', '1');
65		$stat_last_death      = $this->getBlockSetting($block_id, 'stat_last_death', '1');
66		$stat_long_life       = $this->getBlockSetting($block_id, 'stat_long_life', '1');
67		$stat_avg_life        = $this->getBlockSetting($block_id, 'stat_avg_life', '1');
68		$stat_most_chil       = $this->getBlockSetting($block_id, 'stat_most_chil', '1');
69		$stat_avg_chil        = $this->getBlockSetting($block_id, 'stat_avg_chil', '1');
70
71		// This can be overriden when embedding in an HTML block
72		$block     = '0';
73		$stat_link = '1';
74
75		foreach (array('show_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', 'stat_link', 'block') as $name) {
76			if (array_key_exists($name, $cfg)) {
77				$$name = $cfg[$name];
78			}
79		}
80
81		$id    = $this->getName() . $block_id;
82		$class = $this->getName() . '_block';
83		if ($ctype === 'gedcom' && Auth::isManager($WT_TREE) || $ctype === 'user' && Auth::check()) {
84			$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>';
85		} else {
86			$title = '';
87		}
88		$title .= $this->getTitle();
89
90		$stats = new Stats($WT_TREE);
91
92		$content = '<b>' . $WT_TREE->getTitleHtml() . '</b><br>';
93
94		if ($show_last_update) {
95			$content .= '<div>' . /* I18N: %s is a date */ I18N::translate('This family tree was last updated on %s.', strip_tags($stats->gedcomUpdated())) . '</div>';
96		}
97	/** Responsive Design */
98
99	$content .= '<div class="stat-table1">';
100		if ($stat_indi) {
101			$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>';
102			$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>';
103			$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>';
104		}
105		if ($stat_surname) {
106			$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>';
107		}
108		if ($stat_fam) {
109			$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>';
110		}
111		if ($stat_sour) {
112			$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>';
113		}
114		if ($stat_media) {
115			$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>';
116		}
117		if ($stat_repo) {
118			$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>';
119		}
120		if ($stat_events) {
121			$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>';
122		}
123		if ($stat_users) {
124			$content .= '<div class="stat-row"><div class="facts_label stat-cell">' . I18N::translate('Total users') . '</div><div class="facts_value stats_value stat-cell">';
125			if (Auth::isManager($WT_TREE)) {
126				$content .= '<a href="admin_users.php">' . $stats->totalUsers() . '</a>';
127			} else {
128				$content .= $stats->totalUsers();
129			}
130			$content .= '</div></div>';
131		}
132		if (!$block) {
133			$content .= '</div><div class="facts_table stat-table2">';
134		}
135		if ($stat_first_birth) {
136			$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>';
137			if (!$block) {
138				$content .= '<div class="facts_value stat-cell left">' . $stats->firstBirth() . '</div>';
139			}
140			$content .= '</div>';
141		}
142		if ($stat_last_birth) {
143			$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>';
144			if (!$block) {
145				$content .= '<div class="facts_value stat-cell left">' . $stats->lastBirth() . '</div>';
146			}
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			if (!$block) {
152				$content .= '<div class="facts_value stat-cell left">' . $stats->firstDeath() . '</div>';
153			}
154			$content .= '</div>';
155		}
156		if ($stat_last_death) {
157			$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>';
158			if (!$block) {
159				$content .= '<div class="facts_value stat-cell left">' . $stats->lastDeath() . '</div>';
160			}
161			$content .= '</div>';
162		}
163		if ($stat_long_life) {
164			$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>';
165			if (!$block) {
166				$content .= '<div class="facts_value stat-cell left">' . $stats->LongestLife() . '</div>';
167			}
168			$content .= '</div>';
169		}
170		if ($stat_avg_life) {
171			$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>';
172			if (!$block) {
173				$content .= '<div class="facts_value stat-cell left">' . I18N::translate('Males') . ':&nbsp;' . $stats->averageLifespanMale();
174				$content .= '&nbsp;&nbsp;&nbsp;' . I18N::translate('Females') . ':&nbsp;' . $stats->averageLifespanFemale() . '</div>';
175			}
176			$content .= '</div>';
177		}
178
179		if ($stat_most_chil && !$block) {
180			$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>';
181			if (!$block) {
182				$content .= '<div class="facts_value stat-cell left">' . $stats->largestFamily() . '</div>';
183			}
184			$content .= '</div>';
185		}
186		if ($stat_avg_chil) {
187			$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>';
188			if (!$block) {
189				$content .= '<div class="facts_value stat-cell left"></div>';
190			}
191			$content .= '</div>';
192		}
193		if ($stat_link) {
194			$content .= '</div><div class="clearfloat"><a href="statistics.php?ged=' . $WT_TREE->getNameUrl() . '" rel="nofollow"><b>' . I18N::translate('View statistics as graphs') . '</b></a></div>';
195		}
196		// NOTE: Print the most common surnames
197		if ($show_common_surnames) {
198			$surnames = FunctionsDb::getCommonSurnames($WT_TREE->getPreference('COMMON_NAMES_THRESHOLD'), $WT_TREE);
199			if (count($surnames) > 0) {
200				$content .= '<p><b>' . I18N::translate('Most common surnames') . '</b></p>';
201				$content .= '<div class="common_surnames">';
202				$i = 0;
203				foreach ($surnames as $indexval => $surname) {
204					if (stristr($surname['name'], '@N.N') === false) {
205						if ($i > 0) {
206							$content .= ', ';
207						}
208						$content .= '<a href="' . "indilist.php?ged=" . $WT_TREE->getNameUrl() . "&amp;surname=" . rawurlencode($surname['name']) . '">' . $surname['name'] . '</a>';
209						$i++;
210					}
211				}
212			}
213		}
214
215		if ($template) {
216			return Theme::theme()->formatBlock($id, $title, $class, $content);
217		} else {
218			return $content;
219		}
220	}
221
222	/** {@inheritdoc} */
223	public function loadAjax() {
224		return true;
225	}
226
227	/** {@inheritdoc} */
228	public function isUserBlock() {
229		return true;
230	}
231
232	/** {@inheritdoc} */
233	public function isGedcomBlock() {
234		return true;
235	}
236
237	/**
238	 * An HTML form to edit block settings
239	 *
240	 * @param int $block_id
241	 */
242	public function configureBlock($block_id) {
243		if (Filter::postBool('save') && Filter::checkCsrf()) {
244			$this->setBlockSetting($block_id, 'show_last_update', Filter::postBool('show_last_update'));
245			$this->setBlockSetting($block_id, 'show_common_surnames', Filter::postBool('show_common_surnames'));
246			$this->setBlockSetting($block_id, 'stat_indi', Filter::postBool('stat_indi'));
247			$this->setBlockSetting($block_id, 'stat_fam', Filter::postBool('stat_fam'));
248			$this->setBlockSetting($block_id, 'stat_sour', Filter::postBool('stat_sour'));
249			$this->setBlockSetting($block_id, 'stat_other', Filter::postBool('stat_other'));
250			$this->setBlockSetting($block_id, 'stat_media', Filter::postBool('stat_media'));
251			$this->setBlockSetting($block_id, 'stat_repo', Filter::postBool('stat_repo'));
252			$this->setBlockSetting($block_id, 'stat_surname', Filter::postBool('stat_surname'));
253			$this->setBlockSetting($block_id, 'stat_events', Filter::postBool('stat_events'));
254			$this->setBlockSetting($block_id, 'stat_users', Filter::postBool('stat_users'));
255			$this->setBlockSetting($block_id, 'stat_first_birth', Filter::postBool('stat_first_birth'));
256			$this->setBlockSetting($block_id, 'stat_last_birth', Filter::postBool('stat_last_birth'));
257			$this->setBlockSetting($block_id, 'stat_first_death', Filter::postBool('stat_first_death'));
258			$this->setBlockSetting($block_id, 'stat_last_death', Filter::postBool('stat_last_death'));
259			$this->setBlockSetting($block_id, 'stat_long_life', Filter::postBool('stat_long_life'));
260			$this->setBlockSetting($block_id, 'stat_avg_life', Filter::postBool('stat_avg_life'));
261			$this->setBlockSetting($block_id, 'stat_most_chil', Filter::postBool('stat_most_chil'));
262			$this->setBlockSetting($block_id, 'stat_avg_chil', Filter::postBool('stat_avg_chil'));
263		}
264
265		$show_last_update     = $this->getBlockSetting($block_id, 'show_last_update', '1');
266		$show_common_surnames = $this->getBlockSetting($block_id, 'show_common_surnames', '1');
267		$stat_indi            = $this->getBlockSetting($block_id, 'stat_indi', '1');
268		$stat_fam             = $this->getBlockSetting($block_id, 'stat_fam', '1');
269		$stat_sour            = $this->getBlockSetting($block_id, 'stat_sour', '1');
270		$stat_media           = $this->getBlockSetting($block_id, 'stat_media', '1');
271		$stat_repo            = $this->getBlockSetting($block_id, 'stat_repo', '1');
272		$stat_surname         = $this->getBlockSetting($block_id, 'stat_surname', '1');
273		$stat_events          = $this->getBlockSetting($block_id, 'stat_events', '1');
274		$stat_users           = $this->getBlockSetting($block_id, 'stat_users', '1');
275		$stat_first_birth     = $this->getBlockSetting($block_id, 'stat_first_birth', '1');
276		$stat_last_birth      = $this->getBlockSetting($block_id, 'stat_last_birth', '1');
277		$stat_first_death     = $this->getBlockSetting($block_id, 'stat_first_death', '1');
278		$stat_last_death      = $this->getBlockSetting($block_id, 'stat_last_death', '1');
279		$stat_long_life       = $this->getBlockSetting($block_id, 'stat_long_life', '1');
280		$stat_avg_life        = $this->getBlockSetting($block_id, 'stat_avg_life', '1');
281		$stat_most_chil       = $this->getBlockSetting($block_id, 'stat_most_chil', '1');
282		$stat_avg_chil        = $this->getBlockSetting($block_id, 'stat_avg_chil', '1');
283
284		echo '<tr><td class="descriptionbox wrap width33">';
285		echo /* I18N: label for yes/no option */ I18N::translate('Show date of last update?');
286		echo '</td><td class="optionbox">';
287		echo FunctionsEdit::editFieldYesNo('show_last_update', $show_last_update);
288		echo '</td></tr>';
289
290		echo '<tr><td class="descriptionbox wrap width33">';
291		echo I18N::translate('Show common surnames?');
292		echo '</td><td class="optionbox">';
293		echo FunctionsEdit::editFieldYesNo('show_common_surnames', $show_common_surnames);
294		echo '</td></tr>';
295
296?>
297	<tr>
298	<td class="descriptionbox wrap width33"><?php echo I18N::translate('Select the stats to show in this block'); ?></td>
299	<td class="optionbox">
300	<table>
301		<tbody>
302			<tr>
303				<td>
304					<label>
305						<input type="checkbox" value="yes" name="stat_indi" <?php echo $stat_indi ? 'checked' : ''; ?>>
306						<?php echo I18N::translate('Individuals'); ?>
307					</label>
308				</td>
309				<td>
310					<label>
311						<input type="checkbox" value="yes" name="stat_first_birth" <?php echo $stat_first_birth ? 'checked' : ''; ?>>
312						<?php echo I18N::translate('Earliest birth year'); ?>
313					</label>
314				</td>
315			</tr>
316			<tr>
317				<td>
318					<label>
319						<input type="checkbox" value="yes" name="stat_surname" <?php echo $stat_surname ? 'checked' : ''; ?>>
320						<?php echo I18N::translate('Total surnames'); ?>
321					</label>
322				</td>
323				<td>
324					<label>
325						<input type="checkbox" value="yes" name="stat_last_birth" <?php echo $stat_last_birth ? 'checked' : ''; ?>>
326						<?php echo I18N::translate('Latest birth year'); ?>
327					</label>
328				</td>
329			</tr>
330			<tr>
331				<td>
332					<label>
333						<input type="checkbox" value="yes" name="stat_fam" <?php echo $stat_fam ? 'checked' : ''; ?>>
334						<?php echo I18N::translate('Families'); ?>
335					</label>
336				</td>
337				<td>
338					<label>
339						<input type="checkbox" value="yes" name="stat_first_death" <?php echo $stat_first_death ? 'checked' : ''; ?>>
340						<?php echo I18N::translate('Earliest death year'); ?>
341					</label>
342				</td>
343			</tr>
344			<tr>
345				<td>
346					<label>
347						<input type="checkbox" value="yes" name="stat_sour" <?php echo $stat_sour ? 'checked' : ''; ?>>
348						<?php echo I18N::translate('Sources'); ?>
349					</label>
350				</td>
351				<td>
352					<label>
353						<input type="checkbox" value="yes" name="stat_last_death" <?php echo $stat_last_death ? 'checked' : ''; ?>>
354						<?php echo I18N::translate('Latest death year'); ?>
355					</label>
356				</td>
357			</tr>
358			<tr>
359				<td>
360					<label>
361						<input type="checkbox" value="yes" name="stat_media" <?php echo $stat_media ? 'checked' : ''; ?>>
362						<?php echo I18N::translate('Media objects'); ?>
363					</label>
364				</td>
365				<td>
366					<label>
367						<input type="checkbox" value="yes" name="stat_long_life" <?php echo $stat_long_life ? 'checked' : ''; ?>>
368						<?php echo I18N::translate('Individual who lived the longest'); ?>
369					</label>
370				</td>
371			</tr>
372			<tr>
373				<td>
374					<label>
375						<input type="checkbox" value="yes" name="stat_repo" <?php echo $stat_repo ? 'checked' : ''; ?>>
376						<?php echo I18N::translate('Repositories'); ?>
377					</label>
378				</td>
379				<td>
380					<label>
381						<input type="checkbox" value="yes" name="stat_avg_life" <?php echo $stat_avg_life ? 'checked' : ''; ?>>
382						<?php echo I18N::translate('Average age at death'); ?>
383					</label>
384				</td>
385			</tr>
386			<tr>
387				<td>
388					<label>
389						<input type="checkbox" value="yes" name="stat_events" <?php echo $stat_events ? 'checked' : ''; ?>>
390						<?php echo I18N::translate('Total events'); ?>
391					</label>
392				</td>
393				<td>
394					<label>
395						<input type="checkbox" value="yes" name="stat_most_chil" <?php echo $stat_most_chil ? 'checked' : ''; ?>>
396						<?php echo I18N::translate('Family with the most children'); ?>
397					</label>
398				</td>
399			</tr>
400			<tr>
401				<td>
402					<label>
403						<input type="checkbox" value="yes" name="stat_users" <?php echo $stat_users ? 'checked' : ''; ?>>
404						<?php echo I18N::translate('Total users'); ?>
405					</label>
406				</td>
407				<td>
408					<label>
409						<input type="checkbox" value="yes" name="stat_avg_chil" <?php echo $stat_avg_chil ? 'checked' : ''; ?>>
410						<?php echo I18N::translate('Average number of children per family'); ?>
411					</label>
412				</td>
413			</tr>
414		</tbody>
415	</table>
416	</td>
417	</tr>
418	<?php
419	}
420}
421