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