1<?php 2 3/** 4 * webtrees: online genealogy 5 * Copyright (C) 2023 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_slice; 34use function array_sum; 35use function count; 36use function extract; 37use function uasort; 38use function uksort; 39use function view; 40 41use const EXTR_OVERWRITE; 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 $info_style = $this->getBlockSetting($block_id, 'infoStyle', self::DEFAULT_STYLE); 102 103 extract($config, EXTR_OVERWRITE); 104 105 $query = DB::table('name') 106 ->where('n_file', '=', $tree->id()) 107 ->where('n_type', '<>', '_MARNM') 108 ->where('n_surn', '<>', '') 109 ->where('n_surn', '<>', Individual::NOMEN_NESCIO) 110 ->select([ 111 $this->binaryColumn('n_surn', 'n_surn'), 112 $this->binaryColumn('n_surname', 'n_surname'), 113 new Expression('COUNT(*) AS total'), 114 ]) 115 ->groupBy([ 116 $this->binaryColumn('n_surn'), 117 $this->binaryColumn('n_surname'), 118 ]); 119 120 /** @var array<array<int>> $top_surnames */ 121 $top_surnames = []; 122 123 foreach ($query->get() as $row) { 124 $row->n_surn = $row->n_surn === '' ? $row->n_surname : $row->n_surn; 125 $row->n_surn = I18N::strtoupper(I18N::language()->normalize($row->n_surn)); 126 127 $top_surnames[$row->n_surn][$row->n_surname] ??= 0; 128 $top_surnames[$row->n_surn][$row->n_surname] += (int) $row->total; 129 } 130 131 uasort($top_surnames, static fn (array $x, array $y): int => array_sum($y) <=> array_sum($x)); 132 133 $top_surnames = array_slice($top_surnames, 0, $num, true); 134 135 // Find a module providing individual lists. 136 $module = $this->module_service 137 ->findByComponent(ModuleListInterface::class, $tree, Auth::user()) 138 ->first(static function (ModuleInterface $module): bool { 139 // The family list extends the individual list 140 return 141 $module instanceof IndividualListModule && 142 !$module instanceof FamilyListModule; 143 }); 144 145 switch ($info_style) { 146 case 'tagcloud': 147 uksort($top_surnames, I18N::comparator()); 148 $content = view('lists/surnames-tag-cloud', [ 149 'module' => $module, 150 'params' => [], 151 'surnames' => $top_surnames, 152 'totals' => true, 153 'tree' => $tree, 154 ]); 155 break; 156 157 case 'list': 158 $content = view('lists/surnames-bullet-list', [ 159 'module' => $module, 160 'params' => [], 161 'surnames' => $top_surnames, 162 'totals' => true, 163 'tree' => $tree, 164 ]); 165 break; 166 167 case 'array': 168 $content = view('lists/surnames-compact-list', [ 169 'module' => $module, 170 'params' => [], 171 'surnames' => $top_surnames, 172 'totals' => true, 173 'tree' => $tree, 174 ]); 175 break; 176 177 case 'table': 178 default: 179 uksort($top_surnames, I18N::comparator()); 180 $content = view('lists/surnames-table', [ 181 'families' => false, 182 'module' => $module, 183 'order' => [[1, 'desc']], 184 'params' => [], 185 'surnames' => $top_surnames, 186 'tree' => $tree, 187 ]); 188 break; 189 } 190 191 if ($context !== self::CONTEXT_EMBED) { 192 $num = count($top_surnames); 193 if ($num === 1) { 194 // I18N: i.e. most popular surname. 195 $title = I18N::translate('Top surname'); 196 } else { 197 // I18N: Title for a list of the most common surnames, %s is a number. Note that a separate translation exists when %s is 1 198 $title = I18N::plural('Top %s surname', 'Top %s surnames', $num, I18N::number($num)); 199 } 200 201 return view('modules/block-template', [ 202 'block' => Str::kebab($this->name()), 203 'id' => $block_id, 204 'config_url' => $this->configUrl($tree, $context, $block_id), 205 'title' => $title, 206 'content' => $content, 207 ]); 208 } 209 210 return $content; 211 } 212 213 /** 214 * Should this block load asynchronously using AJAX? 215 * 216 * Simple blocks are faster in-line, more complex ones can be loaded later. 217 * 218 * @return bool 219 */ 220 public function loadAjax(): bool 221 { 222 return false; 223 } 224 225 /** 226 * Can this block be shown on the user’s home page? 227 * 228 * @return bool 229 */ 230 public function isUserBlock(): bool 231 { 232 return true; 233 } 234 235 /** 236 * Can this block be shown on the tree’s home page? 237 * 238 * @return bool 239 */ 240 public function isTreeBlock(): bool 241 { 242 return true; 243 } 244 245 /** 246 * Update the configuration for a block. 247 * 248 * @param ServerRequestInterface $request 249 * @param int $block_id 250 * 251 * @return void 252 */ 253 public function saveBlockConfiguration(ServerRequestInterface $request, int $block_id): void 254 { 255 $num = Validator::parsedBody($request)->integer('num'); 256 $info_style = Validator::parsedBody($request)->string('infoStyle'); 257 258 $this->setBlockSetting($block_id, 'num', (string) $num); 259 $this->setBlockSetting($block_id, 'infoStyle', $info_style); 260 } 261 262 /** 263 * An HTML form to edit block settings 264 * 265 * @param Tree $tree 266 * @param int $block_id 267 * 268 * @return string 269 */ 270 public function editBlockConfiguration(Tree $tree, int $block_id): string 271 { 272 $num = $this->getBlockSetting($block_id, 'num', self::DEFAULT_NUMBER); 273 $info_style = $this->getBlockSetting($block_id, 'infoStyle', self::DEFAULT_STYLE); 274 275 $info_styles = [ 276 /* I18N: An option in a list-box */ 277 'list' => I18N::translate('bullet list'), 278 /* I18N: An option in a list-box */ 279 'array' => I18N::translate('compact list'), 280 /* I18N: An option in a list-box */ 281 'table' => I18N::translate('table'), 282 /* I18N: An option in a list-box */ 283 'tagcloud' => I18N::translate('tag cloud'), 284 ]; 285 286 return view('modules/top10_surnames/config', [ 287 'num' => $num, 288 'info_style' => $info_style, 289 'info_styles' => $info_styles, 290 ]); 291 } 292 293 /** 294 * This module assumes the database will use binary collation on the name columns. 295 * Until we convert MySQL databases to use utf8_bin, we need to do this at run-time. 296 * 297 * @param string $column 298 * @param string|null $alias 299 * 300 * @return Expression 301 */ 302 private function binaryColumn(string $column, string $alias = null): Expression 303 { 304 if (DB::connection()->getDriverName() === 'mysql') { 305 $sql = 'CAST(' . $column . ' AS binary)'; 306 } else { 307 $sql = $column; 308 } 309 310 if ($alias !== null) { 311 $sql .= ' AS ' . $alias; 312 } 313 314 return new Expression($sql); 315 } 316} 317