xref: /webtrees/app/Module/TopGivenNamesModule.php (revision 1001c3a8c3f3447dd95f8a9481ce18ff581bd6da)
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\Filter;
20use Fisharebest\Webtrees\I18N;
21use Fisharebest\Webtrees\Stats;
22use Fisharebest\Webtrees\Tree;
23
24/**
25 * Class TopGivenNamesModule
26 */
27class TopGivenNamesModule extends AbstractModule implements ModuleBlockInterface {
28	/** {@inheritdoc} */
29	public function getTitle() {
30		return /* I18N: Name of a module. Top=Most common */ I18N::translate('Top given names');
31	}
32
33	/** {@inheritdoc} */
34	public function getDescription() {
35		return /* I18N: Description of the “Top given names” module */ I18N::translate('A list of the most popular given names.');
36	}
37
38	/**
39	 * Generate the HTML content of this block.
40	 *
41	 * @param Tree     $tree
42	 * @param int      $block_id
43	 * @param bool     $template
44	 * @param string[] $cfg
45	 *
46	 * @return string
47	 */
48	public function getBlock(Tree $tree, int $block_id, bool $template = true, array $cfg = []): string {
49		global $ctype;
50
51		$num       = $this->getBlockSetting($block_id, 'num', '10');
52		$infoStyle = $this->getBlockSetting($block_id, 'infoStyle', 'table');
53
54		extract($cfg, EXTR_OVERWRITE);
55
56		$stats = new Stats($tree);
57
58		switch ($infoStyle) {
59			case 'list':
60				$males   = $stats->commonGivenMaleTotals([1, $num, 'rcount']);
61				$females = $stats->commonGivenFemaleTotals([1, $num, 'rcount']);
62				$content = view('blocks/top-given-names-list', [
63					'males'   => $males,
64					'females' => $females,
65				]);
66				break;
67			default:
68			case 'table':
69				$males   = $stats->commonGivenMaleTable([1, $num, 'rcount']);
70				$females = $stats->commonGivenFemaleTable([1, $num, 'rcount']);
71				$content = view('blocks/top-given-names-table', [
72					'males'   => $males,
73					'females' => $females,
74				]);
75				break;
76		}
77
78		if ($template) {
79			if ($num == 1) {
80				// I18N: i.e. most popular given name.
81				$title = I18N::translate('Top given name');
82			} else {
83				// I18N: Title for a list of the most common given names, %s is a number. Note that a separate translation exists when %s is 1
84				$title = I18N::plural('Top %s given name', 'Top %s given names', $num, I18N::number($num));
85			}
86
87			if ($ctype === 'gedcom' && Auth::isManager($tree)) {
88				$config_url = route('tree-page-block-edit', ['block_id' => $block_id, 'ged' => $tree->getName()]);
89			} elseif ($ctype === 'user' && Auth::check()) {
90				$config_url = route('user-page-block-edit', ['block_id' => $block_id, 'ged' => $tree->getName()]);
91			} else {
92				$config_url = '';
93			}
94
95			return view('blocks/template', [
96				'block'      => str_replace('_', '-', $this->getName()),
97				'id'         => $block_id,
98				'config_url' => $config_url,
99				'title'      => $title,
100				'content'    => $content,
101			]);
102		} else {
103			return $content;
104		}
105	}
106
107	/** {@inheritdoc} */
108	public function loadAjax(): bool {
109		return false;
110	}
111
112	/** {@inheritdoc} */
113	public function isUserBlock(): bool {
114		return true;
115	}
116
117	/** {@inheritdoc} */
118	public function isGedcomBlock(): bool {
119		return true;
120	}
121
122	/**
123	 * An HTML form to edit block settings
124	 *
125	 * @param Tree $tree
126	 * @param int  $block_id
127	 *
128	 * @return void
129	 */
130	public function configureBlock(Tree $tree, int $block_id) {
131		if ($_SERVER['REQUEST_METHOD'] === 'POST') {
132			$this->setBlockSetting($block_id, 'num', Filter::postInteger('num', 1, 10000, 10));
133			$this->setBlockSetting($block_id, 'infoStyle', Filter::post('infoStyle', 'list|table', 'table'));
134
135			return;
136		}
137
138		$num       = $this->getBlockSetting($block_id, 'num', '10');
139		$infoStyle = $this->getBlockSetting($block_id, 'infoStyle', 'table');
140
141		$info_styles = [
142			'list'  => /* I18N: An option in a list-box */ I18N::translate('list'),
143			'table' => /* I18N: An option in a list-box */ I18N::translate('table'),
144		];
145
146		echo view('blocks/top-given-names-config', [
147			'infoStyle'   => $infoStyle,
148			'info_styles' => $info_styles,
149			'num'         => $num,
150		]);
151	}
152}
153