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