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