1<?php 2 3/** 4 * webtrees: online genealogy 5 * Copyright (C) 2022 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\I18N; 24use Fisharebest\Webtrees\Individual; 25use Fisharebest\Webtrees\Services\ModuleService; 26use Fisharebest\Webtrees\Tree; 27use Fisharebest\Webtrees\Validator; 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; 34use function count; 35use function extract; 36use function uasort; 37use function uksort; 38use function view; 39 40use const EXTR_OVERWRITE; 41use const PHP_INT_MAX; 42 43/** 44 * Class TopSurnamesModule 45 */ 46class TopSurnamesModule extends AbstractModule implements ModuleBlockInterface 47{ 48 use ModuleBlockTrait; 49 50 // Default values for new blocks. 51 private const DEFAULT_NUMBER = '10'; 52 private const DEFAULT_STYLE = 'table'; 53 54 private ModuleService $module_service; 55 56 /** 57 * TopSurnamesModule constructor. 58 * 59 * @param ModuleService $module_service 60 */ 61 public function __construct(ModuleService $module_service) 62 { 63 $this->module_service = $module_service; 64 } 65 66 /** 67 * How should this module be identified in the control panel, etc.? 68 * 69 * @return string 70 */ 71 public function title(): string 72 { 73 /* I18N: Name of a module. Top=Most common */ 74 return I18N::translate('Top surnames'); 75 } 76 77 /** 78 * A sentence describing what this module does. 79 * 80 * @return string 81 */ 82 public function description(): string 83 { 84 /* I18N: Description of the “Top surnames” module */ 85 return I18N::translate('A list of the most popular surnames.'); 86 } 87 88 /** 89 * Generate the HTML content of this block. 90 * 91 * @param Tree $tree 92 * @param int $block_id 93 * @param string $context 94 * @param array<string,string> $config 95 * 96 * @return string 97 */ 98 public function getBlock(Tree $tree, int $block_id, string $context, array $config = []): string 99 { 100 $num = (int) $this->getBlockSetting($block_id, 'num', self::DEFAULT_NUMBER); 101 $infoStyle = $this->getBlockSetting($block_id, 'infoStyle', self::DEFAULT_STYLE); 102 103 extract($config, EXTR_OVERWRITE); 104 105 // Use the count of base surnames. 106 $top_surnames = DB::table('name') 107 ->where('n_file', '=', $tree->id()) 108 ->where('n_type', '<>', '_MARNM') 109 ->whereNotIn('n_surn', [Individual::NOMEN_NESCIO, '']) 110 ->groupBy(['n_surn']) 111 ->orderByDesc(new Expression('COUNT(n_surn)')) 112 ->take($num) 113 ->pluck('n_surn'); 114 115 $all_surnames = []; 116 117 foreach ($top_surnames as $top_surname) { 118 $variants = DB::table('name') 119 ->where('n_file', '=', $tree->id()) 120 ->where(new Expression('n_surn /*! COLLATE utf8_bin */'), '=', $top_surname) 121 ->groupBy([new Expression('n_surname /*! COLLATE utf8_bin */')]) 122 ->select([new Expression('n_surname /*! COLLATE utf8_bin */ AS surname'), new Expression('count(*) AS total')]) 123 ->pluck('total', 'surname') 124 ->map(static fn (string $n): int => (int) $n) 125 ->all(); 126 127 $all_surnames[$top_surname] = $variants; 128 } 129 130 // Find a module providing individual lists. 131 $module = $this->module_service 132 ->findByComponent(ModuleListInterface::class, $tree, Auth::user()) 133 ->first(static function (ModuleInterface $module): bool { 134 // The family list extends the individual list 135 return 136 $module instanceof IndividualListModule && 137 !$module instanceof FamilyListModule; 138 }); 139 140 switch ($infoStyle) { 141 case 'tagcloud': 142 uksort($all_surnames, I18N::comparator()); 143 $content = view('lists/surnames-tag-cloud', [ 144 'module' => $module, 145 'surnames' => $all_surnames, 146 'totals' => true, 147 'tree' => $tree, 148 ]); 149 break; 150 151 case 'list': 152 uasort($all_surnames, static fn (array $a, array $b): int => array_sum($b) <=> array_sum($a)); 153 $content = view('lists/surnames-bullet-list', [ 154 'module' => $module, 155 'surnames' => $all_surnames, 156 'totals' => true, 157 'tree' => $tree, 158 ]); 159 break; 160 161 case 'array': 162 uasort($all_surnames, static fn (array $a, array $b): int => array_sum($b) <=> array_sum($a)); 163 $content = view('lists/surnames-compact-list', [ 164 'module' => $module, 165 'surnames' => $all_surnames, 166 'totals' => true, 167 'tree' => $tree, 168 ]); 169 break; 170 171 case 'table': 172 default: 173 uksort($all_surnames, I18N::comparator()); 174 $content = view('lists/surnames-table', [ 175 'families' => false, 176 'module' => $module, 177 'order' => [[1, 'desc']], 178 'surnames' => $all_surnames, 179 'tree' => $tree, 180 ]); 181 break; 182 } 183 184 if ($context !== self::CONTEXT_EMBED) { 185 $num = count($top_surnames); 186 if ($num === 1) { 187 // I18N: i.e. most popular surname. 188 $title = I18N::translate('Top surname'); 189 } else { 190 // I18N: Title for a list of the most common surnames, %s is a number. Note that a separate translation exists when %s is 1 191 $title = I18N::plural('Top %s surname', 'Top %s surnames', $num, I18N::number($num)); 192 } 193 194 return view('modules/block-template', [ 195 'block' => Str::kebab($this->name()), 196 'id' => $block_id, 197 'config_url' => $this->configUrl($tree, $context, $block_id), 198 'title' => $title, 199 'content' => $content, 200 ]); 201 } 202 203 return $content; 204 } 205 206 /** 207 * Should this block load asynchronously using AJAX? 208 * 209 * Simple blocks are faster in-line, more complex ones can be loaded later. 210 * 211 * @return bool 212 */ 213 public function loadAjax(): bool 214 { 215 return false; 216 } 217 218 /** 219 * Can this block be shown on the user’s home page? 220 * 221 * @return bool 222 */ 223 public function isUserBlock(): bool 224 { 225 return true; 226 } 227 228 /** 229 * Can this block be shown on the tree’s home page? 230 * 231 * @return bool 232 */ 233 public function isTreeBlock(): bool 234 { 235 return true; 236 } 237 238 /** 239 * Update the configuration for a block. 240 * 241 * @param ServerRequestInterface $request 242 * @param int $block_id 243 * 244 * @return void 245 */ 246 public function saveBlockConfiguration(ServerRequestInterface $request, int $block_id): void 247 { 248 $num = Validator::parsedBody($request)->integer('num'); 249 $info_style = Validator::parsedBody($request)->string('infoStyle'); 250 251 $this->setBlockSetting($block_id, 'num', (string) $num); 252 $this->setBlockSetting($block_id, 'infoStyle', $info_style); 253 } 254 255 /** 256 * An HTML form to edit block settings 257 * 258 * @param Tree $tree 259 * @param int $block_id 260 * 261 * @return string 262 */ 263 public function editBlockConfiguration(Tree $tree, int $block_id): string 264 { 265 $num = $this->getBlockSetting($block_id, 'num', self::DEFAULT_NUMBER); 266 $info_style = $this->getBlockSetting($block_id, 'infoStyle', self::DEFAULT_STYLE); 267 268 $info_styles = [ 269 /* I18N: An option in a list-box */ 270 'list' => I18N::translate('bullet list'), 271 /* I18N: An option in a list-box */ 272 'array' => I18N::translate('compact list'), 273 /* I18N: An option in a list-box */ 274 'table' => I18N::translate('table'), 275 /* I18N: An option in a list-box */ 276 'tagcloud' => I18N::translate('tag cloud'), 277 ]; 278 279 return view('modules/top10_surnames/config', [ 280 'num' => $num, 281 'info_style' => $info_style, 282 'info_styles' => $info_styles, 283 ]); 284 } 285} 286