xref: /webtrees/app/Module/TopSurnamesModule.php (revision 5bfc689774bb9a6401271c4ed15a6d50652c991b)
18c2e8227SGreg Roach<?php
23976b470SGreg Roach
38c2e8227SGreg Roach/**
48c2e8227SGreg Roach * webtrees: online genealogy
5*5bfc6897SGreg 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;
278d7ed87eSGreg Roachuse Illuminate\Database\Capsule\Manager as DB;
28a69f5655SGreg Roachuse Illuminate\Database\Query\Expression;
291e7a7a28SGreg Roachuse Illuminate\Support\Str;
306ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
318c2e8227SGreg Roach
32632b1edfSGreg Roachuse function array_sum;
33cd1ec0d0SGreg Roachuse function count;
34cd1ec0d0SGreg Roachuse function extract;
35cd1ec0d0SGreg Roachuse function uasort;
36cd1ec0d0SGreg Roachuse function uksort;
37cd1ec0d0SGreg Roachuse function view;
38cd1ec0d0SGreg Roach
39cd1ec0d0SGreg Roachuse const EXTR_OVERWRITE;
40632b1edfSGreg Roach
418c2e8227SGreg Roach/**
428c2e8227SGreg Roach * Class TopSurnamesModule
438c2e8227SGreg Roach */
4437eb8894SGreg Roachclass TopSurnamesModule extends AbstractModule implements ModuleBlockInterface
45c1010edaSGreg Roach{
4649a243cbSGreg Roach    use ModuleBlockTrait;
4749a243cbSGreg Roach
48c385536dSGreg Roach    // Default values for new blocks.
4916d6367aSGreg Roach    private const DEFAULT_NUMBER = '10';
5016d6367aSGreg Roach    private const DEFAULT_STYLE  = 'table';
51c385536dSGreg Roach
5243f2f523SGreg Roach    private ModuleService $module_service;
532e4f3c66SGreg Roach
542e4f3c66SGreg Roach    /**
552e4f3c66SGreg Roach     * TopSurnamesModule constructor.
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);
99c385536dSGreg Roach        $infoStyle = $this->getBlockSetting($block_id, 'infoStyle', self::DEFAULT_STYLE);
1008c2e8227SGreg Roach
1013caaa4d2SGreg Roach        extract($config, EXTR_OVERWRITE);
1028c2e8227SGreg Roach
10380536c2dSGreg Roach        // Use the count of base surnames.
1048d7ed87eSGreg Roach        $top_surnames = DB::table('name')
1058d7ed87eSGreg Roach            ->where('n_file', '=', $tree->id())
1068d7ed87eSGreg Roach            ->where('n_type', '<>', '_MARNM')
1078fb4e87cSGreg Roach            ->whereNotIn('n_surn', [Individual::NOMEN_NESCIO, ''])
1087f5c2944SGreg Roach            ->groupBy(['n_surn'])
109a69f5655SGreg Roach            ->orderByDesc(new Expression('COUNT(n_surn)'))
1108d7ed87eSGreg Roach            ->take($num)
1118d7ed87eSGreg Roach            ->pluck('n_surn');
1128c2e8227SGreg Roach
11313abd6f3SGreg Roach        $all_surnames = [];
11480536c2dSGreg Roach
1158d7ed87eSGreg Roach        foreach ($top_surnames as $top_surname) {
1168d7ed87eSGreg Roach            $variants = DB::table('name')
1178d7ed87eSGreg Roach                ->where('n_file', '=', $tree->id())
118a69f5655SGreg Roach                ->where(new Expression('n_surn /*! COLLATE utf8_bin */'), '=', $top_surname)
1197f5c2944SGreg Roach                ->groupBy(['surname'])
120a69f5655SGreg Roach                ->select([new Expression('n_surname /*! COLLATE utf8_bin */ AS surname'), new Expression('count(*) AS total')])
1218d7ed87eSGreg Roach                ->pluck('total', 'surname')
1224c78e066SGreg Roach                ->map(static fn (string $n): int => (int) $n)
1238d7ed87eSGreg Roach                ->all();
124f8c9edfeSGreg Roach
12580536c2dSGreg Roach            $all_surnames[$top_surname] = $variants;
1268c2e8227SGreg Roach        }
1278c2e8227SGreg Roach
1282e4f3c66SGreg Roach        // Find a module providing individual lists.
1292e4f3c66SGreg Roach        $module = $this->module_service
1302e4f3c66SGreg Roach            ->findByComponent(ModuleListInterface::class, $tree, Auth::user())
1312e4f3c66SGreg Roach            ->first(static function (ModuleInterface $module): bool {
132ba738272SGreg Roach                // The family list extends the individual list
133ba738272SGreg Roach                return
134ba738272SGreg Roach                    $module instanceof IndividualListModule &&
135ba738272SGreg Roach                    !$module instanceof FamilyListModule;
13667992b6aSRichard Cissee            });
13767992b6aSRichard Cissee
1388c2e8227SGreg Roach        switch ($infoStyle) {
1398c2e8227SGreg Roach            case 'tagcloud':
14037646143SGreg Roach                uksort($all_surnames, I18N::comparator());
141cd1ec0d0SGreg Roach                $content = view('lists/surnames-tag-cloud', [
142cd1ec0d0SGreg Roach                    'module'   => $module,
143cd1ec0d0SGreg Roach                    'surnames' => $all_surnames,
144cd1ec0d0SGreg Roach                    'totals'   => true,
145cd1ec0d0SGreg Roach                    'tree'     => $tree,
146cd1ec0d0SGreg Roach                ]);
1478c2e8227SGreg Roach                break;
148cd1ec0d0SGreg Roach
1498c2e8227SGreg Roach            case 'list':
15005babb96SGreg Roach                uasort($all_surnames, static fn (array $a, array $b): int => array_sum($b) <=> array_sum($a));
151cd1ec0d0SGreg Roach                $content = view('lists/surnames-bullet-list', [
152cd1ec0d0SGreg Roach                    'module'   => $module,
153cd1ec0d0SGreg Roach                    'surnames' => $all_surnames,
154cd1ec0d0SGreg Roach                    'totals'   => true,
155cd1ec0d0SGreg Roach                    'tree'     => $tree,
156cd1ec0d0SGreg Roach                ]);
1578c2e8227SGreg Roach                break;
158cd1ec0d0SGreg Roach
1598c2e8227SGreg Roach            case 'array':
16005babb96SGreg Roach                uasort($all_surnames, static fn (array $a, array $b): int => array_sum($b) <=> array_sum($a));
161cd1ec0d0SGreg Roach                $content = view('lists/surnames-compact-list', [
162cd1ec0d0SGreg Roach                    'module'   => $module,
163cd1ec0d0SGreg Roach                    'surnames' => $all_surnames,
164cd1ec0d0SGreg Roach                    'totals'   => true,
165cd1ec0d0SGreg Roach                    'tree'     => $tree,
166cd1ec0d0SGreg Roach                ]);
1678c2e8227SGreg Roach                break;
168cd1ec0d0SGreg Roach
1698c2e8227SGreg Roach            case 'table':
1708c2e8227SGreg Roach            default:
171cf184a4dSGreg Roach                $content = view('lists/surnames-table', [
17267992b6aSRichard Cissee                    'families' => false,
173c9128110SGreg Roach                    'module'   => $module,
174c9128110SGreg Roach                    'order'    => [[1, 'desc']],
175c9128110SGreg Roach                    'surnames' => $all_surnames,
176e490cd80SGreg Roach                    'tree'     => $tree,
1776ab54c76SGreg Roach                ]);
1788c2e8227SGreg Roach                break;
1798c2e8227SGreg Roach        }
1808c2e8227SGreg Roach
1813caaa4d2SGreg Roach        if ($context !== self::CONTEXT_EMBED) {
18280536c2dSGreg Roach            $num = count($top_surnames);
18380536c2dSGreg Roach            if ($num === 1) {
1848cbbfdceSGreg Roach                // I18N: i.e. most popular surname.
1858cbbfdceSGreg Roach                $title = I18N::translate('Top surname');
1868cbbfdceSGreg Roach            } else {
1878cbbfdceSGreg 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
1888cbbfdceSGreg Roach                $title = I18N::plural('Top %s surname', 'Top %s surnames', $num, I18N::number($num));
1898cbbfdceSGreg Roach            }
1908cbbfdceSGreg Roach
191147e99aaSGreg Roach            return view('modules/block-template', [
1921e7a7a28SGreg Roach                'block'      => Str::kebab($this->name()),
1939c6524dcSGreg Roach                'id'         => $block_id,
1943caaa4d2SGreg Roach                'config_url' => $this->configUrl($tree, $context, $block_id),
1958cbbfdceSGreg Roach                'title'      => $title,
1969c6524dcSGreg Roach                'content'    => $content,
1979c6524dcSGreg Roach            ]);
1988c2e8227SGreg Roach        }
199b2ce94c6SRico Sonntag
200b2ce94c6SRico Sonntag        return $content;
2018c2e8227SGreg Roach    }
2028c2e8227SGreg Roach
2033caaa4d2SGreg Roach    /**
2043caaa4d2SGreg Roach     * Should this block load asynchronously using AJAX?
2053caaa4d2SGreg Roach     *
2063caaa4d2SGreg Roach     * Simple blocks are faster in-line, more complex ones can be loaded later.
2073caaa4d2SGreg Roach     *
2083caaa4d2SGreg Roach     * @return bool
2093caaa4d2SGreg Roach     */
210c1010edaSGreg Roach    public function loadAjax(): bool
211c1010edaSGreg Roach    {
2124ecf4f82SGreg Roach        return false;
2138c2e8227SGreg Roach    }
2148c2e8227SGreg Roach
2153caaa4d2SGreg Roach    /**
2163caaa4d2SGreg Roach     * Can this block be shown on the user’s home page?
2173caaa4d2SGreg Roach     *
2183caaa4d2SGreg Roach     * @return bool
2193caaa4d2SGreg Roach     */
220c1010edaSGreg Roach    public function isUserBlock(): bool
221c1010edaSGreg Roach    {
2228c2e8227SGreg Roach        return true;
2238c2e8227SGreg Roach    }
2248c2e8227SGreg Roach
2253caaa4d2SGreg Roach    /**
2263caaa4d2SGreg Roach     * Can this block be shown on the tree’s home page?
2273caaa4d2SGreg Roach     *
2283caaa4d2SGreg Roach     * @return bool
2293caaa4d2SGreg Roach     */
23063276d8fSGreg Roach    public function isTreeBlock(): bool
231c1010edaSGreg Roach    {
2328c2e8227SGreg Roach        return true;
2338c2e8227SGreg Roach    }
2348c2e8227SGreg Roach
23576692c8bSGreg Roach    /**
236a45f9889SGreg Roach     * Update the configuration for a block.
237a45f9889SGreg Roach     *
2386ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
239a45f9889SGreg Roach     * @param int     $block_id
240a45f9889SGreg Roach     *
241a45f9889SGreg Roach     * @return void
242a45f9889SGreg Roach     */
2436ccdf4f0SGreg Roach    public function saveBlockConfiguration(ServerRequestInterface $request, int $block_id): void
244a45f9889SGreg Roach    {
245b46c87bdSGreg Roach        $params = (array) $request->getParsedBody();
246c50a90beSGreg Roach
247c50a90beSGreg Roach        $this->setBlockSetting($block_id, 'num', $params['num']);
248c50a90beSGreg Roach        $this->setBlockSetting($block_id, 'infoStyle', $params['infoStyle']);
249a45f9889SGreg Roach    }
250a45f9889SGreg Roach
251a45f9889SGreg Roach    /**
25276692c8bSGreg Roach     * An HTML form to edit block settings
25376692c8bSGreg Roach     *
254e490cd80SGreg Roach     * @param Tree $tree
25576692c8bSGreg Roach     * @param int  $block_id
256a9430be8SGreg Roach     *
2573caaa4d2SGreg Roach     * @return string
25876692c8bSGreg Roach     */
2593caaa4d2SGreg Roach    public function editBlockConfiguration(Tree $tree, int $block_id): string
260c1010edaSGreg Roach    {
261c385536dSGreg Roach        $num        = $this->getBlockSetting($block_id, 'num', self::DEFAULT_NUMBER);
2627c2c99faSGreg Roach        $info_style = $this->getBlockSetting($block_id, 'infoStyle', self::DEFAULT_STYLE);
2638c2e8227SGreg Roach
264c385536dSGreg Roach        $info_styles = [
265bbb76c12SGreg Roach            /* I18N: An option in a list-box */
266bbb76c12SGreg Roach            'list'     => I18N::translate('bullet list'),
267bbb76c12SGreg Roach            /* I18N: An option in a list-box */
268bbb76c12SGreg Roach            'array'    => I18N::translate('compact list'),
269bbb76c12SGreg Roach            /* I18N: An option in a list-box */
270bbb76c12SGreg Roach            'table'    => I18N::translate('table'),
271bbb76c12SGreg Roach            /* I18N: An option in a list-box */
272bbb76c12SGreg Roach            'tagcloud' => I18N::translate('tag cloud'),
273c385536dSGreg Roach        ];
2748c2e8227SGreg Roach
2753caaa4d2SGreg Roach        return view('modules/top10_surnames/config', [
276c385536dSGreg Roach            'num'         => $num,
277d6837001SGreg Roach            'info_style'  => $info_style,
278c385536dSGreg Roach            'info_styles' => $info_styles,
279c385536dSGreg Roach        ]);
2808c2e8227SGreg Roach    }
2818c2e8227SGreg Roach}
282