xref: /webtrees/app/Module/TopGivenNamesModule.php (revision cbc1590a8c715aa2d88bd745610b899587bd9563)
1<?php
2namespace Fisharebest\Webtrees;
3
4/**
5 * webtrees: online genealogy
6 * Copyright (C) 2015 webtrees development team
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19/**
20 * Class TopGivenNamesModule
21 */
22class TopGivenNamesModule extends AbstractModule implements ModuleBlockInterface {
23	/** {@inheritdoc} */
24	public function getTitle() {
25		return /* I18N: Name of a module.  Top=Most common */ I18N::translate('Top given names');
26	}
27
28	/** {@inheritdoc} */
29	public function getDescription() {
30		return /* I18N: Description of the “Top given names” module */ I18N::translate('A list of the most popular given names.');
31	}
32
33	/** {@inheritdoc} */
34	public function getBlock($block_id, $template = true, $cfg = null) {
35		global $ctype, $WT_TREE;
36
37		$num       = $this->getBlockSetting($block_id, 'num', '10');
38		$infoStyle = $this->getBlockSetting($block_id, 'infoStyle', 'table');
39		$block     = $this->getBlockSetting($block_id, 'block', '0');
40
41		if ($cfg) {
42			foreach (array('num', 'infoStyle', 'block') as $name) {
43				if (array_key_exists($name, $cfg)) {
44					$$name = $cfg[$name];
45				}
46			}
47		}
48
49		$stats = new Stats($WT_TREE);
50
51		$id    = $this->getName() . $block_id;
52		$class = $this->getName() . '_block';
53		if ($ctype === 'gedcom' && Auth::isManager($WT_TREE) || $ctype === 'user' && Auth::check()) {
54			$title = '<a class="icon-admin" title="' . I18N::translate('Configure') . '" href="block_edit.php?block_id=' . $block_id . '&amp;ged=' . $WT_TREE->getNameHtml() . '&amp;ctype=' . $ctype . '"></a>';
55		} else {
56			$title = '';
57		}
58		if ($num == 1) {
59			// I18N: i.e. most popular given name.
60			$title .= I18N::translate('Top given name');
61		} else {
62			// 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
63			$title .= I18N::plural('Top %s given name', 'Top %s given names', $num, I18N::number($num));
64		}
65
66		$content = '<div class="normal_inner_block">';
67		//Select List or Table
68		switch ($infoStyle) {
69		case "list": // Output style 1:  Simple list style.  Better suited to left side of page.
70			if (I18N::direction() === 'ltr') {
71				$padding = 'padding-left: 15px';
72			} else {
73				$padding = 'padding-right: 15px';
74			}
75			$params = array(1, $num, 'rcount');
76			// List Female names
77			$totals = $stats->commonGivenFemaleTotals($params);
78			if ($totals) {
79				$content .= '<b>' . I18N::translate('Females') . '</b><div class="wrap" style="' . $padding . '">' . $totals . '</div><br>';
80			}
81			// List Male names
82			$totals = $stats->commonGivenMaleTotals($params);
83			if ($totals) {
84				$content .= '<b>' . I18N::translate('Males') . '</b><div class="wrap" style="' . $padding . '">' . $totals . '</div><br>';
85			}
86			break;
87		case "table": // Style 2: Tabular format.  Narrow, 2 or 3 column table, good on right side of page
88			$params = array(1, $num, 'rcount');
89			$content .= '<table style="margin:auto;">
90						<tr valign="top">
91						<td>' . $stats->commonGivenFemaleTable($params) . '</td>
92						<td>' . $stats->commonGivenMaleTable($params) . '</td>';
93			$content .= '</tr></table>';
94			break;
95		}
96		$content .= "</div>";
97
98		if ($template) {
99			if ($block) {
100				$class .= ' small_inner_block';
101			}
102
103			return Theme::theme()->formatBlock($id, $title, $class, $content);
104		} else {
105			return $content;
106		}
107	}
108
109	/** {@inheritdoc} */
110	public function loadAjax() {
111		return true;
112	}
113
114	/** {@inheritdoc} */
115	public function isUserBlock() {
116		return true;
117	}
118
119	/** {@inheritdoc} */
120	public function isGedcomBlock() {
121		return true;
122	}
123
124	/** {@inheritdoc} */
125	public function configureBlock($block_id) {
126		if (Filter::postBool('save') && Filter::checkCsrf()) {
127			$this->setBlockSetting($block_id, 'num', Filter::postInteger('num', 1, 10000, 10));
128			$this->setBlockSetting($block_id, 'infoStyle', Filter::post('infoStyle', 'list|table', 'table'));
129			$this->setBlockSetting($block_id, 'block', Filter::postBool('block'));
130		}
131
132		$num       = $this->getBlockSetting($block_id, 'num', '10');
133		$infoStyle = $this->getBlockSetting($block_id, 'infoStyle', 'table');
134		$block     = $this->getBlockSetting($block_id, 'block', '0');
135
136		echo '<tr><td class="descriptionbox wrap width33">';
137		echo I18N::translate('Number of items to show');
138		echo '</td><td class="optionbox">';
139		echo '<input type="text" name="num" size="2" value="', $num, '">';
140		echo '</td></tr>';
141
142		echo '<tr><td class="descriptionbox wrap width33">';
143		echo I18N::translate('Presentation style');
144		echo '</td><td class="optionbox">';
145		echo select_edit_control('infoStyle', array('list' => I18N::translate('list'), 'table' => I18N::translate('table')), null, $infoStyle, '');
146		echo '</td></tr>';
147
148		echo '<tr><td class="descriptionbox wrap width33">';
149		echo /* I18N: label for a yes/no option */ I18N::translate('Add a scrollbar when block contents grow');
150		echo '</td><td class="optionbox">';
151		echo edit_field_yes_no('block', $block);
152		echo '</td></tr>';
153	}
154}
155