xref: /webtrees/app/Module/TopGivenNamesModule.php (revision ffd703ea1e658c7dcb5e5f1f9ef137a420f2d167)
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			return Theme::theme()->formatBlock($id, $title, $class, $content);
103		} else {
104			return $content;
105		}
106	}
107
108	/** {@inheritdoc} */
109	public function loadAjax() {
110		return true;
111	}
112
113	/** {@inheritdoc} */
114	public function isUserBlock() {
115		return true;
116	}
117
118	/** {@inheritdoc} */
119	public function isGedcomBlock() {
120		return true;
121	}
122
123	/** {@inheritdoc} */
124	public function configureBlock($block_id) {
125		if (Filter::postBool('save') && Filter::checkCsrf()) {
126			$this->setBlockSetting($block_id, 'num', Filter::postInteger('num', 1, 10000, 10));
127			$this->setBlockSetting($block_id, 'infoStyle', Filter::post('infoStyle', 'list|table', 'table'));
128			$this->setBlockSetting($block_id, 'block', Filter::postBool('block'));
129		}
130
131		$num       = $this->getBlockSetting($block_id, 'num', '10');
132		$infoStyle = $this->getBlockSetting($block_id, 'infoStyle', 'table');
133		$block     = $this->getBlockSetting($block_id, 'block', '0');
134
135		echo '<tr><td class="descriptionbox wrap width33">';
136		echo I18N::translate('Number of items to show');
137		echo '</td><td class="optionbox">';
138		echo '<input type="text" name="num" size="2" value="', $num, '">';
139		echo '</td></tr>';
140
141		echo '<tr><td class="descriptionbox wrap width33">';
142		echo I18N::translate('Presentation style');
143		echo '</td><td class="optionbox">';
144		echo select_edit_control('infoStyle', array('list'=> I18N::translate('list'), 'table'=> I18N::translate('table')), null, $infoStyle, '');
145		echo '</td></tr>';
146
147		echo '<tr><td class="descriptionbox wrap width33">';
148		echo /* I18N: label for a yes/no option */ I18N::translate('Add a scrollbar when block contents grow');
149		echo '</td><td class="optionbox">';
150		echo edit_field_yes_no('block', $block);
151		echo '</td></tr>';
152	}
153}
154