18c2e8227SGreg Roach<?php 28c2e8227SGreg Roach/** 38c2e8227SGreg Roach * webtrees: online genealogy 48fcd0d32SGreg Roach * Copyright (C) 2019 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 */ 16e7f56f2aSGreg Roachdeclare(strict_types=1); 17e7f56f2aSGreg Roach 1876692c8bSGreg Roachnamespace Fisharebest\Webtrees\Module; 1976692c8bSGreg Roach 200e62c4b8SGreg Roachuse Fisharebest\Webtrees\Auth; 213d7a8a4cSGreg Roachuse Fisharebest\Webtrees\Functions\FunctionsPrintLists; 220e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N; 23e490cd80SGreg Roachuse Fisharebest\Webtrees\Tree; 2467992b6aSRichard Cisseeuse Fisharebest\Webtrees\Services\ModuleService; 258d7ed87eSGreg Roachuse Illuminate\Database\Capsule\Manager as DB; 261e7a7a28SGreg Roachuse Illuminate\Support\Str; 27*6ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface; 288c2e8227SGreg Roach 298c2e8227SGreg Roach/** 308c2e8227SGreg Roach * Class TopSurnamesModule 318c2e8227SGreg Roach */ 3237eb8894SGreg Roachclass TopSurnamesModule extends AbstractModule implements ModuleBlockInterface 33c1010edaSGreg Roach{ 3449a243cbSGreg Roach use ModuleBlockTrait; 3549a243cbSGreg Roach 36c385536dSGreg Roach // Default values for new blocks. 3716d6367aSGreg Roach private const DEFAULT_NUMBER = '10'; 3816d6367aSGreg Roach private const DEFAULT_STYLE = 'table'; 39c385536dSGreg Roach 4076692c8bSGreg Roach /** 410cfd6963SGreg Roach * How should this module be identified in the control panel, etc.? 4276692c8bSGreg Roach * 4376692c8bSGreg Roach * @return string 4476692c8bSGreg Roach */ 4549a243cbSGreg Roach public function title(): string 46c1010edaSGreg Roach { 47bbb76c12SGreg Roach /* I18N: Name of a module. Top=Most common */ 48bbb76c12SGreg Roach return I18N::translate('Top surnames'); 498c2e8227SGreg Roach } 508c2e8227SGreg Roach 5176692c8bSGreg Roach /** 5276692c8bSGreg Roach * A sentence describing what this module does. 5376692c8bSGreg Roach * 5476692c8bSGreg Roach * @return string 5576692c8bSGreg Roach */ 5649a243cbSGreg Roach public function description(): string 57c1010edaSGreg Roach { 58bbb76c12SGreg Roach /* I18N: Description of the “Top surnames” module */ 59bbb76c12SGreg Roach return I18N::translate('A list of the most popular surnames.'); 608c2e8227SGreg Roach } 618c2e8227SGreg Roach 6276692c8bSGreg Roach /** 6376692c8bSGreg Roach * Generate the HTML content of this block. 6476692c8bSGreg Roach * 65e490cd80SGreg Roach * @param Tree $tree 6676692c8bSGreg Roach * @param int $block_id 675f2ae573SGreg Roach * @param string $ctype 68727f238cSGreg Roach * @param string[] $cfg 6976692c8bSGreg Roach * 7076692c8bSGreg Roach * @return string 7176692c8bSGreg Roach */ 725f2ae573SGreg Roach public function getBlock(Tree $tree, int $block_id, string $ctype = '', array $cfg = []): string 73c1010edaSGreg Roach { 7480536c2dSGreg Roach $num = (int) $this->getBlockSetting($block_id, 'num', self::DEFAULT_NUMBER); 75c385536dSGreg Roach $infoStyle = $this->getBlockSetting($block_id, 'infoStyle', self::DEFAULT_STYLE); 768c2e8227SGreg Roach 77c385536dSGreg Roach extract($cfg, EXTR_OVERWRITE); 788c2e8227SGreg Roach 7980536c2dSGreg Roach // Use the count of base surnames. 808d7ed87eSGreg Roach $top_surnames = DB::table('name') 818d7ed87eSGreg Roach ->where('n_file', '=', $tree->id()) 828d7ed87eSGreg Roach ->where('n_type', '<>', '_MARNM') 838d7ed87eSGreg Roach ->whereNotIn('n_surn', ['@N.N.', '']) 848d7ed87eSGreg Roach ->groupBy('n_surn') 858d7ed87eSGreg Roach ->orderByDesc(DB::raw('COUNT(n_surn)')) 868d7ed87eSGreg Roach ->take($num) 878d7ed87eSGreg Roach ->pluck('n_surn'); 888c2e8227SGreg Roach 8913abd6f3SGreg Roach $all_surnames = []; 9080536c2dSGreg Roach 918d7ed87eSGreg Roach foreach ($top_surnames as $top_surname) { 928d7ed87eSGreg Roach $variants = DB::table('name') 938d7ed87eSGreg Roach ->where('n_file', '=', $tree->id()) 948d7ed87eSGreg Roach ->where(DB::raw('n_surn /*! COLLATE utf8_bin */'), '=', $top_surname) 958d7ed87eSGreg Roach ->groupBy('surname') 968d7ed87eSGreg Roach ->select([DB::raw('n_surname /*! COLLATE utf8_bin */ AS surname'), DB::raw('count(*) AS total')]) 978d7ed87eSGreg Roach ->pluck('total', 'surname') 988d7ed87eSGreg Roach ->all(); 99f8c9edfeSGreg Roach 10080536c2dSGreg Roach $all_surnames[$top_surname] = $variants; 1018c2e8227SGreg Roach } 1028c2e8227SGreg Roach 10367992b6aSRichard Cissee //find a module providing individual lists 1040b5fd0a6SGreg Roach $module = app(ModuleService::class)->findByComponent(ModuleListInterface::class, $tree, Auth::user())->first(static function (ModuleInterface $module) { 10567992b6aSRichard Cissee return $module instanceof IndividualListModule; 10667992b6aSRichard Cissee }); 10767992b6aSRichard Cissee 1088c2e8227SGreg Roach switch ($infoStyle) { 1098c2e8227SGreg Roach case 'tagcloud': 11080536c2dSGreg Roach uksort($all_surnames, [I18N::class, 'strcasecmp']); 11167992b6aSRichard Cissee $content = FunctionsPrintLists::surnameTagCloud($all_surnames, $module, true, $tree); 1128c2e8227SGreg Roach break; 1138c2e8227SGreg Roach case 'list': 114a515be7cSGreg Roach uasort($all_surnames, [$this, 'surnameCountSort']); 11567992b6aSRichard Cissee $content = FunctionsPrintLists::surnameList($all_surnames, 1, true, $module, $tree); 1168c2e8227SGreg Roach break; 1178c2e8227SGreg Roach case 'array': 118a515be7cSGreg Roach uasort($all_surnames, [$this, 'surnameCountSort']); 11967992b6aSRichard Cissee $content = FunctionsPrintLists::surnameList($all_surnames, 2, true, $module, $tree); 1208c2e8227SGreg Roach break; 1218c2e8227SGreg Roach case 'table': 1228c2e8227SGreg Roach default: 123cf184a4dSGreg Roach $content = view('lists/surnames-table', [ 1246ab54c76SGreg Roach 'surnames' => $all_surnames, 12567992b6aSRichard Cissee 'module' => $module, 12667992b6aSRichard Cissee 'families' => false, 127e490cd80SGreg Roach 'tree' => $tree, 1286ab54c76SGreg Roach ]); 1298c2e8227SGreg Roach break; 1308c2e8227SGreg Roach } 1318c2e8227SGreg Roach 1326a8879feSGreg Roach if ($ctype !== '') { 13380536c2dSGreg Roach $num = count($top_surnames); 13480536c2dSGreg Roach if ($num === 1) { 1358cbbfdceSGreg Roach // I18N: i.e. most popular surname. 1368cbbfdceSGreg Roach $title = I18N::translate('Top surname'); 1378cbbfdceSGreg Roach } else { 1388cbbfdceSGreg 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 1398cbbfdceSGreg Roach $title = I18N::plural('Top %s surname', 'Top %s surnames', $num, I18N::number($num)); 1408cbbfdceSGreg Roach } 1418cbbfdceSGreg Roach 142e490cd80SGreg Roach if ($ctype === 'gedcom' && Auth::isManager($tree)) { 143c1010edaSGreg Roach $config_url = route('tree-page-block-edit', [ 144c1010edaSGreg Roach 'block_id' => $block_id, 145aa6f03bbSGreg Roach 'ged' => $tree->name(), 146c1010edaSGreg Roach ]); 147397e599aSGreg Roach } elseif ($ctype === 'user' && Auth::check()) { 148c1010edaSGreg Roach $config_url = route('user-page-block-edit', [ 149c1010edaSGreg Roach 'block_id' => $block_id, 150aa6f03bbSGreg Roach 'ged' => $tree->name(), 151c1010edaSGreg Roach ]); 1528cbbfdceSGreg Roach } else { 1538cbbfdceSGreg Roach $config_url = ''; 1548cbbfdceSGreg Roach } 1558cbbfdceSGreg Roach 156147e99aaSGreg Roach return view('modules/block-template', [ 1571e7a7a28SGreg Roach 'block' => Str::kebab($this->name()), 1589c6524dcSGreg Roach 'id' => $block_id, 1598cbbfdceSGreg Roach 'config_url' => $config_url, 1608cbbfdceSGreg Roach 'title' => $title, 1619c6524dcSGreg Roach 'content' => $content, 1629c6524dcSGreg Roach ]); 1638c2e8227SGreg Roach } 164b2ce94c6SRico Sonntag 165b2ce94c6SRico Sonntag return $content; 1668c2e8227SGreg Roach } 1678c2e8227SGreg Roach 1688c2e8227SGreg Roach /** {@inheritdoc} */ 169c1010edaSGreg Roach public function loadAjax(): bool 170c1010edaSGreg Roach { 1714ecf4f82SGreg Roach return false; 1728c2e8227SGreg Roach } 1738c2e8227SGreg Roach 1748c2e8227SGreg Roach /** {@inheritdoc} */ 175c1010edaSGreg Roach public function isUserBlock(): bool 176c1010edaSGreg Roach { 1778c2e8227SGreg Roach return true; 1788c2e8227SGreg Roach } 1798c2e8227SGreg Roach 1808c2e8227SGreg Roach /** {@inheritdoc} */ 18163276d8fSGreg Roach public function isTreeBlock(): bool 182c1010edaSGreg Roach { 1838c2e8227SGreg Roach return true; 1848c2e8227SGreg Roach } 1858c2e8227SGreg Roach 18676692c8bSGreg Roach /** 187a45f9889SGreg Roach * Update the configuration for a block. 188a45f9889SGreg Roach * 189*6ccdf4f0SGreg Roach * @param ServerRequestInterface $request 190a45f9889SGreg Roach * @param int $block_id 191a45f9889SGreg Roach * 192a45f9889SGreg Roach * @return void 193a45f9889SGreg Roach */ 194*6ccdf4f0SGreg Roach public function saveBlockConfiguration(ServerRequestInterface $request, int $block_id): void 195a45f9889SGreg Roach { 196a45f9889SGreg Roach $this->setBlockSetting($block_id, 'num', $request->get('num', self::DEFAULT_NUMBER)); 197a45f9889SGreg Roach $this->setBlockSetting($block_id, 'infoStyle', $request->get('infoStyle', self::DEFAULT_STYLE)); 198a45f9889SGreg Roach } 199a45f9889SGreg Roach 200a45f9889SGreg Roach /** 20176692c8bSGreg Roach * An HTML form to edit block settings 20276692c8bSGreg Roach * 203e490cd80SGreg Roach * @param Tree $tree 20476692c8bSGreg Roach * @param int $block_id 205a9430be8SGreg Roach * 206a9430be8SGreg Roach * @return void 20776692c8bSGreg Roach */ 208e364afe4SGreg Roach public function editBlockConfiguration(Tree $tree, int $block_id): void 209c1010edaSGreg Roach { 210c385536dSGreg Roach $num = $this->getBlockSetting($block_id, 'num', self::DEFAULT_NUMBER); 211c385536dSGreg Roach $infoStyle = $this->getBlockSetting($block_id, 'infoStyle', self::DEFAULT_STYLE); 2128c2e8227SGreg Roach 213c385536dSGreg Roach $info_styles = [ 214bbb76c12SGreg Roach /* I18N: An option in a list-box */ 215bbb76c12SGreg Roach 'list' => I18N::translate('bullet list'), 216bbb76c12SGreg Roach /* I18N: An option in a list-box */ 217bbb76c12SGreg Roach 'array' => I18N::translate('compact list'), 218bbb76c12SGreg Roach /* I18N: An option in a list-box */ 219bbb76c12SGreg Roach 'table' => I18N::translate('table'), 220bbb76c12SGreg Roach /* I18N: An option in a list-box */ 221bbb76c12SGreg Roach 'tagcloud' => I18N::translate('tag cloud'), 222c385536dSGreg Roach ]; 2238c2e8227SGreg Roach 224147e99aaSGreg Roach echo view('modules/top10_surnames/config', [ 225c385536dSGreg Roach 'num' => $num, 226c385536dSGreg Roach 'infoStyle' => $infoStyle, 227c385536dSGreg Roach 'info_styles' => $info_styles, 228c385536dSGreg Roach ]); 2298c2e8227SGreg Roach } 230e15d3b66SRico Sonntag 231e15d3b66SRico Sonntag /** 232e15d3b66SRico Sonntag * Sort (lists of counts of similar) surname by total count. 233e15d3b66SRico Sonntag * 234e15d3b66SRico Sonntag * @param string[] $a 235e15d3b66SRico Sonntag * @param string[] $b 236e15d3b66SRico Sonntag * 237e15d3b66SRico Sonntag * @return int 238e15d3b66SRico Sonntag */ 239e15d3b66SRico Sonntag private function surnameCountSort(array $a, array $b): int 240e15d3b66SRico Sonntag { 241e15d3b66SRico Sonntag return array_sum($b) - array_sum($a); 242e15d3b66SRico Sonntag } 2438c2e8227SGreg Roach} 244