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