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