xref: /webtrees/app/Module/FamilyTreeStatisticsModule.php (revision c385536d61a9b9bed37314ecc08afa8429a269ef)
18c2e8227SGreg Roach<?php
28c2e8227SGreg Roach/**
38c2e8227SGreg Roach * webtrees: online genealogy
41062a142SGreg Roach * Copyright (C) 2018 webtrees development team
58c2e8227SGreg Roach * This program is free software: you can redistribute it and/or modify
68c2e8227SGreg Roach * it under the terms of the GNU General Public License as published by
78c2e8227SGreg Roach * the Free Software Foundation, either version 3 of the License, or
88c2e8227SGreg Roach * (at your option) any later version.
98c2e8227SGreg Roach * This program is distributed in the hope that it will be useful,
108c2e8227SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
118c2e8227SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
128c2e8227SGreg Roach * GNU General Public License for more details.
138c2e8227SGreg Roach * You should have received a copy of the GNU General Public License
148c2e8227SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
158c2e8227SGreg Roach */
1676692c8bSGreg Roachnamespace Fisharebest\Webtrees\Module;
1776692c8bSGreg Roach
180e62c4b8SGreg Roachuse Fisharebest\Webtrees\Auth;
190e62c4b8SGreg Roachuse Fisharebest\Webtrees\Filter;
203d7a8a4cSGreg Roachuse Fisharebest\Webtrees\Functions\FunctionsDb;
21f36626dbSGreg Roachuse Fisharebest\Webtrees\Functions\FunctionsPrintLists;
220e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N;
23f36626dbSGreg Roachuse Fisharebest\Webtrees\Query\QueryName;
240e62c4b8SGreg Roachuse Fisharebest\Webtrees\Stats;
258c2e8227SGreg Roach
268c2e8227SGreg Roach/**
278c2e8227SGreg Roach * Class FamilyTreeStatisticsModule
288c2e8227SGreg Roach */
29e2a378d3SGreg Roachclass FamilyTreeStatisticsModule extends AbstractModule implements ModuleBlockInterface {
30f36626dbSGreg Roach	/** Show this number of surnames by default */
31f36626dbSGreg Roach	const DEFAULT_NUMBER_OF_SURNAMES = 10;
32f36626dbSGreg Roach
338c2e8227SGreg Roach	/** {@inheritdoc} */
348c2e8227SGreg Roach	public function getTitle() {
351d3c0c1aSGreg Roach		return /* I18N: Name of a module */
361d3c0c1aSGreg Roach			I18N::translate('Statistics');
378c2e8227SGreg Roach	}
388c2e8227SGreg Roach
398c2e8227SGreg Roach	/** {@inheritdoc} */
408c2e8227SGreg Roach	public function getDescription() {
411d3c0c1aSGreg Roach		return /* I18N: Description of “Statistics” module */
421d3c0c1aSGreg Roach			I18N::translate('The size of the family tree, earliest and latest events, common names, etc.');
438c2e8227SGreg Roach	}
448c2e8227SGreg Roach
4576692c8bSGreg Roach	/**
4676692c8bSGreg Roach	 * Generate the HTML content of this block.
4776692c8bSGreg Roach	 *
4876692c8bSGreg Roach	 * @param int      $block_id
4976692c8bSGreg Roach	 * @param bool     $template
50727f238cSGreg Roach	 * @param string[] $cfg
5176692c8bSGreg Roach	 *
5276692c8bSGreg Roach	 * @return string
5376692c8bSGreg Roach	 */
54a9430be8SGreg Roach	public function getBlock($block_id, $template = true, $cfg = []): string {
558c2e8227SGreg Roach		global $WT_TREE, $ctype;
568c2e8227SGreg Roach
57e2a378d3SGreg Roach		$show_last_update     = $this->getBlockSetting($block_id, 'show_last_update', '1');
58e2a378d3SGreg Roach		$show_common_surnames = $this->getBlockSetting($block_id, 'show_common_surnames', '1');
59f36626dbSGreg Roach		$number_of_surnames   = $this->getBlockSetting($block_id, 'number_of_surnames', self::DEFAULT_NUMBER_OF_SURNAMES);
60e2a378d3SGreg Roach		$stat_indi            = $this->getBlockSetting($block_id, 'stat_indi', '1');
61e2a378d3SGreg Roach		$stat_fam             = $this->getBlockSetting($block_id, 'stat_fam', '1');
62e2a378d3SGreg Roach		$stat_sour            = $this->getBlockSetting($block_id, 'stat_sour', '1');
63e2a378d3SGreg Roach		$stat_media           = $this->getBlockSetting($block_id, 'stat_media', '1');
64e2a378d3SGreg Roach		$stat_repo            = $this->getBlockSetting($block_id, 'stat_repo', '1');
65e2a378d3SGreg Roach		$stat_surname         = $this->getBlockSetting($block_id, 'stat_surname', '1');
66e2a378d3SGreg Roach		$stat_events          = $this->getBlockSetting($block_id, 'stat_events', '1');
67e2a378d3SGreg Roach		$stat_users           = $this->getBlockSetting($block_id, 'stat_users', '1');
68e2a378d3SGreg Roach		$stat_first_birth     = $this->getBlockSetting($block_id, 'stat_first_birth', '1');
69e2a378d3SGreg Roach		$stat_last_birth      = $this->getBlockSetting($block_id, 'stat_last_birth', '1');
70e2a378d3SGreg Roach		$stat_first_death     = $this->getBlockSetting($block_id, 'stat_first_death', '1');
71e2a378d3SGreg Roach		$stat_last_death      = $this->getBlockSetting($block_id, 'stat_last_death', '1');
72e2a378d3SGreg Roach		$stat_long_life       = $this->getBlockSetting($block_id, 'stat_long_life', '1');
73e2a378d3SGreg Roach		$stat_avg_life        = $this->getBlockSetting($block_id, 'stat_avg_life', '1');
74e2a378d3SGreg Roach		$stat_most_chil       = $this->getBlockSetting($block_id, 'stat_most_chil', '1');
75e2a378d3SGreg Roach		$stat_avg_chil        = $this->getBlockSetting($block_id, 'stat_avg_chil', '1');
768c2e8227SGreg Roach
77*c385536dSGreg Roach		extract($cfg, EXTR_OVERWRITE);
788c2e8227SGreg Roach
798c2e8227SGreg Roach		if ($show_common_surnames) {
80f36626dbSGreg Roach			$surnames = FunctionsDb::getTopSurnames($WT_TREE->getTreeId(), 0, (int) $number_of_surnames);
81e0275e5bSGreg Roach
8213abd6f3SGreg Roach			$all_surnames = [];
83f36626dbSGreg Roach			foreach (array_keys($surnames) as $surname) {
84f36626dbSGreg Roach				$all_surnames = array_merge($all_surnames, QueryName::surnames($WT_TREE, $surname, '', false, false));
85f36626dbSGreg Roach			}
86f36626dbSGreg Roach			ksort($all_surnames);
871d3c0c1aSGreg Roach
881d3c0c1aSGreg Roach			$surnames = FunctionsPrintLists::surnameList($all_surnames, 2, false, 'indilist.php', $WT_TREE);
891d3c0c1aSGreg Roach		} else {
901d3c0c1aSGreg Roach			$surnames = '';
918c2e8227SGreg Roach		}
921d3c0c1aSGreg Roach
93225e381fSGreg Roach		$content = view('blocks/family-tree-statistics', [
941d3c0c1aSGreg Roach			'show_last_update'     => $show_last_update,
951d3c0c1aSGreg Roach			'show_common_surnames' => $show_common_surnames,
961d3c0c1aSGreg Roach			'number_of_surnames'   => $number_of_surnames,
971d3c0c1aSGreg Roach			'stat_indi'            => $stat_indi,
981d3c0c1aSGreg Roach			'stat_fam'             => $stat_fam,
991d3c0c1aSGreg Roach			'stat_sour'            => $stat_sour,
1001d3c0c1aSGreg Roach			'stat_media'           => $stat_media,
1011d3c0c1aSGreg Roach			'stat_repo'            => $stat_repo,
1021d3c0c1aSGreg Roach			'stat_surname'         => $stat_surname,
1031d3c0c1aSGreg Roach			'stat_events'          => $stat_events,
1041d3c0c1aSGreg Roach			'stat_users'           => $stat_users,
1051d3c0c1aSGreg Roach			'stat_first_birth'     => $stat_first_birth,
1061d3c0c1aSGreg Roach			'stat_last_birth'      => $stat_last_birth,
1071d3c0c1aSGreg Roach			'stat_first_death'     => $stat_first_death,
1081d3c0c1aSGreg Roach			'stat_last_death'      => $stat_last_death,
1091d3c0c1aSGreg Roach			'stat_long_life'       => $stat_long_life,
1101d3c0c1aSGreg Roach			'stat_avg_life'        => $stat_avg_life,
1111d3c0c1aSGreg Roach			'stat_most_chil'       => $stat_most_chil,
1121d3c0c1aSGreg Roach			'stat_avg_chil'        => $stat_avg_chil,
1131d3c0c1aSGreg Roach			'stats'                => new Stats($WT_TREE),
1141d3c0c1aSGreg Roach			'surnames'             => $surnames,
1151d3c0c1aSGreg Roach		]);
11628941e3cSGreg Roach
1178c2e8227SGreg Roach		if ($template) {
118397e599aSGreg Roach			if ($ctype === 'gedcom' && Auth::isManager($WT_TREE)) {
119397e599aSGreg Roach				$config_url = route('tree-page-block-edit', ['block_id' => $block_id, 'ged' => $WT_TREE->getName()]);
120397e599aSGreg Roach			} elseif ($ctype === 'user' && Auth::check()) {
121397e599aSGreg Roach				$config_url = route('user-page-block-edit', ['block_id' => $block_id, 'ged' => $WT_TREE->getName()]);
1229c6524dcSGreg Roach			} else {
1239c6524dcSGreg Roach				$config_url = '';
1249c6524dcSGreg Roach			}
1259c6524dcSGreg Roach
126225e381fSGreg Roach			return view('blocks/template', [
1279c6524dcSGreg Roach				'block'      => str_replace('_', '-', $this->getName()),
1289c6524dcSGreg Roach				'id'         => $block_id,
1299c6524dcSGreg Roach				'config_url' => $config_url,
1309c6524dcSGreg Roach				'title'      => $this->getTitle(),
1319c6524dcSGreg Roach				'content'    => $content,
1329c6524dcSGreg Roach			]);
1338c2e8227SGreg Roach		} else {
1348c2e8227SGreg Roach			return $content;
1358c2e8227SGreg Roach		}
1368c2e8227SGreg Roach	}
1378c2e8227SGreg Roach
1388c2e8227SGreg Roach	/** {@inheritdoc} */
139a9430be8SGreg Roach	public function loadAjax(): bool {
1408c2e8227SGreg Roach		return true;
1418c2e8227SGreg Roach	}
1428c2e8227SGreg Roach
1438c2e8227SGreg Roach	/** {@inheritdoc} */
144a9430be8SGreg Roach	public function isUserBlock(): bool {
1458c2e8227SGreg Roach		return true;
1468c2e8227SGreg Roach	}
1478c2e8227SGreg Roach
1488c2e8227SGreg Roach	/** {@inheritdoc} */
149a9430be8SGreg Roach	public function isGedcomBlock(): bool {
1508c2e8227SGreg Roach		return true;
1518c2e8227SGreg Roach	}
1528c2e8227SGreg Roach
15376692c8bSGreg Roach	/**
15476692c8bSGreg Roach	 * An HTML form to edit block settings
15576692c8bSGreg Roach	 *
15676692c8bSGreg Roach	 * @param int $block_id
157a9430be8SGreg Roach	 *
158a9430be8SGreg Roach	 * @return void
15976692c8bSGreg Roach	 */
160be9a728cSGreg Roach	public function configureBlock($block_id) {
161*c385536dSGreg Roach		if ($_SERVER['REQUEST_METHOD'] === 'POST') {
162e2a378d3SGreg Roach			$this->setBlockSetting($block_id, 'show_last_update', Filter::postBool('show_last_update'));
163e2a378d3SGreg Roach			$this->setBlockSetting($block_id, 'show_common_surnames', Filter::postBool('show_common_surnames'));
164f36626dbSGreg Roach			$this->setBlockSetting($block_id, 'number_of_surnames', Filter::postInteger('number_of_surnames'));
165e2a378d3SGreg Roach			$this->setBlockSetting($block_id, 'stat_indi', Filter::postBool('stat_indi'));
166e2a378d3SGreg Roach			$this->setBlockSetting($block_id, 'stat_fam', Filter::postBool('stat_fam'));
167e2a378d3SGreg Roach			$this->setBlockSetting($block_id, 'stat_sour', Filter::postBool('stat_sour'));
168e2a378d3SGreg Roach			$this->setBlockSetting($block_id, 'stat_other', Filter::postBool('stat_other'));
169e2a378d3SGreg Roach			$this->setBlockSetting($block_id, 'stat_media', Filter::postBool('stat_media'));
170e2a378d3SGreg Roach			$this->setBlockSetting($block_id, 'stat_repo', Filter::postBool('stat_repo'));
171e2a378d3SGreg Roach			$this->setBlockSetting($block_id, 'stat_surname', Filter::postBool('stat_surname'));
172e2a378d3SGreg Roach			$this->setBlockSetting($block_id, 'stat_events', Filter::postBool('stat_events'));
173e2a378d3SGreg Roach			$this->setBlockSetting($block_id, 'stat_users', Filter::postBool('stat_users'));
174e2a378d3SGreg Roach			$this->setBlockSetting($block_id, 'stat_first_birth', Filter::postBool('stat_first_birth'));
175e2a378d3SGreg Roach			$this->setBlockSetting($block_id, 'stat_last_birth', Filter::postBool('stat_last_birth'));
176e2a378d3SGreg Roach			$this->setBlockSetting($block_id, 'stat_first_death', Filter::postBool('stat_first_death'));
177e2a378d3SGreg Roach			$this->setBlockSetting($block_id, 'stat_last_death', Filter::postBool('stat_last_death'));
178e2a378d3SGreg Roach			$this->setBlockSetting($block_id, 'stat_long_life', Filter::postBool('stat_long_life'));
179e2a378d3SGreg Roach			$this->setBlockSetting($block_id, 'stat_avg_life', Filter::postBool('stat_avg_life'));
180e2a378d3SGreg Roach			$this->setBlockSetting($block_id, 'stat_most_chil', Filter::postBool('stat_most_chil'));
181e2a378d3SGreg Roach			$this->setBlockSetting($block_id, 'stat_avg_chil', Filter::postBool('stat_avg_chil'));
182*c385536dSGreg Roach
183*c385536dSGreg Roach			return;
1848c2e8227SGreg Roach		}
1858c2e8227SGreg Roach
186e2a378d3SGreg Roach		$show_last_update     = $this->getBlockSetting($block_id, 'show_last_update', '1');
187e2a378d3SGreg Roach		$show_common_surnames = $this->getBlockSetting($block_id, 'show_common_surnames', '1');
188f36626dbSGreg Roach		$number_of_surnames   = $this->getBlockSetting($block_id, 'number_of_surnames', self::DEFAULT_NUMBER_OF_SURNAMES);
189e2a378d3SGreg Roach		$stat_indi            = $this->getBlockSetting($block_id, 'stat_indi', '1');
190e2a378d3SGreg Roach		$stat_fam             = $this->getBlockSetting($block_id, 'stat_fam', '1');
191e2a378d3SGreg Roach		$stat_sour            = $this->getBlockSetting($block_id, 'stat_sour', '1');
192e2a378d3SGreg Roach		$stat_media           = $this->getBlockSetting($block_id, 'stat_media', '1');
193e2a378d3SGreg Roach		$stat_repo            = $this->getBlockSetting($block_id, 'stat_repo', '1');
194e2a378d3SGreg Roach		$stat_surname         = $this->getBlockSetting($block_id, 'stat_surname', '1');
195e2a378d3SGreg Roach		$stat_events          = $this->getBlockSetting($block_id, 'stat_events', '1');
196e2a378d3SGreg Roach		$stat_users           = $this->getBlockSetting($block_id, 'stat_users', '1');
197e2a378d3SGreg Roach		$stat_first_birth     = $this->getBlockSetting($block_id, 'stat_first_birth', '1');
198e2a378d3SGreg Roach		$stat_last_birth      = $this->getBlockSetting($block_id, 'stat_last_birth', '1');
199e2a378d3SGreg Roach		$stat_first_death     = $this->getBlockSetting($block_id, 'stat_first_death', '1');
200e2a378d3SGreg Roach		$stat_last_death      = $this->getBlockSetting($block_id, 'stat_last_death', '1');
201e2a378d3SGreg Roach		$stat_long_life       = $this->getBlockSetting($block_id, 'stat_long_life', '1');
202e2a378d3SGreg Roach		$stat_avg_life        = $this->getBlockSetting($block_id, 'stat_avg_life', '1');
203e2a378d3SGreg Roach		$stat_most_chil       = $this->getBlockSetting($block_id, 'stat_most_chil', '1');
204e2a378d3SGreg Roach		$stat_avg_chil        = $this->getBlockSetting($block_id, 'stat_avg_chil', '1');
2058c2e8227SGreg Roach
206*c385536dSGreg Roach		echo view('blocks/family-tree-statistics-config', [
207*c385536dSGreg Roach			'show_last_update'     => $show_last_update,
208*c385536dSGreg Roach			'show_common_surnames' => $show_common_surnames,
209*c385536dSGreg Roach			'number_of_surnames'   => $number_of_surnames,
210*c385536dSGreg Roach			'stat_indi'            => $stat_indi,
211*c385536dSGreg Roach			'stat_fam'             => $stat_fam,
212*c385536dSGreg Roach			'stat_sour'            => $stat_sour,
213*c385536dSGreg Roach			'stat_media'           => $stat_media,
214*c385536dSGreg Roach			'stat_repo'            => $stat_repo,
215*c385536dSGreg Roach			'stat_surname'         => $stat_surname,
216*c385536dSGreg Roach			'stat_events'          => $stat_events,
217*c385536dSGreg Roach			'stat_users'           => $stat_users,
218*c385536dSGreg Roach			'stat_first_birth'     => $stat_first_birth,
219*c385536dSGreg Roach			'stat_last_birth'      => $stat_last_birth,
220*c385536dSGreg Roach			'stat_first_death'     => $stat_first_death,
221*c385536dSGreg Roach			'stat_last_death'      => $stat_last_death,
222*c385536dSGreg Roach			'stat_long_life'       => $stat_long_life,
223*c385536dSGreg Roach			'stat_avg_life'        => $stat_avg_life,
224*c385536dSGreg Roach			'stat_most_chil'       => $stat_most_chil,
225*c385536dSGreg Roach			'stat_avg_chil'        => $stat_avg_chil,
226*c385536dSGreg Roach		]);
2278c2e8227SGreg Roach	}
2288c2e8227SGreg Roach}
229