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