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