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