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_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 'surnames' => $top_surnames, 151 'totals' => true, 152 'tree' => $tree, 153 ]); 154 break; 155 156 case 'list': 157 $content = view('lists/surnames-bullet-list', [ 158 'module' => $module, 159 'surnames' => $top_surnames, 160 'totals' => true, 161 'tree' => $tree, 162 ]); 163 break; 164 165 case 'array': 166 $content = view('lists/surnames-compact-list', [ 167 'module' => $module, 168 'surnames' => $top_surnames, 169 'totals' => true, 170 'tree' => $tree, 171 ]); 172 break; 173 174 case 'table': 175 default: 176 uksort($top_surnames, I18N::comparator()); 177 $content = view('lists/surnames-table', [ 178 'families' => false, 179 'module' => $module, 180 'order' => [[1, 'desc']], 181 'surnames' => $top_surnames, 182 'tree' => $tree, 183 ]); 184 break; 185 } 186 187 if ($context !== self::CONTEXT_EMBED) { 188 $num = count($top_surnames); 189 if ($num === 1) { 190 // I18N: i.e. most popular surname. 191 $title = I18N::translate('Top surname'); 192 } else { 193 // I18N: Title for a list of the most common surnames, %s is a number. Note that a separate translation exists when %s is 1 194 $title = I18N::plural('Top %s surname', 'Top %s surnames', $num, I18N::number($num)); 195 } 196 197 return view('modules/block-template', [ 198 'block' => Str::kebab($this->name()), 199 'id' => $block_id, 200 'config_url' => $this->configUrl($tree, $context, $block_id), 201 'title' => $title, 202 'content' => $content, 203 ]); 204 } 205 206 return $content; 207 } 208 209 /** 210 * Should this block load asynchronously using AJAX? 211 * 212 * Simple blocks are faster in-line, more complex ones can be loaded later. 213 * 214 * @return bool 215 */ 216 public function loadAjax(): bool 217 { 218 return false; 219 } 220 221 /** 222 * Can this block be shown on the user’s home page? 223 * 224 * @return bool 225 */ 226 public function isUserBlock(): bool 227 { 228 return true; 229 } 230 231 /** 232 * Can this block be shown on the tree’s home page? 233 * 234 * @return bool 235 */ 236 public function isTreeBlock(): bool 237 { 238 return true; 239 } 240 241 /** 242 * Update the configuration for a block. 243 * 244 * @param ServerRequestInterface $request 245 * @param int $block_id 246 * 247 * @return void 248 */ 249 public function saveBlockConfiguration(ServerRequestInterface $request, int $block_id): void 250 { 251 $num = Validator::parsedBody($request)->integer('num'); 252 $info_style = Validator::parsedBody($request)->string('infoStyle'); 253 254 $this->setBlockSetting($block_id, 'num', (string) $num); 255 $this->setBlockSetting($block_id, 'infoStyle', $info_style); 256 } 257 258 /** 259 * An HTML form to edit block settings 260 * 261 * @param Tree $tree 262 * @param int $block_id 263 * 264 * @return string 265 */ 266 public function editBlockConfiguration(Tree $tree, int $block_id): string 267 { 268 $num = $this->getBlockSetting($block_id, 'num', self::DEFAULT_NUMBER); 269 $info_style = $this->getBlockSetting($block_id, 'infoStyle', self::DEFAULT_STYLE); 270 271 $info_styles = [ 272 /* I18N: An option in a list-box */ 273 'list' => I18N::translate('bullet list'), 274 /* I18N: An option in a list-box */ 275 'array' => I18N::translate('compact list'), 276 /* I18N: An option in a list-box */ 277 'table' => I18N::translate('table'), 278 /* I18N: An option in a list-box */ 279 'tagcloud' => I18N::translate('tag cloud'), 280 ]; 281 282 return view('modules/top10_surnames/config', [ 283 'num' => $num, 284 'info_style' => $info_style, 285 'info_styles' => $info_styles, 286 ]); 287 } 288 289 /** 290 * This module assumes the database will use binary collation on the name columns. 291 * Until we convert MySQL databases to use utf8_bin, we need to do this at run-time. 292 * 293 * @param string $column 294 * @param string|null $alias 295 * 296 * @return Expression 297 */ 298 private function binaryColumn(string $column, string $alias = null): Expression 299 { 300 if (DB::connection()->getDriverName() === 'mysql') { 301 $sql = 'CAST(' . $column . ' AS binary)'; 302 } else { 303 $sql = $column; 304 } 305 306 if ($alias !== null) { 307 $sql .= ' AS ' . $alias; 308 } 309 310 return new Expression($sql); 311 } 312} 313