xref: /webtrees/app/Module/FamilyTreeStatisticsModule.php (revision e490cd80ad2d75f9f6adf9b41698ad3f3f058276)
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;
25*e490cd80SGreg Roachuse Fisharebest\Webtrees\Tree;
268c2e8227SGreg Roach
278c2e8227SGreg Roach/**
288c2e8227SGreg Roach * Class FamilyTreeStatisticsModule
298c2e8227SGreg Roach */
30e2a378d3SGreg Roachclass FamilyTreeStatisticsModule extends AbstractModule implements ModuleBlockInterface {
31f36626dbSGreg Roach	/** Show this number of surnames by default */
32f36626dbSGreg Roach	const DEFAULT_NUMBER_OF_SURNAMES = 10;
33f36626dbSGreg Roach
348c2e8227SGreg Roach	/** {@inheritdoc} */
358c2e8227SGreg Roach	public function getTitle() {
361d3c0c1aSGreg Roach		return /* I18N: Name of a module */
371d3c0c1aSGreg Roach			I18N::translate('Statistics');
388c2e8227SGreg Roach	}
398c2e8227SGreg Roach
408c2e8227SGreg Roach	/** {@inheritdoc} */
418c2e8227SGreg Roach	public function getDescription() {
421d3c0c1aSGreg Roach		return /* I18N: Description of “Statistics” module */
431d3c0c1aSGreg Roach			I18N::translate('The size of the family tree, earliest and latest events, common names, etc.');
448c2e8227SGreg Roach	}
458c2e8227SGreg Roach
4676692c8bSGreg Roach	/**
4776692c8bSGreg Roach	 * Generate the HTML content of this block.
4876692c8bSGreg Roach	 *
49*e490cd80SGreg Roach	 * @param Tree     $tree
5076692c8bSGreg Roach	 * @param int      $block_id
5176692c8bSGreg Roach	 * @param bool     $template
52727f238cSGreg Roach	 * @param string[] $cfg
5376692c8bSGreg Roach	 *
5476692c8bSGreg Roach	 * @return string
5576692c8bSGreg Roach	 */
56*e490cd80SGreg Roach	public function getBlock(Tree $tree, int $block_id, bool $template = true, array $cfg = []): string {
57*e490cd80SGreg Roach		global $ctype;
588c2e8227SGreg Roach
59e2a378d3SGreg Roach		$show_last_update     = $this->getBlockSetting($block_id, 'show_last_update', '1');
60e2a378d3SGreg Roach		$show_common_surnames = $this->getBlockSetting($block_id, 'show_common_surnames', '1');
61f36626dbSGreg Roach		$number_of_surnames   = $this->getBlockSetting($block_id, 'number_of_surnames', self::DEFAULT_NUMBER_OF_SURNAMES);
62e2a378d3SGreg Roach		$stat_indi            = $this->getBlockSetting($block_id, 'stat_indi', '1');
63e2a378d3SGreg Roach		$stat_fam             = $this->getBlockSetting($block_id, 'stat_fam', '1');
64e2a378d3SGreg Roach		$stat_sour            = $this->getBlockSetting($block_id, 'stat_sour', '1');
65e2a378d3SGreg Roach		$stat_media           = $this->getBlockSetting($block_id, 'stat_media', '1');
66e2a378d3SGreg Roach		$stat_repo            = $this->getBlockSetting($block_id, 'stat_repo', '1');
67e2a378d3SGreg Roach		$stat_surname         = $this->getBlockSetting($block_id, 'stat_surname', '1');
68e2a378d3SGreg Roach		$stat_events          = $this->getBlockSetting($block_id, 'stat_events', '1');
69e2a378d3SGreg Roach		$stat_users           = $this->getBlockSetting($block_id, 'stat_users', '1');
70e2a378d3SGreg Roach		$stat_first_birth     = $this->getBlockSetting($block_id, 'stat_first_birth', '1');
71e2a378d3SGreg Roach		$stat_last_birth      = $this->getBlockSetting($block_id, 'stat_last_birth', '1');
72e2a378d3SGreg Roach		$stat_first_death     = $this->getBlockSetting($block_id, 'stat_first_death', '1');
73e2a378d3SGreg Roach		$stat_last_death      = $this->getBlockSetting($block_id, 'stat_last_death', '1');
74e2a378d3SGreg Roach		$stat_long_life       = $this->getBlockSetting($block_id, 'stat_long_life', '1');
75e2a378d3SGreg Roach		$stat_avg_life        = $this->getBlockSetting($block_id, 'stat_avg_life', '1');
76e2a378d3SGreg Roach		$stat_most_chil       = $this->getBlockSetting($block_id, 'stat_most_chil', '1');
77e2a378d3SGreg Roach		$stat_avg_chil        = $this->getBlockSetting($block_id, 'stat_avg_chil', '1');
788c2e8227SGreg Roach
79c385536dSGreg Roach		extract($cfg, EXTR_OVERWRITE);
808c2e8227SGreg Roach
818c2e8227SGreg Roach		if ($show_common_surnames) {
82*e490cd80SGreg Roach			$surnames = FunctionsDb::getTopSurnames($tree->getTreeId(), 0, (int) $number_of_surnames);
83e0275e5bSGreg Roach
8413abd6f3SGreg Roach			$all_surnames = [];
85f36626dbSGreg Roach			foreach (array_keys($surnames) as $surname) {
86*e490cd80SGreg Roach				$all_surnames = array_merge($all_surnames, QueryName::surnames($tree, $surname, '', false, false));
87f36626dbSGreg Roach			}
88f36626dbSGreg Roach			ksort($all_surnames);
891d3c0c1aSGreg Roach
90*e490cd80SGreg Roach			$surnames = FunctionsPrintLists::surnameList($all_surnames, 2, false, 'individual-list', $tree);
911d3c0c1aSGreg Roach		} else {
921d3c0c1aSGreg Roach			$surnames = '';
938c2e8227SGreg Roach		}
941d3c0c1aSGreg Roach
95225e381fSGreg Roach		$content = view('blocks/family-tree-statistics', [
961d3c0c1aSGreg Roach			'show_last_update'     => $show_last_update,
971d3c0c1aSGreg Roach			'show_common_surnames' => $show_common_surnames,
981d3c0c1aSGreg Roach			'number_of_surnames'   => $number_of_surnames,
991d3c0c1aSGreg Roach			'stat_indi'            => $stat_indi,
1001d3c0c1aSGreg Roach			'stat_fam'             => $stat_fam,
1011d3c0c1aSGreg Roach			'stat_sour'            => $stat_sour,
1021d3c0c1aSGreg Roach			'stat_media'           => $stat_media,
1031d3c0c1aSGreg Roach			'stat_repo'            => $stat_repo,
1041d3c0c1aSGreg Roach			'stat_surname'         => $stat_surname,
1051d3c0c1aSGreg Roach			'stat_events'          => $stat_events,
1061d3c0c1aSGreg Roach			'stat_users'           => $stat_users,
1071d3c0c1aSGreg Roach			'stat_first_birth'     => $stat_first_birth,
1081d3c0c1aSGreg Roach			'stat_last_birth'      => $stat_last_birth,
1091d3c0c1aSGreg Roach			'stat_first_death'     => $stat_first_death,
1101d3c0c1aSGreg Roach			'stat_last_death'      => $stat_last_death,
1111d3c0c1aSGreg Roach			'stat_long_life'       => $stat_long_life,
1121d3c0c1aSGreg Roach			'stat_avg_life'        => $stat_avg_life,
1131d3c0c1aSGreg Roach			'stat_most_chil'       => $stat_most_chil,
1141d3c0c1aSGreg Roach			'stat_avg_chil'        => $stat_avg_chil,
115*e490cd80SGreg Roach			'stats'                => new Stats($tree),
1161d3c0c1aSGreg Roach			'surnames'             => $surnames,
1171d3c0c1aSGreg Roach		]);
11828941e3cSGreg Roach
1198c2e8227SGreg Roach		if ($template) {
120*e490cd80SGreg Roach			if ($ctype === 'gedcom' && Auth::isManager($tree)) {
121*e490cd80SGreg Roach				$config_url = route('tree-page-block-edit', ['block_id' => $block_id, 'ged' => $tree->getName()]);
122397e599aSGreg Roach			} elseif ($ctype === 'user' && Auth::check()) {
123*e490cd80SGreg Roach				$config_url = route('user-page-block-edit', ['block_id' => $block_id, 'ged' => $tree->getName()]);
1249c6524dcSGreg Roach			} else {
1259c6524dcSGreg Roach				$config_url = '';
1269c6524dcSGreg Roach			}
1279c6524dcSGreg Roach
128225e381fSGreg Roach			return view('blocks/template', [
1299c6524dcSGreg Roach				'block'      => str_replace('_', '-', $this->getName()),
1309c6524dcSGreg Roach				'id'         => $block_id,
1319c6524dcSGreg Roach				'config_url' => $config_url,
1329c6524dcSGreg Roach				'title'      => $this->getTitle(),
1339c6524dcSGreg Roach				'content'    => $content,
1349c6524dcSGreg Roach			]);
1358c2e8227SGreg Roach		} else {
1368c2e8227SGreg Roach			return $content;
1378c2e8227SGreg Roach		}
1388c2e8227SGreg Roach	}
1398c2e8227SGreg Roach
1408c2e8227SGreg Roach	/** {@inheritdoc} */
141a9430be8SGreg Roach	public function loadAjax(): bool {
1428c2e8227SGreg Roach		return true;
1438c2e8227SGreg Roach	}
1448c2e8227SGreg Roach
1458c2e8227SGreg Roach	/** {@inheritdoc} */
146a9430be8SGreg Roach	public function isUserBlock(): bool {
1478c2e8227SGreg Roach		return true;
1488c2e8227SGreg Roach	}
1498c2e8227SGreg Roach
1508c2e8227SGreg Roach	/** {@inheritdoc} */
151a9430be8SGreg Roach	public function isGedcomBlock(): bool {
1528c2e8227SGreg Roach		return true;
1538c2e8227SGreg Roach	}
1548c2e8227SGreg Roach
15576692c8bSGreg Roach	/**
15676692c8bSGreg Roach	 * An HTML form to edit block settings
15776692c8bSGreg Roach	 *
158*e490cd80SGreg Roach	 * @param Tree $tree
15976692c8bSGreg Roach	 * @param int  $block_id
160a9430be8SGreg Roach	 *
161a9430be8SGreg Roach	 * @return void
16276692c8bSGreg Roach	 */
163*e490cd80SGreg Roach	public function configureBlock(Tree $tree, int $block_id) {
164c385536dSGreg Roach		if ($_SERVER['REQUEST_METHOD'] === 'POST') {
165e2a378d3SGreg Roach			$this->setBlockSetting($block_id, 'show_last_update', Filter::postBool('show_last_update'));
166e2a378d3SGreg Roach			$this->setBlockSetting($block_id, 'show_common_surnames', Filter::postBool('show_common_surnames'));
167f36626dbSGreg Roach			$this->setBlockSetting($block_id, 'number_of_surnames', Filter::postInteger('number_of_surnames'));
168e2a378d3SGreg Roach			$this->setBlockSetting($block_id, 'stat_indi', Filter::postBool('stat_indi'));
169e2a378d3SGreg Roach			$this->setBlockSetting($block_id, 'stat_fam', Filter::postBool('stat_fam'));
170e2a378d3SGreg Roach			$this->setBlockSetting($block_id, 'stat_sour', Filter::postBool('stat_sour'));
171e2a378d3SGreg Roach			$this->setBlockSetting($block_id, 'stat_other', Filter::postBool('stat_other'));
172e2a378d3SGreg Roach			$this->setBlockSetting($block_id, 'stat_media', Filter::postBool('stat_media'));
173e2a378d3SGreg Roach			$this->setBlockSetting($block_id, 'stat_repo', Filter::postBool('stat_repo'));
174e2a378d3SGreg Roach			$this->setBlockSetting($block_id, 'stat_surname', Filter::postBool('stat_surname'));
175e2a378d3SGreg Roach			$this->setBlockSetting($block_id, 'stat_events', Filter::postBool('stat_events'));
176e2a378d3SGreg Roach			$this->setBlockSetting($block_id, 'stat_users', Filter::postBool('stat_users'));
177e2a378d3SGreg Roach			$this->setBlockSetting($block_id, 'stat_first_birth', Filter::postBool('stat_first_birth'));
178e2a378d3SGreg Roach			$this->setBlockSetting($block_id, 'stat_last_birth', Filter::postBool('stat_last_birth'));
179e2a378d3SGreg Roach			$this->setBlockSetting($block_id, 'stat_first_death', Filter::postBool('stat_first_death'));
180e2a378d3SGreg Roach			$this->setBlockSetting($block_id, 'stat_last_death', Filter::postBool('stat_last_death'));
181e2a378d3SGreg Roach			$this->setBlockSetting($block_id, 'stat_long_life', Filter::postBool('stat_long_life'));
182e2a378d3SGreg Roach			$this->setBlockSetting($block_id, 'stat_avg_life', Filter::postBool('stat_avg_life'));
183e2a378d3SGreg Roach			$this->setBlockSetting($block_id, 'stat_most_chil', Filter::postBool('stat_most_chil'));
184e2a378d3SGreg Roach			$this->setBlockSetting($block_id, 'stat_avg_chil', Filter::postBool('stat_avg_chil'));
185c385536dSGreg Roach
186c385536dSGreg Roach			return;
1878c2e8227SGreg Roach		}
1888c2e8227SGreg Roach
189e2a378d3SGreg Roach		$show_last_update     = $this->getBlockSetting($block_id, 'show_last_update', '1');
190e2a378d3SGreg Roach		$show_common_surnames = $this->getBlockSetting($block_id, 'show_common_surnames', '1');
191f36626dbSGreg Roach		$number_of_surnames   = $this->getBlockSetting($block_id, 'number_of_surnames', self::DEFAULT_NUMBER_OF_SURNAMES);
192e2a378d3SGreg Roach		$stat_indi            = $this->getBlockSetting($block_id, 'stat_indi', '1');
193e2a378d3SGreg Roach		$stat_fam             = $this->getBlockSetting($block_id, 'stat_fam', '1');
194e2a378d3SGreg Roach		$stat_sour            = $this->getBlockSetting($block_id, 'stat_sour', '1');
195e2a378d3SGreg Roach		$stat_media           = $this->getBlockSetting($block_id, 'stat_media', '1');
196e2a378d3SGreg Roach		$stat_repo            = $this->getBlockSetting($block_id, 'stat_repo', '1');
197e2a378d3SGreg Roach		$stat_surname         = $this->getBlockSetting($block_id, 'stat_surname', '1');
198e2a378d3SGreg Roach		$stat_events          = $this->getBlockSetting($block_id, 'stat_events', '1');
199e2a378d3SGreg Roach		$stat_users           = $this->getBlockSetting($block_id, 'stat_users', '1');
200e2a378d3SGreg Roach		$stat_first_birth     = $this->getBlockSetting($block_id, 'stat_first_birth', '1');
201e2a378d3SGreg Roach		$stat_last_birth      = $this->getBlockSetting($block_id, 'stat_last_birth', '1');
202e2a378d3SGreg Roach		$stat_first_death     = $this->getBlockSetting($block_id, 'stat_first_death', '1');
203e2a378d3SGreg Roach		$stat_last_death      = $this->getBlockSetting($block_id, 'stat_last_death', '1');
204e2a378d3SGreg Roach		$stat_long_life       = $this->getBlockSetting($block_id, 'stat_long_life', '1');
205e2a378d3SGreg Roach		$stat_avg_life        = $this->getBlockSetting($block_id, 'stat_avg_life', '1');
206e2a378d3SGreg Roach		$stat_most_chil       = $this->getBlockSetting($block_id, 'stat_most_chil', '1');
207e2a378d3SGreg Roach		$stat_avg_chil        = $this->getBlockSetting($block_id, 'stat_avg_chil', '1');
2088c2e8227SGreg Roach
209c385536dSGreg Roach		echo view('blocks/family-tree-statistics-config', [
210c385536dSGreg Roach			'show_last_update'     => $show_last_update,
211c385536dSGreg Roach			'show_common_surnames' => $show_common_surnames,
212c385536dSGreg Roach			'number_of_surnames'   => $number_of_surnames,
213c385536dSGreg Roach			'stat_indi'            => $stat_indi,
214c385536dSGreg Roach			'stat_fam'             => $stat_fam,
215c385536dSGreg Roach			'stat_sour'            => $stat_sour,
216c385536dSGreg Roach			'stat_media'           => $stat_media,
217c385536dSGreg Roach			'stat_repo'            => $stat_repo,
218c385536dSGreg Roach			'stat_surname'         => $stat_surname,
219c385536dSGreg Roach			'stat_events'          => $stat_events,
220c385536dSGreg Roach			'stat_users'           => $stat_users,
221c385536dSGreg Roach			'stat_first_birth'     => $stat_first_birth,
222c385536dSGreg Roach			'stat_last_birth'      => $stat_last_birth,
223c385536dSGreg Roach			'stat_first_death'     => $stat_first_death,
224c385536dSGreg Roach			'stat_last_death'      => $stat_last_death,
225c385536dSGreg Roach			'stat_long_life'       => $stat_long_life,
226c385536dSGreg Roach			'stat_avg_life'        => $stat_avg_life,
227c385536dSGreg Roach			'stat_most_chil'       => $stat_most_chil,
228c385536dSGreg Roach			'stat_avg_chil'        => $stat_avg_chil,
229c385536dSGreg Roach		]);
2308c2e8227SGreg Roach	}
2318c2e8227SGreg Roach}
232