xref: /webtrees/app/Module/TopSurnamesModule.php (revision d11be7027e34e3121be11cc025421873364403f9)
18c2e8227SGreg Roach<?php
23976b470SGreg Roach
38c2e8227SGreg Roach/**
48c2e8227SGreg Roach * webtrees: online genealogy
5*d11be702SGreg 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;
230e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N;
248fb4e87cSGreg Roachuse Fisharebest\Webtrees\Individual;
2567992b6aSRichard Cisseeuse Fisharebest\Webtrees\Services\ModuleService;
2609482a55SGreg Roachuse Fisharebest\Webtrees\Tree;
27748dbe15SGreg 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
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     * 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);
101b86c018aSGreg Roach        $info_style = $this->getBlockSetting($block_id, 'infoStyle', self::DEFAULT_STYLE);
1028c2e8227SGreg Roach
1033caaa4d2SGreg Roach        extract($config, EXTR_OVERWRITE);
1048c2e8227SGreg Roach
105b86c018aSGreg Roach        $query = DB::table('name')
1068d7ed87eSGreg Roach            ->where('n_file', '=', $tree->id())
1078d7ed87eSGreg Roach            ->where('n_type', '<>', '_MARNM')
108b86c018aSGreg Roach            ->where('n_surn', '<>', '')
109b86c018aSGreg Roach            ->where('n_surn', '<>', Individual::NOMEN_NESCIO)
110b86c018aSGreg Roach            ->select([
111b86c018aSGreg Roach                $this->binaryColumn('n_surn', 'n_surn'),
112b86c018aSGreg Roach                $this->binaryColumn('n_surname', 'n_surname'),
113b86c018aSGreg Roach                new Expression('COUNT(*) AS total'),
114b86c018aSGreg Roach            ])
115b86c018aSGreg Roach            ->groupBy([
116b86c018aSGreg Roach                $this->binaryColumn('n_surn'),
117b86c018aSGreg Roach                $this->binaryColumn('n_surname'),
118b86c018aSGreg Roach            ]);
1198c2e8227SGreg Roach
120b86c018aSGreg Roach        /** @var array<array<int>> $top_surnames */
121b86c018aSGreg Roach        $top_surnames = [];
12280536c2dSGreg Roach
123b86c018aSGreg Roach        foreach ($query->get() as $row) {
124b86c018aSGreg Roach            $row->n_surn = $row->n_surn === '' ? $row->n_surname : $row->n_surn;
125b86c018aSGreg Roach            $row->n_surn = I18N::strtoupper(I18N::language()->normalize($row->n_surn));
126f8c9edfeSGreg Roach
127b86c018aSGreg Roach            $top_surnames[$row->n_surn][$row->n_surname] ??= 0;
128b86c018aSGreg Roach            $top_surnames[$row->n_surn][$row->n_surname] += (int) $row->total;
1298c2e8227SGreg Roach        }
1308c2e8227SGreg Roach
131b86c018aSGreg Roach        uasort($top_surnames, static fn (array $x, array $y): int => array_sum($y) <=> array_sum($x));
132b86c018aSGreg Roach
133b86c018aSGreg Roach        $top_surnames = array_slice($top_surnames, 0, $num, true);
134b86c018aSGreg Roach
1352e4f3c66SGreg Roach        // Find a module providing individual lists.
1362e4f3c66SGreg Roach        $module = $this->module_service
1372e4f3c66SGreg Roach            ->findByComponent(ModuleListInterface::class, $tree, Auth::user())
1382e4f3c66SGreg Roach            ->first(static function (ModuleInterface $module): bool {
139ba738272SGreg Roach                // The family list extends the individual list
140ba738272SGreg Roach                return
141ba738272SGreg Roach                    $module instanceof IndividualListModule &&
142ba738272SGreg Roach                    !$module instanceof FamilyListModule;
14367992b6aSRichard Cissee            });
14467992b6aSRichard Cissee
145b86c018aSGreg Roach        switch ($info_style) {
1468c2e8227SGreg Roach            case 'tagcloud':
147b86c018aSGreg Roach                uksort($top_surnames, I18N::comparator());
148cd1ec0d0SGreg Roach                $content = view('lists/surnames-tag-cloud', [
149cd1ec0d0SGreg Roach                    'module'   => $module,
150b86c018aSGreg Roach                    'surnames' => $top_surnames,
151cd1ec0d0SGreg Roach                    'totals'   => true,
152cd1ec0d0SGreg Roach                    'tree'     => $tree,
153cd1ec0d0SGreg Roach                ]);
1548c2e8227SGreg Roach                break;
155cd1ec0d0SGreg Roach
1568c2e8227SGreg Roach            case 'list':
157cd1ec0d0SGreg Roach                $content = view('lists/surnames-bullet-list', [
158cd1ec0d0SGreg Roach                    'module'   => $module,
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,
168b86c018aSGreg Roach                    'surnames' => $top_surnames,
169cd1ec0d0SGreg Roach                    'totals'   => true,
170cd1ec0d0SGreg Roach                    'tree'     => $tree,
171cd1ec0d0SGreg Roach                ]);
1728c2e8227SGreg Roach                break;
173cd1ec0d0SGreg Roach
1748c2e8227SGreg Roach            case 'table':
1758c2e8227SGreg Roach            default:
176b86c018aSGreg Roach                uksort($top_surnames, I18N::comparator());
177cf184a4dSGreg Roach                $content = view('lists/surnames-table', [
17867992b6aSRichard Cissee                    'families' => false,
179c9128110SGreg Roach                    'module'   => $module,
180c9128110SGreg Roach                    'order'    => [[1, 'desc']],
181b86c018aSGreg Roach                    'surnames' => $top_surnames,
182e490cd80SGreg Roach                    'tree'     => $tree,
1836ab54c76SGreg Roach                ]);
1848c2e8227SGreg Roach                break;
1858c2e8227SGreg Roach        }
1868c2e8227SGreg Roach
1873caaa4d2SGreg Roach        if ($context !== self::CONTEXT_EMBED) {
18880536c2dSGreg Roach            $num = count($top_surnames);
18980536c2dSGreg Roach            if ($num === 1) {
1908cbbfdceSGreg Roach                // I18N: i.e. most popular surname.
1918cbbfdceSGreg Roach                $title = I18N::translate('Top surname');
1928cbbfdceSGreg Roach            } else {
1938cbbfdceSGreg 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
1948cbbfdceSGreg Roach                $title = I18N::plural('Top %s surname', 'Top %s surnames', $num, I18N::number($num));
1958cbbfdceSGreg Roach            }
1968cbbfdceSGreg Roach
197147e99aaSGreg Roach            return view('modules/block-template', [
1981e7a7a28SGreg Roach                'block'      => Str::kebab($this->name()),
1999c6524dcSGreg Roach                'id'         => $block_id,
2003caaa4d2SGreg Roach                'config_url' => $this->configUrl($tree, $context, $block_id),
2018cbbfdceSGreg Roach                'title'      => $title,
2029c6524dcSGreg Roach                'content'    => $content,
2039c6524dcSGreg Roach            ]);
2048c2e8227SGreg Roach        }
205b2ce94c6SRico Sonntag
206b2ce94c6SRico Sonntag        return $content;
2078c2e8227SGreg Roach    }
2088c2e8227SGreg Roach
2093caaa4d2SGreg Roach    /**
2103caaa4d2SGreg Roach     * Should this block load asynchronously using AJAX?
2113caaa4d2SGreg Roach     *
2123caaa4d2SGreg Roach     * Simple blocks are faster in-line, more complex ones can be loaded later.
2133caaa4d2SGreg Roach     *
2143caaa4d2SGreg Roach     * @return bool
2153caaa4d2SGreg Roach     */
216c1010edaSGreg Roach    public function loadAjax(): bool
217c1010edaSGreg Roach    {
2184ecf4f82SGreg Roach        return false;
2198c2e8227SGreg Roach    }
2208c2e8227SGreg Roach
2213caaa4d2SGreg Roach    /**
2223caaa4d2SGreg Roach     * Can this block be shown on the user’s home page?
2233caaa4d2SGreg Roach     *
2243caaa4d2SGreg Roach     * @return bool
2253caaa4d2SGreg Roach     */
226c1010edaSGreg Roach    public function isUserBlock(): bool
227c1010edaSGreg Roach    {
2288c2e8227SGreg Roach        return true;
2298c2e8227SGreg Roach    }
2308c2e8227SGreg Roach
2313caaa4d2SGreg Roach    /**
2323caaa4d2SGreg Roach     * Can this block be shown on the tree’s home page?
2333caaa4d2SGreg Roach     *
2343caaa4d2SGreg Roach     * @return bool
2353caaa4d2SGreg Roach     */
23663276d8fSGreg Roach    public function isTreeBlock(): bool
237c1010edaSGreg Roach    {
2388c2e8227SGreg Roach        return true;
2398c2e8227SGreg Roach    }
2408c2e8227SGreg Roach
24176692c8bSGreg Roach    /**
242a45f9889SGreg Roach     * Update the configuration for a block.
243a45f9889SGreg Roach     *
2446ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
245a45f9889SGreg Roach     * @param int     $block_id
246a45f9889SGreg Roach     *
247a45f9889SGreg Roach     * @return void
248a45f9889SGreg Roach     */
2496ccdf4f0SGreg Roach    public function saveBlockConfiguration(ServerRequestInterface $request, int $block_id): void
250a45f9889SGreg Roach    {
251748dbe15SGreg Roach        $num        = Validator::parsedBody($request)->integer('num');
252748dbe15SGreg Roach        $info_style = Validator::parsedBody($request)->string('infoStyle');
253c50a90beSGreg Roach
254748dbe15SGreg Roach        $this->setBlockSetting($block_id, 'num', (string) $num);
255748dbe15SGreg Roach        $this->setBlockSetting($block_id, 'infoStyle', $info_style);
256a45f9889SGreg Roach    }
257a45f9889SGreg Roach
258a45f9889SGreg Roach    /**
25976692c8bSGreg Roach     * An HTML form to edit block settings
26076692c8bSGreg Roach     *
261e490cd80SGreg Roach     * @param Tree $tree
26276692c8bSGreg Roach     * @param int  $block_id
263a9430be8SGreg Roach     *
2643caaa4d2SGreg Roach     * @return string
26576692c8bSGreg Roach     */
2663caaa4d2SGreg Roach    public function editBlockConfiguration(Tree $tree, int $block_id): string
267c1010edaSGreg Roach    {
268c385536dSGreg Roach        $num        = $this->getBlockSetting($block_id, 'num', self::DEFAULT_NUMBER);
2697c2c99faSGreg Roach        $info_style = $this->getBlockSetting($block_id, 'infoStyle', self::DEFAULT_STYLE);
2708c2e8227SGreg Roach
271c385536dSGreg Roach        $info_styles = [
272bbb76c12SGreg Roach            /* I18N: An option in a list-box */
273bbb76c12SGreg Roach            'list'     => I18N::translate('bullet list'),
274bbb76c12SGreg Roach            /* I18N: An option in a list-box */
275bbb76c12SGreg Roach            'array'    => I18N::translate('compact list'),
276bbb76c12SGreg Roach            /* I18N: An option in a list-box */
277bbb76c12SGreg Roach            'table'    => I18N::translate('table'),
278bbb76c12SGreg Roach            /* I18N: An option in a list-box */
279bbb76c12SGreg Roach            'tagcloud' => I18N::translate('tag cloud'),
280c385536dSGreg Roach        ];
2818c2e8227SGreg Roach
2823caaa4d2SGreg Roach        return view('modules/top10_surnames/config', [
283c385536dSGreg Roach            'num'         => $num,
284d6837001SGreg Roach            'info_style'  => $info_style,
285c385536dSGreg Roach            'info_styles' => $info_styles,
286c385536dSGreg Roach        ]);
2878c2e8227SGreg Roach    }
288b86c018aSGreg Roach
289b86c018aSGreg Roach    /**
290b86c018aSGreg Roach     * This module assumes the database will use binary collation on the name columns.
291b86c018aSGreg Roach     * Until we convert MySQL databases to use utf8_bin, we need to do this at run-time.
292b86c018aSGreg Roach     *
293b86c018aSGreg Roach     * @param string      $column
294b86c018aSGreg Roach     * @param string|null $alias
295b86c018aSGreg Roach     *
296b86c018aSGreg Roach     * @return Expression
297b86c018aSGreg Roach     */
298b86c018aSGreg Roach    private function binaryColumn(string $column, string $alias = null): Expression
299b86c018aSGreg Roach    {
300b86c018aSGreg Roach        if (DB::connection()->getDriverName() === 'mysql') {
301b86c018aSGreg Roach            $sql = 'CAST(' . $column . ' AS binary)';
302b86c018aSGreg Roach        } else {
303b86c018aSGreg Roach            $sql = $column;
304b86c018aSGreg Roach        }
305b86c018aSGreg Roach
306b86c018aSGreg Roach        if ($alias !== null) {
307b86c018aSGreg Roach            $sql .= ' AS ' . $alias;
308b86c018aSGreg Roach        }
309b86c018aSGreg Roach
310b86c018aSGreg Roach        return new Expression($sql);
311b86c018aSGreg Roach    }
3128c2e8227SGreg Roach}
313