18c2e8227SGreg Roach<?php 23976b470SGreg Roach 38c2e8227SGreg Roach/** 48c2e8227SGreg Roach * webtrees: online genealogy 589f7189bSGreg Roach * Copyright (C) 2021 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 1589f7189bSGreg Roach * along with this program. If not, see <https://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; 258fb4e87cSGreg Roachuse Fisharebest\Webtrees\Individual; 26e490cd80SGreg Roachuse Fisharebest\Webtrees\Tree; 2767992b6aSRichard Cisseeuse Fisharebest\Webtrees\Services\ModuleService; 288d7ed87eSGreg Roachuse Illuminate\Database\Capsule\Manager as DB; 29a69f5655SGreg Roachuse Illuminate\Database\Query\Expression; 301e7a7a28SGreg Roachuse Illuminate\Support\Str; 316ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface; 328c2e8227SGreg Roach 338c2e8227SGreg Roach/** 348c2e8227SGreg Roach * Class TopSurnamesModule 358c2e8227SGreg Roach */ 3637eb8894SGreg Roachclass TopSurnamesModule extends AbstractModule implements ModuleBlockInterface 37c1010edaSGreg Roach{ 3849a243cbSGreg Roach use ModuleBlockTrait; 3949a243cbSGreg Roach 40c385536dSGreg Roach // Default values for new blocks. 4116d6367aSGreg Roach private const DEFAULT_NUMBER = '10'; 4216d6367aSGreg Roach private const DEFAULT_STYLE = 'table'; 43c385536dSGreg Roach 442e4f3c66SGreg Roach /** @var ModuleService */ 452e4f3c66SGreg Roach private $module_service; 462e4f3c66SGreg Roach 472e4f3c66SGreg Roach /** 482e4f3c66SGreg Roach * TopSurnamesModule constructor. 492e4f3c66SGreg Roach * 502e4f3c66SGreg Roach * @param ModuleService $module_service 512e4f3c66SGreg Roach */ 522e4f3c66SGreg Roach public function __construct(ModuleService $module_service) 532e4f3c66SGreg Roach { 542e4f3c66SGreg Roach $this->module_service = $module_service; 552e4f3c66SGreg Roach } 562e4f3c66SGreg Roach 5776692c8bSGreg Roach /** 580cfd6963SGreg Roach * How should this module be identified in the control panel, etc.? 5976692c8bSGreg Roach * 6076692c8bSGreg Roach * @return string 6176692c8bSGreg Roach */ 6249a243cbSGreg Roach public function title(): string 63c1010edaSGreg Roach { 64bbb76c12SGreg Roach /* I18N: Name of a module. Top=Most common */ 65bbb76c12SGreg Roach return I18N::translate('Top surnames'); 668c2e8227SGreg Roach } 678c2e8227SGreg Roach 6876692c8bSGreg Roach /** 6976692c8bSGreg Roach * A sentence describing what this module does. 7076692c8bSGreg Roach * 7176692c8bSGreg Roach * @return string 7276692c8bSGreg Roach */ 7349a243cbSGreg Roach public function description(): string 74c1010edaSGreg Roach { 75bbb76c12SGreg Roach /* I18N: Description of the “Top surnames” module */ 76bbb76c12SGreg Roach return I18N::translate('A list of the most popular surnames.'); 778c2e8227SGreg Roach } 788c2e8227SGreg Roach 7976692c8bSGreg Roach /** 8076692c8bSGreg Roach * Generate the HTML content of this block. 8176692c8bSGreg Roach * 82e490cd80SGreg Roach * @param Tree $tree 8376692c8bSGreg Roach * @param int $block_id 843caaa4d2SGreg Roach * @param string $context 853caaa4d2SGreg Roach * @param string[] $config 8676692c8bSGreg Roach * 8776692c8bSGreg Roach * @return string 8876692c8bSGreg Roach */ 893caaa4d2SGreg Roach public function getBlock(Tree $tree, int $block_id, string $context, array $config = []): string 90c1010edaSGreg Roach { 9180536c2dSGreg Roach $num = (int) $this->getBlockSetting($block_id, 'num', self::DEFAULT_NUMBER); 92c385536dSGreg Roach $infoStyle = $this->getBlockSetting($block_id, 'infoStyle', self::DEFAULT_STYLE); 938c2e8227SGreg Roach 943caaa4d2SGreg Roach extract($config, EXTR_OVERWRITE); 958c2e8227SGreg Roach 9680536c2dSGreg Roach // Use the count of base surnames. 978d7ed87eSGreg Roach $top_surnames = DB::table('name') 988d7ed87eSGreg Roach ->where('n_file', '=', $tree->id()) 998d7ed87eSGreg Roach ->where('n_type', '<>', '_MARNM') 1008fb4e87cSGreg Roach ->whereNotIn('n_surn', [Individual::NOMEN_NESCIO, '']) 1017f5c2944SGreg Roach ->groupBy(['n_surn']) 102a69f5655SGreg Roach ->orderByDesc(new Expression('COUNT(n_surn)')) 1038d7ed87eSGreg Roach ->take($num) 1048d7ed87eSGreg Roach ->pluck('n_surn'); 1058c2e8227SGreg Roach 10613abd6f3SGreg Roach $all_surnames = []; 10780536c2dSGreg Roach 1088d7ed87eSGreg Roach foreach ($top_surnames as $top_surname) { 1098d7ed87eSGreg Roach $variants = DB::table('name') 1108d7ed87eSGreg Roach ->where('n_file', '=', $tree->id()) 111a69f5655SGreg Roach ->where(new Expression('n_surn /*! COLLATE utf8_bin */'), '=', $top_surname) 1127f5c2944SGreg Roach ->groupBy(['surname']) 113a69f5655SGreg Roach ->select([new Expression('n_surname /*! COLLATE utf8_bin */ AS surname'), new Expression('count(*) AS total')]) 1148d7ed87eSGreg Roach ->pluck('total', 'surname') 1157dc4edbcSGreg Roach ->map(static function ($n): int { 1167dc4edbcSGreg Roach // Some database drivers return numeric columns strings. 1177dc4edbcSGreg Roach return (int) $n; 1187dc4edbcSGreg Roach }) 1198d7ed87eSGreg Roach ->all(); 120f8c9edfeSGreg Roach 12180536c2dSGreg Roach $all_surnames[$top_surname] = $variants; 1228c2e8227SGreg Roach } 1238c2e8227SGreg Roach 1242e4f3c66SGreg Roach // Find a module providing individual lists. 1252e4f3c66SGreg Roach $module = $this->module_service 1262e4f3c66SGreg Roach ->findByComponent(ModuleListInterface::class, $tree, Auth::user()) 1272e4f3c66SGreg Roach ->first(static function (ModuleInterface $module): bool { 128ba738272SGreg Roach // The family list extends the individual list 129ba738272SGreg Roach return 130ba738272SGreg Roach $module instanceof IndividualListModule && 131ba738272SGreg Roach !$module instanceof FamilyListModule; 13267992b6aSRichard Cissee }); 13367992b6aSRichard Cissee 1348c2e8227SGreg Roach switch ($infoStyle) { 1358c2e8227SGreg Roach case 'tagcloud': 136*37646143SGreg Roach uksort($all_surnames, I18N::comparator()); 13767992b6aSRichard Cissee $content = FunctionsPrintLists::surnameTagCloud($all_surnames, $module, true, $tree); 1388c2e8227SGreg Roach break; 1398c2e8227SGreg Roach case 'list': 140a515be7cSGreg Roach uasort($all_surnames, [$this, 'surnameCountSort']); 14167992b6aSRichard Cissee $content = FunctionsPrintLists::surnameList($all_surnames, 1, true, $module, $tree); 1428c2e8227SGreg Roach break; 1438c2e8227SGreg Roach case 'array': 144a515be7cSGreg Roach uasort($all_surnames, [$this, 'surnameCountSort']); 14567992b6aSRichard Cissee $content = FunctionsPrintLists::surnameList($all_surnames, 2, true, $module, $tree); 1468c2e8227SGreg Roach break; 1478c2e8227SGreg Roach case 'table': 1488c2e8227SGreg Roach default: 149cf184a4dSGreg Roach $content = view('lists/surnames-table', [ 1506ab54c76SGreg Roach 'surnames' => $all_surnames, 15167992b6aSRichard Cissee 'module' => $module, 15267992b6aSRichard Cissee 'families' => false, 153e490cd80SGreg Roach 'tree' => $tree, 1546ab54c76SGreg Roach ]); 1558c2e8227SGreg Roach break; 1568c2e8227SGreg Roach } 1578c2e8227SGreg Roach 1583caaa4d2SGreg Roach if ($context !== self::CONTEXT_EMBED) { 15980536c2dSGreg Roach $num = count($top_surnames); 16080536c2dSGreg Roach if ($num === 1) { 1618cbbfdceSGreg Roach // I18N: i.e. most popular surname. 1628cbbfdceSGreg Roach $title = I18N::translate('Top surname'); 1638cbbfdceSGreg Roach } else { 1648cbbfdceSGreg 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 1658cbbfdceSGreg Roach $title = I18N::plural('Top %s surname', 'Top %s surnames', $num, I18N::number($num)); 1668cbbfdceSGreg Roach } 1678cbbfdceSGreg Roach 168147e99aaSGreg Roach return view('modules/block-template', [ 1691e7a7a28SGreg Roach 'block' => Str::kebab($this->name()), 1709c6524dcSGreg Roach 'id' => $block_id, 1713caaa4d2SGreg Roach 'config_url' => $this->configUrl($tree, $context, $block_id), 1728cbbfdceSGreg Roach 'title' => $title, 1739c6524dcSGreg Roach 'content' => $content, 1749c6524dcSGreg Roach ]); 1758c2e8227SGreg Roach } 176b2ce94c6SRico Sonntag 177b2ce94c6SRico Sonntag return $content; 1788c2e8227SGreg Roach } 1798c2e8227SGreg Roach 1803caaa4d2SGreg Roach /** 1813caaa4d2SGreg Roach * Should this block load asynchronously using AJAX? 1823caaa4d2SGreg Roach * 1833caaa4d2SGreg Roach * Simple blocks are faster in-line, more complex ones can be loaded later. 1843caaa4d2SGreg Roach * 1853caaa4d2SGreg Roach * @return bool 1863caaa4d2SGreg Roach */ 187c1010edaSGreg Roach public function loadAjax(): bool 188c1010edaSGreg Roach { 1894ecf4f82SGreg Roach return false; 1908c2e8227SGreg Roach } 1918c2e8227SGreg Roach 1923caaa4d2SGreg Roach /** 1933caaa4d2SGreg Roach * Can this block be shown on the user’s home page? 1943caaa4d2SGreg Roach * 1953caaa4d2SGreg Roach * @return bool 1963caaa4d2SGreg Roach */ 197c1010edaSGreg Roach public function isUserBlock(): bool 198c1010edaSGreg Roach { 1998c2e8227SGreg Roach return true; 2008c2e8227SGreg Roach } 2018c2e8227SGreg Roach 2023caaa4d2SGreg Roach /** 2033caaa4d2SGreg Roach * Can this block be shown on the tree’s home page? 2043caaa4d2SGreg Roach * 2053caaa4d2SGreg Roach * @return bool 2063caaa4d2SGreg Roach */ 20763276d8fSGreg Roach public function isTreeBlock(): bool 208c1010edaSGreg Roach { 2098c2e8227SGreg Roach return true; 2108c2e8227SGreg Roach } 2118c2e8227SGreg Roach 21276692c8bSGreg Roach /** 213a45f9889SGreg Roach * Update the configuration for a block. 214a45f9889SGreg Roach * 2156ccdf4f0SGreg Roach * @param ServerRequestInterface $request 216a45f9889SGreg Roach * @param int $block_id 217a45f9889SGreg Roach * 218a45f9889SGreg Roach * @return void 219a45f9889SGreg Roach */ 2206ccdf4f0SGreg Roach public function saveBlockConfiguration(ServerRequestInterface $request, int $block_id): void 221a45f9889SGreg Roach { 222b46c87bdSGreg Roach $params = (array) $request->getParsedBody(); 223c50a90beSGreg Roach 224c50a90beSGreg Roach $this->setBlockSetting($block_id, 'num', $params['num']); 225c50a90beSGreg Roach $this->setBlockSetting($block_id, 'infoStyle', $params['infoStyle']); 226a45f9889SGreg Roach } 227a45f9889SGreg Roach 228a45f9889SGreg Roach /** 22976692c8bSGreg Roach * An HTML form to edit block settings 23076692c8bSGreg Roach * 231e490cd80SGreg Roach * @param Tree $tree 23276692c8bSGreg Roach * @param int $block_id 233a9430be8SGreg Roach * 2343caaa4d2SGreg Roach * @return string 23576692c8bSGreg Roach */ 2363caaa4d2SGreg Roach public function editBlockConfiguration(Tree $tree, int $block_id): string 237c1010edaSGreg Roach { 238c385536dSGreg Roach $num = $this->getBlockSetting($block_id, 'num', self::DEFAULT_NUMBER); 2397c2c99faSGreg Roach $info_style = $this->getBlockSetting($block_id, 'infoStyle', self::DEFAULT_STYLE); 2408c2e8227SGreg Roach 241c385536dSGreg Roach $info_styles = [ 242bbb76c12SGreg Roach /* I18N: An option in a list-box */ 243bbb76c12SGreg Roach 'list' => I18N::translate('bullet list'), 244bbb76c12SGreg Roach /* I18N: An option in a list-box */ 245bbb76c12SGreg Roach 'array' => I18N::translate('compact list'), 246bbb76c12SGreg Roach /* I18N: An option in a list-box */ 247bbb76c12SGreg Roach 'table' => I18N::translate('table'), 248bbb76c12SGreg Roach /* I18N: An option in a list-box */ 249bbb76c12SGreg Roach 'tagcloud' => I18N::translate('tag cloud'), 250c385536dSGreg Roach ]; 2518c2e8227SGreg Roach 2523caaa4d2SGreg Roach return view('modules/top10_surnames/config', [ 253c385536dSGreg Roach 'num' => $num, 254d6837001SGreg Roach 'info_style' => $info_style, 255c385536dSGreg Roach 'info_styles' => $info_styles, 256c385536dSGreg Roach ]); 2578c2e8227SGreg Roach } 258e15d3b66SRico Sonntag 259e15d3b66SRico Sonntag /** 260e15d3b66SRico Sonntag * Sort (lists of counts of similar) surname by total count. 261e15d3b66SRico Sonntag * 262e15d3b66SRico Sonntag * @param string[] $a 263e15d3b66SRico Sonntag * @param string[] $b 264e15d3b66SRico Sonntag * 265e15d3b66SRico Sonntag * @return int 266e15d3b66SRico Sonntag */ 267e15d3b66SRico Sonntag private function surnameCountSort(array $a, array $b): int 268e15d3b66SRico Sonntag { 269e15d3b66SRico Sonntag return array_sum($b) - array_sum($a); 270e15d3b66SRico Sonntag } 2718c2e8227SGreg Roach} 272