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