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