1<?php 2 3/** 4 * webtrees: online genealogy 5 * Copyright (C) 2021 webtrees development team 6 * This program is free software: you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation, either version 3 of the License, or 9 * (at your option) any later version. 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * You should have received a copy of the GNU General Public License 15 * along with this program. If not, see <https://www.gnu.org/licenses/>. 16 */ 17 18declare(strict_types=1); 19 20namespace Fisharebest\Webtrees\Module; 21 22use Fisharebest\Webtrees\Auth; 23use Fisharebest\Webtrees\Functions\FunctionsPrintLists; 24use Fisharebest\Webtrees\I18N; 25use Fisharebest\Webtrees\Individual; 26use Fisharebest\Webtrees\Services\ModuleService; 27use Fisharebest\Webtrees\Tree; 28use Illuminate\Database\Capsule\Manager as DB; 29use Illuminate\Database\Query\Expression; 30use Illuminate\Support\Str; 31use Psr\Http\Message\ServerRequestInterface; 32 33use function array_sum; 34 35/** 36 * Class TopSurnamesModule 37 */ 38class TopSurnamesModule extends AbstractModule implements ModuleBlockInterface 39{ 40 use ModuleBlockTrait; 41 42 // Default values for new blocks. 43 private const DEFAULT_NUMBER = '10'; 44 private const DEFAULT_STYLE = 'table'; 45 46 private ModuleService $module_service; 47 48 /** 49 * TopSurnamesModule constructor. 50 * 51 * @param ModuleService $module_service 52 */ 53 public function __construct(ModuleService $module_service) 54 { 55 $this->module_service = $module_service; 56 } 57 58 /** 59 * How should this module be identified in the control panel, etc.? 60 * 61 * @return string 62 */ 63 public function title(): string 64 { 65 /* I18N: Name of a module. Top=Most common */ 66 return I18N::translate('Top surnames'); 67 } 68 69 /** 70 * A sentence describing what this module does. 71 * 72 * @return string 73 */ 74 public function description(): string 75 { 76 /* I18N: Description of the “Top surnames” module */ 77 return I18N::translate('A list of the most popular surnames.'); 78 } 79 80 /** 81 * Generate the HTML content of this block. 82 * 83 * @param Tree $tree 84 * @param int $block_id 85 * @param string $context 86 * @param array<string> $config 87 * 88 * @return string 89 */ 90 public function getBlock(Tree $tree, int $block_id, string $context, array $config = []): string 91 { 92 $num = (int) $this->getBlockSetting($block_id, 'num', self::DEFAULT_NUMBER); 93 $infoStyle = $this->getBlockSetting($block_id, 'infoStyle', self::DEFAULT_STYLE); 94 95 extract($config, EXTR_OVERWRITE); 96 97 // Use the count of base surnames. 98 $top_surnames = DB::table('name') 99 ->where('n_file', '=', $tree->id()) 100 ->where('n_type', '<>', '_MARNM') 101 ->whereNotIn('n_surn', [Individual::NOMEN_NESCIO, '']) 102 ->groupBy(['n_surn']) 103 ->orderByDesc(new Expression('COUNT(n_surn)')) 104 ->take($num) 105 ->pluck('n_surn'); 106 107 $all_surnames = []; 108 109 foreach ($top_surnames as $top_surname) { 110 $variants = DB::table('name') 111 ->where('n_file', '=', $tree->id()) 112 ->where(new Expression('n_surn /*! COLLATE utf8_bin */'), '=', $top_surname) 113 ->groupBy(['surname']) 114 ->select([new Expression('n_surname /*! COLLATE utf8_bin */ AS surname'), new Expression('count(*) AS total')]) 115 ->pluck('total', 'surname') 116 ->map(static fn (string $n): int => (int) $n) 117 ->all(); 118 119 $all_surnames[$top_surname] = $variants; 120 } 121 122 // Find a module providing individual lists. 123 $module = $this->module_service 124 ->findByComponent(ModuleListInterface::class, $tree, Auth::user()) 125 ->first(static function (ModuleInterface $module): bool { 126 // The family list extends the individual list 127 return 128 $module instanceof IndividualListModule && 129 !$module instanceof FamilyListModule; 130 }); 131 132 switch ($infoStyle) { 133 case 'tagcloud': 134 uksort($all_surnames, I18N::comparator()); 135 $content = FunctionsPrintLists::surnameTagCloud($all_surnames, $module, true, $tree); 136 break; 137 case 'list': 138 uasort($all_surnames, fn (array $a, array $b): int => array_sum($b) <=> array_sum($a)); 139 $content = FunctionsPrintLists::surnameList($all_surnames, 1, true, $module, $tree); 140 break; 141 case 'array': 142 uasort($all_surnames, fn (array $a, array $b): int => array_sum($b) <=> array_sum($a)); 143 $content = FunctionsPrintLists::surnameList($all_surnames, 2, true, $module, $tree); 144 break; 145 case 'table': 146 default: 147 $content = view('lists/surnames-table', [ 148 'surnames' => $all_surnames, 149 'module' => $module, 150 'families' => false, 151 'tree' => $tree, 152 ]); 153 break; 154 } 155 156 if ($context !== self::CONTEXT_EMBED) { 157 $num = count($top_surnames); 158 if ($num === 1) { 159 // I18N: i.e. most popular surname. 160 $title = I18N::translate('Top surname'); 161 } else { 162 // I18N: Title for a list of the most common surnames, %s is a number. Note that a separate translation exists when %s is 1 163 $title = I18N::plural('Top %s surname', 'Top %s surnames', $num, I18N::number($num)); 164 } 165 166 return view('modules/block-template', [ 167 'block' => Str::kebab($this->name()), 168 'id' => $block_id, 169 'config_url' => $this->configUrl($tree, $context, $block_id), 170 'title' => $title, 171 'content' => $content, 172 ]); 173 } 174 175 return $content; 176 } 177 178 /** 179 * Should this block load asynchronously using AJAX? 180 * 181 * Simple blocks are faster in-line, more complex ones can be loaded later. 182 * 183 * @return bool 184 */ 185 public function loadAjax(): bool 186 { 187 return false; 188 } 189 190 /** 191 * Can this block be shown on the user’s home page? 192 * 193 * @return bool 194 */ 195 public function isUserBlock(): bool 196 { 197 return true; 198 } 199 200 /** 201 * Can this block be shown on the tree’s home page? 202 * 203 * @return bool 204 */ 205 public function isTreeBlock(): bool 206 { 207 return true; 208 } 209 210 /** 211 * Update the configuration for a block. 212 * 213 * @param ServerRequestInterface $request 214 * @param int $block_id 215 * 216 * @return void 217 */ 218 public function saveBlockConfiguration(ServerRequestInterface $request, int $block_id): void 219 { 220 $params = (array) $request->getParsedBody(); 221 222 $this->setBlockSetting($block_id, 'num', $params['num']); 223 $this->setBlockSetting($block_id, 'infoStyle', $params['infoStyle']); 224 } 225 226 /** 227 * An HTML form to edit block settings 228 * 229 * @param Tree $tree 230 * @param int $block_id 231 * 232 * @return string 233 */ 234 public function editBlockConfiguration(Tree $tree, int $block_id): string 235 { 236 $num = $this->getBlockSetting($block_id, 'num', self::DEFAULT_NUMBER); 237 $info_style = $this->getBlockSetting($block_id, 'infoStyle', self::DEFAULT_STYLE); 238 239 $info_styles = [ 240 /* I18N: An option in a list-box */ 241 'list' => I18N::translate('bullet list'), 242 /* I18N: An option in a list-box */ 243 'array' => I18N::translate('compact list'), 244 /* I18N: An option in a list-box */ 245 'table' => I18N::translate('table'), 246 /* I18N: An option in a list-box */ 247 'tagcloud' => I18N::translate('tag cloud'), 248 ]; 249 250 return view('modules/top10_surnames/config', [ 251 'num' => $num, 252 'info_style' => $info_style, 253 'info_styles' => $info_styles, 254 ]); 255 } 256} 257