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; 2709482a55SGreg 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 8709482a55SGreg 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') 117*4c78e066SGreg Roach ->map(static fn (string $n): int => (int) $n) 1188d7ed87eSGreg Roach ->all(); 119f8c9edfeSGreg Roach 12080536c2dSGreg Roach $all_surnames[$top_surname] = $variants; 1218c2e8227SGreg Roach } 1228c2e8227SGreg Roach 1232e4f3c66SGreg Roach // Find a module providing individual lists. 1242e4f3c66SGreg Roach $module = $this->module_service 1252e4f3c66SGreg Roach ->findByComponent(ModuleListInterface::class, $tree, Auth::user()) 1262e4f3c66SGreg Roach ->first(static function (ModuleInterface $module): bool { 127ba738272SGreg Roach // The family list extends the individual list 128ba738272SGreg Roach return 129ba738272SGreg Roach $module instanceof IndividualListModule && 130ba738272SGreg Roach !$module instanceof FamilyListModule; 13167992b6aSRichard Cissee }); 13267992b6aSRichard Cissee 1338c2e8227SGreg Roach switch ($infoStyle) { 1348c2e8227SGreg Roach case 'tagcloud': 13537646143SGreg Roach uksort($all_surnames, I18N::comparator()); 13667992b6aSRichard Cissee $content = FunctionsPrintLists::surnameTagCloud($all_surnames, $module, true, $tree); 1378c2e8227SGreg Roach break; 1388c2e8227SGreg Roach case 'list': 139632b1edfSGreg Roach uasort($all_surnames, fn (array $a, array $b): int => array_sum($b) <=> array_sum($a)); 14067992b6aSRichard Cissee $content = FunctionsPrintLists::surnameList($all_surnames, 1, true, $module, $tree); 1418c2e8227SGreg Roach break; 1428c2e8227SGreg Roach case 'array': 143632b1edfSGreg Roach uasort($all_surnames, fn (array $a, array $b): int => array_sum($b) <=> array_sum($a)); 14467992b6aSRichard Cissee $content = FunctionsPrintLists::surnameList($all_surnames, 2, true, $module, $tree); 1458c2e8227SGreg Roach break; 1468c2e8227SGreg Roach case 'table': 1478c2e8227SGreg Roach default: 148cf184a4dSGreg Roach $content = view('lists/surnames-table', [ 1496ab54c76SGreg Roach 'surnames' => $all_surnames, 15067992b6aSRichard Cissee 'module' => $module, 15167992b6aSRichard Cissee 'families' => false, 152e490cd80SGreg Roach 'tree' => $tree, 1536ab54c76SGreg Roach ]); 1548c2e8227SGreg Roach break; 1558c2e8227SGreg Roach } 1568c2e8227SGreg Roach 1573caaa4d2SGreg Roach if ($context !== self::CONTEXT_EMBED) { 15880536c2dSGreg Roach $num = count($top_surnames); 15980536c2dSGreg Roach if ($num === 1) { 1608cbbfdceSGreg Roach // I18N: i.e. most popular surname. 1618cbbfdceSGreg Roach $title = I18N::translate('Top surname'); 1628cbbfdceSGreg Roach } else { 1638cbbfdceSGreg 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 1648cbbfdceSGreg Roach $title = I18N::plural('Top %s surname', 'Top %s surnames', $num, I18N::number($num)); 1658cbbfdceSGreg Roach } 1668cbbfdceSGreg Roach 167147e99aaSGreg Roach return view('modules/block-template', [ 1681e7a7a28SGreg Roach 'block' => Str::kebab($this->name()), 1699c6524dcSGreg Roach 'id' => $block_id, 1703caaa4d2SGreg Roach 'config_url' => $this->configUrl($tree, $context, $block_id), 1718cbbfdceSGreg Roach 'title' => $title, 1729c6524dcSGreg Roach 'content' => $content, 1739c6524dcSGreg Roach ]); 1748c2e8227SGreg Roach } 175b2ce94c6SRico Sonntag 176b2ce94c6SRico Sonntag return $content; 1778c2e8227SGreg Roach } 1788c2e8227SGreg Roach 1793caaa4d2SGreg Roach /** 1803caaa4d2SGreg Roach * Should this block load asynchronously using AJAX? 1813caaa4d2SGreg Roach * 1823caaa4d2SGreg Roach * Simple blocks are faster in-line, more complex ones can be loaded later. 1833caaa4d2SGreg Roach * 1843caaa4d2SGreg Roach * @return bool 1853caaa4d2SGreg Roach */ 186c1010edaSGreg Roach public function loadAjax(): bool 187c1010edaSGreg Roach { 1884ecf4f82SGreg Roach return false; 1898c2e8227SGreg Roach } 1908c2e8227SGreg Roach 1913caaa4d2SGreg Roach /** 1923caaa4d2SGreg Roach * Can this block be shown on the user’s home page? 1933caaa4d2SGreg Roach * 1943caaa4d2SGreg Roach * @return bool 1953caaa4d2SGreg Roach */ 196c1010edaSGreg Roach public function isUserBlock(): bool 197c1010edaSGreg Roach { 1988c2e8227SGreg Roach return true; 1998c2e8227SGreg Roach } 2008c2e8227SGreg Roach 2013caaa4d2SGreg Roach /** 2023caaa4d2SGreg Roach * Can this block be shown on the tree’s home page? 2033caaa4d2SGreg Roach * 2043caaa4d2SGreg Roach * @return bool 2053caaa4d2SGreg Roach */ 20663276d8fSGreg Roach public function isTreeBlock(): bool 207c1010edaSGreg Roach { 2088c2e8227SGreg Roach return true; 2098c2e8227SGreg Roach } 2108c2e8227SGreg Roach 21176692c8bSGreg Roach /** 212a45f9889SGreg Roach * Update the configuration for a block. 213a45f9889SGreg Roach * 2146ccdf4f0SGreg Roach * @param ServerRequestInterface $request 215a45f9889SGreg Roach * @param int $block_id 216a45f9889SGreg Roach * 217a45f9889SGreg Roach * @return void 218a45f9889SGreg Roach */ 2196ccdf4f0SGreg Roach public function saveBlockConfiguration(ServerRequestInterface $request, int $block_id): void 220a45f9889SGreg Roach { 221b46c87bdSGreg Roach $params = (array) $request->getParsedBody(); 222c50a90beSGreg Roach 223c50a90beSGreg Roach $this->setBlockSetting($block_id, 'num', $params['num']); 224c50a90beSGreg Roach $this->setBlockSetting($block_id, 'infoStyle', $params['infoStyle']); 225a45f9889SGreg Roach } 226a45f9889SGreg Roach 227a45f9889SGreg Roach /** 22876692c8bSGreg Roach * An HTML form to edit block settings 22976692c8bSGreg Roach * 230e490cd80SGreg Roach * @param Tree $tree 23176692c8bSGreg Roach * @param int $block_id 232a9430be8SGreg Roach * 2333caaa4d2SGreg Roach * @return string 23476692c8bSGreg Roach */ 2353caaa4d2SGreg Roach public function editBlockConfiguration(Tree $tree, int $block_id): string 236c1010edaSGreg Roach { 237c385536dSGreg Roach $num = $this->getBlockSetting($block_id, 'num', self::DEFAULT_NUMBER); 2387c2c99faSGreg Roach $info_style = $this->getBlockSetting($block_id, 'infoStyle', self::DEFAULT_STYLE); 2398c2e8227SGreg Roach 240c385536dSGreg Roach $info_styles = [ 241bbb76c12SGreg Roach /* I18N: An option in a list-box */ 242bbb76c12SGreg Roach 'list' => I18N::translate('bullet list'), 243bbb76c12SGreg Roach /* I18N: An option in a list-box */ 244bbb76c12SGreg Roach 'array' => I18N::translate('compact list'), 245bbb76c12SGreg Roach /* I18N: An option in a list-box */ 246bbb76c12SGreg Roach 'table' => I18N::translate('table'), 247bbb76c12SGreg Roach /* I18N: An option in a list-box */ 248bbb76c12SGreg Roach 'tagcloud' => I18N::translate('tag cloud'), 249c385536dSGreg Roach ]; 2508c2e8227SGreg Roach 2513caaa4d2SGreg Roach return view('modules/top10_surnames/config', [ 252c385536dSGreg Roach 'num' => $num, 253d6837001SGreg Roach 'info_style' => $info_style, 254c385536dSGreg Roach 'info_styles' => $info_styles, 255c385536dSGreg Roach ]); 2568c2e8227SGreg Roach } 2578c2e8227SGreg Roach} 258