xref: /webtrees/app/Module/TopSurnamesModule.php (revision 748dbe155a6d19d66918ad136947fa23ee8f8469)
18c2e8227SGreg Roach<?php
23976b470SGreg Roach
38c2e8227SGreg Roach/**
48c2e8227SGreg Roach * webtrees: online genealogy
55bfc6897SGreg Roach * Copyright (C) 2022 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;
230e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N;
248fb4e87cSGreg Roachuse Fisharebest\Webtrees\Individual;
2567992b6aSRichard Cisseeuse Fisharebest\Webtrees\Services\ModuleService;
2609482a55SGreg Roachuse Fisharebest\Webtrees\Tree;
27*748dbe15SGreg Roachuse Fisharebest\Webtrees\Validator;
288d7ed87eSGreg Roachuse Illuminate\Database\Capsule\Manager as DB;
29a69f5655SGreg Roachuse Illuminate\Database\Query\Expression;
301e7a7a28SGreg Roachuse Illuminate\Support\Str;
316ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
328c2e8227SGreg Roach
33632b1edfSGreg Roachuse function array_sum;
34cd1ec0d0SGreg Roachuse function count;
35cd1ec0d0SGreg Roachuse function extract;
36cd1ec0d0SGreg Roachuse function uasort;
37cd1ec0d0SGreg Roachuse function uksort;
38cd1ec0d0SGreg Roachuse function view;
39cd1ec0d0SGreg Roach
40cd1ec0d0SGreg Roachuse const EXTR_OVERWRITE;
41*748dbe15SGreg Roachuse const PHP_INT_MAX;
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     * TopSurnamesModule constructor.
582e4f3c66SGreg Roach     *
592e4f3c66SGreg Roach     * @param ModuleService $module_service
602e4f3c66SGreg Roach     */
612e4f3c66SGreg Roach    public function __construct(ModuleService $module_service)
622e4f3c66SGreg Roach    {
632e4f3c66SGreg Roach        $this->module_service = $module_service;
642e4f3c66SGreg Roach    }
652e4f3c66SGreg Roach
6676692c8bSGreg Roach    /**
670cfd6963SGreg Roach     * How should this module be identified in the control panel, etc.?
6876692c8bSGreg Roach     *
6976692c8bSGreg Roach     * @return string
7076692c8bSGreg Roach     */
7149a243cbSGreg Roach    public function title(): string
72c1010edaSGreg Roach    {
73bbb76c12SGreg Roach        /* I18N: Name of a module. Top=Most common */
74bbb76c12SGreg Roach        return I18N::translate('Top surnames');
758c2e8227SGreg Roach    }
768c2e8227SGreg Roach
7776692c8bSGreg Roach    /**
7876692c8bSGreg Roach     * A sentence describing what this module does.
7976692c8bSGreg Roach     *
8076692c8bSGreg Roach     * @return string
8176692c8bSGreg Roach     */
8249a243cbSGreg Roach    public function description(): string
83c1010edaSGreg Roach    {
84bbb76c12SGreg Roach        /* I18N: Description of the “Top surnames” module */
85bbb76c12SGreg Roach        return I18N::translate('A list of the most popular surnames.');
868c2e8227SGreg Roach    }
878c2e8227SGreg Roach
8876692c8bSGreg Roach    /**
8976692c8bSGreg Roach     * Generate the HTML content of this block.
9076692c8bSGreg Roach     *
91e490cd80SGreg Roach     * @param Tree                 $tree
9276692c8bSGreg Roach     * @param int                  $block_id
933caaa4d2SGreg Roach     * @param string               $context
9476d39c55SGreg Roach     * @param array<string,string> $config
9576692c8bSGreg Roach     *
9676692c8bSGreg Roach     * @return string
9776692c8bSGreg Roach     */
983caaa4d2SGreg Roach    public function getBlock(Tree $tree, int $block_id, string $context, array $config = []): string
99c1010edaSGreg Roach    {
10080536c2dSGreg Roach        $num       = (int) $this->getBlockSetting($block_id, 'num', self::DEFAULT_NUMBER);
101c385536dSGreg Roach        $infoStyle = $this->getBlockSetting($block_id, 'infoStyle', self::DEFAULT_STYLE);
1028c2e8227SGreg Roach
1033caaa4d2SGreg Roach        extract($config, EXTR_OVERWRITE);
1048c2e8227SGreg Roach
10580536c2dSGreg Roach        // Use the count of base surnames.
1068d7ed87eSGreg Roach        $top_surnames = DB::table('name')
1078d7ed87eSGreg Roach            ->where('n_file', '=', $tree->id())
1088d7ed87eSGreg Roach            ->where('n_type', '<>', '_MARNM')
1098fb4e87cSGreg Roach            ->whereNotIn('n_surn', [Individual::NOMEN_NESCIO, ''])
1107f5c2944SGreg Roach            ->groupBy(['n_surn'])
111a69f5655SGreg Roach            ->orderByDesc(new Expression('COUNT(n_surn)'))
1128d7ed87eSGreg Roach            ->take($num)
1138d7ed87eSGreg Roach            ->pluck('n_surn');
1148c2e8227SGreg Roach
11513abd6f3SGreg Roach        $all_surnames = [];
11680536c2dSGreg Roach
1178d7ed87eSGreg Roach        foreach ($top_surnames as $top_surname) {
1188d7ed87eSGreg Roach            $variants = DB::table('name')
1198d7ed87eSGreg Roach                ->where('n_file', '=', $tree->id())
120a69f5655SGreg Roach                ->where(new Expression('n_surn /*! COLLATE utf8_bin */'), '=', $top_surname)
1214a9a6095SGreg Roach                ->groupBy([new Expression('n_surname /*! COLLATE utf8_bin */')])
122a69f5655SGreg Roach                ->select([new Expression('n_surname /*! COLLATE utf8_bin */ AS surname'), new Expression('count(*) AS total')])
1238d7ed87eSGreg Roach                ->pluck('total', 'surname')
1244c78e066SGreg Roach                ->map(static fn (string $n): int => (int) $n)
1258d7ed87eSGreg Roach                ->all();
126f8c9edfeSGreg Roach
12780536c2dSGreg Roach            $all_surnames[$top_surname] = $variants;
1288c2e8227SGreg Roach        }
1298c2e8227SGreg Roach
1302e4f3c66SGreg Roach        // Find a module providing individual lists.
1312e4f3c66SGreg Roach        $module = $this->module_service
1322e4f3c66SGreg Roach            ->findByComponent(ModuleListInterface::class, $tree, Auth::user())
1332e4f3c66SGreg Roach            ->first(static function (ModuleInterface $module): bool {
134ba738272SGreg Roach                // The family list extends the individual list
135ba738272SGreg Roach                return
136ba738272SGreg Roach                    $module instanceof IndividualListModule &&
137ba738272SGreg Roach                    !$module instanceof FamilyListModule;
13867992b6aSRichard Cissee            });
13967992b6aSRichard Cissee
1408c2e8227SGreg Roach        switch ($infoStyle) {
1418c2e8227SGreg Roach            case 'tagcloud':
14237646143SGreg Roach                uksort($all_surnames, I18N::comparator());
143cd1ec0d0SGreg Roach                $content = view('lists/surnames-tag-cloud', [
144cd1ec0d0SGreg Roach                    'module'   => $module,
145cd1ec0d0SGreg Roach                    'surnames' => $all_surnames,
146cd1ec0d0SGreg Roach                    'totals'   => true,
147cd1ec0d0SGreg Roach                    'tree'     => $tree,
148cd1ec0d0SGreg Roach                ]);
1498c2e8227SGreg Roach                break;
150cd1ec0d0SGreg Roach
1518c2e8227SGreg Roach            case 'list':
15205babb96SGreg Roach                uasort($all_surnames, static fn (array $a, array $b): int => array_sum($b) <=> array_sum($a));
153cd1ec0d0SGreg Roach                $content = view('lists/surnames-bullet-list', [
154cd1ec0d0SGreg Roach                    'module'   => $module,
155cd1ec0d0SGreg Roach                    'surnames' => $all_surnames,
156cd1ec0d0SGreg Roach                    'totals'   => true,
157cd1ec0d0SGreg Roach                    'tree'     => $tree,
158cd1ec0d0SGreg Roach                ]);
1598c2e8227SGreg Roach                break;
160cd1ec0d0SGreg Roach
1618c2e8227SGreg Roach            case 'array':
16205babb96SGreg Roach                uasort($all_surnames, static fn (array $a, array $b): int => array_sum($b) <=> array_sum($a));
163cd1ec0d0SGreg Roach                $content = view('lists/surnames-compact-list', [
164cd1ec0d0SGreg Roach                    'module'   => $module,
165cd1ec0d0SGreg Roach                    'surnames' => $all_surnames,
166cd1ec0d0SGreg Roach                    'totals'   => true,
167cd1ec0d0SGreg Roach                    'tree'     => $tree,
168cd1ec0d0SGreg Roach                ]);
1698c2e8227SGreg Roach                break;
170cd1ec0d0SGreg Roach
1718c2e8227SGreg Roach            case 'table':
1728c2e8227SGreg Roach            default:
1734a9a6095SGreg Roach                uksort($all_surnames, I18N::comparator());
174cf184a4dSGreg Roach                $content = view('lists/surnames-table', [
17567992b6aSRichard Cissee                    'families' => false,
176c9128110SGreg Roach                    'module'   => $module,
177c9128110SGreg Roach                    'order'    => [[1, 'desc']],
178c9128110SGreg Roach                    'surnames' => $all_surnames,
179e490cd80SGreg Roach                    'tree'     => $tree,
1806ab54c76SGreg Roach                ]);
1818c2e8227SGreg Roach                break;
1828c2e8227SGreg Roach        }
1838c2e8227SGreg Roach
1843caaa4d2SGreg Roach        if ($context !== self::CONTEXT_EMBED) {
18580536c2dSGreg Roach            $num = count($top_surnames);
18680536c2dSGreg Roach            if ($num === 1) {
1878cbbfdceSGreg Roach                // I18N: i.e. most popular surname.
1888cbbfdceSGreg Roach                $title = I18N::translate('Top surname');
1898cbbfdceSGreg Roach            } else {
1908cbbfdceSGreg 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
1918cbbfdceSGreg Roach                $title = I18N::plural('Top %s surname', 'Top %s surnames', $num, I18N::number($num));
1928cbbfdceSGreg Roach            }
1938cbbfdceSGreg Roach
194147e99aaSGreg Roach            return view('modules/block-template', [
1951e7a7a28SGreg Roach                'block'      => Str::kebab($this->name()),
1969c6524dcSGreg Roach                'id'         => $block_id,
1973caaa4d2SGreg Roach                'config_url' => $this->configUrl($tree, $context, $block_id),
1988cbbfdceSGreg Roach                'title'      => $title,
1999c6524dcSGreg Roach                'content'    => $content,
2009c6524dcSGreg Roach            ]);
2018c2e8227SGreg Roach        }
202b2ce94c6SRico Sonntag
203b2ce94c6SRico Sonntag        return $content;
2048c2e8227SGreg Roach    }
2058c2e8227SGreg Roach
2063caaa4d2SGreg Roach    /**
2073caaa4d2SGreg Roach     * Should this block load asynchronously using AJAX?
2083caaa4d2SGreg Roach     *
2093caaa4d2SGreg Roach     * Simple blocks are faster in-line, more complex ones can be loaded later.
2103caaa4d2SGreg Roach     *
2113caaa4d2SGreg Roach     * @return bool
2123caaa4d2SGreg Roach     */
213c1010edaSGreg Roach    public function loadAjax(): bool
214c1010edaSGreg Roach    {
2154ecf4f82SGreg Roach        return false;
2168c2e8227SGreg Roach    }
2178c2e8227SGreg Roach
2183caaa4d2SGreg Roach    /**
2193caaa4d2SGreg Roach     * Can this block be shown on the user’s home page?
2203caaa4d2SGreg Roach     *
2213caaa4d2SGreg Roach     * @return bool
2223caaa4d2SGreg Roach     */
223c1010edaSGreg Roach    public function isUserBlock(): bool
224c1010edaSGreg Roach    {
2258c2e8227SGreg Roach        return true;
2268c2e8227SGreg Roach    }
2278c2e8227SGreg Roach
2283caaa4d2SGreg Roach    /**
2293caaa4d2SGreg Roach     * Can this block be shown on the tree’s home page?
2303caaa4d2SGreg Roach     *
2313caaa4d2SGreg Roach     * @return bool
2323caaa4d2SGreg Roach     */
23363276d8fSGreg Roach    public function isTreeBlock(): bool
234c1010edaSGreg Roach    {
2358c2e8227SGreg Roach        return true;
2368c2e8227SGreg Roach    }
2378c2e8227SGreg Roach
23876692c8bSGreg Roach    /**
239a45f9889SGreg Roach     * Update the configuration for a block.
240a45f9889SGreg Roach     *
2416ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
242a45f9889SGreg Roach     * @param int     $block_id
243a45f9889SGreg Roach     *
244a45f9889SGreg Roach     * @return void
245a45f9889SGreg Roach     */
2466ccdf4f0SGreg Roach    public function saveBlockConfiguration(ServerRequestInterface $request, int $block_id): void
247a45f9889SGreg Roach    {
248*748dbe15SGreg Roach        $num        = Validator::parsedBody($request)->integer('num');
249*748dbe15SGreg Roach        $info_style = Validator::parsedBody($request)->string('infoStyle');
250c50a90beSGreg Roach
251*748dbe15SGreg Roach        $this->setBlockSetting($block_id, 'num', (string) $num);
252*748dbe15SGreg Roach        $this->setBlockSetting($block_id, 'infoStyle', $info_style);
253a45f9889SGreg Roach    }
254a45f9889SGreg Roach
255a45f9889SGreg Roach    /**
25676692c8bSGreg Roach     * An HTML form to edit block settings
25776692c8bSGreg Roach     *
258e490cd80SGreg Roach     * @param Tree $tree
25976692c8bSGreg Roach     * @param int  $block_id
260a9430be8SGreg Roach     *
2613caaa4d2SGreg Roach     * @return string
26276692c8bSGreg Roach     */
2633caaa4d2SGreg Roach    public function editBlockConfiguration(Tree $tree, int $block_id): string
264c1010edaSGreg Roach    {
265c385536dSGreg Roach        $num        = $this->getBlockSetting($block_id, 'num', self::DEFAULT_NUMBER);
2667c2c99faSGreg Roach        $info_style = $this->getBlockSetting($block_id, 'infoStyle', self::DEFAULT_STYLE);
2678c2e8227SGreg Roach
268c385536dSGreg Roach        $info_styles = [
269bbb76c12SGreg Roach            /* I18N: An option in a list-box */
270bbb76c12SGreg Roach            'list'     => I18N::translate('bullet list'),
271bbb76c12SGreg Roach            /* I18N: An option in a list-box */
272bbb76c12SGreg Roach            'array'    => I18N::translate('compact list'),
273bbb76c12SGreg Roach            /* I18N: An option in a list-box */
274bbb76c12SGreg Roach            'table'    => I18N::translate('table'),
275bbb76c12SGreg Roach            /* I18N: An option in a list-box */
276bbb76c12SGreg Roach            'tagcloud' => I18N::translate('tag cloud'),
277c385536dSGreg Roach        ];
2788c2e8227SGreg Roach
2793caaa4d2SGreg Roach        return view('modules/top10_surnames/config', [
280c385536dSGreg Roach            'num'         => $num,
281d6837001SGreg Roach            'info_style'  => $info_style,
282c385536dSGreg Roach            'info_styles' => $info_styles,
283c385536dSGreg Roach        ]);
2848c2e8227SGreg Roach    }
2858c2e8227SGreg Roach}
286