xref: /webtrees/app/Module/FamilyTreeStatisticsModule.php (revision 9210c54388a598f8a5435f00e581e86ccdf5736a)
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 */
96				I18N::translate('This family tree was last updated on %s.', strip_tags($stats->gedcomUpdated())) . '</div>';
97		}
98		/** Responsive Design */
99
100		$content .= '<div class="stat-table1">';
101		if ($stat_indi) {
102			$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>';
103			$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>';
104			$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>';
105		}
106		if ($stat_surname) {
107			$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>';
108		}
109		if ($stat_fam) {
110			$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>';
111		}
112		if ($stat_sour) {
113			$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>';
114		}
115		if ($stat_media) {
116			$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>';
117		}
118		if ($stat_repo) {
119			$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>';
120		}
121		if ($stat_events) {
122			$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>';
123		}
124		if ($stat_users) {
125			$content .= '<div class="stat-row"><div class="facts_label stat-cell">' . I18N::translate('Total users') . '</div><div class="facts_value stats_value stat-cell">';
126			if (Auth::isManager($WT_TREE)) {
127				$content .= '<a href="admin_users.php">' . $stats->totalUsers() . '</a>';
128			} else {
129				$content .= $stats->totalUsers();
130			}
131			$content .= '</div></div>';
132		}
133		if (!$block) {
134			$content .= '</div><div class="facts_table stat-table2">';
135		}
136		if ($stat_first_birth) {
137			$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>';
138			if (!$block) {
139				$content .= '<div class="facts_value stat-cell left">' . $stats->firstBirth() . '</div>';
140			}
141			$content .= '</div>';
142		}
143		if ($stat_last_birth) {
144			$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>';
145			if (!$block) {
146				$content .= '<div class="facts_value stat-cell left">' . $stats->lastBirth() . '</div>';
147			}
148			$content .= '</div>';
149		}
150		if ($stat_first_death) {
151			$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>';
152			if (!$block) {
153				$content .= '<div class="facts_value stat-cell left">' . $stats->firstDeath() . '</div>';
154			}
155			$content .= '</div>';
156		}
157		if ($stat_last_death) {
158			$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>';
159			if (!$block) {
160				$content .= '<div class="facts_value stat-cell left">' . $stats->lastDeath() . '</div>';
161			}
162			$content .= '</div>';
163		}
164		if ($stat_long_life) {
165			$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>';
166			if (!$block) {
167				$content .= '<div class="facts_value stat-cell left">' . $stats->longestLife() . '</div>';
168			}
169			$content .= '</div>';
170		}
171		if ($stat_avg_life) {
172			$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>';
173			if (!$block) {
174				$content .= '<div class="facts_value stat-cell left">' . I18N::translate('Males') . ':&nbsp;' . $stats->averageLifespanMale();
175				$content .= '&nbsp;&nbsp;&nbsp;' . I18N::translate('Females') . ':&nbsp;' . $stats->averageLifespanFemale() . '</div>';
176			}
177			$content .= '</div>';
178		}
179
180		if ($stat_most_chil && !$block) {
181			$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>';
182			if (!$block) {
183				$content .= '<div class="facts_value stat-cell left">' . $stats->largestFamily() . '</div>';
184			}
185			$content .= '</div>';
186		}
187		if ($stat_avg_chil) {
188			$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>';
189			if (!$block) {
190				$content .= '<div class="facts_value stat-cell left"></div>';
191			}
192			$content .= '</div>';
193		}
194		$content .= '</div>';
195		if ($stat_link) {
196			$content .= '<div class="clearfloat"><a href="statistics.php?ged=' . $WT_TREE->getNameUrl() . '" rel="nofollow"><b>' . I18N::translate('View statistics as graphs') . '</b></a></div>';
197		}
198
199		if ($show_common_surnames) {
200			$surnames = FunctionsDb::getCommonSurnames($WT_TREE->getPreference('COMMON_NAMES_THRESHOLD'), $WT_TREE);
201			if (count($surnames) > 0) {
202				$content .= '<div class="clearfloat"><p><b>' . I18N::translate('Most common surnames') . '</b></p>';
203				$content .= '<div class="common_surnames">';
204				$i = 0;
205				foreach ($surnames as $indexval => $surname) {
206					if (stristr($surname['name'], '@N.N') === false) {
207						if ($i > 0) {
208							$content .= ', ';
209						}
210						$content .= '<a href="' . "indilist.php?ged=" . $WT_TREE->getNameUrl() . "&amp;surname=" . rawurlencode($surname['name']) . '">' . $surname['name'] . '</a>';
211						$i++;
212					}
213				}
214				$content .= '</div>';
215			}
216		}
217
218		if ($template) {
219			return Theme::theme()->formatBlock($id, $title, $class, $content);
220		} else {
221			return $content;
222		}
223	}
224
225	/** {@inheritdoc} */
226	public function loadAjax() {
227		return true;
228	}
229
230	/** {@inheritdoc} */
231	public function isUserBlock() {
232		return true;
233	}
234
235	/** {@inheritdoc} */
236	public function isGedcomBlock() {
237		return true;
238	}
239
240	/**
241	 * An HTML form to edit block settings
242	 *
243	 * @param int $block_id
244	 */
245	public function configureBlock($block_id) {
246		if (Filter::postBool('save') && Filter::checkCsrf()) {
247			$this->setBlockSetting($block_id, 'show_last_update', Filter::postBool('show_last_update'));
248			$this->setBlockSetting($block_id, 'show_common_surnames', Filter::postBool('show_common_surnames'));
249			$this->setBlockSetting($block_id, 'stat_indi', Filter::postBool('stat_indi'));
250			$this->setBlockSetting($block_id, 'stat_fam', Filter::postBool('stat_fam'));
251			$this->setBlockSetting($block_id, 'stat_sour', Filter::postBool('stat_sour'));
252			$this->setBlockSetting($block_id, 'stat_other', Filter::postBool('stat_other'));
253			$this->setBlockSetting($block_id, 'stat_media', Filter::postBool('stat_media'));
254			$this->setBlockSetting($block_id, 'stat_repo', Filter::postBool('stat_repo'));
255			$this->setBlockSetting($block_id, 'stat_surname', Filter::postBool('stat_surname'));
256			$this->setBlockSetting($block_id, 'stat_events', Filter::postBool('stat_events'));
257			$this->setBlockSetting($block_id, 'stat_users', Filter::postBool('stat_users'));
258			$this->setBlockSetting($block_id, 'stat_first_birth', Filter::postBool('stat_first_birth'));
259			$this->setBlockSetting($block_id, 'stat_last_birth', Filter::postBool('stat_last_birth'));
260			$this->setBlockSetting($block_id, 'stat_first_death', Filter::postBool('stat_first_death'));
261			$this->setBlockSetting($block_id, 'stat_last_death', Filter::postBool('stat_last_death'));
262			$this->setBlockSetting($block_id, 'stat_long_life', Filter::postBool('stat_long_life'));
263			$this->setBlockSetting($block_id, 'stat_avg_life', Filter::postBool('stat_avg_life'));
264			$this->setBlockSetting($block_id, 'stat_most_chil', Filter::postBool('stat_most_chil'));
265			$this->setBlockSetting($block_id, 'stat_avg_chil', Filter::postBool('stat_avg_chil'));
266		}
267
268		$show_last_update     = $this->getBlockSetting($block_id, 'show_last_update', '1');
269		$show_common_surnames = $this->getBlockSetting($block_id, 'show_common_surnames', '1');
270		$stat_indi            = $this->getBlockSetting($block_id, 'stat_indi', '1');
271		$stat_fam             = $this->getBlockSetting($block_id, 'stat_fam', '1');
272		$stat_sour            = $this->getBlockSetting($block_id, 'stat_sour', '1');
273		$stat_media           = $this->getBlockSetting($block_id, 'stat_media', '1');
274		$stat_repo            = $this->getBlockSetting($block_id, 'stat_repo', '1');
275		$stat_surname         = $this->getBlockSetting($block_id, 'stat_surname', '1');
276		$stat_events          = $this->getBlockSetting($block_id, 'stat_events', '1');
277		$stat_users           = $this->getBlockSetting($block_id, 'stat_users', '1');
278		$stat_first_birth     = $this->getBlockSetting($block_id, 'stat_first_birth', '1');
279		$stat_last_birth      = $this->getBlockSetting($block_id, 'stat_last_birth', '1');
280		$stat_first_death     = $this->getBlockSetting($block_id, 'stat_first_death', '1');
281		$stat_last_death      = $this->getBlockSetting($block_id, 'stat_last_death', '1');
282		$stat_long_life       = $this->getBlockSetting($block_id, 'stat_long_life', '1');
283		$stat_avg_life        = $this->getBlockSetting($block_id, 'stat_avg_life', '1');
284		$stat_most_chil       = $this->getBlockSetting($block_id, 'stat_most_chil', '1');
285		$stat_avg_chil        = $this->getBlockSetting($block_id, 'stat_avg_chil', '1');
286
287		?>
288		<tr>
289			<td class="descriptionbox wrap width33">
290				<?php echo /* I18N: label for yes/no option */
291				I18N::translate('Show date of last update?') ?>
292			</td>
293			<td class="optionbox">
294				<?php echo FunctionsEdit::editFieldYesNo('show_last_update', $show_last_update) ?>
295			</td>
296		</tr>
297		<tr>
298			<td class="descriptionbox wrap width33">
299				<?php echo I18N::translate('Show common surnames?') ?>
300			</td>
301			<td class="optionbox">
302				<?php echo FunctionsEdit::editFieldYesNo('show_common_surnames', $show_common_surnames) ?>
303			</td>
304		</tr>
305		<tr>
306			<td class="descriptionbox wrap width33"><?php echo I18N::translate('Select the stats to show in this block'); ?></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		<?php
427	}
428}
429