xref: /webtrees/app/Module/FamilyTreeStatisticsModule.php (revision 16e7dcbf4079d7ea9a84244c8f3c93e4df7f69a9)
1<?php
2/**
3 * webtrees: online genealogy
4 * Copyright (C) 2018 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\FunctionsPrintLists;
22use Fisharebest\Webtrees\I18N;
23use Fisharebest\Webtrees\Query\QueryName;
24use Fisharebest\Webtrees\Stats;
25use Fisharebest\Webtrees\Tree;
26
27/**
28 * Class FamilyTreeStatisticsModule
29 */
30class FamilyTreeStatisticsModule extends AbstractModule implements ModuleBlockInterface {
31	/** Show this number of surnames by default */
32	const DEFAULT_NUMBER_OF_SURNAMES = 10;
33
34	/** {@inheritdoc} */
35	public function getTitle() {
36		return /* I18N: Name of a module */
37			I18N::translate('Statistics');
38	}
39
40	/** {@inheritdoc} */
41	public function getDescription() {
42		return /* I18N: Description of “Statistics” module */
43			I18N::translate('The size of the family tree, earliest and latest events, common names, etc.');
44	}
45
46	/**
47	 * Generate the HTML content of this block.
48	 *
49	 * @param Tree     $tree
50	 * @param int      $block_id
51	 * @param bool     $template
52	 * @param string[] $cfg
53	 *
54	 * @return string
55	 */
56	public function getBlock(Tree $tree, int $block_id, bool $template = true, array $cfg = []): string {
57		global $ctype;
58
59		$show_last_update     = $this->getBlockSetting($block_id, 'show_last_update', '1');
60		$show_common_surnames = $this->getBlockSetting($block_id, 'show_common_surnames', '1');
61		$number_of_surnames   = $this->getBlockSetting($block_id, 'number_of_surnames', self::DEFAULT_NUMBER_OF_SURNAMES);
62		$stat_indi            = $this->getBlockSetting($block_id, 'stat_indi', '1');
63		$stat_fam             = $this->getBlockSetting($block_id, 'stat_fam', '1');
64		$stat_sour            = $this->getBlockSetting($block_id, 'stat_sour', '1');
65		$stat_media           = $this->getBlockSetting($block_id, 'stat_media', '1');
66		$stat_repo            = $this->getBlockSetting($block_id, 'stat_repo', '1');
67		$stat_surname         = $this->getBlockSetting($block_id, 'stat_surname', '1');
68		$stat_events          = $this->getBlockSetting($block_id, 'stat_events', '1');
69		$stat_users           = $this->getBlockSetting($block_id, 'stat_users', '1');
70		$stat_first_birth     = $this->getBlockSetting($block_id, 'stat_first_birth', '1');
71		$stat_last_birth      = $this->getBlockSetting($block_id, 'stat_last_birth', '1');
72		$stat_first_death     = $this->getBlockSetting($block_id, 'stat_first_death', '1');
73		$stat_last_death      = $this->getBlockSetting($block_id, 'stat_last_death', '1');
74		$stat_long_life       = $this->getBlockSetting($block_id, 'stat_long_life', '1');
75		$stat_avg_life        = $this->getBlockSetting($block_id, 'stat_avg_life', '1');
76		$stat_most_chil       = $this->getBlockSetting($block_id, 'stat_most_chil', '1');
77		$stat_avg_chil        = $this->getBlockSetting($block_id, 'stat_avg_chil', '1');
78
79		extract($cfg, EXTR_OVERWRITE);
80
81		if ($show_common_surnames) {
82			$surnames = FunctionsDb::getTopSurnames($tree->getTreeId(), 0, (int) $number_of_surnames);
83
84			$all_surnames = [];
85			foreach (array_keys($surnames) as $surname) {
86				$all_surnames = array_merge($all_surnames, QueryName::surnames($tree, $surname, '', false, false));
87			}
88			ksort($all_surnames);
89
90			$surnames = FunctionsPrintLists::surnameList($all_surnames, 2, false, 'individual-list', $tree);
91		} else {
92			$surnames = '';
93		}
94
95		$content = view('blocks/family-tree-statistics', [
96			'show_last_update'     => $show_last_update,
97			'show_common_surnames' => $show_common_surnames,
98			'number_of_surnames'   => $number_of_surnames,
99			'stat_indi'            => $stat_indi,
100			'stat_fam'             => $stat_fam,
101			'stat_sour'            => $stat_sour,
102			'stat_media'           => $stat_media,
103			'stat_repo'            => $stat_repo,
104			'stat_surname'         => $stat_surname,
105			'stat_events'          => $stat_events,
106			'stat_users'           => $stat_users,
107			'stat_first_birth'     => $stat_first_birth,
108			'stat_last_birth'      => $stat_last_birth,
109			'stat_first_death'     => $stat_first_death,
110			'stat_last_death'      => $stat_last_death,
111			'stat_long_life'       => $stat_long_life,
112			'stat_avg_life'        => $stat_avg_life,
113			'stat_most_chil'       => $stat_most_chil,
114			'stat_avg_chil'        => $stat_avg_chil,
115			'stats'                => new Stats($tree),
116			'surnames'             => $surnames,
117		]);
118
119		if ($template) {
120			if ($ctype === 'gedcom' && Auth::isManager($tree)) {
121				$config_url = route('tree-page-block-edit', ['block_id' => $block_id, 'ged' => $tree->getName()]);
122			} elseif ($ctype === 'user' && Auth::check()) {
123				$config_url = route('user-page-block-edit', ['block_id' => $block_id, 'ged' => $tree->getName()]);
124			} else {
125				$config_url = '';
126			}
127
128			return view('blocks/template', [
129				'block'      => str_replace('_', '-', $this->getName()),
130				'id'         => $block_id,
131				'config_url' => $config_url,
132				'title'      => $this->getTitle(),
133				'content'    => $content,
134			]);
135		} else {
136			return $content;
137		}
138	}
139
140	/** {@inheritdoc} */
141	public function loadAjax(): bool {
142		return true;
143	}
144
145	/** {@inheritdoc} */
146	public function isUserBlock(): bool {
147		return true;
148	}
149
150	/** {@inheritdoc} */
151	public function isGedcomBlock(): bool {
152		return true;
153	}
154
155	/**
156	 * An HTML form to edit block settings
157	 *
158	 * @param Tree $tree
159	 * @param int  $block_id
160	 *
161	 * @return void
162	 */
163	public function configureBlock(Tree $tree, int $block_id) {
164		if ($_SERVER['REQUEST_METHOD'] === 'POST') {
165			$this->setBlockSetting($block_id, 'show_last_update', Filter::postBool('show_last_update'));
166			$this->setBlockSetting($block_id, 'show_common_surnames', Filter::postBool('show_common_surnames'));
167			$this->setBlockSetting($block_id, 'number_of_surnames', Filter::postInteger('number_of_surnames'));
168			$this->setBlockSetting($block_id, 'stat_indi', Filter::postBool('stat_indi'));
169			$this->setBlockSetting($block_id, 'stat_fam', Filter::postBool('stat_fam'));
170			$this->setBlockSetting($block_id, 'stat_sour', Filter::postBool('stat_sour'));
171			$this->setBlockSetting($block_id, 'stat_other', Filter::postBool('stat_other'));
172			$this->setBlockSetting($block_id, 'stat_media', Filter::postBool('stat_media'));
173			$this->setBlockSetting($block_id, 'stat_repo', Filter::postBool('stat_repo'));
174			$this->setBlockSetting($block_id, 'stat_surname', Filter::postBool('stat_surname'));
175			$this->setBlockSetting($block_id, 'stat_events', Filter::postBool('stat_events'));
176			$this->setBlockSetting($block_id, 'stat_users', Filter::postBool('stat_users'));
177			$this->setBlockSetting($block_id, 'stat_first_birth', Filter::postBool('stat_first_birth'));
178			$this->setBlockSetting($block_id, 'stat_last_birth', Filter::postBool('stat_last_birth'));
179			$this->setBlockSetting($block_id, 'stat_first_death', Filter::postBool('stat_first_death'));
180			$this->setBlockSetting($block_id, 'stat_last_death', Filter::postBool('stat_last_death'));
181			$this->setBlockSetting($block_id, 'stat_long_life', Filter::postBool('stat_long_life'));
182			$this->setBlockSetting($block_id, 'stat_avg_life', Filter::postBool('stat_avg_life'));
183			$this->setBlockSetting($block_id, 'stat_most_chil', Filter::postBool('stat_most_chil'));
184			$this->setBlockSetting($block_id, 'stat_avg_chil', Filter::postBool('stat_avg_chil'));
185
186			return;
187		}
188
189		$show_last_update     = $this->getBlockSetting($block_id, 'show_last_update', '1');
190		$show_common_surnames = $this->getBlockSetting($block_id, 'show_common_surnames', '1');
191		$number_of_surnames   = $this->getBlockSetting($block_id, 'number_of_surnames', self::DEFAULT_NUMBER_OF_SURNAMES);
192		$stat_indi            = $this->getBlockSetting($block_id, 'stat_indi', '1');
193		$stat_fam             = $this->getBlockSetting($block_id, 'stat_fam', '1');
194		$stat_sour            = $this->getBlockSetting($block_id, 'stat_sour', '1');
195		$stat_media           = $this->getBlockSetting($block_id, 'stat_media', '1');
196		$stat_repo            = $this->getBlockSetting($block_id, 'stat_repo', '1');
197		$stat_surname         = $this->getBlockSetting($block_id, 'stat_surname', '1');
198		$stat_events          = $this->getBlockSetting($block_id, 'stat_events', '1');
199		$stat_users           = $this->getBlockSetting($block_id, 'stat_users', '1');
200		$stat_first_birth     = $this->getBlockSetting($block_id, 'stat_first_birth', '1');
201		$stat_last_birth      = $this->getBlockSetting($block_id, 'stat_last_birth', '1');
202		$stat_first_death     = $this->getBlockSetting($block_id, 'stat_first_death', '1');
203		$stat_last_death      = $this->getBlockSetting($block_id, 'stat_last_death', '1');
204		$stat_long_life       = $this->getBlockSetting($block_id, 'stat_long_life', '1');
205		$stat_avg_life        = $this->getBlockSetting($block_id, 'stat_avg_life', '1');
206		$stat_most_chil       = $this->getBlockSetting($block_id, 'stat_most_chil', '1');
207		$stat_avg_chil        = $this->getBlockSetting($block_id, 'stat_avg_chil', '1');
208
209		echo view('blocks/family-tree-statistics-config', [
210			'show_last_update'     => $show_last_update,
211			'show_common_surnames' => $show_common_surnames,
212			'number_of_surnames'   => $number_of_surnames,
213			'stat_indi'            => $stat_indi,
214			'stat_fam'             => $stat_fam,
215			'stat_sour'            => $stat_sour,
216			'stat_media'           => $stat_media,
217			'stat_repo'            => $stat_repo,
218			'stat_surname'         => $stat_surname,
219			'stat_events'          => $stat_events,
220			'stat_users'           => $stat_users,
221			'stat_first_birth'     => $stat_first_birth,
222			'stat_last_birth'      => $stat_last_birth,
223			'stat_first_death'     => $stat_first_death,
224			'stat_last_death'      => $stat_last_death,
225			'stat_long_life'       => $stat_long_life,
226			'stat_avg_life'        => $stat_avg_life,
227			'stat_most_chil'       => $stat_most_chil,
228			'stat_avg_chil'        => $stat_avg_chil,
229		]);
230	}
231}
232