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; 19*80536c2dSGreg Roachuse Fisharebest\Webtrees\Database; 200e62c4b8SGreg Roachuse Fisharebest\Webtrees\Filter; 213d7a8a4cSGreg Roachuse Fisharebest\Webtrees\Functions\FunctionsPrintLists; 220e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N; 23e490cd80SGreg Roachuse Fisharebest\Webtrees\Tree; 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. 31c385536dSGreg 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 */ 39c1010edaSGreg Roach public function getTitle() 40c1010edaSGreg Roach { 41c1010edaSGreg Roach return /* I18N: Name of a module. Top=Most common */ 42c1010edaSGreg Roach 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 */ 50c1010edaSGreg Roach public function getDescription() 51c1010edaSGreg Roach { 52c1010edaSGreg Roach return /* I18N: Description of the “Top surnames” module */ 53c1010edaSGreg Roach 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 70*80536c2dSGreg 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 75*80536c2dSGreg Roach // Use the count of base surnames. 76*80536c2dSGreg Roach $top_surnames = Database::prepare( 77*80536c2dSGreg Roach "SELECT n_surn FROM `##name`" . 78*80536c2dSGreg Roach " WHERE n_file = :tree_id AND n_type != '_MARNM' AND n_surn NOT IN ('@N.N.', '')" . 79*80536c2dSGreg Roach " GROUP BY n_surn" . 80*80536c2dSGreg Roach " ORDER BY COUNT(n_surn) DESC" . 81*80536c2dSGreg Roach " LIMIT :limit" 82*80536c2dSGreg Roach )->execute([ 83*80536c2dSGreg Roach 'tree_id' => $tree->getTreeId(), 84*80536c2dSGreg Roach 'limit' => $num, 85*80536c2dSGreg Roach ])->fetchOneColumn(); 868c2e8227SGreg Roach 8713abd6f3SGreg Roach $all_surnames = []; 88*80536c2dSGreg Roach foreach ($top_surnames as $top_surname) { 89*80536c2dSGreg Roach $variants = Database::prepare( 90*80536c2dSGreg Roach "SELECT n_surname COLLATE utf8_bin, COUNT(*) FROM `##name` WHERE n_file = :tree_id AND n_surn COLLATE :collate = :surname GROUP BY 1" 91*80536c2dSGreg Roach )->execute([ 92*80536c2dSGreg Roach 'collate' => I18N::collation(), 93*80536c2dSGreg Roach 'surname' => $top_surname, 94*80536c2dSGreg Roach 'tree_id' => $tree->getTreeId(), 95*80536c2dSGreg Roach ])->fetchAssoc(); 96*80536c2dSGreg Roach 97*80536c2dSGreg Roach $all_surnames[$top_surname] = $variants; 988c2e8227SGreg Roach } 998c2e8227SGreg Roach 1008c2e8227SGreg Roach switch ($infoStyle) { 1018c2e8227SGreg Roach case 'tagcloud': 102*80536c2dSGreg Roach //uksort($all_surnames, '\Fisharebest\Webtrees\I18N::strcasecmp'); 103*80536c2dSGreg Roach uksort($all_surnames, [I18N::class, 'strcasecmp']); 104e490cd80SGreg Roach $content = FunctionsPrintLists::surnameTagCloud($all_surnames, 'individual-list', true, $tree); 1058c2e8227SGreg Roach break; 1068c2e8227SGreg Roach case 'list': 107e490cd80SGreg Roach $content = FunctionsPrintLists::surnameList($all_surnames, 1, true, 'individual-list', $tree); 1088c2e8227SGreg Roach break; 1098c2e8227SGreg Roach case 'array': 110e490cd80SGreg Roach $content = FunctionsPrintLists::surnameList($all_surnames, 2, true, 'individual-list', $tree); 1118c2e8227SGreg Roach break; 1128c2e8227SGreg Roach case 'table': 1138c2e8227SGreg Roach default: 114cf184a4dSGreg Roach $content = view('lists/surnames-table', [ 1156ab54c76SGreg Roach 'surnames' => $all_surnames, 1166ab54c76SGreg Roach 'route' => 'individual-list', 117e490cd80SGreg Roach 'tree' => $tree, 1186ab54c76SGreg Roach ]); 1198c2e8227SGreg Roach break; 1208c2e8227SGreg Roach } 1218c2e8227SGreg Roach 1228c2e8227SGreg Roach if ($template) { 123*80536c2dSGreg Roach $num = count($top_surnames); 124*80536c2dSGreg Roach if ($num === 1) { 1258cbbfdceSGreg Roach // I18N: i.e. most popular surname. 1268cbbfdceSGreg Roach $title = I18N::translate('Top surname'); 1278cbbfdceSGreg Roach } else { 1288cbbfdceSGreg 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 1298cbbfdceSGreg Roach $title = I18N::plural('Top %s surname', 'Top %s surnames', $num, I18N::number($num)); 1308cbbfdceSGreg Roach } 1318cbbfdceSGreg Roach 132e490cd80SGreg Roach if ($ctype === 'gedcom' && Auth::isManager($tree)) { 133c1010edaSGreg Roach $config_url = route('tree-page-block-edit', [ 134c1010edaSGreg Roach 'block_id' => $block_id, 135c1010edaSGreg Roach 'ged' => $tree->getName(), 136c1010edaSGreg Roach ]); 137397e599aSGreg Roach } elseif ($ctype === 'user' && Auth::check()) { 138c1010edaSGreg Roach $config_url = route('user-page-block-edit', [ 139c1010edaSGreg Roach 'block_id' => $block_id, 140c1010edaSGreg Roach 'ged' => $tree->getName(), 141c1010edaSGreg Roach ]); 1428cbbfdceSGreg Roach } else { 1438cbbfdceSGreg Roach $config_url = ''; 1448cbbfdceSGreg Roach } 1458cbbfdceSGreg Roach 146147e99aaSGreg Roach return view('modules/block-template', [ 1479c6524dcSGreg Roach 'block' => str_replace('_', '-', $this->getName()), 1489c6524dcSGreg Roach 'id' => $block_id, 1498cbbfdceSGreg Roach 'config_url' => $config_url, 1508cbbfdceSGreg Roach 'title' => $title, 1519c6524dcSGreg Roach 'content' => $content, 1529c6524dcSGreg Roach ]); 1538c2e8227SGreg Roach } else { 1548c2e8227SGreg Roach return $content; 1558c2e8227SGreg Roach } 1568c2e8227SGreg Roach } 1578c2e8227SGreg Roach 1588c2e8227SGreg Roach /** {@inheritdoc} */ 159c1010edaSGreg Roach public function loadAjax(): bool 160c1010edaSGreg Roach { 1614ecf4f82SGreg Roach return false; 1628c2e8227SGreg Roach } 1638c2e8227SGreg Roach 1648c2e8227SGreg Roach /** {@inheritdoc} */ 165c1010edaSGreg Roach public function isUserBlock(): bool 166c1010edaSGreg Roach { 1678c2e8227SGreg Roach return true; 1688c2e8227SGreg Roach } 1698c2e8227SGreg Roach 1708c2e8227SGreg Roach /** {@inheritdoc} */ 171c1010edaSGreg Roach public function isGedcomBlock(): bool 172c1010edaSGreg Roach { 1738c2e8227SGreg Roach return true; 1748c2e8227SGreg Roach } 1758c2e8227SGreg Roach 17676692c8bSGreg Roach /** 17776692c8bSGreg Roach * An HTML form to edit block settings 17876692c8bSGreg Roach * 179e490cd80SGreg Roach * @param Tree $tree 18076692c8bSGreg Roach * @param int $block_id 181a9430be8SGreg Roach * 182a9430be8SGreg Roach * @return void 18376692c8bSGreg Roach */ 184c1010edaSGreg Roach public function configureBlock(Tree $tree, int $block_id) 185c1010edaSGreg Roach { 186c385536dSGreg Roach if ($_SERVER['REQUEST_METHOD'] === 'POST') { 187e2a378d3SGreg Roach $this->setBlockSetting($block_id, 'num', Filter::postInteger('num', 1, 10000, 10)); 188c385536dSGreg Roach $this->setBlockSetting($block_id, 'infoStyle', Filter::post('infoStyle', 'list|array|table|tagcloud', self::DEFAULT_STYLE)); 189c385536dSGreg Roach 190c385536dSGreg Roach return; 1918c2e8227SGreg Roach } 1928c2e8227SGreg Roach 193c385536dSGreg Roach $num = $this->getBlockSetting($block_id, 'num', self::DEFAULT_NUMBER); 194c385536dSGreg Roach $infoStyle = $this->getBlockSetting($block_id, 'infoStyle', self::DEFAULT_STYLE); 1958c2e8227SGreg Roach 196c385536dSGreg Roach $info_styles = [ 197c1010edaSGreg Roach 'list' => /* I18N: An option in a list-box */ 198c1010edaSGreg Roach I18N::translate('bullet list'), 199c1010edaSGreg Roach 'array' => /* I18N: An option in a list-box */ 200c1010edaSGreg Roach I18N::translate('compact list'), 201c1010edaSGreg Roach 'table' => /* I18N: An option in a list-box */ 202c1010edaSGreg Roach I18N::translate('table'), 203c1010edaSGreg Roach 'tagcloud' => /* I18N: An option in a list-box */ 204c1010edaSGreg Roach I18N::translate('tag cloud'), 205c385536dSGreg Roach ]; 2068c2e8227SGreg Roach 207147e99aaSGreg Roach echo view('modules/top10_surnames/config', [ 208c385536dSGreg Roach 'num' => $num, 209c385536dSGreg Roach 'infoStyle' => $infoStyle, 210c385536dSGreg Roach 'info_styles' => $info_styles, 211c385536dSGreg Roach ]); 2128c2e8227SGreg Roach } 2138c2e8227SGreg Roach} 214