xref: /webtrees/app/Module/TopSurnamesModule.php (revision 7413816e6dd2d50e569034fb804f3dce7471bb94)
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;
236f4ec3caSGreg 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
7549a243cbSGreg Roach    public function description(): string
76c1010edaSGreg Roach    {
77bbb76c12SGreg Roach        /* I18N: Description of the “Top surnames” module */
78bbb76c12SGreg Roach        return I18N::translate('A list of the most popular surnames.');
798c2e8227SGreg Roach    }
808c2e8227SGreg Roach
8176692c8bSGreg Roach    /**
8276692c8bSGreg Roach     * Generate the HTML content of this block.
8376692c8bSGreg Roach     *
84e490cd80SGreg Roach     * @param Tree                 $tree
8576692c8bSGreg Roach     * @param int                  $block_id
863caaa4d2SGreg Roach     * @param string               $context
8776d39c55SGreg Roach     * @param array<string,string> $config
8876692c8bSGreg Roach     *
8976692c8bSGreg Roach     * @return string
9076692c8bSGreg Roach     */
913caaa4d2SGreg Roach    public function getBlock(Tree $tree, int $block_id, string $context, array $config = []): string
92c1010edaSGreg Roach    {
9380536c2dSGreg Roach        $num        = (int) $this->getBlockSetting($block_id, 'num', self::DEFAULT_NUMBER);
94b86c018aSGreg Roach        $info_style = $this->getBlockSetting($block_id, 'infoStyle', self::DEFAULT_STYLE);
958c2e8227SGreg Roach
963caaa4d2SGreg Roach        extract($config, EXTR_OVERWRITE);
978c2e8227SGreg Roach
98b86c018aSGreg Roach        $query = DB::table('name')
998d7ed87eSGreg Roach            ->where('n_file', '=', $tree->id())
1008d7ed87eSGreg Roach            ->where('n_type', '<>', '_MARNM')
101b86c018aSGreg Roach            ->where('n_surn', '<>', '')
102b86c018aSGreg Roach            ->where('n_surn', '<>', Individual::NOMEN_NESCIO)
103b86c018aSGreg Roach            ->select([
10452550490SGreg Roach                DB::binaryColumn('n_surn', 'n_surn'),
10552550490SGreg Roach                DB::binaryColumn('n_surname', 'n_surname'),
106b86c018aSGreg Roach                new Expression('COUNT(*) AS total'),
107b86c018aSGreg Roach            ])
108b86c018aSGreg Roach            ->groupBy([
10952550490SGreg Roach                DB::binaryColumn('n_surn'),
11052550490SGreg Roach                DB::binaryColumn('n_surname'),
111b86c018aSGreg Roach            ]);
1128c2e8227SGreg Roach
113b86c018aSGreg Roach        /** @var array<array<int>> $top_surnames */
114b86c018aSGreg Roach        $top_surnames = [];
11580536c2dSGreg Roach
116b86c018aSGreg Roach        foreach ($query->get() as $row) {
117b86c018aSGreg Roach            $row->n_surn = $row->n_surn === '' ? $row->n_surname : $row->n_surn;
118b86c018aSGreg Roach            $row->n_surn = I18N::strtoupper(I18N::language()->normalize($row->n_surn));
119f8c9edfeSGreg Roach
120b86c018aSGreg Roach            $top_surnames[$row->n_surn][$row->n_surname] ??= 0;
121b86c018aSGreg Roach            $top_surnames[$row->n_surn][$row->n_surname] += (int) $row->total;
1228c2e8227SGreg Roach        }
1238c2e8227SGreg Roach
124b86c018aSGreg Roach        uasort($top_surnames, static fn (array $x, array $y): int => array_sum($y) <=> array_sum($x));
125b86c018aSGreg Roach
126b86c018aSGreg Roach        $top_surnames = array_slice($top_surnames, 0, $num, true);
127b86c018aSGreg Roach
1282e4f3c66SGreg Roach        // Find a module providing individual lists.
1292e4f3c66SGreg Roach        $module = $this->module_service
1302e4f3c66SGreg Roach            ->findByComponent(ModuleListInterface::class, $tree, Auth::user())
131*00ef1d3aSGreg Roach            ->first(static fn (ModuleInterface $module): bool => $module instanceof IndividualListModule);
13267992b6aSRichard Cissee
133b86c018aSGreg Roach        switch ($info_style) {
1348c2e8227SGreg Roach            case 'tagcloud':
135b86c018aSGreg Roach                uksort($top_surnames, I18N::comparator());
136cd1ec0d0SGreg Roach                $content = view('lists/surnames-tag-cloud', [
137cd1ec0d0SGreg Roach                    'module'   => $module,
1387a3804a2SGreg Roach                    'params'   => [],
139b86c018aSGreg Roach                    'surnames' => $top_surnames,
140cd1ec0d0SGreg Roach                    'totals'   => true,
141cd1ec0d0SGreg Roach                    'tree'     => $tree,
142cd1ec0d0SGreg Roach                ]);
1438c2e8227SGreg Roach                break;
144cd1ec0d0SGreg Roach
1458c2e8227SGreg Roach            case 'list':
146cd1ec0d0SGreg Roach                $content = view('lists/surnames-bullet-list', [
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 'array':
156cd1ec0d0SGreg Roach                $content = view('lists/surnames-compact-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 'table':
1668c2e8227SGreg Roach            default:
167b86c018aSGreg Roach                uksort($top_surnames, I18N::comparator());
168cf184a4dSGreg Roach                $content = view('lists/surnames-table', [
16967992b6aSRichard Cissee                    'families' => false,
170c9128110SGreg Roach                    'module'   => $module,
171c9128110SGreg Roach                    'order'    => [[1, 'desc']],
1727a3804a2SGreg Roach                    'params'   => [],
173b86c018aSGreg Roach                    'surnames' => $top_surnames,
174e490cd80SGreg Roach                    'tree'     => $tree,
1756ab54c76SGreg Roach                ]);
1768c2e8227SGreg Roach                break;
1778c2e8227SGreg Roach        }
1788c2e8227SGreg Roach
1793caaa4d2SGreg Roach        if ($context !== self::CONTEXT_EMBED) {
18080536c2dSGreg Roach            $num = count($top_surnames);
18180536c2dSGreg Roach            if ($num === 1) {
1828cbbfdceSGreg Roach                // I18N: i.e. most popular surname.
1838cbbfdceSGreg Roach                $title = I18N::translate('Top surname');
1848cbbfdceSGreg Roach            } else {
1858cbbfdceSGreg 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
1868cbbfdceSGreg Roach                $title = I18N::plural('Top %s surname', 'Top %s surnames', $num, I18N::number($num));
1878cbbfdceSGreg Roach            }
1888cbbfdceSGreg Roach
189147e99aaSGreg Roach            return view('modules/block-template', [
1901e7a7a28SGreg Roach                'block'      => Str::kebab($this->name()),
1919c6524dcSGreg Roach                'id'         => $block_id,
1923caaa4d2SGreg Roach                'config_url' => $this->configUrl($tree, $context, $block_id),
1938cbbfdceSGreg Roach                'title'      => $title,
1949c6524dcSGreg Roach                'content'    => $content,
1959c6524dcSGreg Roach            ]);
1968c2e8227SGreg Roach        }
197b2ce94c6SRico Sonntag
198b2ce94c6SRico Sonntag        return $content;
1998c2e8227SGreg Roach    }
2008c2e8227SGreg Roach
2013caaa4d2SGreg Roach    /**
2023caaa4d2SGreg Roach     * Should this block load asynchronously using AJAX?
2033caaa4d2SGreg Roach     *
2043caaa4d2SGreg Roach     * Simple blocks are faster in-line, more complex ones can be loaded later.
2053caaa4d2SGreg Roach     *
2063caaa4d2SGreg Roach     * @return bool
2073caaa4d2SGreg Roach     */
208c1010edaSGreg Roach    public function loadAjax(): bool
209c1010edaSGreg Roach    {
2104ecf4f82SGreg Roach        return false;
2118c2e8227SGreg Roach    }
2128c2e8227SGreg Roach
2133caaa4d2SGreg Roach    /**
2143caaa4d2SGreg Roach     * Can this block be shown on the user’s home page?
2153caaa4d2SGreg Roach     *
2163caaa4d2SGreg Roach     * @return bool
2173caaa4d2SGreg Roach     */
218c1010edaSGreg Roach    public function isUserBlock(): bool
219c1010edaSGreg Roach    {
2208c2e8227SGreg Roach        return true;
2218c2e8227SGreg Roach    }
2228c2e8227SGreg Roach
2233caaa4d2SGreg Roach    /**
2243caaa4d2SGreg Roach     * Can this block be shown on the tree’s home page?
2253caaa4d2SGreg Roach     *
2263caaa4d2SGreg Roach     * @return bool
2273caaa4d2SGreg Roach     */
22863276d8fSGreg Roach    public function isTreeBlock(): bool
229c1010edaSGreg Roach    {
2308c2e8227SGreg Roach        return true;
2318c2e8227SGreg Roach    }
2328c2e8227SGreg Roach
23376692c8bSGreg Roach    /**
234a45f9889SGreg Roach     * Update the configuration for a block.
235a45f9889SGreg Roach     *
2366ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
237a45f9889SGreg Roach     * @param int     $block_id
238a45f9889SGreg Roach     *
239a45f9889SGreg Roach     * @return void
240a45f9889SGreg Roach     */
2416ccdf4f0SGreg Roach    public function saveBlockConfiguration(ServerRequestInterface $request, int $block_id): void
242a45f9889SGreg Roach    {
243748dbe15SGreg Roach        $num        = Validator::parsedBody($request)->integer('num');
244748dbe15SGreg Roach        $info_style = Validator::parsedBody($request)->string('infoStyle');
245c50a90beSGreg Roach
246748dbe15SGreg Roach        $this->setBlockSetting($block_id, 'num', (string) $num);
247748dbe15SGreg Roach        $this->setBlockSetting($block_id, 'infoStyle', $info_style);
248a45f9889SGreg Roach    }
249a45f9889SGreg Roach
250a45f9889SGreg Roach    /**
25176692c8bSGreg Roach     * An HTML form to edit block settings
25276692c8bSGreg Roach     *
253e490cd80SGreg Roach     * @param Tree $tree
25476692c8bSGreg Roach     * @param int  $block_id
255a9430be8SGreg Roach     *
2563caaa4d2SGreg Roach     * @return string
25776692c8bSGreg Roach     */
2583caaa4d2SGreg Roach    public function editBlockConfiguration(Tree $tree, int $block_id): string
259c1010edaSGreg Roach    {
260c385536dSGreg Roach        $num        = $this->getBlockSetting($block_id, 'num', self::DEFAULT_NUMBER);
2617c2c99faSGreg Roach        $info_style = $this->getBlockSetting($block_id, 'infoStyle', self::DEFAULT_STYLE);
2628c2e8227SGreg Roach
263c385536dSGreg Roach        $info_styles = [
264bbb76c12SGreg Roach            /* I18N: An option in a list-box */
265bbb76c12SGreg Roach            'list'     => I18N::translate('bullet list'),
266bbb76c12SGreg Roach            /* I18N: An option in a list-box */
267bbb76c12SGreg Roach            'array'    => I18N::translate('compact list'),
268bbb76c12SGreg Roach            /* I18N: An option in a list-box */
269bbb76c12SGreg Roach            'table'    => I18N::translate('table'),
270bbb76c12SGreg Roach            /* I18N: An option in a list-box */
271bbb76c12SGreg Roach            'tagcloud' => I18N::translate('tag cloud'),
272c385536dSGreg Roach        ];
2738c2e8227SGreg Roach
2743caaa4d2SGreg Roach        return view('modules/top10_surnames/config', [
275c385536dSGreg Roach            'num'         => $num,
276d6837001SGreg Roach            'info_style'  => $info_style,
277c385536dSGreg Roach            'info_styles' => $info_styles,
278c385536dSGreg Roach        ]);
2798c2e8227SGreg Roach    }
2808c2e8227SGreg Roach}
281