xref: /webtrees/app/Module/FamilyTreeStatisticsModule.php (revision 789ec362434585ae73a2e5d1f6b504dfe510008b)
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		if ($stat_link) {
195			$content .= '<div class="clearfloat"><a href="statistics.php?ged=' . $WT_TREE->getNameUrl() . '" rel="nofollow"><b>' . I18N::translate('View statistics as graphs') . '</b></a></div>';
196		}
197
198		if ($show_common_surnames) {
199			$surnames = FunctionsDb::getCommonSurnames($WT_TREE->getPreference('COMMON_NAMES_THRESHOLD'), $WT_TREE);
200			if (count($surnames) > 0) {
201				$content .= '<p><b>' . I18N::translate('Most common surnames') . '</b></p>';
202				$content .= '<div class="common_surnames">';
203				$i = 0;
204				foreach ($surnames as $indexval => $surname) {
205					if (stristr($surname['name'], '@N.N') === false) {
206						if ($i > 0) {
207							$content .= ', ';
208						}
209						$content .= '<a href="' . "indilist.php?ged=" . $WT_TREE->getNameUrl() . "&amp;surname=" . rawurlencode($surname['name']) . '">' . $surname['name'] . '</a>';
210						$i++;
211					}
212				}
213				$content .= '</div>';
214			}
215		}
216		$content .= '</div>';
217		if ($template) {
218			return Theme::theme()->formatBlock($id, $title, $class, $content);
219		} else {
220			return $content;
221		}
222	}
223
224	/** {@inheritdoc} */
225	public function loadAjax() {
226		return true;
227	}
228
229	/** {@inheritdoc} */
230	public function isUserBlock() {
231		return true;
232	}
233
234	/** {@inheritdoc} */
235	public function isGedcomBlock() {
236		return true;
237	}
238
239	/**
240	 * An HTML form to edit block settings
241	 *
242	 * @param int $block_id
243	 */
244	public function configureBlock($block_id) {
245		if (Filter::postBool('save') && Filter::checkCsrf()) {
246			$this->setBlockSetting($block_id, 'show_last_update', Filter::postBool('show_last_update'));
247			$this->setBlockSetting($block_id, 'show_common_surnames', Filter::postBool('show_common_surnames'));
248			$this->setBlockSetting($block_id, 'stat_indi', Filter::postBool('stat_indi'));
249			$this->setBlockSetting($block_id, 'stat_fam', Filter::postBool('stat_fam'));
250			$this->setBlockSetting($block_id, 'stat_sour', Filter::postBool('stat_sour'));
251			$this->setBlockSetting($block_id, 'stat_other', Filter::postBool('stat_other'));
252			$this->setBlockSetting($block_id, 'stat_media', Filter::postBool('stat_media'));
253			$this->setBlockSetting($block_id, 'stat_repo', Filter::postBool('stat_repo'));
254			$this->setBlockSetting($block_id, 'stat_surname', Filter::postBool('stat_surname'));
255			$this->setBlockSetting($block_id, 'stat_events', Filter::postBool('stat_events'));
256			$this->setBlockSetting($block_id, 'stat_users', Filter::postBool('stat_users'));
257			$this->setBlockSetting($block_id, 'stat_first_birth', Filter::postBool('stat_first_birth'));
258			$this->setBlockSetting($block_id, 'stat_last_birth', Filter::postBool('stat_last_birth'));
259			$this->setBlockSetting($block_id, 'stat_first_death', Filter::postBool('stat_first_death'));
260			$this->setBlockSetting($block_id, 'stat_last_death', Filter::postBool('stat_last_death'));
261			$this->setBlockSetting($block_id, 'stat_long_life', Filter::postBool('stat_long_life'));
262			$this->setBlockSetting($block_id, 'stat_avg_life', Filter::postBool('stat_avg_life'));
263			$this->setBlockSetting($block_id, 'stat_most_chil', Filter::postBool('stat_most_chil'));
264			$this->setBlockSetting($block_id, 'stat_avg_chil', Filter::postBool('stat_avg_chil'));
265		}
266
267		$show_last_update     = $this->getBlockSetting($block_id, 'show_last_update', '1');
268		$show_common_surnames = $this->getBlockSetting($block_id, 'show_common_surnames', '1');
269		$stat_indi            = $this->getBlockSetting($block_id, 'stat_indi', '1');
270		$stat_fam             = $this->getBlockSetting($block_id, 'stat_fam', '1');
271		$stat_sour            = $this->getBlockSetting($block_id, 'stat_sour', '1');
272		$stat_media           = $this->getBlockSetting($block_id, 'stat_media', '1');
273		$stat_repo            = $this->getBlockSetting($block_id, 'stat_repo', '1');
274		$stat_surname         = $this->getBlockSetting($block_id, 'stat_surname', '1');
275		$stat_events          = $this->getBlockSetting($block_id, 'stat_events', '1');
276		$stat_users           = $this->getBlockSetting($block_id, 'stat_users', '1');
277		$stat_first_birth     = $this->getBlockSetting($block_id, 'stat_first_birth', '1');
278		$stat_last_birth      = $this->getBlockSetting($block_id, 'stat_last_birth', '1');
279		$stat_first_death     = $this->getBlockSetting($block_id, 'stat_first_death', '1');
280		$stat_last_death      = $this->getBlockSetting($block_id, 'stat_last_death', '1');
281		$stat_long_life       = $this->getBlockSetting($block_id, 'stat_long_life', '1');
282		$stat_avg_life        = $this->getBlockSetting($block_id, 'stat_avg_life', '1');
283		$stat_most_chil       = $this->getBlockSetting($block_id, 'stat_most_chil', '1');
284		$stat_avg_chil        = $this->getBlockSetting($block_id, 'stat_avg_chil', '1');
285
286		?>
287		<tr>
288			<td class="descriptionbox wrap width33">
289				<?php echo /* I18N: label for yes/no option */
290				I18N::translate('Show date of last update?') ?>
291			</td>
292			<td class="optionbox">
293				<?php echo FunctionsEdit::editFieldYesNo('show_last_update', $show_last_update) ?>
294			</td>
295		</tr>
296		<tr>
297			<td class="descriptionbox wrap width33">
298				<?php echo I18N::translate('Show common surnames?') ?>
299			</td>
300			<td class="optionbox">
301				<?php echo FunctionsEdit::editFieldYesNo('show_common_surnames', $show_common_surnames) ?>
302			</td>
303		</tr>
304		<tr>
305			<td class="descriptionbox wrap width33"><?php echo I18N::translate('Select the stats to show in this block'); ?></td>
306			<td class="optionbox">
307				<table>
308					<tbody>
309						<tr>
310							<td>
311								<label>
312									<input type="checkbox" value="yes" name="stat_indi" <?php echo $stat_indi ? 'checked' : ''; ?>>
313									<?php echo I18N::translate('Individuals'); ?>
314								</label>
315							</td>
316							<td>
317								<label>
318									<input type="checkbox" value="yes" name="stat_first_birth" <?php echo $stat_first_birth ? 'checked' : ''; ?>>
319									<?php echo I18N::translate('Earliest birth year'); ?>
320								</label>
321							</td>
322						</tr>
323						<tr>
324							<td>
325								<label>
326									<input type="checkbox" value="yes" name="stat_surname" <?php echo $stat_surname ? 'checked' : ''; ?>>
327									<?php echo I18N::translate('Total surnames'); ?>
328								</label>
329							</td>
330							<td>
331								<label>
332									<input type="checkbox" value="yes" name="stat_last_birth" <?php echo $stat_last_birth ? 'checked' : ''; ?>>
333									<?php echo I18N::translate('Latest birth year'); ?>
334								</label>
335							</td>
336						</tr>
337						<tr>
338							<td>
339								<label>
340									<input type="checkbox" value="yes" name="stat_fam" <?php echo $stat_fam ? 'checked' : ''; ?>>
341									<?php echo I18N::translate('Families'); ?>
342								</label>
343							</td>
344							<td>
345								<label>
346									<input type="checkbox" value="yes" name="stat_first_death" <?php echo $stat_first_death ? 'checked' : ''; ?>>
347									<?php echo I18N::translate('Earliest death year'); ?>
348								</label>
349							</td>
350						</tr>
351						<tr>
352							<td>
353								<label>
354									<input type="checkbox" value="yes" name="stat_sour" <?php echo $stat_sour ? 'checked' : ''; ?>>
355									<?php echo I18N::translate('Sources'); ?>
356								</label>
357							</td>
358							<td>
359								<label>
360									<input type="checkbox" value="yes" name="stat_last_death" <?php echo $stat_last_death ? 'checked' : ''; ?>>
361									<?php echo I18N::translate('Latest death year'); ?>
362								</label>
363							</td>
364						</tr>
365						<tr>
366							<td>
367								<label>
368									<input type="checkbox" value="yes" name="stat_media" <?php echo $stat_media ? 'checked' : ''; ?>>
369									<?php echo I18N::translate('Media objects'); ?>
370								</label>
371							</td>
372							<td>
373								<label>
374									<input type="checkbox" value="yes" name="stat_long_life" <?php echo $stat_long_life ? 'checked' : ''; ?>>
375									<?php echo I18N::translate('Individual who lived the longest'); ?>
376								</label>
377							</td>
378						</tr>
379						<tr>
380							<td>
381								<label>
382									<input type="checkbox" value="yes" name="stat_repo" <?php echo $stat_repo ? 'checked' : ''; ?>>
383									<?php echo I18N::translate('Repositories'); ?>
384								</label>
385							</td>
386							<td>
387								<label>
388									<input type="checkbox" value="yes" name="stat_avg_life" <?php echo $stat_avg_life ? 'checked' : ''; ?>>
389									<?php echo I18N::translate('Average age at death'); ?>
390								</label>
391							</td>
392						</tr>
393						<tr>
394							<td>
395								<label>
396									<input type="checkbox" value="yes" name="stat_events" <?php echo $stat_events ? 'checked' : ''; ?>>
397									<?php echo I18N::translate('Total events'); ?>
398								</label>
399							</td>
400							<td>
401								<label>
402									<input type="checkbox" value="yes" name="stat_most_chil" <?php echo $stat_most_chil ? 'checked' : ''; ?>>
403									<?php echo I18N::translate('Family with the most children'); ?>
404								</label>
405							</td>
406						</tr>
407						<tr>
408							<td>
409								<label>
410									<input type="checkbox" value="yes" name="stat_users" <?php echo $stat_users ? 'checked' : ''; ?>>
411									<?php echo I18N::translate('Total users'); ?>
412								</label>
413							</td>
414							<td>
415								<label>
416									<input type="checkbox" value="yes" name="stat_avg_chil" <?php echo $stat_avg_chil ? 'checked' : ''; ?>>
417									<?php echo I18N::translate('Average number of children per family'); ?>
418								</label>
419							</td>
420						</tr>
421					</tbody>
422				</table>
423			</td>
424		</tr>
425		<?php
426	}
427}
428