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