xref: /webtrees/app/Module/FamilyTreeStatisticsModule.php (revision d2681c37325a35ab01be82034f4afd3b58010fb8)
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\Bootstrap4;
20use Fisharebest\Webtrees\Filter;
21use Fisharebest\Webtrees\Functions\FunctionsDb;
22use Fisharebest\Webtrees\Functions\FunctionsPrintLists;
23use Fisharebest\Webtrees\Html;
24use Fisharebest\Webtrees\I18N;
25use Fisharebest\Webtrees\Query\QueryName;
26use Fisharebest\Webtrees\Stats;
27
28/**
29 * Class FamilyTreeStatisticsModule
30 */
31class FamilyTreeStatisticsModule extends AbstractModule implements ModuleBlockInterface {
32	/** Show this number of surnames by default */
33	const DEFAULT_NUMBER_OF_SURNAMES = 10;
34
35	/** {@inheritdoc} */
36	public function getTitle() {
37		return /* I18N: Name of a module */
38			I18N::translate('Statistics');
39	}
40
41	/** {@inheritdoc} */
42	public function getDescription() {
43		return /* I18N: Description of “Statistics” module */
44			I18N::translate('The size of the family tree, earliest and latest events, common names, etc.');
45	}
46
47	/**
48	 * Generate the HTML content of this block.
49	 *
50	 * @param int      $block_id
51	 * @param bool     $template
52	 * @param string[] $cfg
53	 *
54	 * @return string
55	 */
56	public function getBlock($block_id, $template = true, $cfg = []): string {
57		global $WT_TREE, $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		foreach (['show_common_surnames', 'number_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'] as $name) {
80			if (array_key_exists($name, $cfg)) {
81				$$name = $cfg[$name];
82			}
83		}
84
85		if ($show_common_surnames) {
86			$surnames = FunctionsDb::getTopSurnames($WT_TREE->getTreeId(), 0, (int) $number_of_surnames);
87
88			$all_surnames = [];
89			foreach (array_keys($surnames) as $surname) {
90				$all_surnames = array_merge($all_surnames, QueryName::surnames($WT_TREE, $surname, '', false, false));
91			}
92			ksort($all_surnames);
93
94			$surnames = FunctionsPrintLists::surnameList($all_surnames, 2, false, 'indilist.php', $WT_TREE);
95		} else {
96			$surnames = '';
97		}
98
99		$content = view('blocks/family-tree-statistics', [
100			'show_last_update'     => $show_last_update,
101			'show_common_surnames' => $show_common_surnames,
102			'number_of_surnames'   => $number_of_surnames,
103			'stat_indi'            => $stat_indi,
104			'stat_fam'             => $stat_fam,
105			'stat_sour'            => $stat_sour,
106			'stat_media'           => $stat_media,
107			'stat_repo'            => $stat_repo,
108			'stat_surname'         => $stat_surname,
109			'stat_events'          => $stat_events,
110			'stat_users'           => $stat_users,
111			'stat_first_birth'     => $stat_first_birth,
112			'stat_last_birth'      => $stat_last_birth,
113			'stat_first_death'     => $stat_first_death,
114			'stat_last_death'      => $stat_last_death,
115			'stat_long_life'       => $stat_long_life,
116			'stat_avg_life'        => $stat_avg_life,
117			'stat_most_chil'       => $stat_most_chil,
118			'stat_avg_chil'        => $stat_avg_chil,
119			'stats'                => new Stats($WT_TREE),
120			'surnames'             => $surnames,
121		]);
122
123		if ($template) {
124			if ($ctype === 'gedcom' && Auth::isManager($WT_TREE) || $ctype === 'user' && Auth::check()) {
125				$config_url = Html::url('block_edit.php', ['block_id' => $block_id, 'ged' => $WT_TREE->getName()]);
126			} else {
127				$config_url = '';
128			}
129
130			return view('blocks/template', [
131				'block'      => str_replace('_', '-', $this->getName()),
132				'id'         => $block_id,
133				'config_url' => $config_url,
134				'title'      => $this->getTitle(),
135				'content'    => $content,
136			]);
137		} else {
138			return $content;
139		}
140	}
141
142	/** {@inheritdoc} */
143	public function loadAjax(): bool {
144		return true;
145	}
146
147	/** {@inheritdoc} */
148	public function isUserBlock(): bool {
149		return true;
150	}
151
152	/** {@inheritdoc} */
153	public function isGedcomBlock(): bool {
154		return true;
155	}
156
157	/**
158	 * An HTML form to edit block settings
159	 *
160	 * @param int $block_id
161	 *
162	 * @return void
163	 */
164	public function configureBlock($block_id) {
165		if (Filter::postBool('save') && Filter::checkCsrf()) {
166			$this->setBlockSetting($block_id, 'show_last_update', Filter::postBool('show_last_update'));
167			$this->setBlockSetting($block_id, 'show_common_surnames', Filter::postBool('show_common_surnames'));
168			$this->setBlockSetting($block_id, 'number_of_surnames', Filter::postInteger('number_of_surnames'));
169			$this->setBlockSetting($block_id, 'stat_indi', Filter::postBool('stat_indi'));
170			$this->setBlockSetting($block_id, 'stat_fam', Filter::postBool('stat_fam'));
171			$this->setBlockSetting($block_id, 'stat_sour', Filter::postBool('stat_sour'));
172			$this->setBlockSetting($block_id, 'stat_other', Filter::postBool('stat_other'));
173			$this->setBlockSetting($block_id, 'stat_media', Filter::postBool('stat_media'));
174			$this->setBlockSetting($block_id, 'stat_repo', Filter::postBool('stat_repo'));
175			$this->setBlockSetting($block_id, 'stat_surname', Filter::postBool('stat_surname'));
176			$this->setBlockSetting($block_id, 'stat_events', Filter::postBool('stat_events'));
177			$this->setBlockSetting($block_id, 'stat_users', Filter::postBool('stat_users'));
178			$this->setBlockSetting($block_id, 'stat_first_birth', Filter::postBool('stat_first_birth'));
179			$this->setBlockSetting($block_id, 'stat_last_birth', Filter::postBool('stat_last_birth'));
180			$this->setBlockSetting($block_id, 'stat_first_death', Filter::postBool('stat_first_death'));
181			$this->setBlockSetting($block_id, 'stat_last_death', Filter::postBool('stat_last_death'));
182			$this->setBlockSetting($block_id, 'stat_long_life', Filter::postBool('stat_long_life'));
183			$this->setBlockSetting($block_id, 'stat_avg_life', Filter::postBool('stat_avg_life'));
184			$this->setBlockSetting($block_id, 'stat_most_chil', Filter::postBool('stat_most_chil'));
185			$this->setBlockSetting($block_id, 'stat_avg_chil', Filter::postBool('stat_avg_chil'));
186		}
187
188		$show_last_update     = $this->getBlockSetting($block_id, 'show_last_update', '1');
189		$show_common_surnames = $this->getBlockSetting($block_id, 'show_common_surnames', '1');
190		$number_of_surnames   = $this->getBlockSetting($block_id, 'number_of_surnames', self::DEFAULT_NUMBER_OF_SURNAMES);
191		$stat_indi            = $this->getBlockSetting($block_id, 'stat_indi', '1');
192		$stat_fam             = $this->getBlockSetting($block_id, 'stat_fam', '1');
193		$stat_sour            = $this->getBlockSetting($block_id, 'stat_sour', '1');
194		$stat_media           = $this->getBlockSetting($block_id, 'stat_media', '1');
195		$stat_repo            = $this->getBlockSetting($block_id, 'stat_repo', '1');
196		$stat_surname         = $this->getBlockSetting($block_id, 'stat_surname', '1');
197		$stat_events          = $this->getBlockSetting($block_id, 'stat_events', '1');
198		$stat_users           = $this->getBlockSetting($block_id, 'stat_users', '1');
199		$stat_first_birth     = $this->getBlockSetting($block_id, 'stat_first_birth', '1');
200		$stat_last_birth      = $this->getBlockSetting($block_id, 'stat_last_birth', '1');
201		$stat_first_death     = $this->getBlockSetting($block_id, 'stat_first_death', '1');
202		$stat_last_death      = $this->getBlockSetting($block_id, 'stat_last_death', '1');
203		$stat_long_life       = $this->getBlockSetting($block_id, 'stat_long_life', '1');
204		$stat_avg_life        = $this->getBlockSetting($block_id, 'stat_avg_life', '1');
205		$stat_most_chil       = $this->getBlockSetting($block_id, 'stat_most_chil', '1');
206		$stat_avg_chil        = $this->getBlockSetting($block_id, 'stat_avg_chil', '1');
207
208		?>
209		<fieldset class="form-group">
210			<div class="row">
211				<legend class="col-form-label col-sm-3">
212					<?= I18N::translate('Last change') ?>
213				</legend>
214				<div class="col-sm-9">
215					<?= Bootstrap4::checkbox(/* I18N: label for yes/no option */
216						I18N::translate('Show date of last update'), false, ['name' => 'show_last_update', 'checked' => (bool) $show_last_update]) ?>
217				</div>
218			</div>
219		</fieldset>
220
221		<fieldset class="form-group">
222			<div class="row">
223				<legend class="col-form-label col-sm-3">
224					<?= I18N::translate('Statistics') ?>
225				</legend>
226				<div class="col-sm-9">
227					<?= Bootstrap4::checkbox(I18N::translate('Individuals'), false, ['name' => 'stat_indi', 'checked' => (bool) $stat_indi]) ?>
228					<?= Bootstrap4::checkbox(I18N::translate('Total surnames'), false, ['name' => 'stat_surname', 'checked' => (bool) $stat_surname]) ?>
229					<?= Bootstrap4::checkbox(I18N::translate('Families'), false, ['name' => 'stat_fam', 'checked' => (bool) $stat_fam]) ?>
230					<?= Bootstrap4::checkbox(I18N::translate('Sources'), false, ['name' => 'stat_sour', 'checked' => (bool) $stat_sour]) ?>
231					<?= Bootstrap4::checkbox(I18N::translate('Media objects'), false, ['name' => 'stat_media', 'checked' => (bool) $stat_media]) ?>
232					<?= Bootstrap4::checkbox(I18N::translate('Repositories'), false, ['name' => 'stat_repo', 'checked' => (bool) $stat_repo]) ?>
233					<?= Bootstrap4::checkbox(I18N::translate('Total events'), false, ['name' => 'stat_events', 'checked' => (bool) $stat_events]) ?>
234					<?= Bootstrap4::checkbox(I18N::translate('Total users'), false, ['name' => 'stat_users', 'checked' => (bool) $stat_users]) ?>
235					<?= Bootstrap4::checkbox(I18N::translate('Earliest birth'), false, ['name' => 'stat_first_birth', 'checked' => (bool) $stat_first_birth]) ?>
236					<?= Bootstrap4::checkbox(I18N::translate('Latest birth'), false, ['name' => 'stat_last_birth', 'checked' => (bool) $stat_last_birth]) ?>
237					<?= Bootstrap4::checkbox(I18N::translate('Earliest death'), false, ['name' => 'stat_first_death', 'checked' => (bool) $stat_first_death]) ?>
238					<?= Bootstrap4::checkbox(I18N::translate('Latest death'), false, ['name' => 'stat_last_death', 'checked' => (bool) $stat_last_death]) ?>
239					<?= Bootstrap4::checkbox(I18N::translate('Individual who lived the longest'), false, ['name' => 'stat_long_life', 'checked' => (bool) $stat_long_life]) ?>
240					<?= Bootstrap4::checkbox(I18N::translate('Average age at death'), false, ['name' => 'stat_avg_life', 'checked' => (bool) $stat_avg_life]) ?>
241					<?= Bootstrap4::checkbox(I18N::translate('Family with the most children'), false, ['name' => 'stat_most_chil', 'checked' => (bool) $stat_most_chil]) ?>
242					<?= Bootstrap4::checkbox(I18N::translate('Average number of children per family'), false, ['name' => 'stat_avg_chil', 'checked' => (bool) $stat_avg_chil]) ?>
243				</div>
244			</div>
245		</fieldset>
246
247		<fieldset class="form-group">
248			<div class="row">
249				<legend class="col-form-label col-sm-3">
250					<label for="show_common_surnames">
251						<?= I18N::translate('Surnames') ?>
252					</label>
253				</legend>
254				<div class="col-sm-9">
255					<?= Bootstrap4::checkbox(I18N::translate('Most common surnames'), false, ['name' => 'show_common_surnames', 'checked' => (bool) $show_common_surnames]) ?>
256					<label for="number_of_surnames">
257						<?= /* I18N: ... to show in a list */
258						I18N::translate('Number of surnames') ?>
259						<input
260							class="form-control"
261							id="number_of_surnames"
262							maxlength="5"
263							name="number_of_surnames"
264							pattern="[1-9][0-9]*"
265							required
266							type="text"
267							value="<?= e($number_of_surnames) ?>"
268						>
269					</label>
270				</div>
271			</div>
272		</fieldset>
273		<?php
274	}
275}
276