xref: /webtrees/app/Module/TopSurnamesModule.php (revision 571e6fcac743cc9b048486db27eb10ae509eb172)
1<?php
2/**
3 * webtrees: online genealogy
4 * Copyright (C) 2018 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\FunctionsPrintLists;
22use Fisharebest\Webtrees\I18N;
23use Fisharebest\Webtrees\Query\QueryName;
24
25/**
26 * Class TopSurnamesModule
27 */
28class TopSurnamesModule extends AbstractModule implements ModuleBlockInterface {
29	// Default values for new blocks.
30	const DEFAULT_NUMBER = 10;
31	const DEFAULT_STYLE  = 'table';
32
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 = []): string {
61		global $WT_TREE, $ctype;
62
63		$num       = $this->getBlockSetting($block_id, 'num', self::DEFAULT_NUMBER);
64		$infoStyle = $this->getBlockSetting($block_id, 'infoStyle', self::DEFAULT_STYLE);
65
66		extract($cfg, EXTR_OVERWRITE);
67
68		// This next function is a bit out of date, and doesn't cope well with surname variants
69		$top_surnames = FunctionsDb::getTopSurnames($WT_TREE->getTreeId(), 0, $num);
70
71		$all_surnames = [];
72		$i            = 0;
73		foreach (array_keys($top_surnames) as $top_surname) {
74			$all_surnames = array_merge($all_surnames, QueryName::surnames($WT_TREE, $top_surname, '', false, false));
75			if (++$i == $num) {
76				break;
77			}
78		}
79		if ($i < $num) {
80			$num = $i;
81		}
82
83		switch ($infoStyle) {
84			case 'tagcloud':
85				uksort($all_surnames, '\Fisharebest\Webtrees\I18N::strcasecmp');
86				$content = FunctionsPrintLists::surnameTagCloud($all_surnames, 'indilist.php', true, $WT_TREE);
87				break;
88			case 'list':
89				uasort($all_surnames, '\Fisharebest\Webtrees\Module\TopSurnamesModule::surnameCountSort');
90				$content = FunctionsPrintLists::surnameList($all_surnames, 1, true, 'indilist.php', $WT_TREE);
91				break;
92			case 'array':
93				uasort($all_surnames, '\Fisharebest\Webtrees\Module\TopSurnamesModule::surnameCountSort');
94				$content = FunctionsPrintLists::surnameList($all_surnames, 2, true, 'indilist.php', $WT_TREE);
95				break;
96			case 'table':
97			default:
98				uasort($all_surnames, '\Fisharebest\Webtrees\Module\TopSurnamesModule::surnameCountSort');
99				$content = FunctionsPrintLists::surnameTable($all_surnames, 'indilist.php', $WT_TREE);
100				break;
101		}
102
103		if ($template) {
104			if ($num == 1) {
105				// I18N: i.e. most popular surname.
106				$title = I18N::translate('Top surname');
107			} else {
108				// I18N: Title for a list of the most common surnames, %s is a number. Note that a separate translation exists when %s is 1
109				$title = I18N::plural('Top %s surname', 'Top %s surnames', $num, I18N::number($num));
110			}
111
112			if ($ctype === 'gedcom' && Auth::isManager($WT_TREE)) {
113				$config_url = route('tree-page-block-edit', ['block_id' => $block_id, 'ged' => $WT_TREE->getName()]);
114			} elseif ($ctype === 'user' && Auth::check()) {
115				$config_url = route('user-page-block-edit', ['block_id' => $block_id, 'ged' => $WT_TREE->getName()]);
116			} else {
117				$config_url = '';
118			}
119
120			return view('blocks/template', [
121				'block'      => str_replace('_', '-', $this->getName()),
122				'id'         => $block_id,
123				'config_url' => $config_url,
124				'title'      => $title,
125				'content'    => $content,
126			]);
127		} else {
128			return $content;
129		}
130	}
131
132	/** {@inheritdoc} */
133	public function loadAjax(): bool {
134		return false;
135	}
136
137	/** {@inheritdoc} */
138	public function isUserBlock(): bool {
139		return true;
140	}
141
142	/** {@inheritdoc} */
143	public function isGedcomBlock(): bool {
144		return true;
145	}
146
147	/**
148	 * An HTML form to edit block settings
149	 *
150	 * @param int $block_id
151	 *
152	 * @return void
153	 */
154	public function configureBlock($block_id) {
155		if ($_SERVER['REQUEST_METHOD'] === 'POST') {
156			$this->setBlockSetting($block_id, 'num', Filter::postInteger('num', 1, 10000, 10));
157			$this->setBlockSetting($block_id, 'infoStyle', Filter::post('infoStyle', 'list|array|table|tagcloud', self::DEFAULT_STYLE));
158
159			return;
160		}
161
162		$num       = $this->getBlockSetting($block_id, 'num', self::DEFAULT_NUMBER);
163		$infoStyle = $this->getBlockSetting($block_id, 'infoStyle', self::DEFAULT_STYLE);
164
165		$info_styles = [
166			'list'     => /* I18N: An option in a list-box */ I18N::translate('bullet list'),
167			'array'    => /* I18N: An option in a list-box */ I18N::translate('compact list'),
168			'table'    => /* I18N: An option in a list-box */ I18N::translate('table'),
169			'tagcloud' => /* I18N: An option in a list-box */ I18N::translate('tag cloud'),
170		];
171
172		echo view('blocks/top-surnames-config', [
173			'num'         => $num,
174			'infoStyle'   => $infoStyle,
175			'info_styles' => $info_styles,
176		]);
177	}
178
179	/**
180	 * Sort (lists of counts of similar) surname by total count.
181	 *
182	 * @param string[][] $a
183	 * @param string[][] $b
184	 *
185	 * @return int
186	 */
187	private static function surnameCountSort($a, $b) {
188		$counta = 0;
189		foreach ($a as $x) {
190			$counta += count($x);
191		}
192		$countb = 0;
193		foreach ($b as $x) {
194			$countb += count($x);
195		}
196
197		return $countb - $counta;
198	}
199}
200