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\DB; 24use Fisharebest\Webtrees\I18N; 25use Fisharebest\Webtrees\Individual; 26use Fisharebest\Webtrees\Services\ModuleService; 27use Fisharebest\Webtrees\Tree; 28use Fisharebest\Webtrees\Validator; 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 * @param ModuleService $module_service 58 */ 59 public function __construct(ModuleService $module_service) 60 { 61 $this->module_service = $module_service; 62 } 63 64 /** 65 * How should this module be identified in the control panel, etc.? 66 * 67 * @return string 68 */ 69 public function title(): string 70 { 71 /* I18N: Name of a module. Top=Most common */ 72 return I18N::translate('Top surnames'); 73 } 74 75 /** 76 * A sentence describing what this module does. 77 * 78 * @return string 79 */ 80 public function description(): string 81 { 82 /* I18N: Description of the “Top surnames” module */ 83 return I18N::translate('A list of the most popular surnames.'); 84 } 85 86 /** 87 * Generate the HTML content of this block. 88 * 89 * @param Tree $tree 90 * @param int $block_id 91 * @param string $context 92 * @param array<string,string> $config 93 * 94 * @return string 95 */ 96 public function getBlock(Tree $tree, int $block_id, string $context, array $config = []): string 97 { 98 $num = (int) $this->getBlockSetting($block_id, 'num', self::DEFAULT_NUMBER); 99 $info_style = $this->getBlockSetting($block_id, 'infoStyle', self::DEFAULT_STYLE); 100 101 extract($config, EXTR_OVERWRITE); 102 103 $query = DB::table('name') 104 ->where('n_file', '=', $tree->id()) 105 ->where('n_type', '<>', '_MARNM') 106 ->where('n_surn', '<>', '') 107 ->where('n_surn', '<>', Individual::NOMEN_NESCIO) 108 ->select([ 109 DB::binaryColumn('n_surn', 'n_surn'), 110 DB::binaryColumn('n_surname', 'n_surname'), 111 new Expression('COUNT(*) AS total'), 112 ]) 113 ->groupBy([ 114 DB::binaryColumn('n_surn'), 115 DB::binaryColumn('n_surname'), 116 ]); 117 118 /** @var array<array<int>> $top_surnames */ 119 $top_surnames = []; 120 121 foreach ($query->get() as $row) { 122 $row->n_surn = $row->n_surn === '' ? $row->n_surname : $row->n_surn; 123 $row->n_surn = I18N::strtoupper(I18N::language()->normalize($row->n_surn)); 124 125 $top_surnames[$row->n_surn][$row->n_surname] ??= 0; 126 $top_surnames[$row->n_surn][$row->n_surname] += (int) $row->total; 127 } 128 129 uasort($top_surnames, static fn (array $x, array $y): int => array_sum($y) <=> array_sum($x)); 130 131 $top_surnames = array_slice($top_surnames, 0, $num, true); 132 133 // Find a module providing individual lists. 134 $module = $this->module_service 135 ->findByComponent(ModuleListInterface::class, $tree, Auth::user()) 136 ->first(static fn (ModuleInterface $module): bool => $module instanceof IndividualListModule); 137 138 switch ($info_style) { 139 case 'tagcloud': 140 uksort($top_surnames, I18N::comparator()); 141 $content = view('lists/surnames-tag-cloud', [ 142 'module' => $module, 143 'params' => [], 144 'surnames' => $top_surnames, 145 'totals' => true, 146 'tree' => $tree, 147 ]); 148 break; 149 150 case 'list': 151 $content = view('lists/surnames-bullet-list', [ 152 'module' => $module, 153 'params' => [], 154 'surnames' => $top_surnames, 155 'totals' => true, 156 'tree' => $tree, 157 ]); 158 break; 159 160 case 'array': 161 $content = view('lists/surnames-compact-list', [ 162 'module' => $module, 163 'params' => [], 164 'surnames' => $top_surnames, 165 'totals' => true, 166 'tree' => $tree, 167 ]); 168 break; 169 170 case 'table': 171 default: 172 uksort($top_surnames, I18N::comparator()); 173 $content = view('lists/surnames-table', [ 174 'families' => false, 175 'module' => $module, 176 'order' => [[1, 'desc']], 177 'params' => [], 178 'surnames' => $top_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