xref: /webtrees/app/Module/TopSurnamesModule.php (revision 05babb969dd66fd95325613c7360d51ed3aa19fc)
18c2e8227SGreg Roach<?php
23976b470SGreg Roach
38c2e8227SGreg Roach/**
48c2e8227SGreg Roach * webtrees: online genealogy
589f7189bSGreg Roach * Copyright (C) 2021 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;
233d7a8a4cSGreg Roachuse Fisharebest\Webtrees\Functions\FunctionsPrintLists;
240e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N;
258fb4e87cSGreg Roachuse Fisharebest\Webtrees\Individual;
2667992b6aSRichard Cisseeuse Fisharebest\Webtrees\Services\ModuleService;
2709482a55SGreg Roachuse Fisharebest\Webtrees\Tree;
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;
34632b1edfSGreg Roach
358c2e8227SGreg Roach/**
368c2e8227SGreg Roach * Class TopSurnamesModule
378c2e8227SGreg Roach */
3837eb8894SGreg Roachclass TopSurnamesModule extends AbstractModule implements ModuleBlockInterface
39c1010edaSGreg Roach{
4049a243cbSGreg Roach    use ModuleBlockTrait;
4149a243cbSGreg Roach
42c385536dSGreg Roach    // Default values for new blocks.
4316d6367aSGreg Roach    private const DEFAULT_NUMBER = '10';
4416d6367aSGreg Roach    private const DEFAULT_STYLE  = 'table';
45c385536dSGreg Roach
4643f2f523SGreg Roach    private ModuleService $module_service;
472e4f3c66SGreg Roach
482e4f3c66SGreg Roach    /**
492e4f3c66SGreg Roach     * TopSurnamesModule constructor.
502e4f3c66SGreg Roach     *
512e4f3c66SGreg Roach     * @param ModuleService $module_service
522e4f3c66SGreg Roach     */
532e4f3c66SGreg Roach    public function __construct(ModuleService $module_service)
542e4f3c66SGreg Roach    {
552e4f3c66SGreg Roach        $this->module_service = $module_service;
562e4f3c66SGreg Roach    }
572e4f3c66SGreg Roach
5876692c8bSGreg Roach    /**
590cfd6963SGreg Roach     * How should this module be identified in the control panel, etc.?
6076692c8bSGreg Roach     *
6176692c8bSGreg Roach     * @return string
6276692c8bSGreg Roach     */
6349a243cbSGreg Roach    public function title(): string
64c1010edaSGreg Roach    {
65bbb76c12SGreg Roach        /* I18N: Name of a module. Top=Most common */
66bbb76c12SGreg Roach        return I18N::translate('Top surnames');
678c2e8227SGreg Roach    }
688c2e8227SGreg Roach
6976692c8bSGreg Roach    /**
7076692c8bSGreg Roach     * A sentence describing what this module does.
7176692c8bSGreg Roach     *
7276692c8bSGreg Roach     * @return string
7376692c8bSGreg Roach     */
7449a243cbSGreg Roach    public function description(): string
75c1010edaSGreg Roach    {
76bbb76c12SGreg Roach        /* I18N: Description of the “Top surnames” module */
77bbb76c12SGreg Roach        return I18N::translate('A list of the most popular surnames.');
788c2e8227SGreg Roach    }
798c2e8227SGreg Roach
8076692c8bSGreg Roach    /**
8176692c8bSGreg Roach     * Generate the HTML content of this block.
8276692c8bSGreg Roach     *
83e490cd80SGreg Roach     * @param Tree          $tree
8476692c8bSGreg Roach     * @param int           $block_id
853caaa4d2SGreg Roach     * @param string        $context
8609482a55SGreg Roach     * @param array<string> $config
8776692c8bSGreg Roach     *
8876692c8bSGreg Roach     * @return string
8976692c8bSGreg Roach     */
903caaa4d2SGreg Roach    public function getBlock(Tree $tree, int $block_id, string $context, array $config = []): string
91c1010edaSGreg Roach    {
9280536c2dSGreg Roach        $num       = (int) $this->getBlockSetting($block_id, 'num', self::DEFAULT_NUMBER);
93c385536dSGreg Roach        $infoStyle = $this->getBlockSetting($block_id, 'infoStyle', self::DEFAULT_STYLE);
948c2e8227SGreg Roach
953caaa4d2SGreg Roach        extract($config, EXTR_OVERWRITE);
968c2e8227SGreg Roach
9780536c2dSGreg Roach        // Use the count of base surnames.
988d7ed87eSGreg Roach        $top_surnames = DB::table('name')
998d7ed87eSGreg Roach            ->where('n_file', '=', $tree->id())
1008d7ed87eSGreg Roach            ->where('n_type', '<>', '_MARNM')
1018fb4e87cSGreg Roach            ->whereNotIn('n_surn', [Individual::NOMEN_NESCIO, ''])
1027f5c2944SGreg Roach            ->groupBy(['n_surn'])
103a69f5655SGreg Roach            ->orderByDesc(new Expression('COUNT(n_surn)'))
1048d7ed87eSGreg Roach            ->take($num)
1058d7ed87eSGreg Roach            ->pluck('n_surn');
1068c2e8227SGreg Roach
10713abd6f3SGreg Roach        $all_surnames = [];
10880536c2dSGreg Roach
1098d7ed87eSGreg Roach        foreach ($top_surnames as $top_surname) {
1108d7ed87eSGreg Roach            $variants = DB::table('name')
1118d7ed87eSGreg Roach                ->where('n_file', '=', $tree->id())
112a69f5655SGreg Roach                ->where(new Expression('n_surn /*! COLLATE utf8_bin */'), '=', $top_surname)
1137f5c2944SGreg Roach                ->groupBy(['surname'])
114a69f5655SGreg Roach                ->select([new Expression('n_surname /*! COLLATE utf8_bin */ AS surname'), new Expression('count(*) AS total')])
1158d7ed87eSGreg Roach                ->pluck('total', 'surname')
1164c78e066SGreg Roach                ->map(static fn (string $n): int => (int) $n)
1178d7ed87eSGreg Roach                ->all();
118f8c9edfeSGreg Roach
11980536c2dSGreg Roach            $all_surnames[$top_surname] = $variants;
1208c2e8227SGreg Roach        }
1218c2e8227SGreg Roach
1222e4f3c66SGreg Roach        // Find a module providing individual lists.
1232e4f3c66SGreg Roach        $module = $this->module_service
1242e4f3c66SGreg Roach            ->findByComponent(ModuleListInterface::class, $tree, Auth::user())
1252e4f3c66SGreg Roach            ->first(static function (ModuleInterface $module): bool {
126ba738272SGreg Roach                // The family list extends the individual list
127ba738272SGreg Roach                return
128ba738272SGreg Roach                    $module instanceof IndividualListModule &&
129ba738272SGreg Roach                    !$module instanceof FamilyListModule;
13067992b6aSRichard Cissee            });
13167992b6aSRichard Cissee
1328c2e8227SGreg Roach        switch ($infoStyle) {
1338c2e8227SGreg Roach            case 'tagcloud':
13437646143SGreg Roach                uksort($all_surnames, I18N::comparator());
13567992b6aSRichard Cissee                $content = FunctionsPrintLists::surnameTagCloud($all_surnames, $module, true, $tree);
1368c2e8227SGreg Roach                break;
1378c2e8227SGreg Roach            case 'list':
138*05babb96SGreg Roach                uasort($all_surnames, static fn (array $a, array $b): int => array_sum($b) <=> array_sum($a));
13967992b6aSRichard Cissee                $content = FunctionsPrintLists::surnameList($all_surnames, 1, true, $module, $tree);
1408c2e8227SGreg Roach                break;
1418c2e8227SGreg Roach            case 'array':
142*05babb96SGreg Roach                uasort($all_surnames, static fn (array $a, array $b): int => array_sum($b) <=> array_sum($a));
14367992b6aSRichard Cissee                $content = FunctionsPrintLists::surnameList($all_surnames, 2, true, $module, $tree);
1448c2e8227SGreg Roach                break;
1458c2e8227SGreg Roach            case 'table':
1468c2e8227SGreg Roach            default:
147cf184a4dSGreg Roach                $content = view('lists/surnames-table', [
1486ab54c76SGreg Roach                    'surnames' => $all_surnames,
14967992b6aSRichard Cissee                    'module'   => $module,
15067992b6aSRichard Cissee                    'families' => false,
151e490cd80SGreg Roach                    'tree'     => $tree,
1526ab54c76SGreg Roach                ]);
1538c2e8227SGreg Roach                break;
1548c2e8227SGreg Roach        }
1558c2e8227SGreg Roach
1563caaa4d2SGreg Roach        if ($context !== self::CONTEXT_EMBED) {
15780536c2dSGreg Roach            $num = count($top_surnames);
15880536c2dSGreg Roach            if ($num === 1) {
1598cbbfdceSGreg Roach                // I18N: i.e. most popular surname.
1608cbbfdceSGreg Roach                $title = I18N::translate('Top surname');
1618cbbfdceSGreg Roach            } else {
1628cbbfdceSGreg 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
1638cbbfdceSGreg Roach                $title = I18N::plural('Top %s surname', 'Top %s surnames', $num, I18N::number($num));
1648cbbfdceSGreg Roach            }
1658cbbfdceSGreg Roach
166147e99aaSGreg Roach            return view('modules/block-template', [
1671e7a7a28SGreg Roach                'block'      => Str::kebab($this->name()),
1689c6524dcSGreg Roach                'id'         => $block_id,
1693caaa4d2SGreg Roach                'config_url' => $this->configUrl($tree, $context, $block_id),
1708cbbfdceSGreg Roach                'title'      => $title,
1719c6524dcSGreg Roach                'content'    => $content,
1729c6524dcSGreg Roach            ]);
1738c2e8227SGreg Roach        }
174b2ce94c6SRico Sonntag
175b2ce94c6SRico Sonntag        return $content;
1768c2e8227SGreg Roach    }
1778c2e8227SGreg Roach
1783caaa4d2SGreg Roach    /**
1793caaa4d2SGreg Roach     * Should this block load asynchronously using AJAX?
1803caaa4d2SGreg Roach     *
1813caaa4d2SGreg Roach     * Simple blocks are faster in-line, more complex ones can be loaded later.
1823caaa4d2SGreg Roach     *
1833caaa4d2SGreg Roach     * @return bool
1843caaa4d2SGreg Roach     */
185c1010edaSGreg Roach    public function loadAjax(): bool
186c1010edaSGreg Roach    {
1874ecf4f82SGreg Roach        return false;
1888c2e8227SGreg Roach    }
1898c2e8227SGreg Roach
1903caaa4d2SGreg Roach    /**
1913caaa4d2SGreg Roach     * Can this block be shown on the user’s home page?
1923caaa4d2SGreg Roach     *
1933caaa4d2SGreg Roach     * @return bool
1943caaa4d2SGreg Roach     */
195c1010edaSGreg Roach    public function isUserBlock(): bool
196c1010edaSGreg Roach    {
1978c2e8227SGreg Roach        return true;
1988c2e8227SGreg Roach    }
1998c2e8227SGreg Roach
2003caaa4d2SGreg Roach    /**
2013caaa4d2SGreg Roach     * Can this block be shown on the tree’s home page?
2023caaa4d2SGreg Roach     *
2033caaa4d2SGreg Roach     * @return bool
2043caaa4d2SGreg Roach     */
20563276d8fSGreg Roach    public function isTreeBlock(): bool
206c1010edaSGreg Roach    {
2078c2e8227SGreg Roach        return true;
2088c2e8227SGreg Roach    }
2098c2e8227SGreg Roach
21076692c8bSGreg Roach    /**
211a45f9889SGreg Roach     * Update the configuration for a block.
212a45f9889SGreg Roach     *
2136ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
214a45f9889SGreg Roach     * @param int     $block_id
215a45f9889SGreg Roach     *
216a45f9889SGreg Roach     * @return void
217a45f9889SGreg Roach     */
2186ccdf4f0SGreg Roach    public function saveBlockConfiguration(ServerRequestInterface $request, int $block_id): void
219a45f9889SGreg Roach    {
220b46c87bdSGreg Roach        $params = (array) $request->getParsedBody();
221c50a90beSGreg Roach
222c50a90beSGreg Roach        $this->setBlockSetting($block_id, 'num', $params['num']);
223c50a90beSGreg Roach        $this->setBlockSetting($block_id, 'infoStyle', $params['infoStyle']);
224a45f9889SGreg Roach    }
225a45f9889SGreg Roach
226a45f9889SGreg Roach    /**
22776692c8bSGreg Roach     * An HTML form to edit block settings
22876692c8bSGreg Roach     *
229e490cd80SGreg Roach     * @param Tree $tree
23076692c8bSGreg Roach     * @param int  $block_id
231a9430be8SGreg Roach     *
2323caaa4d2SGreg Roach     * @return string
23376692c8bSGreg Roach     */
2343caaa4d2SGreg Roach    public function editBlockConfiguration(Tree $tree, int $block_id): string
235c1010edaSGreg Roach    {
236c385536dSGreg Roach        $num        = $this->getBlockSetting($block_id, 'num', self::DEFAULT_NUMBER);
2377c2c99faSGreg Roach        $info_style = $this->getBlockSetting($block_id, 'infoStyle', self::DEFAULT_STYLE);
2388c2e8227SGreg Roach
239c385536dSGreg Roach        $info_styles = [
240bbb76c12SGreg Roach            /* I18N: An option in a list-box */
241bbb76c12SGreg Roach            'list'     => I18N::translate('bullet list'),
242bbb76c12SGreg Roach            /* I18N: An option in a list-box */
243bbb76c12SGreg Roach            'array'    => I18N::translate('compact list'),
244bbb76c12SGreg Roach            /* I18N: An option in a list-box */
245bbb76c12SGreg Roach            'table'    => I18N::translate('table'),
246bbb76c12SGreg Roach            /* I18N: An option in a list-box */
247bbb76c12SGreg Roach            'tagcloud' => I18N::translate('tag cloud'),
248c385536dSGreg Roach        ];
2498c2e8227SGreg Roach
2503caaa4d2SGreg Roach        return view('modules/top10_surnames/config', [
251c385536dSGreg Roach            'num'         => $num,
252d6837001SGreg Roach            'info_style'  => $info_style,
253c385536dSGreg Roach            'info_styles' => $info_styles,
254c385536dSGreg Roach        ]);
2558c2e8227SGreg Roach    }
2568c2e8227SGreg Roach}
257