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