xref: /webtrees/app/Module/TopSurnamesModule.php (revision 6f4ec3cadc983f0a7294108c634bef48846b4311)
18c2e8227SGreg Roach<?php
23976b470SGreg Roach
38c2e8227SGreg Roach/**
48c2e8227SGreg Roach * webtrees: online genealogy
5d11be702SGreg Roach * Copyright (C) 2023 webtrees development team
68c2e8227SGreg Roach * This program is free software: you can redistribute it and/or modify
78c2e8227SGreg Roach * it under the terms of the GNU General Public License as published by
88c2e8227SGreg Roach * the Free Software Foundation, either version 3 of the License, or
98c2e8227SGreg Roach * (at your option) any later version.
108c2e8227SGreg Roach * This program is distributed in the hope that it will be useful,
118c2e8227SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
128c2e8227SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
138c2e8227SGreg Roach * GNU General Public License for more details.
148c2e8227SGreg Roach * You should have received a copy of the GNU General Public License
1589f7189bSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>.
168c2e8227SGreg Roach */
17fcfa147eSGreg Roach
18e7f56f2aSGreg Roachdeclare(strict_types=1);
19e7f56f2aSGreg Roach
2076692c8bSGreg Roachnamespace Fisharebest\Webtrees\Module;
2176692c8bSGreg Roach
220e62c4b8SGreg Roachuse Fisharebest\Webtrees\Auth;
23*6f4ec3caSGreg Roachuse Fisharebest\Webtrees\DB;
240e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N;
258fb4e87cSGreg Roachuse Fisharebest\Webtrees\Individual;
2667992b6aSRichard Cisseeuse Fisharebest\Webtrees\Services\ModuleService;
2709482a55SGreg Roachuse Fisharebest\Webtrees\Tree;
28748dbe15SGreg Roachuse Fisharebest\Webtrees\Validator;
29a69f5655SGreg Roachuse Illuminate\Database\Query\Expression;
301e7a7a28SGreg Roachuse Illuminate\Support\Str;
316ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
328c2e8227SGreg Roach
33b86c018aSGreg Roachuse function array_slice;
34632b1edfSGreg Roachuse function array_sum;
35cd1ec0d0SGreg Roachuse function count;
36cd1ec0d0SGreg Roachuse function extract;
37cd1ec0d0SGreg Roachuse function uasort;
38cd1ec0d0SGreg Roachuse function uksort;
39cd1ec0d0SGreg Roachuse function view;
40cd1ec0d0SGreg Roach
41cd1ec0d0SGreg Roachuse const EXTR_OVERWRITE;
42632b1edfSGreg Roach
438c2e8227SGreg Roach/**
448c2e8227SGreg Roach * Class TopSurnamesModule
458c2e8227SGreg Roach */
4637eb8894SGreg Roachclass TopSurnamesModule extends AbstractModule implements ModuleBlockInterface
47c1010edaSGreg Roach{
4849a243cbSGreg Roach    use ModuleBlockTrait;
4949a243cbSGreg Roach
50c385536dSGreg Roach    // Default values for new blocks.
5116d6367aSGreg Roach    private const DEFAULT_NUMBER = '10';
5216d6367aSGreg Roach    private const DEFAULT_STYLE  = 'table';
53c385536dSGreg Roach
5443f2f523SGreg Roach    private ModuleService $module_service;
552e4f3c66SGreg Roach
562e4f3c66SGreg Roach    /**
572e4f3c66SGreg Roach     * @param ModuleService $module_service
582e4f3c66SGreg Roach     */
592e4f3c66SGreg Roach    public function __construct(ModuleService $module_service)
602e4f3c66SGreg Roach    {
612e4f3c66SGreg Roach        $this->module_service = $module_service;
622e4f3c66SGreg Roach    }
632e4f3c66SGreg Roach
6476692c8bSGreg Roach    /**
650cfd6963SGreg Roach     * How should this module be identified in the control panel, etc.?
6676692c8bSGreg Roach     *
6776692c8bSGreg Roach     * @return string
6876692c8bSGreg Roach     */
6949a243cbSGreg Roach    public function title(): string
70c1010edaSGreg Roach    {
71bbb76c12SGreg Roach        /* I18N: Name of a module. Top=Most common */
72bbb76c12SGreg Roach        return I18N::translate('Top surnames');
738c2e8227SGreg Roach    }
748c2e8227SGreg Roach
7576692c8bSGreg Roach    /**
7676692c8bSGreg Roach     * A sentence describing what this module does.
7776692c8bSGreg Roach     *
7876692c8bSGreg Roach     * @return string
7976692c8bSGreg Roach     */
8049a243cbSGreg Roach    public function description(): string
81c1010edaSGreg Roach    {
82bbb76c12SGreg Roach        /* I18N: Description of the “Top surnames” module */
83bbb76c12SGreg Roach        return I18N::translate('A list of the most popular surnames.');
848c2e8227SGreg Roach    }
858c2e8227SGreg Roach
8676692c8bSGreg Roach    /**
8776692c8bSGreg Roach     * Generate the HTML content of this block.
8876692c8bSGreg Roach     *
89e490cd80SGreg Roach     * @param Tree                 $tree
9076692c8bSGreg Roach     * @param int                  $block_id
913caaa4d2SGreg Roach     * @param string               $context
9276d39c55SGreg Roach     * @param array<string,string> $config
9376692c8bSGreg Roach     *
9476692c8bSGreg Roach     * @return string
9576692c8bSGreg Roach     */
963caaa4d2SGreg Roach    public function getBlock(Tree $tree, int $block_id, string $context, array $config = []): string
97c1010edaSGreg Roach    {
9880536c2dSGreg Roach        $num        = (int) $this->getBlockSetting($block_id, 'num', self::DEFAULT_NUMBER);
99b86c018aSGreg Roach        $info_style = $this->getBlockSetting($block_id, 'infoStyle', self::DEFAULT_STYLE);
1008c2e8227SGreg Roach
1013caaa4d2SGreg Roach        extract($config, EXTR_OVERWRITE);
1028c2e8227SGreg Roach
103b86c018aSGreg Roach        $query = DB::table('name')
1048d7ed87eSGreg Roach            ->where('n_file', '=', $tree->id())
1058d7ed87eSGreg Roach            ->where('n_type', '<>', '_MARNM')
106b86c018aSGreg Roach            ->where('n_surn', '<>', '')
107b86c018aSGreg Roach            ->where('n_surn', '<>', Individual::NOMEN_NESCIO)
108b86c018aSGreg Roach            ->select([
109b86c018aSGreg Roach                $this->binaryColumn('n_surn', 'n_surn'),
110b86c018aSGreg Roach                $this->binaryColumn('n_surname', 'n_surname'),
111b86c018aSGreg Roach                new Expression('COUNT(*) AS total'),
112b86c018aSGreg Roach            ])
113b86c018aSGreg Roach            ->groupBy([
114b86c018aSGreg Roach                $this->binaryColumn('n_surn'),
115b86c018aSGreg Roach                $this->binaryColumn('n_surname'),
116b86c018aSGreg Roach            ]);
1178c2e8227SGreg Roach
118b86c018aSGreg Roach        /** @var array<array<int>> $top_surnames */
119b86c018aSGreg Roach        $top_surnames = [];
12080536c2dSGreg Roach
121b86c018aSGreg Roach        foreach ($query->get() as $row) {
122b86c018aSGreg Roach            $row->n_surn = $row->n_surn === '' ? $row->n_surname : $row->n_surn;
123b86c018aSGreg Roach            $row->n_surn = I18N::strtoupper(I18N::language()->normalize($row->n_surn));
124f8c9edfeSGreg Roach
125b86c018aSGreg Roach            $top_surnames[$row->n_surn][$row->n_surname] ??= 0;
126b86c018aSGreg Roach            $top_surnames[$row->n_surn][$row->n_surname] += (int) $row->total;
1278c2e8227SGreg Roach        }
1288c2e8227SGreg Roach
129b86c018aSGreg Roach        uasort($top_surnames, static fn (array $x, array $y): int => array_sum($y) <=> array_sum($x));
130b86c018aSGreg Roach
131b86c018aSGreg Roach        $top_surnames = array_slice($top_surnames, 0, $num, true);
132b86c018aSGreg Roach
1332e4f3c66SGreg Roach        // Find a module providing individual lists.
1342e4f3c66SGreg Roach        $module = $this->module_service
1352e4f3c66SGreg Roach            ->findByComponent(ModuleListInterface::class, $tree, Auth::user())
1362e4f3c66SGreg Roach            ->first(static function (ModuleInterface $module): bool {
137ba738272SGreg Roach                // The family list extends the individual list
138ba738272SGreg Roach                return
139ba738272SGreg Roach                    $module instanceof IndividualListModule &&
140ba738272SGreg Roach                    !$module instanceof FamilyListModule;
14167992b6aSRichard Cissee            });
14267992b6aSRichard Cissee
143b86c018aSGreg Roach        switch ($info_style) {
1448c2e8227SGreg Roach            case 'tagcloud':
145b86c018aSGreg Roach                uksort($top_surnames, I18N::comparator());
146cd1ec0d0SGreg Roach                $content = view('lists/surnames-tag-cloud', [
147cd1ec0d0SGreg Roach                    'module'   => $module,
1487a3804a2SGreg Roach                    'params'   => [],
149b86c018aSGreg Roach                    'surnames' => $top_surnames,
150cd1ec0d0SGreg Roach                    'totals'   => true,
151cd1ec0d0SGreg Roach                    'tree'     => $tree,
152cd1ec0d0SGreg Roach                ]);
1538c2e8227SGreg Roach                break;
154cd1ec0d0SGreg Roach
1558c2e8227SGreg Roach            case 'list':
156cd1ec0d0SGreg Roach                $content = view('lists/surnames-bullet-list', [
157cd1ec0d0SGreg Roach                    'module'   => $module,
1587a3804a2SGreg Roach                    'params'   => [],
159b86c018aSGreg Roach                    'surnames' => $top_surnames,
160cd1ec0d0SGreg Roach                    'totals'   => true,
161cd1ec0d0SGreg Roach                    'tree'     => $tree,
162cd1ec0d0SGreg Roach                ]);
1638c2e8227SGreg Roach                break;
164cd1ec0d0SGreg Roach
1658c2e8227SGreg Roach            case 'array':
166cd1ec0d0SGreg Roach                $content = view('lists/surnames-compact-list', [
167cd1ec0d0SGreg Roach                    'module'   => $module,
1687a3804a2SGreg Roach                    'params'   => [],
169b86c018aSGreg Roach                    'surnames' => $top_surnames,
170cd1ec0d0SGreg Roach                    'totals'   => true,
171cd1ec0d0SGreg Roach                    'tree'     => $tree,
172cd1ec0d0SGreg Roach                ]);
1738c2e8227SGreg Roach                break;
174cd1ec0d0SGreg Roach
1758c2e8227SGreg Roach            case 'table':
1768c2e8227SGreg Roach            default:
177b86c018aSGreg Roach                uksort($top_surnames, I18N::comparator());
178cf184a4dSGreg Roach                $content = view('lists/surnames-table', [
17967992b6aSRichard Cissee                    'families' => false,
180c9128110SGreg Roach                    'module'   => $module,
181c9128110SGreg Roach                    'order'    => [[1, 'desc']],
1827a3804a2SGreg Roach                    'params'   => [],
183b86c018aSGreg Roach                    'surnames' => $top_surnames,
184e490cd80SGreg Roach                    'tree'     => $tree,
1856ab54c76SGreg Roach                ]);
1868c2e8227SGreg Roach                break;
1878c2e8227SGreg Roach        }
1888c2e8227SGreg Roach
1893caaa4d2SGreg Roach        if ($context !== self::CONTEXT_EMBED) {
19080536c2dSGreg Roach            $num = count($top_surnames);
19180536c2dSGreg Roach            if ($num === 1) {
1928cbbfdceSGreg Roach                // I18N: i.e. most popular surname.
1938cbbfdceSGreg Roach                $title = I18N::translate('Top surname');
1948cbbfdceSGreg Roach            } else {
1958cbbfdceSGreg Roach                // I18N: Title for a list of the most common surnames, %s is a number. Note that a separate translation exists when %s is 1
1968cbbfdceSGreg Roach                $title = I18N::plural('Top %s surname', 'Top %s surnames', $num, I18N::number($num));
1978cbbfdceSGreg Roach            }
1988cbbfdceSGreg Roach
199147e99aaSGreg Roach            return view('modules/block-template', [
2001e7a7a28SGreg Roach                'block'      => Str::kebab($this->name()),
2019c6524dcSGreg Roach                'id'         => $block_id,
2023caaa4d2SGreg Roach                'config_url' => $this->configUrl($tree, $context, $block_id),
2038cbbfdceSGreg Roach                'title'      => $title,
2049c6524dcSGreg Roach                'content'    => $content,
2059c6524dcSGreg Roach            ]);
2068c2e8227SGreg Roach        }
207b2ce94c6SRico Sonntag
208b2ce94c6SRico Sonntag        return $content;
2098c2e8227SGreg Roach    }
2108c2e8227SGreg Roach
2113caaa4d2SGreg Roach    /**
2123caaa4d2SGreg Roach     * Should this block load asynchronously using AJAX?
2133caaa4d2SGreg Roach     *
2143caaa4d2SGreg Roach     * Simple blocks are faster in-line, more complex ones can be loaded later.
2153caaa4d2SGreg Roach     *
2163caaa4d2SGreg Roach     * @return bool
2173caaa4d2SGreg Roach     */
218c1010edaSGreg Roach    public function loadAjax(): bool
219c1010edaSGreg Roach    {
2204ecf4f82SGreg Roach        return false;
2218c2e8227SGreg Roach    }
2228c2e8227SGreg Roach
2233caaa4d2SGreg Roach    /**
2243caaa4d2SGreg Roach     * Can this block be shown on the user’s home page?
2253caaa4d2SGreg Roach     *
2263caaa4d2SGreg Roach     * @return bool
2273caaa4d2SGreg Roach     */
228c1010edaSGreg Roach    public function isUserBlock(): bool
229c1010edaSGreg Roach    {
2308c2e8227SGreg Roach        return true;
2318c2e8227SGreg Roach    }
2328c2e8227SGreg Roach
2333caaa4d2SGreg Roach    /**
2343caaa4d2SGreg Roach     * Can this block be shown on the tree’s home page?
2353caaa4d2SGreg Roach     *
2363caaa4d2SGreg Roach     * @return bool
2373caaa4d2SGreg Roach     */
23863276d8fSGreg Roach    public function isTreeBlock(): bool
239c1010edaSGreg Roach    {
2408c2e8227SGreg Roach        return true;
2418c2e8227SGreg Roach    }
2428c2e8227SGreg Roach
24376692c8bSGreg Roach    /**
244a45f9889SGreg Roach     * Update the configuration for a block.
245a45f9889SGreg Roach     *
2466ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
247a45f9889SGreg Roach     * @param int     $block_id
248a45f9889SGreg Roach     *
249a45f9889SGreg Roach     * @return void
250a45f9889SGreg Roach     */
2516ccdf4f0SGreg Roach    public function saveBlockConfiguration(ServerRequestInterface $request, int $block_id): void
252a45f9889SGreg Roach    {
253748dbe15SGreg Roach        $num        = Validator::parsedBody($request)->integer('num');
254748dbe15SGreg Roach        $info_style = Validator::parsedBody($request)->string('infoStyle');
255c50a90beSGreg Roach
256748dbe15SGreg Roach        $this->setBlockSetting($block_id, 'num', (string) $num);
257748dbe15SGreg Roach        $this->setBlockSetting($block_id, 'infoStyle', $info_style);
258a45f9889SGreg Roach    }
259a45f9889SGreg Roach
260a45f9889SGreg Roach    /**
26176692c8bSGreg Roach     * An HTML form to edit block settings
26276692c8bSGreg Roach     *
263e490cd80SGreg Roach     * @param Tree $tree
26476692c8bSGreg Roach     * @param int  $block_id
265a9430be8SGreg Roach     *
2663caaa4d2SGreg Roach     * @return string
26776692c8bSGreg Roach     */
2683caaa4d2SGreg Roach    public function editBlockConfiguration(Tree $tree, int $block_id): string
269c1010edaSGreg Roach    {
270c385536dSGreg Roach        $num        = $this->getBlockSetting($block_id, 'num', self::DEFAULT_NUMBER);
2717c2c99faSGreg Roach        $info_style = $this->getBlockSetting($block_id, 'infoStyle', self::DEFAULT_STYLE);
2728c2e8227SGreg Roach
273c385536dSGreg Roach        $info_styles = [
274bbb76c12SGreg Roach            /* I18N: An option in a list-box */
275bbb76c12SGreg Roach            'list'     => I18N::translate('bullet list'),
276bbb76c12SGreg Roach            /* I18N: An option in a list-box */
277bbb76c12SGreg Roach            'array'    => I18N::translate('compact list'),
278bbb76c12SGreg Roach            /* I18N: An option in a list-box */
279bbb76c12SGreg Roach            'table'    => I18N::translate('table'),
280bbb76c12SGreg Roach            /* I18N: An option in a list-box */
281bbb76c12SGreg Roach            'tagcloud' => I18N::translate('tag cloud'),
282c385536dSGreg Roach        ];
2838c2e8227SGreg Roach
2843caaa4d2SGreg Roach        return view('modules/top10_surnames/config', [
285c385536dSGreg Roach            'num'         => $num,
286d6837001SGreg Roach            'info_style'  => $info_style,
287c385536dSGreg Roach            'info_styles' => $info_styles,
288c385536dSGreg Roach        ]);
2898c2e8227SGreg Roach    }
290b86c018aSGreg Roach
291b86c018aSGreg Roach    /**
292b86c018aSGreg Roach     * This module assumes the database will use binary collation on the name columns.
293b86c018aSGreg Roach     * Until we convert MySQL databases to use utf8_bin, we need to do this at run-time.
294b86c018aSGreg Roach     *
295b86c018aSGreg Roach     * @param string      $column
296b86c018aSGreg Roach     * @param string|null $alias
297b86c018aSGreg Roach     *
298b86c018aSGreg Roach     * @return Expression
299b86c018aSGreg Roach     */
300b86c018aSGreg Roach    private function binaryColumn(string $column, string $alias = null): Expression
301b86c018aSGreg Roach    {
302b86c018aSGreg Roach        if (DB::connection()->getDriverName() === 'mysql') {
303b86c018aSGreg Roach            $sql = 'CAST(' . $column . ' AS binary)';
304b86c018aSGreg Roach        } else {
305b86c018aSGreg Roach            $sql = $column;
306b86c018aSGreg Roach        }
307b86c018aSGreg Roach
308b86c018aSGreg Roach        if ($alias !== null) {
309b86c018aSGreg Roach            $sql .= ' AS ' . $alias;
310b86c018aSGreg Roach        }
311b86c018aSGreg Roach
312b86c018aSGreg Roach        return new Expression($sql);
313b86c018aSGreg Roach    }
3148c2e8227SGreg Roach}
315