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; 2667992b6aSRichard Cisseeuse Fisharebest\Webtrees\Services\ModuleService; 27*09482a55SGreg Roachuse Fisharebest\Webtrees\Tree; 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 33632b1edfSGreg Roachuse function array_sum; 34632b1edfSGreg Roach 358c2e8227SGreg Roach/** 368c2e8227SGreg Roach * Class TopSurnamesModule 378c2e8227SGreg Roach */ 3837eb8894SGreg Roachclass TopSurnamesModule extends AbstractModule implements ModuleBlockInterface 39c1010edaSGreg Roach{ 4049a243cbSGreg Roach use ModuleBlockTrait; 4149a243cbSGreg Roach 42c385536dSGreg Roach // Default values for new blocks. 4316d6367aSGreg Roach private const DEFAULT_NUMBER = '10'; 4416d6367aSGreg Roach private const DEFAULT_STYLE = 'table'; 45c385536dSGreg Roach 462e4f3c66SGreg Roach /** @var ModuleService */ 472e4f3c66SGreg Roach private $module_service; 482e4f3c66SGreg Roach 492e4f3c66SGreg Roach /** 502e4f3c66SGreg Roach * TopSurnamesModule constructor. 512e4f3c66SGreg Roach * 522e4f3c66SGreg Roach * @param ModuleService $module_service 532e4f3c66SGreg Roach */ 542e4f3c66SGreg Roach public function __construct(ModuleService $module_service) 552e4f3c66SGreg Roach { 562e4f3c66SGreg Roach $this->module_service = $module_service; 572e4f3c66SGreg Roach } 582e4f3c66SGreg Roach 5976692c8bSGreg Roach /** 600cfd6963SGreg Roach * How should this module be identified in the control panel, etc.? 6176692c8bSGreg Roach * 6276692c8bSGreg Roach * @return string 6376692c8bSGreg Roach */ 6449a243cbSGreg Roach public function title(): string 65c1010edaSGreg Roach { 66bbb76c12SGreg Roach /* I18N: Name of a module. Top=Most common */ 67bbb76c12SGreg Roach return I18N::translate('Top surnames'); 688c2e8227SGreg Roach } 698c2e8227SGreg Roach 7076692c8bSGreg Roach /** 7176692c8bSGreg Roach * A sentence describing what this module does. 7276692c8bSGreg Roach * 7376692c8bSGreg Roach * @return string 7476692c8bSGreg Roach */ 7549a243cbSGreg Roach public function description(): string 76c1010edaSGreg Roach { 77bbb76c12SGreg Roach /* I18N: Description of the “Top surnames” module */ 78bbb76c12SGreg Roach return I18N::translate('A list of the most popular surnames.'); 798c2e8227SGreg Roach } 808c2e8227SGreg Roach 8176692c8bSGreg Roach /** 8276692c8bSGreg Roach * Generate the HTML content of this block. 8376692c8bSGreg Roach * 84e490cd80SGreg Roach * @param Tree $tree 8576692c8bSGreg Roach * @param int $block_id 863caaa4d2SGreg Roach * @param string $context 87*09482a55SGreg Roach * @param array<string> $config 8876692c8bSGreg Roach * 8976692c8bSGreg Roach * @return string 9076692c8bSGreg Roach */ 913caaa4d2SGreg Roach public function getBlock(Tree $tree, int $block_id, string $context, array $config = []): string 92c1010edaSGreg Roach { 9380536c2dSGreg Roach $num = (int) $this->getBlockSetting($block_id, 'num', self::DEFAULT_NUMBER); 94c385536dSGreg Roach $infoStyle = $this->getBlockSetting($block_id, 'infoStyle', self::DEFAULT_STYLE); 958c2e8227SGreg Roach 963caaa4d2SGreg Roach extract($config, EXTR_OVERWRITE); 978c2e8227SGreg Roach 9880536c2dSGreg Roach // Use the count of base surnames. 998d7ed87eSGreg Roach $top_surnames = DB::table('name') 1008d7ed87eSGreg Roach ->where('n_file', '=', $tree->id()) 1018d7ed87eSGreg Roach ->where('n_type', '<>', '_MARNM') 1028fb4e87cSGreg Roach ->whereNotIn('n_surn', [Individual::NOMEN_NESCIO, '']) 1037f5c2944SGreg Roach ->groupBy(['n_surn']) 104a69f5655SGreg Roach ->orderByDesc(new Expression('COUNT(n_surn)')) 1058d7ed87eSGreg Roach ->take($num) 1068d7ed87eSGreg Roach ->pluck('n_surn'); 1078c2e8227SGreg Roach 10813abd6f3SGreg Roach $all_surnames = []; 10980536c2dSGreg Roach 1108d7ed87eSGreg Roach foreach ($top_surnames as $top_surname) { 1118d7ed87eSGreg Roach $variants = DB::table('name') 1128d7ed87eSGreg Roach ->where('n_file', '=', $tree->id()) 113a69f5655SGreg Roach ->where(new Expression('n_surn /*! COLLATE utf8_bin */'), '=', $top_surname) 1147f5c2944SGreg Roach ->groupBy(['surname']) 115a69f5655SGreg Roach ->select([new Expression('n_surname /*! COLLATE utf8_bin */ AS surname'), new Expression('count(*) AS total')]) 1168d7ed87eSGreg Roach ->pluck('total', 'surname') 1177dc4edbcSGreg Roach ->map(static function ($n): int { 1187dc4edbcSGreg Roach // Some database drivers return numeric columns strings. 1197dc4edbcSGreg Roach return (int) $n; 1207dc4edbcSGreg Roach }) 1218d7ed87eSGreg Roach ->all(); 122f8c9edfeSGreg Roach 12380536c2dSGreg Roach $all_surnames[$top_surname] = $variants; 1248c2e8227SGreg Roach } 1258c2e8227SGreg Roach 1262e4f3c66SGreg Roach // Find a module providing individual lists. 1272e4f3c66SGreg Roach $module = $this->module_service 1282e4f3c66SGreg Roach ->findByComponent(ModuleListInterface::class, $tree, Auth::user()) 1292e4f3c66SGreg Roach ->first(static function (ModuleInterface $module): bool { 130ba738272SGreg Roach // The family list extends the individual list 131ba738272SGreg Roach return 132ba738272SGreg Roach $module instanceof IndividualListModule && 133ba738272SGreg Roach !$module instanceof FamilyListModule; 13467992b6aSRichard Cissee }); 13567992b6aSRichard Cissee 1368c2e8227SGreg Roach switch ($infoStyle) { 1378c2e8227SGreg Roach case 'tagcloud': 13837646143SGreg Roach uksort($all_surnames, I18N::comparator()); 13967992b6aSRichard Cissee $content = FunctionsPrintLists::surnameTagCloud($all_surnames, $module, true, $tree); 1408c2e8227SGreg Roach break; 1418c2e8227SGreg Roach case 'list': 142632b1edfSGreg Roach uasort($all_surnames, fn (array $a, array $b): int => array_sum($b) <=> array_sum($a)); 14367992b6aSRichard Cissee $content = FunctionsPrintLists::surnameList($all_surnames, 1, true, $module, $tree); 1448c2e8227SGreg Roach break; 1458c2e8227SGreg Roach case 'array': 146632b1edfSGreg Roach uasort($all_surnames, fn (array $a, array $b): int => array_sum($b) <=> array_sum($a)); 14767992b6aSRichard Cissee $content = FunctionsPrintLists::surnameList($all_surnames, 2, true, $module, $tree); 1488c2e8227SGreg Roach break; 1498c2e8227SGreg Roach case 'table': 1508c2e8227SGreg Roach default: 151cf184a4dSGreg Roach $content = view('lists/surnames-table', [ 1526ab54c76SGreg Roach 'surnames' => $all_surnames, 15367992b6aSRichard Cissee 'module' => $module, 15467992b6aSRichard Cissee 'families' => false, 155e490cd80SGreg Roach 'tree' => $tree, 1566ab54c76SGreg Roach ]); 1578c2e8227SGreg Roach break; 1588c2e8227SGreg Roach } 1598c2e8227SGreg Roach 1603caaa4d2SGreg Roach if ($context !== self::CONTEXT_EMBED) { 16180536c2dSGreg Roach $num = count($top_surnames); 16280536c2dSGreg Roach if ($num === 1) { 1638cbbfdceSGreg Roach // I18N: i.e. most popular surname. 1648cbbfdceSGreg Roach $title = I18N::translate('Top surname'); 1658cbbfdceSGreg Roach } else { 1668cbbfdceSGreg 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 1678cbbfdceSGreg Roach $title = I18N::plural('Top %s surname', 'Top %s surnames', $num, I18N::number($num)); 1688cbbfdceSGreg Roach } 1698cbbfdceSGreg Roach 170147e99aaSGreg Roach return view('modules/block-template', [ 1711e7a7a28SGreg Roach 'block' => Str::kebab($this->name()), 1729c6524dcSGreg Roach 'id' => $block_id, 1733caaa4d2SGreg Roach 'config_url' => $this->configUrl($tree, $context, $block_id), 1748cbbfdceSGreg Roach 'title' => $title, 1759c6524dcSGreg Roach 'content' => $content, 1769c6524dcSGreg Roach ]); 1778c2e8227SGreg Roach } 178b2ce94c6SRico Sonntag 179b2ce94c6SRico Sonntag return $content; 1808c2e8227SGreg Roach } 1818c2e8227SGreg Roach 1823caaa4d2SGreg Roach /** 1833caaa4d2SGreg Roach * Should this block load asynchronously using AJAX? 1843caaa4d2SGreg Roach * 1853caaa4d2SGreg Roach * Simple blocks are faster in-line, more complex ones can be loaded later. 1863caaa4d2SGreg Roach * 1873caaa4d2SGreg Roach * @return bool 1883caaa4d2SGreg Roach */ 189c1010edaSGreg Roach public function loadAjax(): bool 190c1010edaSGreg Roach { 1914ecf4f82SGreg Roach return false; 1928c2e8227SGreg Roach } 1938c2e8227SGreg Roach 1943caaa4d2SGreg Roach /** 1953caaa4d2SGreg Roach * Can this block be shown on the user’s home page? 1963caaa4d2SGreg Roach * 1973caaa4d2SGreg Roach * @return bool 1983caaa4d2SGreg Roach */ 199c1010edaSGreg Roach public function isUserBlock(): bool 200c1010edaSGreg Roach { 2018c2e8227SGreg Roach return true; 2028c2e8227SGreg Roach } 2038c2e8227SGreg Roach 2043caaa4d2SGreg Roach /** 2053caaa4d2SGreg Roach * Can this block be shown on the tree’s home page? 2063caaa4d2SGreg Roach * 2073caaa4d2SGreg Roach * @return bool 2083caaa4d2SGreg Roach */ 20963276d8fSGreg Roach public function isTreeBlock(): bool 210c1010edaSGreg Roach { 2118c2e8227SGreg Roach return true; 2128c2e8227SGreg Roach } 2138c2e8227SGreg Roach 21476692c8bSGreg Roach /** 215a45f9889SGreg Roach * Update the configuration for a block. 216a45f9889SGreg Roach * 2176ccdf4f0SGreg Roach * @param ServerRequestInterface $request 218a45f9889SGreg Roach * @param int $block_id 219a45f9889SGreg Roach * 220a45f9889SGreg Roach * @return void 221a45f9889SGreg Roach */ 2226ccdf4f0SGreg Roach public function saveBlockConfiguration(ServerRequestInterface $request, int $block_id): void 223a45f9889SGreg Roach { 224b46c87bdSGreg Roach $params = (array) $request->getParsedBody(); 225c50a90beSGreg Roach 226c50a90beSGreg Roach $this->setBlockSetting($block_id, 'num', $params['num']); 227c50a90beSGreg Roach $this->setBlockSetting($block_id, 'infoStyle', $params['infoStyle']); 228a45f9889SGreg Roach } 229a45f9889SGreg Roach 230a45f9889SGreg Roach /** 23176692c8bSGreg Roach * An HTML form to edit block settings 23276692c8bSGreg Roach * 233e490cd80SGreg Roach * @param Tree $tree 23476692c8bSGreg Roach * @param int $block_id 235a9430be8SGreg Roach * 2363caaa4d2SGreg Roach * @return string 23776692c8bSGreg Roach */ 2383caaa4d2SGreg Roach public function editBlockConfiguration(Tree $tree, int $block_id): string 239c1010edaSGreg Roach { 240c385536dSGreg Roach $num = $this->getBlockSetting($block_id, 'num', self::DEFAULT_NUMBER); 2417c2c99faSGreg Roach $info_style = $this->getBlockSetting($block_id, 'infoStyle', self::DEFAULT_STYLE); 2428c2e8227SGreg Roach 243c385536dSGreg Roach $info_styles = [ 244bbb76c12SGreg Roach /* I18N: An option in a list-box */ 245bbb76c12SGreg Roach 'list' => I18N::translate('bullet list'), 246bbb76c12SGreg Roach /* I18N: An option in a list-box */ 247bbb76c12SGreg Roach 'array' => I18N::translate('compact list'), 248bbb76c12SGreg Roach /* I18N: An option in a list-box */ 249bbb76c12SGreg Roach 'table' => I18N::translate('table'), 250bbb76c12SGreg Roach /* I18N: An option in a list-box */ 251bbb76c12SGreg Roach 'tagcloud' => I18N::translate('tag cloud'), 252c385536dSGreg Roach ]; 2538c2e8227SGreg Roach 2543caaa4d2SGreg Roach return view('modules/top10_surnames/config', [ 255c385536dSGreg Roach 'num' => $num, 256d6837001SGreg Roach 'info_style' => $info_style, 257c385536dSGreg Roach 'info_styles' => $info_styles, 258c385536dSGreg Roach ]); 2598c2e8227SGreg Roach } 2608c2e8227SGreg Roach} 261