xref: /webtrees/app/Module/TopSurnamesModule.php (revision ba7382720e9c9bed7002eea441dd5da97cb145ad)
18c2e8227SGreg Roach<?php
23976b470SGreg Roach
38c2e8227SGreg Roach/**
48c2e8227SGreg Roach * webtrees: online genealogy
58fcd0d32SGreg Roach * Copyright (C) 2019 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
158c2e8227SGreg Roach * along with this program. If not, see <http://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;
25e490cd80SGreg Roachuse Fisharebest\Webtrees\Tree;
2667992b6aSRichard Cisseeuse Fisharebest\Webtrees\Services\ModuleService;
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
328c2e8227SGreg Roach/**
338c2e8227SGreg Roach * Class TopSurnamesModule
348c2e8227SGreg Roach */
3537eb8894SGreg Roachclass TopSurnamesModule extends AbstractModule implements ModuleBlockInterface
36c1010edaSGreg Roach{
3749a243cbSGreg Roach    use ModuleBlockTrait;
3849a243cbSGreg Roach
39c385536dSGreg Roach    // Default values for new blocks.
4016d6367aSGreg Roach    private const DEFAULT_NUMBER = '10';
4116d6367aSGreg Roach    private const DEFAULT_STYLE  = 'table';
42c385536dSGreg Roach
432e4f3c66SGreg Roach    /** @var ModuleService */
442e4f3c66SGreg Roach    private $module_service;
452e4f3c66SGreg Roach
462e4f3c66SGreg Roach    /**
472e4f3c66SGreg Roach     * TopSurnamesModule constructor.
482e4f3c66SGreg Roach     *
492e4f3c66SGreg Roach     * @param ModuleService $module_service
502e4f3c66SGreg Roach     */
512e4f3c66SGreg Roach    public function __construct(ModuleService $module_service)
522e4f3c66SGreg Roach    {
532e4f3c66SGreg Roach        $this->module_service = $module_service;
542e4f3c66SGreg Roach    }
552e4f3c66SGreg Roach
5676692c8bSGreg Roach    /**
570cfd6963SGreg Roach     * How should this module be identified in the control panel, etc.?
5876692c8bSGreg Roach     *
5976692c8bSGreg Roach     * @return string
6076692c8bSGreg Roach     */
6149a243cbSGreg Roach    public function title(): string
62c1010edaSGreg Roach    {
63bbb76c12SGreg Roach        /* I18N: Name of a module. Top=Most common */
64bbb76c12SGreg Roach        return I18N::translate('Top surnames');
658c2e8227SGreg Roach    }
668c2e8227SGreg Roach
6776692c8bSGreg Roach    /**
6876692c8bSGreg Roach     * A sentence describing what this module does.
6976692c8bSGreg Roach     *
7076692c8bSGreg Roach     * @return string
7176692c8bSGreg Roach     */
7249a243cbSGreg Roach    public function description(): string
73c1010edaSGreg Roach    {
74bbb76c12SGreg Roach        /* I18N: Description of the “Top surnames” module */
75bbb76c12SGreg Roach        return I18N::translate('A list of the most popular surnames.');
768c2e8227SGreg Roach    }
778c2e8227SGreg Roach
7876692c8bSGreg Roach    /**
7976692c8bSGreg Roach     * Generate the HTML content of this block.
8076692c8bSGreg Roach     *
81e490cd80SGreg Roach     * @param Tree     $tree
8276692c8bSGreg Roach     * @param int      $block_id
833caaa4d2SGreg Roach     * @param string   $context
843caaa4d2SGreg Roach     * @param string[] $config
8576692c8bSGreg Roach     *
8676692c8bSGreg Roach     * @return string
8776692c8bSGreg Roach     */
883caaa4d2SGreg Roach    public function getBlock(Tree $tree, int $block_id, string $context, array $config = []): string
89c1010edaSGreg Roach    {
9080536c2dSGreg Roach        $num       = (int) $this->getBlockSetting($block_id, 'num', self::DEFAULT_NUMBER);
91c385536dSGreg Roach        $infoStyle = $this->getBlockSetting($block_id, 'infoStyle', self::DEFAULT_STYLE);
928c2e8227SGreg Roach
933caaa4d2SGreg Roach        extract($config, EXTR_OVERWRITE);
948c2e8227SGreg Roach
9580536c2dSGreg Roach        // Use the count of base surnames.
968d7ed87eSGreg Roach        $top_surnames = DB::table('name')
978d7ed87eSGreg Roach            ->where('n_file', '=', $tree->id())
988d7ed87eSGreg Roach            ->where('n_type', '<>', '_MARNM')
998d7ed87eSGreg Roach            ->whereNotIn('n_surn', ['@N.N.', ''])
1007f5c2944SGreg Roach            ->groupBy(['n_surn'])
101a69f5655SGreg Roach            ->orderByDesc(new Expression('COUNT(n_surn)'))
1028d7ed87eSGreg Roach            ->take($num)
1038d7ed87eSGreg Roach            ->pluck('n_surn');
1048c2e8227SGreg Roach
10513abd6f3SGreg Roach        $all_surnames = [];
10680536c2dSGreg Roach
1078d7ed87eSGreg Roach        foreach ($top_surnames as $top_surname) {
1088d7ed87eSGreg Roach            $variants = DB::table('name')
1098d7ed87eSGreg Roach                ->where('n_file', '=', $tree->id())
110a69f5655SGreg Roach                ->where(new Expression('n_surn /*! COLLATE utf8_bin */'), '=', $top_surname)
1117f5c2944SGreg Roach                ->groupBy(['surname'])
112a69f5655SGreg Roach                ->select([new Expression('n_surname /*! COLLATE utf8_bin */ AS surname'), new Expression('count(*) AS total')])
1138d7ed87eSGreg Roach                ->pluck('total', 'surname')
1147dc4edbcSGreg Roach                ->map(static function ($n): int {
1157dc4edbcSGreg Roach                    // Some database drivers return numeric columns strings.
1167dc4edbcSGreg Roach                    return (int) $n;
1177dc4edbcSGreg Roach                })
1188d7ed87eSGreg Roach                ->all();
119f8c9edfeSGreg Roach
12080536c2dSGreg Roach            $all_surnames[$top_surname] = $variants;
1218c2e8227SGreg Roach        }
1228c2e8227SGreg Roach
1232e4f3c66SGreg Roach        // Find a module providing individual lists.
1242e4f3c66SGreg Roach        $module = $this->module_service
1252e4f3c66SGreg Roach            ->findByComponent(ModuleListInterface::class, $tree, Auth::user())
1262e4f3c66SGreg Roach            ->first(static function (ModuleInterface $module): bool {
127*ba738272SGreg Roach                // The family list extends the individual list
128*ba738272SGreg Roach                return
129*ba738272SGreg Roach                    $module instanceof IndividualListModule &&
130*ba738272SGreg Roach                    !$module instanceof FamilyListModule;
13167992b6aSRichard Cissee            });
13267992b6aSRichard Cissee
1338c2e8227SGreg Roach        switch ($infoStyle) {
1348c2e8227SGreg Roach            case 'tagcloud':
13580536c2dSGreg Roach                uksort($all_surnames, [I18N::class, 'strcasecmp']);
13667992b6aSRichard Cissee                $content = FunctionsPrintLists::surnameTagCloud($all_surnames, $module, true, $tree);
1378c2e8227SGreg Roach                break;
1388c2e8227SGreg Roach            case 'list':
139a515be7cSGreg Roach                uasort($all_surnames, [$this, 'surnameCountSort']);
14067992b6aSRichard Cissee                $content = FunctionsPrintLists::surnameList($all_surnames, 1, true, $module, $tree);
1418c2e8227SGreg Roach                break;
1428c2e8227SGreg Roach            case 'array':
143a515be7cSGreg Roach                uasort($all_surnames, [$this, 'surnameCountSort']);
14467992b6aSRichard Cissee                $content = FunctionsPrintLists::surnameList($all_surnames, 2, true, $module, $tree);
1458c2e8227SGreg Roach                break;
1468c2e8227SGreg Roach            case 'table':
1478c2e8227SGreg Roach            default:
148cf184a4dSGreg Roach                $content = view('lists/surnames-table', [
1496ab54c76SGreg Roach                    'surnames' => $all_surnames,
15067992b6aSRichard Cissee                    'module'   => $module,
15167992b6aSRichard Cissee                    'families' => false,
152e490cd80SGreg Roach                    'tree'     => $tree,
1536ab54c76SGreg Roach                ]);
1548c2e8227SGreg Roach                break;
1558c2e8227SGreg Roach        }
1568c2e8227SGreg Roach
1573caaa4d2SGreg Roach        if ($context !== self::CONTEXT_EMBED) {
15880536c2dSGreg Roach            $num = count($top_surnames);
15980536c2dSGreg Roach            if ($num === 1) {
1608cbbfdceSGreg Roach                // I18N: i.e. most popular surname.
1618cbbfdceSGreg Roach                $title = I18N::translate('Top surname');
1628cbbfdceSGreg Roach            } else {
1638cbbfdceSGreg 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
1648cbbfdceSGreg Roach                $title = I18N::plural('Top %s surname', 'Top %s surnames', $num, I18N::number($num));
1658cbbfdceSGreg Roach            }
1668cbbfdceSGreg Roach
167147e99aaSGreg Roach            return view('modules/block-template', [
1681e7a7a28SGreg Roach                'block'      => Str::kebab($this->name()),
1699c6524dcSGreg Roach                'id'         => $block_id,
1703caaa4d2SGreg Roach                'config_url' => $this->configUrl($tree, $context, $block_id),
1718cbbfdceSGreg Roach                'title'      => $title,
1729c6524dcSGreg Roach                'content'    => $content,
1739c6524dcSGreg Roach            ]);
1748c2e8227SGreg Roach        }
175b2ce94c6SRico Sonntag
176b2ce94c6SRico Sonntag        return $content;
1778c2e8227SGreg Roach    }
1788c2e8227SGreg Roach
1793caaa4d2SGreg Roach    /**
1803caaa4d2SGreg Roach     * Should this block load asynchronously using AJAX?
1813caaa4d2SGreg Roach     *
1823caaa4d2SGreg Roach     * Simple blocks are faster in-line, more complex ones can be loaded later.
1833caaa4d2SGreg Roach     *
1843caaa4d2SGreg Roach     * @return bool
1853caaa4d2SGreg Roach     */
186c1010edaSGreg Roach    public function loadAjax(): bool
187c1010edaSGreg Roach    {
1884ecf4f82SGreg Roach        return false;
1898c2e8227SGreg Roach    }
1908c2e8227SGreg Roach
1913caaa4d2SGreg Roach    /**
1923caaa4d2SGreg Roach     * Can this block be shown on the user’s home page?
1933caaa4d2SGreg Roach     *
1943caaa4d2SGreg Roach     * @return bool
1953caaa4d2SGreg Roach     */
196c1010edaSGreg Roach    public function isUserBlock(): bool
197c1010edaSGreg Roach    {
1988c2e8227SGreg Roach        return true;
1998c2e8227SGreg Roach    }
2008c2e8227SGreg Roach
2013caaa4d2SGreg Roach    /**
2023caaa4d2SGreg Roach     * Can this block be shown on the tree’s home page?
2033caaa4d2SGreg Roach     *
2043caaa4d2SGreg Roach     * @return bool
2053caaa4d2SGreg Roach     */
20663276d8fSGreg Roach    public function isTreeBlock(): bool
207c1010edaSGreg Roach    {
2088c2e8227SGreg Roach        return true;
2098c2e8227SGreg Roach    }
2108c2e8227SGreg Roach
21176692c8bSGreg Roach    /**
212a45f9889SGreg Roach     * Update the configuration for a block.
213a45f9889SGreg Roach     *
2146ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
215a45f9889SGreg Roach     * @param int     $block_id
216a45f9889SGreg Roach     *
217a45f9889SGreg Roach     * @return void
218a45f9889SGreg Roach     */
2196ccdf4f0SGreg Roach    public function saveBlockConfiguration(ServerRequestInterface $request, int $block_id): void
220a45f9889SGreg Roach    {
221b46c87bdSGreg Roach        $params = (array) $request->getParsedBody();
222c50a90beSGreg Roach
223c50a90beSGreg Roach        $this->setBlockSetting($block_id, 'num', $params['num']);
224c50a90beSGreg Roach        $this->setBlockSetting($block_id, 'infoStyle', $params['infoStyle']);
225a45f9889SGreg Roach    }
226a45f9889SGreg Roach
227a45f9889SGreg Roach    /**
22876692c8bSGreg Roach     * An HTML form to edit block settings
22976692c8bSGreg Roach     *
230e490cd80SGreg Roach     * @param Tree $tree
23176692c8bSGreg Roach     * @param int  $block_id
232a9430be8SGreg Roach     *
2333caaa4d2SGreg Roach     * @return string
23476692c8bSGreg Roach     */
2353caaa4d2SGreg Roach    public function editBlockConfiguration(Tree $tree, int $block_id): string
236c1010edaSGreg Roach    {
237c385536dSGreg Roach        $num       = $this->getBlockSetting($block_id, 'num', self::DEFAULT_NUMBER);
238c385536dSGreg Roach        $infoStyle = $this->getBlockSetting($block_id, 'infoStyle', self::DEFAULT_STYLE);
2398c2e8227SGreg Roach
240c385536dSGreg Roach        $info_styles = [
241bbb76c12SGreg Roach            /* I18N: An option in a list-box */
242bbb76c12SGreg Roach            'list'     => I18N::translate('bullet list'),
243bbb76c12SGreg Roach            /* I18N: An option in a list-box */
244bbb76c12SGreg Roach            'array'    => I18N::translate('compact list'),
245bbb76c12SGreg Roach            /* I18N: An option in a list-box */
246bbb76c12SGreg Roach            'table'    => I18N::translate('table'),
247bbb76c12SGreg Roach            /* I18N: An option in a list-box */
248bbb76c12SGreg Roach            'tagcloud' => I18N::translate('tag cloud'),
249c385536dSGreg Roach        ];
2508c2e8227SGreg Roach
2513caaa4d2SGreg Roach        return view('modules/top10_surnames/config', [
252c385536dSGreg Roach            'num'         => $num,
253c385536dSGreg Roach            'infoStyle'   => $infoStyle,
254c385536dSGreg Roach            'info_styles' => $info_styles,
255c385536dSGreg Roach        ]);
2568c2e8227SGreg Roach    }
257e15d3b66SRico Sonntag
258e15d3b66SRico Sonntag    /**
259e15d3b66SRico Sonntag     * Sort (lists of counts of similar) surname by total count.
260e15d3b66SRico Sonntag     *
261e15d3b66SRico Sonntag     * @param string[] $a
262e15d3b66SRico Sonntag     * @param string[] $b
263e15d3b66SRico Sonntag     *
264e15d3b66SRico Sonntag     * @return int
265e15d3b66SRico Sonntag     */
266e15d3b66SRico Sonntag    private function surnameCountSort(array $a, array $b): int
267e15d3b66SRico Sonntag    {
268e15d3b66SRico Sonntag        return array_sum($b) - array_sum($a);
269e15d3b66SRico Sonntag    }
2708c2e8227SGreg Roach}
271