xref: /webtrees/app/Module/TopSurnamesModule.php (revision 76692c8b291f16d9251d67f27078779f6737fe7e)
1<?php
2/**
3 * webtrees: online genealogy
4 * Copyright (C) 2015 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\Filter;
20use Fisharebest\Webtrees\Functions\FunctionsDb;
21use Fisharebest\Webtrees\Functions\FunctionsEdit;
22use Fisharebest\Webtrees\Functions\FunctionsPrintLists;
23use Fisharebest\Webtrees\I18N;
24use Fisharebest\Webtrees\Query\QueryName;
25use Fisharebest\Webtrees\Theme;
26
27/**
28 * Class TopSurnamesModule
29 */
30class TopSurnamesModule extends AbstractModule implements ModuleBlockInterface {
31	/**
32	 * How should this module be labelled on tabs, menus, etc.?
33	 *
34	 * @return string
35	 */
36	public function getTitle() {
37		return /* I18N: Name of a module.  Top=Most common */ I18N::translate('Top surnames');
38	}
39
40	/**
41	 * A sentence describing what this module does.
42	 *
43	 * @return string
44	 */
45	public function getDescription() {
46		return /* I18N: Description of the “Top surnames” module */ I18N::translate('A list of the most popular surnames.');
47	}
48
49	/**
50	 * Generate the HTML content of this block.
51	 *
52	 * @param int   $block_id
53	 * @param bool  $template
54	 * @param array $cfg
55	 *
56	 * @return string
57	 */
58	public function getBlock($block_id, $template = true, $cfg = null) {
59		global $WT_TREE, $ctype;
60
61		$COMMON_NAMES_REMOVE    = $WT_TREE->getPreference('COMMON_NAMES_REMOVE');
62		$COMMON_NAMES_THRESHOLD = $WT_TREE->getPreference('COMMON_NAMES_THRESHOLD');
63
64		$num       = $this->getBlockSetting($block_id, 'num', '10');
65		$infoStyle = $this->getBlockSetting($block_id, 'infoStyle', 'table');
66		$block     = $this->getBlockSetting($block_id, 'block', '0');
67
68		foreach (array('num', 'infoStyle', 'block') as $name) {
69			if (array_key_exists($name, $cfg)) {
70				$$name = $cfg[$name];
71			}
72		}
73
74		// This next function is a bit out of date, and doesn't cope well with surname variants
75		$top_surnames = FunctionsDb::getTopSurnames($WT_TREE->getTreeId(), $COMMON_NAMES_THRESHOLD, $num);
76
77		// Remove names found in the "Remove Names" list
78		if ($COMMON_NAMES_REMOVE) {
79			foreach (preg_split("/[,; ]+/", $COMMON_NAMES_REMOVE) as $delname) {
80				unset($top_surnames[$delname]);
81				unset($top_surnames[I18N::strtoupper($delname)]);
82			}
83		}
84
85		$all_surnames = array();
86		$i            = 0;
87		foreach (array_keys($top_surnames) as $top_surname) {
88			$all_surnames = array_merge($all_surnames, QueryName::surnames($WT_TREE, $top_surname, '', false, false));
89			if (++$i == $num) {
90				break;
91			}
92		}
93		if ($i < $num) {
94			$num = $i;
95		}
96		$id    = $this->getName() . $block_id;
97		$class = $this->getName() . '_block';
98		if ($ctype === 'gedcom' && Auth::isManager($WT_TREE) || $ctype === 'user' && Auth::check()) {
99			$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>';
100		} else {
101			$title = '';
102		}
103
104		if ($num == 1) {
105			// I18N: i.e. most popular surname.
106			$title .= I18N::translate('Top surname');
107		} else {
108			// I18N: Title for a list of the most common surnames, %s is a number.  Note that a separate translation exists when %s is 1
109			$title .= I18N::plural('Top %s surname', 'Top %s surnames', $num, I18N::number($num));
110		}
111
112		switch ($infoStyle) {
113		case 'tagcloud':
114			uksort($all_surnames, '\Fisharebest\Webtrees\I18N::strcasecmp');
115			$content = FunctionsPrintLists::surnameTagCloud($all_surnames, 'indilist.php', true, $WT_TREE);
116			break;
117		case 'list':
118			uasort($all_surnames, '\Fisharebest\Webtrees\Module\TopSurnamesModule::surnameCountSort');
119			$content = FunctionsPrintLists::surnameList($all_surnames, '1', true, 'indilist.php', $WT_TREE);
120			break;
121		case 'array':
122			uasort($all_surnames, '\Fisharebest\Webtrees\Module\TopSurnamesModule::surnameCountSort');
123			$content = FunctionsPrintLists::surnameList($all_surnames, '2', true, 'indilist.php', $WT_TREE);
124			break;
125		case 'table':
126		default:
127			uasort($all_surnames, '\Fisharebest\Webtrees\Module\TopSurnamesModule::surnameCountSort');
128			$content = FunctionsPrintLists::surnameTable($all_surnames, 'indilist.php', $WT_TREE);
129			break;
130		}
131
132		if ($template) {
133			if ($block) {
134				$class .= ' small_inner_block';
135			}
136
137			return Theme::theme()->formatBlock($id, $title, $class, $content);
138		} else {
139
140			return $content;
141		}
142	}
143
144	/** {@inheritdoc} */
145	public function loadAjax() {
146		return true;
147	}
148
149	/** {@inheritdoc} */
150	public function isUserBlock() {
151		return true;
152	}
153
154	/** {@inheritdoc} */
155	public function isGedcomBlock() {
156		return true;
157	}
158
159	/**
160	 * An HTML form to edit block settings
161	 *
162	 * @param int $block_id
163	 */
164	public function configureBlock($block_id) {
165		if (Filter::postBool('save') && Filter::checkCsrf()) {
166			$this->setBlockSetting($block_id, 'num', Filter::postInteger('num', 1, 10000, 10));
167			$this->setBlockSetting($block_id, 'infoStyle', Filter::post('infoStyle', 'list|array|table|tagcloud', 'table'));
168			$this->setBlockSetting($block_id, 'block', Filter::postBool('block'));
169		}
170
171		$num       = $this->getBlockSetting($block_id, 'num', '10');
172		$infoStyle = $this->getBlockSetting($block_id, 'infoStyle', 'table');
173		$block     = $this->getBlockSetting($block_id, 'block', '0');
174
175		echo '<tr><td class="descriptionbox wrap width33">';
176		echo I18N::translate('Number of items to show');
177		echo '</td><td class="optionbox">';
178		echo '<input type="text" name="num" size="2" value="', $num, '">';
179		echo '</td></tr>';
180
181		echo '<tr><td class="descriptionbox wrap width33">';
182		echo I18N::translate('Presentation style');
183		echo '</td><td class="optionbox">';
184		echo FunctionsEdit::selectEditControl('infoStyle', array('list' => I18N::translate('bullet list'), 'array' => I18N::translate('compact list'), 'table' => I18N::translate('table'), 'tagcloud' => I18N::translate('tag cloud')), null, $infoStyle, '');
185		echo '</td></tr>';
186
187		echo '<tr><td class="descriptionbox wrap width33">';
188		echo /* I18N: label for a yes/no option */ I18N::translate('Add a scrollbar when block contents grow');
189		echo '</td><td class="optionbox">';
190		echo FunctionsEdit::editFieldYesNo('block', $block);
191		echo '</td></tr>';
192	}
193
194	/**
195	 * Sort (lists of counts of similar) surname by total count.
196	 *
197	 * @param string[] $a
198	 * @param string[] $b
199	 *
200	 * @return int
201	 */
202	private static function surnameCountSort($a, $b) {
203		$counta = 0;
204		foreach ($a as $x) {
205			$counta += count($x);
206		}
207		$countb = 0;
208		foreach ($b as $x) {
209			$countb += count($x);
210		}
211
212		return $countb - $counta;
213	}
214}
215