18c2e8227SGreg Roach<?php 23976b470SGreg Roach 38c2e8227SGreg Roach/** 48c2e8227SGreg Roach * webtrees: online genealogy 58fcd0d32SGreg Roach * Copyright (C) 2019 webtrees development team 68c2e8227SGreg Roach * This program is free software: you can redistribute it and/or modify 78c2e8227SGreg Roach * it under the terms of the GNU General Public License as published by 88c2e8227SGreg Roach * the Free Software Foundation, either version 3 of the License, or 98c2e8227SGreg Roach * (at your option) any later version. 108c2e8227SGreg Roach * This program is distributed in the hope that it will be useful, 118c2e8227SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of 128c2e8227SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 138c2e8227SGreg Roach * GNU General Public License for more details. 148c2e8227SGreg Roach * You should have received a copy of the GNU General Public License 158c2e8227SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>. 168c2e8227SGreg Roach */ 17fcfa147eSGreg Roach 18e7f56f2aSGreg Roachdeclare(strict_types=1); 19e7f56f2aSGreg Roach 2076692c8bSGreg Roachnamespace Fisharebest\Webtrees\Module; 2176692c8bSGreg Roach 220e62c4b8SGreg Roachuse Fisharebest\Webtrees\Auth; 233d7a8a4cSGreg Roachuse Fisharebest\Webtrees\Functions\FunctionsPrintLists; 240e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N; 25e490cd80SGreg Roachuse Fisharebest\Webtrees\Tree; 2667992b6aSRichard Cisseeuse Fisharebest\Webtrees\Services\ModuleService; 278d7ed87eSGreg Roachuse Illuminate\Database\Capsule\Manager as DB; 28a69f5655SGreg Roachuse Illuminate\Database\Query\Expression; 291e7a7a28SGreg Roachuse Illuminate\Support\Str; 306ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface; 318c2e8227SGreg Roach 328c2e8227SGreg Roach/** 338c2e8227SGreg Roach * Class TopSurnamesModule 348c2e8227SGreg Roach */ 3537eb8894SGreg Roachclass TopSurnamesModule extends AbstractModule implements ModuleBlockInterface 36c1010edaSGreg Roach{ 3749a243cbSGreg Roach use ModuleBlockTrait; 3849a243cbSGreg Roach 39c385536dSGreg Roach // Default values for new blocks. 4016d6367aSGreg Roach private const DEFAULT_NUMBER = '10'; 4116d6367aSGreg Roach private const DEFAULT_STYLE = 'table'; 42c385536dSGreg Roach 432e4f3c66SGreg Roach /** @var ModuleService */ 442e4f3c66SGreg Roach private $module_service; 452e4f3c66SGreg Roach 462e4f3c66SGreg Roach /** 472e4f3c66SGreg Roach * TopSurnamesModule constructor. 482e4f3c66SGreg Roach * 492e4f3c66SGreg Roach * @param ModuleService $module_service 502e4f3c66SGreg Roach */ 512e4f3c66SGreg Roach public function __construct(ModuleService $module_service) 522e4f3c66SGreg Roach { 532e4f3c66SGreg Roach $this->module_service = $module_service; 542e4f3c66SGreg Roach } 552e4f3c66SGreg Roach 5676692c8bSGreg Roach /** 570cfd6963SGreg Roach * How should this module be identified in the control panel, etc.? 5876692c8bSGreg Roach * 5976692c8bSGreg Roach * @return string 6076692c8bSGreg Roach */ 6149a243cbSGreg Roach public function title(): string 62c1010edaSGreg Roach { 63bbb76c12SGreg Roach /* I18N: Name of a module. Top=Most common */ 64bbb76c12SGreg Roach return I18N::translate('Top surnames'); 658c2e8227SGreg Roach } 668c2e8227SGreg Roach 6776692c8bSGreg Roach /** 6876692c8bSGreg Roach * A sentence describing what this module does. 6976692c8bSGreg Roach * 7076692c8bSGreg Roach * @return string 7176692c8bSGreg Roach */ 7249a243cbSGreg Roach public function description(): string 73c1010edaSGreg Roach { 74bbb76c12SGreg Roach /* I18N: Description of the “Top surnames” module */ 75bbb76c12SGreg Roach return I18N::translate('A list of the most popular surnames.'); 768c2e8227SGreg Roach } 778c2e8227SGreg Roach 7876692c8bSGreg Roach /** 7976692c8bSGreg Roach * Generate the HTML content of this block. 8076692c8bSGreg Roach * 81e490cd80SGreg Roach * @param Tree $tree 8276692c8bSGreg Roach * @param int $block_id 833caaa4d2SGreg Roach * @param string $context 843caaa4d2SGreg Roach * @param string[] $config 8576692c8bSGreg Roach * 8676692c8bSGreg Roach * @return string 8776692c8bSGreg Roach */ 883caaa4d2SGreg Roach public function getBlock(Tree $tree, int $block_id, string $context, array $config = []): string 89c1010edaSGreg Roach { 9080536c2dSGreg Roach $num = (int) $this->getBlockSetting($block_id, 'num', self::DEFAULT_NUMBER); 91c385536dSGreg Roach $infoStyle = $this->getBlockSetting($block_id, 'infoStyle', self::DEFAULT_STYLE); 928c2e8227SGreg Roach 933caaa4d2SGreg Roach extract($config, EXTR_OVERWRITE); 948c2e8227SGreg Roach 9580536c2dSGreg Roach // Use the count of base surnames. 968d7ed87eSGreg Roach $top_surnames = DB::table('name') 978d7ed87eSGreg Roach ->where('n_file', '=', $tree->id()) 988d7ed87eSGreg Roach ->where('n_type', '<>', '_MARNM') 998d7ed87eSGreg Roach ->whereNotIn('n_surn', ['@N.N.', '']) 1007f5c2944SGreg Roach ->groupBy(['n_surn']) 101a69f5655SGreg Roach ->orderByDesc(new Expression('COUNT(n_surn)')) 1028d7ed87eSGreg Roach ->take($num) 1038d7ed87eSGreg Roach ->pluck('n_surn'); 1048c2e8227SGreg Roach 10513abd6f3SGreg Roach $all_surnames = []; 10680536c2dSGreg Roach 1078d7ed87eSGreg Roach foreach ($top_surnames as $top_surname) { 1088d7ed87eSGreg Roach $variants = DB::table('name') 1098d7ed87eSGreg Roach ->where('n_file', '=', $tree->id()) 110a69f5655SGreg Roach ->where(new Expression('n_surn /*! COLLATE utf8_bin */'), '=', $top_surname) 1117f5c2944SGreg Roach ->groupBy(['surname']) 112a69f5655SGreg Roach ->select([new Expression('n_surname /*! COLLATE utf8_bin */ AS surname'), new Expression('count(*) AS total')]) 1138d7ed87eSGreg Roach ->pluck('total', 'surname') 1148d7ed87eSGreg Roach ->all(); 115f8c9edfeSGreg Roach 11680536c2dSGreg Roach $all_surnames[$top_surname] = $variants; 1178c2e8227SGreg Roach } 1188c2e8227SGreg Roach 1192e4f3c66SGreg Roach // Find a module providing individual lists. 1202e4f3c66SGreg Roach $module = $this->module_service 1212e4f3c66SGreg Roach ->findByComponent(ModuleListInterface::class, $tree, Auth::user()) 1222e4f3c66SGreg Roach ->first(static function (ModuleInterface $module): bool { 12367992b6aSRichard Cissee return $module instanceof IndividualListModule; 12467992b6aSRichard Cissee }); 12567992b6aSRichard Cissee 1268c2e8227SGreg Roach switch ($infoStyle) { 1278c2e8227SGreg Roach case 'tagcloud': 12880536c2dSGreg Roach uksort($all_surnames, [I18N::class, 'strcasecmp']); 12967992b6aSRichard Cissee $content = FunctionsPrintLists::surnameTagCloud($all_surnames, $module, true, $tree); 1308c2e8227SGreg Roach break; 1318c2e8227SGreg Roach case 'list': 132a515be7cSGreg Roach uasort($all_surnames, [$this, 'surnameCountSort']); 13367992b6aSRichard Cissee $content = FunctionsPrintLists::surnameList($all_surnames, 1, true, $module, $tree); 1348c2e8227SGreg Roach break; 1358c2e8227SGreg Roach case 'array': 136a515be7cSGreg Roach uasort($all_surnames, [$this, 'surnameCountSort']); 13767992b6aSRichard Cissee $content = FunctionsPrintLists::surnameList($all_surnames, 2, true, $module, $tree); 1388c2e8227SGreg Roach break; 1398c2e8227SGreg Roach case 'table': 1408c2e8227SGreg Roach default: 141cf184a4dSGreg Roach $content = view('lists/surnames-table', [ 1426ab54c76SGreg Roach 'surnames' => $all_surnames, 14367992b6aSRichard Cissee 'module' => $module, 14467992b6aSRichard Cissee 'families' => false, 145e490cd80SGreg Roach 'tree' => $tree, 1466ab54c76SGreg Roach ]); 1478c2e8227SGreg Roach break; 1488c2e8227SGreg Roach } 1498c2e8227SGreg Roach 1503caaa4d2SGreg Roach if ($context !== self::CONTEXT_EMBED) { 15180536c2dSGreg Roach $num = count($top_surnames); 15280536c2dSGreg Roach if ($num === 1) { 1538cbbfdceSGreg Roach // I18N: i.e. most popular surname. 1548cbbfdceSGreg Roach $title = I18N::translate('Top surname'); 1558cbbfdceSGreg Roach } else { 1568cbbfdceSGreg 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 1578cbbfdceSGreg Roach $title = I18N::plural('Top %s surname', 'Top %s surnames', $num, I18N::number($num)); 1588cbbfdceSGreg Roach } 1598cbbfdceSGreg Roach 160147e99aaSGreg Roach return view('modules/block-template', [ 1611e7a7a28SGreg Roach 'block' => Str::kebab($this->name()), 1629c6524dcSGreg Roach 'id' => $block_id, 1633caaa4d2SGreg Roach 'config_url' => $this->configUrl($tree, $context, $block_id), 1648cbbfdceSGreg Roach 'title' => $title, 1659c6524dcSGreg Roach 'content' => $content, 1669c6524dcSGreg Roach ]); 1678c2e8227SGreg Roach } 168b2ce94c6SRico Sonntag 169b2ce94c6SRico Sonntag return $content; 1708c2e8227SGreg Roach } 1718c2e8227SGreg Roach 1723caaa4d2SGreg Roach /** 1733caaa4d2SGreg Roach * Should this block load asynchronously using AJAX? 1743caaa4d2SGreg Roach * 1753caaa4d2SGreg Roach * Simple blocks are faster in-line, more complex ones can be loaded later. 1763caaa4d2SGreg Roach * 1773caaa4d2SGreg Roach * @return bool 1783caaa4d2SGreg Roach */ 179c1010edaSGreg Roach public function loadAjax(): bool 180c1010edaSGreg Roach { 1814ecf4f82SGreg Roach return false; 1828c2e8227SGreg Roach } 1838c2e8227SGreg Roach 1843caaa4d2SGreg Roach /** 1853caaa4d2SGreg Roach * Can this block be shown on the user’s home page? 1863caaa4d2SGreg Roach * 1873caaa4d2SGreg Roach * @return bool 1883caaa4d2SGreg Roach */ 189c1010edaSGreg Roach public function isUserBlock(): bool 190c1010edaSGreg Roach { 1918c2e8227SGreg Roach return true; 1928c2e8227SGreg Roach } 1938c2e8227SGreg Roach 1943caaa4d2SGreg Roach /** 1953caaa4d2SGreg Roach * Can this block be shown on the tree’s home page? 1963caaa4d2SGreg Roach * 1973caaa4d2SGreg Roach * @return bool 1983caaa4d2SGreg Roach */ 19963276d8fSGreg Roach public function isTreeBlock(): bool 200c1010edaSGreg Roach { 2018c2e8227SGreg Roach return true; 2028c2e8227SGreg Roach } 2038c2e8227SGreg Roach 20476692c8bSGreg Roach /** 205a45f9889SGreg Roach * Update the configuration for a block. 206a45f9889SGreg Roach * 2076ccdf4f0SGreg Roach * @param ServerRequestInterface $request 208a45f9889SGreg Roach * @param int $block_id 209a45f9889SGreg Roach * 210a45f9889SGreg Roach * @return void 211a45f9889SGreg Roach */ 2126ccdf4f0SGreg Roach public function saveBlockConfiguration(ServerRequestInterface $request, int $block_id): void 213a45f9889SGreg Roach { 214*b46c87bdSGreg Roach $params = (array) $request->getParsedBody(); 215c50a90beSGreg Roach 216c50a90beSGreg Roach $this->setBlockSetting($block_id, 'num', $params['num']); 217c50a90beSGreg Roach $this->setBlockSetting($block_id, 'infoStyle', $params['infoStyle']); 218a45f9889SGreg Roach } 219a45f9889SGreg Roach 220a45f9889SGreg Roach /** 22176692c8bSGreg Roach * An HTML form to edit block settings 22276692c8bSGreg Roach * 223e490cd80SGreg Roach * @param Tree $tree 22476692c8bSGreg Roach * @param int $block_id 225a9430be8SGreg Roach * 2263caaa4d2SGreg Roach * @return string 22776692c8bSGreg Roach */ 2283caaa4d2SGreg Roach public function editBlockConfiguration(Tree $tree, int $block_id): string 229c1010edaSGreg Roach { 230c385536dSGreg Roach $num = $this->getBlockSetting($block_id, 'num', self::DEFAULT_NUMBER); 231c385536dSGreg Roach $infoStyle = $this->getBlockSetting($block_id, 'infoStyle', self::DEFAULT_STYLE); 2328c2e8227SGreg Roach 233c385536dSGreg Roach $info_styles = [ 234bbb76c12SGreg Roach /* I18N: An option in a list-box */ 235bbb76c12SGreg Roach 'list' => I18N::translate('bullet list'), 236bbb76c12SGreg Roach /* I18N: An option in a list-box */ 237bbb76c12SGreg Roach 'array' => I18N::translate('compact list'), 238bbb76c12SGreg Roach /* I18N: An option in a list-box */ 239bbb76c12SGreg Roach 'table' => I18N::translate('table'), 240bbb76c12SGreg Roach /* I18N: An option in a list-box */ 241bbb76c12SGreg Roach 'tagcloud' => I18N::translate('tag cloud'), 242c385536dSGreg Roach ]; 2438c2e8227SGreg Roach 2443caaa4d2SGreg Roach return view('modules/top10_surnames/config', [ 245c385536dSGreg Roach 'num' => $num, 246c385536dSGreg Roach 'infoStyle' => $infoStyle, 247c385536dSGreg Roach 'info_styles' => $info_styles, 248c385536dSGreg Roach ]); 2498c2e8227SGreg Roach } 250e15d3b66SRico Sonntag 251e15d3b66SRico Sonntag /** 252e15d3b66SRico Sonntag * Sort (lists of counts of similar) surname by total count. 253e15d3b66SRico Sonntag * 254e15d3b66SRico Sonntag * @param string[] $a 255e15d3b66SRico Sonntag * @param string[] $b 256e15d3b66SRico Sonntag * 257e15d3b66SRico Sonntag * @return int 258e15d3b66SRico Sonntag */ 259e15d3b66SRico Sonntag private function surnameCountSort(array $a, array $b): int 260e15d3b66SRico Sonntag { 261e15d3b66SRico Sonntag return array_sum($b) - array_sum($a); 262e15d3b66SRico Sonntag } 2638c2e8227SGreg Roach} 264