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