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