18c2e8227SGreg Roach<?php 28c2e8227SGreg Roach/** 38c2e8227SGreg Roach * webtrees: online genealogy 41062a142SGreg Roach * Copyright (C) 2018 webtrees development team 58c2e8227SGreg Roach * This program is free software: you can redistribute it and/or modify 68c2e8227SGreg Roach * it under the terms of the GNU General Public License as published by 78c2e8227SGreg Roach * the Free Software Foundation, either version 3 of the License, or 88c2e8227SGreg Roach * (at your option) any later version. 98c2e8227SGreg Roach * This program is distributed in the hope that it will be useful, 108c2e8227SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of 118c2e8227SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 128c2e8227SGreg Roach * GNU General Public License for more details. 138c2e8227SGreg Roach * You should have received a copy of the GNU General Public License 148c2e8227SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>. 158c2e8227SGreg Roach */ 1676692c8bSGreg Roachnamespace Fisharebest\Webtrees\Module; 1776692c8bSGreg Roach 180e62c4b8SGreg Roachuse Fisharebest\Webtrees\Auth; 1980536c2dSGreg Roachuse Fisharebest\Webtrees\Database; 203d7a8a4cSGreg Roachuse Fisharebest\Webtrees\Functions\FunctionsPrintLists; 210e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N; 22e490cd80SGreg Roachuse Fisharebest\Webtrees\Tree; 23a45f9889SGreg Roachuse Symfony\Component\HttpFoundation\Request; 248c2e8227SGreg Roach 258c2e8227SGreg Roach/** 268c2e8227SGreg Roach * Class TopSurnamesModule 278c2e8227SGreg Roach */ 28c1010edaSGreg Roachclass TopSurnamesModule extends AbstractModule implements ModuleBlockInterface 29c1010edaSGreg Roach{ 30c385536dSGreg Roach // Default values for new blocks. 31a45f9889SGreg Roach const DEFAULT_NUMBER = '10'; 32c385536dSGreg Roach const DEFAULT_STYLE = 'table'; 33c385536dSGreg Roach 3476692c8bSGreg Roach /** 3576692c8bSGreg Roach * How should this module be labelled on tabs, menus, etc.? 3676692c8bSGreg Roach * 3776692c8bSGreg Roach * @return string 3876692c8bSGreg Roach */ 398f53f488SRico Sonntag public function getTitle(): string 40c1010edaSGreg Roach { 41bbb76c12SGreg Roach /* I18N: Name of a module. Top=Most common */ 42bbb76c12SGreg Roach return I18N::translate('Top surnames'); 438c2e8227SGreg Roach } 448c2e8227SGreg Roach 4576692c8bSGreg Roach /** 4676692c8bSGreg Roach * A sentence describing what this module does. 4776692c8bSGreg Roach * 4876692c8bSGreg Roach * @return string 4976692c8bSGreg Roach */ 508f53f488SRico Sonntag public function getDescription(): string 51c1010edaSGreg Roach { 52bbb76c12SGreg Roach /* I18N: Description of the “Top surnames” module */ 53bbb76c12SGreg Roach return I18N::translate('A list of the most popular surnames.'); 548c2e8227SGreg Roach } 558c2e8227SGreg Roach 5676692c8bSGreg Roach /** 5776692c8bSGreg Roach * Generate the HTML content of this block. 5876692c8bSGreg Roach * 59e490cd80SGreg Roach * @param Tree $tree 6076692c8bSGreg Roach * @param int $block_id 6176692c8bSGreg Roach * @param bool $template 62727f238cSGreg Roach * @param string[] $cfg 6376692c8bSGreg Roach * 6476692c8bSGreg Roach * @return string 6576692c8bSGreg Roach */ 66c1010edaSGreg Roach public function getBlock(Tree $tree, int $block_id, bool $template = true, array $cfg = []): string 67c1010edaSGreg Roach { 68e490cd80SGreg Roach global $ctype; 698c2e8227SGreg Roach 7080536c2dSGreg Roach $num = (int) $this->getBlockSetting($block_id, 'num', self::DEFAULT_NUMBER); 71c385536dSGreg Roach $infoStyle = $this->getBlockSetting($block_id, 'infoStyle', self::DEFAULT_STYLE); 728c2e8227SGreg Roach 73c385536dSGreg Roach extract($cfg, EXTR_OVERWRITE); 748c2e8227SGreg Roach 7580536c2dSGreg Roach // Use the count of base surnames. 7680536c2dSGreg Roach $top_surnames = Database::prepare( 7780536c2dSGreg Roach "SELECT n_surn FROM `##name`" . 7880536c2dSGreg Roach " WHERE n_file = :tree_id AND n_type != '_MARNM' AND n_surn NOT IN ('@N.N.', '')" . 7980536c2dSGreg Roach " GROUP BY n_surn" . 8080536c2dSGreg Roach " ORDER BY COUNT(n_surn) DESC" . 8180536c2dSGreg Roach " LIMIT :limit" 8280536c2dSGreg Roach )->execute([ 8380536c2dSGreg Roach 'tree_id' => $tree->getTreeId(), 8480536c2dSGreg Roach 'limit' => $num, 8580536c2dSGreg Roach ])->fetchOneColumn(); 868c2e8227SGreg Roach 8713abd6f3SGreg Roach $all_surnames = []; 8880536c2dSGreg Roach foreach ($top_surnames as $top_surname) { 8980536c2dSGreg Roach $variants = Database::prepare( 9080536c2dSGreg Roach "SELECT n_surname COLLATE utf8_bin, COUNT(*) FROM `##name` WHERE n_file = :tree_id AND n_surn COLLATE :collate = :surname GROUP BY 1" 9180536c2dSGreg Roach )->execute([ 9280536c2dSGreg Roach 'collate' => I18N::collation(), 9380536c2dSGreg Roach 'surname' => $top_surname, 9480536c2dSGreg Roach 'tree_id' => $tree->getTreeId(), 9580536c2dSGreg Roach ])->fetchAssoc(); 9680536c2dSGreg Roach 9780536c2dSGreg Roach $all_surnames[$top_surname] = $variants; 988c2e8227SGreg Roach } 998c2e8227SGreg Roach 1008c2e8227SGreg Roach switch ($infoStyle) { 1018c2e8227SGreg Roach case 'tagcloud': 10280536c2dSGreg Roach uksort($all_surnames, [I18N::class, 'strcasecmp']); 103e490cd80SGreg Roach $content = FunctionsPrintLists::surnameTagCloud($all_surnames, 'individual-list', true, $tree); 1048c2e8227SGreg Roach break; 1058c2e8227SGreg Roach case 'list': 106*a515be7cSGreg Roach uasort($all_surnames, [$this, 'surnameCountSort']); 107e490cd80SGreg Roach $content = FunctionsPrintLists::surnameList($all_surnames, 1, true, 'individual-list', $tree); 1088c2e8227SGreg Roach break; 1098c2e8227SGreg Roach case 'array': 110*a515be7cSGreg Roach uasort($all_surnames, [$this, 'surnameCountSort']); 111e490cd80SGreg Roach $content = FunctionsPrintLists::surnameList($all_surnames, 2, true, 'individual-list', $tree); 1128c2e8227SGreg Roach break; 1138c2e8227SGreg Roach case 'table': 1148c2e8227SGreg Roach default: 115cf184a4dSGreg Roach $content = view('lists/surnames-table', [ 1166ab54c76SGreg Roach 'surnames' => $all_surnames, 1176ab54c76SGreg Roach 'route' => 'individual-list', 118e490cd80SGreg Roach 'tree' => $tree, 1196ab54c76SGreg Roach ]); 1208c2e8227SGreg Roach break; 1218c2e8227SGreg Roach } 1228c2e8227SGreg Roach 1238c2e8227SGreg Roach if ($template) { 12480536c2dSGreg Roach $num = count($top_surnames); 12580536c2dSGreg Roach if ($num === 1) { 1268cbbfdceSGreg Roach // I18N: i.e. most popular surname. 1278cbbfdceSGreg Roach $title = I18N::translate('Top surname'); 1288cbbfdceSGreg Roach } else { 1298cbbfdceSGreg Roach // I18N: Title for a list of the most common surnames, %s is a number. Note that a separate translation exists when %s is 1 1308cbbfdceSGreg Roach $title = I18N::plural('Top %s surname', 'Top %s surnames', $num, I18N::number($num)); 1318cbbfdceSGreg Roach } 1328cbbfdceSGreg Roach 133e490cd80SGreg Roach if ($ctype === 'gedcom' && Auth::isManager($tree)) { 134c1010edaSGreg Roach $config_url = route('tree-page-block-edit', [ 135c1010edaSGreg Roach 'block_id' => $block_id, 136c1010edaSGreg Roach 'ged' => $tree->getName(), 137c1010edaSGreg Roach ]); 138397e599aSGreg Roach } elseif ($ctype === 'user' && Auth::check()) { 139c1010edaSGreg Roach $config_url = route('user-page-block-edit', [ 140c1010edaSGreg Roach 'block_id' => $block_id, 141c1010edaSGreg Roach 'ged' => $tree->getName(), 142c1010edaSGreg Roach ]); 1438cbbfdceSGreg Roach } else { 1448cbbfdceSGreg Roach $config_url = ''; 1458cbbfdceSGreg Roach } 1468cbbfdceSGreg Roach 147147e99aaSGreg Roach return view('modules/block-template', [ 1489c6524dcSGreg Roach 'block' => str_replace('_', '-', $this->getName()), 1499c6524dcSGreg Roach 'id' => $block_id, 1508cbbfdceSGreg Roach 'config_url' => $config_url, 1518cbbfdceSGreg Roach 'title' => $title, 1529c6524dcSGreg Roach 'content' => $content, 1539c6524dcSGreg Roach ]); 1548c2e8227SGreg Roach } 155b2ce94c6SRico Sonntag 156b2ce94c6SRico Sonntag return $content; 1578c2e8227SGreg Roach } 1588c2e8227SGreg Roach 1598c2e8227SGreg Roach /** {@inheritdoc} */ 160c1010edaSGreg Roach public function loadAjax(): bool 161c1010edaSGreg Roach { 1624ecf4f82SGreg Roach return false; 1638c2e8227SGreg Roach } 1648c2e8227SGreg Roach 1658c2e8227SGreg Roach /** {@inheritdoc} */ 166c1010edaSGreg Roach public function isUserBlock(): bool 167c1010edaSGreg Roach { 1688c2e8227SGreg Roach return true; 1698c2e8227SGreg Roach } 1708c2e8227SGreg Roach 1718c2e8227SGreg Roach /** {@inheritdoc} */ 172c1010edaSGreg Roach public function isGedcomBlock(): bool 173c1010edaSGreg Roach { 1748c2e8227SGreg Roach return true; 1758c2e8227SGreg Roach } 1768c2e8227SGreg Roach 17776692c8bSGreg Roach /** 178a45f9889SGreg Roach * Update the configuration for a block. 179a45f9889SGreg Roach * 180a45f9889SGreg Roach * @param Request $request 181a45f9889SGreg Roach * @param int $block_id 182a45f9889SGreg Roach * 183a45f9889SGreg Roach * @return void 184a45f9889SGreg Roach */ 185a45f9889SGreg Roach public function saveBlockConfiguration(Request $request, int $block_id) 186a45f9889SGreg Roach { 187a45f9889SGreg Roach $this->setBlockSetting($block_id, 'num', $request->get('num', self::DEFAULT_NUMBER)); 188a45f9889SGreg Roach $this->setBlockSetting($block_id, 'infoStyle', $request->get('infoStyle', self::DEFAULT_STYLE)); 189a45f9889SGreg Roach } 190a45f9889SGreg Roach 191a45f9889SGreg Roach /** 19276692c8bSGreg Roach * An HTML form to edit block settings 19376692c8bSGreg Roach * 194e490cd80SGreg Roach * @param Tree $tree 19576692c8bSGreg Roach * @param int $block_id 196a9430be8SGreg Roach * 197a9430be8SGreg Roach * @return void 19876692c8bSGreg Roach */ 199a45f9889SGreg Roach public function editBlockConfiguration(Tree $tree, int $block_id) 200c1010edaSGreg Roach { 201c385536dSGreg Roach $num = $this->getBlockSetting($block_id, 'num', self::DEFAULT_NUMBER); 202c385536dSGreg Roach $infoStyle = $this->getBlockSetting($block_id, 'infoStyle', self::DEFAULT_STYLE); 2038c2e8227SGreg Roach 204c385536dSGreg Roach $info_styles = [ 205bbb76c12SGreg Roach /* I18N: An option in a list-box */ 206bbb76c12SGreg Roach 'list' => I18N::translate('bullet list'), 207bbb76c12SGreg Roach /* I18N: An option in a list-box */ 208bbb76c12SGreg Roach 'array' => I18N::translate('compact list'), 209bbb76c12SGreg Roach /* I18N: An option in a list-box */ 210bbb76c12SGreg Roach 'table' => I18N::translate('table'), 211bbb76c12SGreg Roach /* I18N: An option in a list-box */ 212bbb76c12SGreg Roach 'tagcloud' => I18N::translate('tag cloud'), 213c385536dSGreg Roach ]; 2148c2e8227SGreg Roach 215147e99aaSGreg Roach echo view('modules/top10_surnames/config', [ 216c385536dSGreg Roach 'num' => $num, 217c385536dSGreg Roach 'infoStyle' => $infoStyle, 218c385536dSGreg Roach 'info_styles' => $info_styles, 219c385536dSGreg Roach ]); 2208c2e8227SGreg Roach } 221e15d3b66SRico Sonntag 222e15d3b66SRico Sonntag /** 223e15d3b66SRico Sonntag * Sort (lists of counts of similar) surname by total count. 224e15d3b66SRico Sonntag * 225e15d3b66SRico Sonntag * @param string[] $a 226e15d3b66SRico Sonntag * @param string[] $b 227e15d3b66SRico Sonntag * 228e15d3b66SRico Sonntag * @return int 229e15d3b66SRico Sonntag */ 230e15d3b66SRico Sonntag private function surnameCountSort(array $a, array $b): int 231e15d3b66SRico Sonntag { 232e15d3b66SRico Sonntag return array_sum($b) - array_sum($a); 233e15d3b66SRico Sonntag } 2348c2e8227SGreg Roach} 235