xref: /webtrees/app/Module/TopSurnamesModule.php (revision a69f5655040bc7cc0734a54d98f1eda262ddd292)
18c2e8227SGreg Roach<?php
28c2e8227SGreg Roach/**
38c2e8227SGreg Roach * webtrees: online genealogy
48fcd0d32SGreg Roach * Copyright (C) 2019 webtrees development team
58c2e8227SGreg Roach * This program is free software: you can redistribute it and/or modify
68c2e8227SGreg Roach * it under the terms of the GNU General Public License as published by
78c2e8227SGreg Roach * the Free Software Foundation, either version 3 of the License, or
88c2e8227SGreg Roach * (at your option) any later version.
98c2e8227SGreg Roach * This program is distributed in the hope that it will be useful,
108c2e8227SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
118c2e8227SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
128c2e8227SGreg Roach * GNU General Public License for more details.
138c2e8227SGreg Roach * You should have received a copy of the GNU General Public License
148c2e8227SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
158c2e8227SGreg Roach */
16e7f56f2aSGreg Roachdeclare(strict_types=1);
17e7f56f2aSGreg Roach
1876692c8bSGreg Roachnamespace Fisharebest\Webtrees\Module;
1976692c8bSGreg Roach
200e62c4b8SGreg Roachuse Fisharebest\Webtrees\Auth;
213d7a8a4cSGreg Roachuse Fisharebest\Webtrees\Functions\FunctionsPrintLists;
220e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N;
23e490cd80SGreg Roachuse Fisharebest\Webtrees\Tree;
2467992b6aSRichard Cisseeuse Fisharebest\Webtrees\Services\ModuleService;
258d7ed87eSGreg Roachuse Illuminate\Database\Capsule\Manager as DB;
26*a69f5655SGreg Roachuse Illuminate\Database\Query\Expression;
271e7a7a28SGreg Roachuse Illuminate\Support\Str;
286ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
298c2e8227SGreg Roach
308c2e8227SGreg Roach/**
318c2e8227SGreg Roach * Class TopSurnamesModule
328c2e8227SGreg Roach */
3337eb8894SGreg Roachclass TopSurnamesModule extends AbstractModule implements ModuleBlockInterface
34c1010edaSGreg Roach{
3549a243cbSGreg Roach    use ModuleBlockTrait;
3649a243cbSGreg Roach
37c385536dSGreg Roach    // Default values for new blocks.
3816d6367aSGreg Roach    private const DEFAULT_NUMBER = '10';
3916d6367aSGreg Roach    private const DEFAULT_STYLE  = 'table';
40c385536dSGreg Roach
4176692c8bSGreg Roach    /**
420cfd6963SGreg Roach     * How should this module be identified in the control panel, etc.?
4376692c8bSGreg Roach     *
4476692c8bSGreg Roach     * @return string
4576692c8bSGreg Roach     */
4649a243cbSGreg Roach    public function title(): string
47c1010edaSGreg Roach    {
48bbb76c12SGreg Roach        /* I18N: Name of a module. Top=Most common */
49bbb76c12SGreg Roach        return I18N::translate('Top surnames');
508c2e8227SGreg Roach    }
518c2e8227SGreg Roach
5276692c8bSGreg Roach    /**
5376692c8bSGreg Roach     * A sentence describing what this module does.
5476692c8bSGreg Roach     *
5576692c8bSGreg Roach     * @return string
5676692c8bSGreg Roach     */
5749a243cbSGreg Roach    public function description(): string
58c1010edaSGreg Roach    {
59bbb76c12SGreg Roach        /* I18N: Description of the “Top surnames” module */
60bbb76c12SGreg Roach        return I18N::translate('A list of the most popular surnames.');
618c2e8227SGreg Roach    }
628c2e8227SGreg Roach
6376692c8bSGreg Roach    /**
6476692c8bSGreg Roach     * Generate the HTML content of this block.
6576692c8bSGreg Roach     *
66e490cd80SGreg Roach     * @param Tree     $tree
6776692c8bSGreg Roach     * @param int      $block_id
683caaa4d2SGreg Roach     * @param string   $context
693caaa4d2SGreg Roach     * @param string[] $config
7076692c8bSGreg Roach     *
7176692c8bSGreg Roach     * @return string
7276692c8bSGreg Roach     */
733caaa4d2SGreg Roach    public function getBlock(Tree $tree, int $block_id, string $context, array $config = []): string
74c1010edaSGreg Roach    {
7580536c2dSGreg Roach        $num       = (int) $this->getBlockSetting($block_id, 'num', self::DEFAULT_NUMBER);
76c385536dSGreg Roach        $infoStyle = $this->getBlockSetting($block_id, 'infoStyle', self::DEFAULT_STYLE);
778c2e8227SGreg Roach
783caaa4d2SGreg Roach        extract($config, EXTR_OVERWRITE);
798c2e8227SGreg Roach
8080536c2dSGreg Roach        // Use the count of base surnames.
818d7ed87eSGreg Roach        $top_surnames = DB::table('name')
828d7ed87eSGreg Roach            ->where('n_file', '=', $tree->id())
838d7ed87eSGreg Roach            ->where('n_type', '<>', '_MARNM')
848d7ed87eSGreg Roach            ->whereNotIn('n_surn', ['@N.N.', ''])
858d7ed87eSGreg Roach            ->groupBy('n_surn')
86*a69f5655SGreg Roach            ->orderByDesc(new Expression('COUNT(n_surn)'))
878d7ed87eSGreg Roach            ->take($num)
888d7ed87eSGreg Roach            ->pluck('n_surn');
898c2e8227SGreg Roach
9013abd6f3SGreg Roach        $all_surnames = [];
9180536c2dSGreg Roach
928d7ed87eSGreg Roach        foreach ($top_surnames as $top_surname) {
938d7ed87eSGreg Roach            $variants = DB::table('name')
948d7ed87eSGreg Roach                ->where('n_file', '=', $tree->id())
95*a69f5655SGreg Roach                ->where(new Expression('n_surn /*! COLLATE utf8_bin */'), '=', $top_surname)
968d7ed87eSGreg Roach                ->groupBy('surname')
97*a69f5655SGreg Roach                ->select([new Expression('n_surname /*! COLLATE utf8_bin */ AS surname'), new Expression('count(*) AS total')])
988d7ed87eSGreg Roach                ->pluck('total', 'surname')
998d7ed87eSGreg Roach                ->all();
100f8c9edfeSGreg Roach
10180536c2dSGreg Roach            $all_surnames[$top_surname] = $variants;
1028c2e8227SGreg Roach        }
1038c2e8227SGreg Roach
10467992b6aSRichard Cissee        //find a module providing individual lists
1050b93976aSGreg Roach        $module = app(ModuleService::class)->findByComponent(ModuleListInterface::class, $tree, Auth::user())->first(static function (ModuleInterface $module): bool {
10667992b6aSRichard Cissee            return $module instanceof IndividualListModule;
10767992b6aSRichard Cissee        });
10867992b6aSRichard Cissee
1098c2e8227SGreg Roach        switch ($infoStyle) {
1108c2e8227SGreg Roach            case 'tagcloud':
11180536c2dSGreg Roach                uksort($all_surnames, [I18N::class, 'strcasecmp']);
11267992b6aSRichard Cissee                $content = FunctionsPrintLists::surnameTagCloud($all_surnames, $module, true, $tree);
1138c2e8227SGreg Roach                break;
1148c2e8227SGreg Roach            case 'list':
115a515be7cSGreg Roach                uasort($all_surnames, [$this, 'surnameCountSort']);
11667992b6aSRichard Cissee                $content = FunctionsPrintLists::surnameList($all_surnames, 1, true, $module, $tree);
1178c2e8227SGreg Roach                break;
1188c2e8227SGreg Roach            case 'array':
119a515be7cSGreg Roach                uasort($all_surnames, [$this, 'surnameCountSort']);
12067992b6aSRichard Cissee                $content = FunctionsPrintLists::surnameList($all_surnames, 2, true, $module, $tree);
1218c2e8227SGreg Roach                break;
1228c2e8227SGreg Roach            case 'table':
1238c2e8227SGreg Roach            default:
124cf184a4dSGreg Roach                $content = view('lists/surnames-table', [
1256ab54c76SGreg Roach                    'surnames' => $all_surnames,
12667992b6aSRichard Cissee                    'module'   => $module,
12767992b6aSRichard Cissee                    'families' => false,
128e490cd80SGreg Roach                    'tree'     => $tree,
1296ab54c76SGreg Roach                ]);
1308c2e8227SGreg Roach                break;
1318c2e8227SGreg Roach        }
1328c2e8227SGreg Roach
1333caaa4d2SGreg Roach        if ($context !== self::CONTEXT_EMBED) {
13480536c2dSGreg Roach            $num = count($top_surnames);
13580536c2dSGreg Roach            if ($num === 1) {
1368cbbfdceSGreg Roach                // I18N: i.e. most popular surname.
1378cbbfdceSGreg Roach                $title = I18N::translate('Top surname');
1388cbbfdceSGreg Roach            } else {
1398cbbfdceSGreg 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
1408cbbfdceSGreg Roach                $title = I18N::plural('Top %s surname', 'Top %s surnames', $num, I18N::number($num));
1418cbbfdceSGreg Roach            }
1428cbbfdceSGreg Roach
143147e99aaSGreg Roach            return view('modules/block-template', [
1441e7a7a28SGreg Roach                'block'      => Str::kebab($this->name()),
1459c6524dcSGreg Roach                'id'         => $block_id,
1463caaa4d2SGreg Roach                'config_url' => $this->configUrl($tree, $context, $block_id),
1478cbbfdceSGreg Roach                'title'      => $title,
1489c6524dcSGreg Roach                'content'    => $content,
1499c6524dcSGreg Roach            ]);
1508c2e8227SGreg Roach        }
151b2ce94c6SRico Sonntag
152b2ce94c6SRico Sonntag        return $content;
1538c2e8227SGreg Roach    }
1548c2e8227SGreg Roach
1553caaa4d2SGreg Roach    /**
1563caaa4d2SGreg Roach     * Should this block load asynchronously using AJAX?
1573caaa4d2SGreg Roach     *
1583caaa4d2SGreg Roach     * Simple blocks are faster in-line, more complex ones can be loaded later.
1593caaa4d2SGreg Roach     *
1603caaa4d2SGreg Roach     * @return bool
1613caaa4d2SGreg Roach     */
162c1010edaSGreg Roach    public function loadAjax(): bool
163c1010edaSGreg Roach    {
1644ecf4f82SGreg Roach        return false;
1658c2e8227SGreg Roach    }
1668c2e8227SGreg Roach
1673caaa4d2SGreg Roach    /**
1683caaa4d2SGreg Roach     * Can this block be shown on the user’s home page?
1693caaa4d2SGreg Roach     *
1703caaa4d2SGreg Roach     * @return bool
1713caaa4d2SGreg Roach     */
172c1010edaSGreg Roach    public function isUserBlock(): bool
173c1010edaSGreg Roach    {
1748c2e8227SGreg Roach        return true;
1758c2e8227SGreg Roach    }
1768c2e8227SGreg Roach
1773caaa4d2SGreg Roach    /**
1783caaa4d2SGreg Roach     * Can this block be shown on the tree’s home page?
1793caaa4d2SGreg Roach     *
1803caaa4d2SGreg Roach     * @return bool
1813caaa4d2SGreg Roach     */
18263276d8fSGreg Roach    public function isTreeBlock(): bool
183c1010edaSGreg Roach    {
1848c2e8227SGreg Roach        return true;
1858c2e8227SGreg Roach    }
1868c2e8227SGreg Roach
18776692c8bSGreg Roach    /**
188a45f9889SGreg Roach     * Update the configuration for a block.
189a45f9889SGreg Roach     *
1906ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
191a45f9889SGreg Roach     * @param int     $block_id
192a45f9889SGreg Roach     *
193a45f9889SGreg Roach     * @return void
194a45f9889SGreg Roach     */
1956ccdf4f0SGreg Roach    public function saveBlockConfiguration(ServerRequestInterface $request, int $block_id): void
196a45f9889SGreg Roach    {
197c50a90beSGreg Roach        $params = $request->getParsedBody();
198c50a90beSGreg Roach
199c50a90beSGreg Roach        $this->setBlockSetting($block_id, 'num', $params['num']);
200c50a90beSGreg Roach        $this->setBlockSetting($block_id, 'infoStyle', $params['infoStyle']);
201a45f9889SGreg Roach    }
202a45f9889SGreg Roach
203a45f9889SGreg Roach    /**
20476692c8bSGreg Roach     * An HTML form to edit block settings
20576692c8bSGreg Roach     *
206e490cd80SGreg Roach     * @param Tree $tree
20776692c8bSGreg Roach     * @param int  $block_id
208a9430be8SGreg Roach     *
2093caaa4d2SGreg Roach     * @return string
21076692c8bSGreg Roach     */
2113caaa4d2SGreg Roach    public function editBlockConfiguration(Tree $tree, int $block_id): string
212c1010edaSGreg Roach    {
213c385536dSGreg Roach        $num       = $this->getBlockSetting($block_id, 'num', self::DEFAULT_NUMBER);
214c385536dSGreg Roach        $infoStyle = $this->getBlockSetting($block_id, 'infoStyle', self::DEFAULT_STYLE);
2158c2e8227SGreg Roach
216c385536dSGreg Roach        $info_styles = [
217bbb76c12SGreg Roach            /* I18N: An option in a list-box */
218bbb76c12SGreg Roach            'list'     => I18N::translate('bullet list'),
219bbb76c12SGreg Roach            /* I18N: An option in a list-box */
220bbb76c12SGreg Roach            'array'    => I18N::translate('compact list'),
221bbb76c12SGreg Roach            /* I18N: An option in a list-box */
222bbb76c12SGreg Roach            'table'    => I18N::translate('table'),
223bbb76c12SGreg Roach            /* I18N: An option in a list-box */
224bbb76c12SGreg Roach            'tagcloud' => I18N::translate('tag cloud'),
225c385536dSGreg Roach        ];
2268c2e8227SGreg Roach
2273caaa4d2SGreg Roach        return view('modules/top10_surnames/config', [
228c385536dSGreg Roach            'num'         => $num,
229c385536dSGreg Roach            'infoStyle'   => $infoStyle,
230c385536dSGreg Roach            'info_styles' => $info_styles,
231c385536dSGreg Roach        ]);
2328c2e8227SGreg Roach    }
233e15d3b66SRico Sonntag
234e15d3b66SRico Sonntag    /**
235e15d3b66SRico Sonntag     * Sort (lists of counts of similar) surname by total count.
236e15d3b66SRico Sonntag     *
237e15d3b66SRico Sonntag     * @param string[] $a
238e15d3b66SRico Sonntag     * @param string[] $b
239e15d3b66SRico Sonntag     *
240e15d3b66SRico Sonntag     * @return int
241e15d3b66SRico Sonntag     */
242e15d3b66SRico Sonntag    private function surnameCountSort(array $a, array $b): int
243e15d3b66SRico Sonntag    {
244e15d3b66SRico Sonntag        return array_sum($b) - array_sum($a);
245e15d3b66SRico Sonntag    }
2468c2e8227SGreg Roach}
247