xref: /webtrees/app/Module/FamilyTreeStatisticsModule.php (revision 241f182934a11b1e3bd3e3fb316f3f3f2c05b074)
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 = []) {
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() {
145		return true;
146	}
147
148	/** {@inheritdoc} */
149	public function isUserBlock() {
150		return true;
151	}
152
153	/** {@inheritdoc} */
154	public function isGedcomBlock() {
155		return true;
156	}
157
158	/**
159	 * An HTML form to edit block settings
160	 *
161	 * @param int $block_id
162	 */
163	public function configureBlock($block_id) {
164		if (Filter::postBool('save') && Filter::checkCsrf()) {
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
187		$show_last_update     = $this->getBlockSetting($block_id, 'show_last_update', '1');
188		$show_common_surnames = $this->getBlockSetting($block_id, 'show_common_surnames', '1');
189		$number_of_surnames   = $this->getBlockSetting($block_id, 'number_of_surnames', self::DEFAULT_NUMBER_OF_SURNAMES);
190		$stat_indi            = $this->getBlockSetting($block_id, 'stat_indi', '1');
191		$stat_fam             = $this->getBlockSetting($block_id, 'stat_fam', '1');
192		$stat_sour            = $this->getBlockSetting($block_id, 'stat_sour', '1');
193		$stat_media           = $this->getBlockSetting($block_id, 'stat_media', '1');
194		$stat_repo            = $this->getBlockSetting($block_id, 'stat_repo', '1');
195		$stat_surname         = $this->getBlockSetting($block_id, 'stat_surname', '1');
196		$stat_events          = $this->getBlockSetting($block_id, 'stat_events', '1');
197		$stat_users           = $this->getBlockSetting($block_id, 'stat_users', '1');
198		$stat_first_birth     = $this->getBlockSetting($block_id, 'stat_first_birth', '1');
199		$stat_last_birth      = $this->getBlockSetting($block_id, 'stat_last_birth', '1');
200		$stat_first_death     = $this->getBlockSetting($block_id, 'stat_first_death', '1');
201		$stat_last_death      = $this->getBlockSetting($block_id, 'stat_last_death', '1');
202		$stat_long_life       = $this->getBlockSetting($block_id, 'stat_long_life', '1');
203		$stat_avg_life        = $this->getBlockSetting($block_id, 'stat_avg_life', '1');
204		$stat_most_chil       = $this->getBlockSetting($block_id, 'stat_most_chil', '1');
205		$stat_avg_chil        = $this->getBlockSetting($block_id, 'stat_avg_chil', '1');
206
207		?>
208		<fieldset class="form-group">
209			<div class="row">
210				<legend class="col-form-legend col-sm-3">
211					<?= I18N::translate('Last change') ?>
212				</legend>
213				<div class="col-sm-9">
214					<?= Bootstrap4::checkbox(/* I18N: label for yes/no option */
215						I18N::translate('Show date of last update'), false, ['name' => 'show_last_update', 'checked' => (bool) $show_last_update]) ?>
216				</div>
217			</div>
218		</fieldset>
219
220		<fieldset class="form-group">
221			<div class="row">
222				<legend class="col-form-legend col-sm-3">
223					<?= I18N::translate('Statistics') ?>
224				</legend>
225				<div class="col-sm-9">
226					<?= Bootstrap4::checkbox(I18N::translate('Individuals'), false, ['name' => 'stat_indi', 'checked' => (bool) $stat_indi]) ?>
227					<?= Bootstrap4::checkbox(I18N::translate('Total surnames'), false, ['name' => 'stat_surname', 'checked' => (bool) $stat_surname]) ?>
228					<?= Bootstrap4::checkbox(I18N::translate('Families'), false, ['name' => 'stat_fam', 'checked' => (bool) $stat_fam]) ?>
229					<?= Bootstrap4::checkbox(I18N::translate('Sources'), false, ['name' => 'stat_sour', 'checked' => (bool) $stat_sour]) ?>
230					<?= Bootstrap4::checkbox(I18N::translate('Media objects'), false, ['name' => 'stat_media', 'checked' => (bool) $stat_media]) ?>
231					<?= Bootstrap4::checkbox(I18N::translate('Repositories'), false, ['name' => 'stat_repo', 'checked' => (bool) $stat_repo]) ?>
232					<?= Bootstrap4::checkbox(I18N::translate('Total events'), false, ['name' => 'stat_events', 'checked' => (bool) $stat_events]) ?>
233					<?= Bootstrap4::checkbox(I18N::translate('Total users'), false, ['name' => 'stat_users', 'checked' => (bool) $stat_users]) ?>
234					<?= Bootstrap4::checkbox(I18N::translate('Earliest birth'), false, ['name' => 'stat_first_birth', 'checked' => (bool) $stat_first_birth]) ?>
235					<?= Bootstrap4::checkbox(I18N::translate('Latest birth'), false, ['name' => 'stat_last_birth', 'checked' => (bool) $stat_last_birth]) ?>
236					<?= Bootstrap4::checkbox(I18N::translate('Earliest death'), false, ['name' => 'stat_first_death', 'checked' => (bool) $stat_first_death]) ?>
237					<?= Bootstrap4::checkbox(I18N::translate('Latest death'), false, ['name' => 'stat_last_death', 'checked' => (bool) $stat_last_death]) ?>
238					<?= Bootstrap4::checkbox(I18N::translate('Individual who lived the longest'), false, ['name' => 'stat_long_life', 'checked' => (bool) $stat_long_life]) ?>
239					<?= Bootstrap4::checkbox(I18N::translate('Average age at death'), false, ['name' => 'stat_avg_life', 'checked' => (bool) $stat_avg_life]) ?>
240					<?= Bootstrap4::checkbox(I18N::translate('Family with the most children'), false, ['name' => 'stat_most_chil', 'checked' => (bool) $stat_most_chil]) ?>
241					<?= Bootstrap4::checkbox(I18N::translate('Average number of children per family'), false, ['name' => 'stat_avg_chil', 'checked' => (bool) $stat_avg_chil]) ?>
242				</div>
243			</div>
244		</fieldset>
245
246		<fieldset class="form-group">
247			<div class="row">
248				<legend class="col-form-legend col-sm-3">
249					<label for="show_common_surnames">
250						<?= I18N::translate('Surnames') ?>
251					</label>
252				</legend>
253				<div class="col-sm-9">
254					<?= Bootstrap4::checkbox(I18N::translate('Most common surnames'), false, ['name' => 'show_common_surnames', 'checked' => (bool) $show_common_surnames]) ?>
255					<label for="number_of_surnames">
256						<?= /* I18N: ... to show in a list */
257						I18N::translate('Number of surnames') ?>
258						<input
259							class="form-control"
260							id="number_of_surnames"
261							maxlength="5"
262							name="number_of_surnames"
263							pattern="[1-9][0-9]*"
264							required
265							type="text"
266							value="<?= Html::escape($number_of_surnames) ?>"
267						>
268					</label>
269				</div>
270			</div>
271		</fieldset>
272		<?php
273	}
274}
275