xref: /webtrees/app/Module/TopSurnamesModule.php (revision 66fb307e54a5f13572b9dfa75f639b2c827bafea)
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\Functions\FunctionsDb;
22use Fisharebest\Webtrees\Functions\FunctionsPrintLists;
23use Fisharebest\Webtrees\Html;
24use Fisharebest\Webtrees\I18N;
25use Fisharebest\Webtrees\Query\QueryName;
26use Fisharebest\Webtrees\View;
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 = []): string {
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
86		switch ($infoStyle) {
87			case 'tagcloud':
88				uksort($all_surnames, '\Fisharebest\Webtrees\I18N::strcasecmp');
89				$content = FunctionsPrintLists::surnameTagCloud($all_surnames, 'indilist.php', true, $WT_TREE);
90				break;
91			case 'list':
92				uasort($all_surnames, '\Fisharebest\Webtrees\Module\TopSurnamesModule::surnameCountSort');
93				$content = FunctionsPrintLists::surnameList($all_surnames, 1, true, 'indilist.php', $WT_TREE);
94				break;
95			case 'array':
96				uasort($all_surnames, '\Fisharebest\Webtrees\Module\TopSurnamesModule::surnameCountSort');
97				$content = FunctionsPrintLists::surnameList($all_surnames, 2, true, 'indilist.php', $WT_TREE);
98				break;
99			case 'table':
100			default:
101				uasort($all_surnames, '\Fisharebest\Webtrees\Module\TopSurnamesModule::surnameCountSort');
102				$content = FunctionsPrintLists::surnameTable($all_surnames, 'indilist.php', $WT_TREE);
103				break;
104		}
105
106		if ($template) {
107			if ($num == 1) {
108				// I18N: i.e. most popular surname.
109				$title = I18N::translate('Top surname');
110			} else {
111				// I18N: Title for a list of the most common surnames, %s is a number. Note that a separate translation exists when %s is 1
112				$title = I18N::plural('Top %s surname', 'Top %s surnames', $num, I18N::number($num));
113			}
114
115			if ($ctype === 'gedcom' && Auth::isManager($WT_TREE) || $ctype === 'user' && Auth::check()) {
116				$config_url = Html::url('block_edit.php', ['block_id' => $block_id, 'ged' => $WT_TREE->getName()]);
117			} else {
118				$config_url = '';
119			}
120
121			return View::make('blocks/template', [
122				'block'      => str_replace('_', '-', $this->getName()),
123				'id'         => $block_id,
124				'config_url' => $config_url,
125				'title'      => $title,
126				'content'    => $content,
127			]);
128		} else {
129			return $content;
130		}
131	}
132
133	/** {@inheritdoc} */
134	public function loadAjax(): bool {
135		return false;
136	}
137
138	/** {@inheritdoc} */
139	public function isUserBlock(): bool {
140		return true;
141	}
142
143	/** {@inheritdoc} */
144	public function isGedcomBlock(): bool {
145		return true;
146	}
147
148	/**
149	 * An HTML form to edit block settings
150	 *
151	 * @param int $block_id
152	 *
153	 * @return void
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