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