xref: /webtrees/app/Module/TopSurnamesModule.php (revision 3caaa4d28e5aa6cc52c831b60066ebdc346fcd50)
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;
261e7a7a28SGreg Roachuse Illuminate\Support\Str;
276ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
288c2e8227SGreg Roach
298c2e8227SGreg Roach/**
308c2e8227SGreg Roach * Class TopSurnamesModule
318c2e8227SGreg Roach */
3237eb8894SGreg Roachclass TopSurnamesModule extends AbstractModule implements ModuleBlockInterface
33c1010edaSGreg Roach{
3449a243cbSGreg Roach    use ModuleBlockTrait;
3549a243cbSGreg Roach
36c385536dSGreg Roach    // Default values for new blocks.
3716d6367aSGreg Roach    private const DEFAULT_NUMBER = '10';
3816d6367aSGreg Roach    private const DEFAULT_STYLE  = 'table';
39c385536dSGreg Roach
4076692c8bSGreg Roach    /**
410cfd6963SGreg Roach     * How should this module be identified in the control panel, etc.?
4276692c8bSGreg Roach     *
4376692c8bSGreg Roach     * @return string
4476692c8bSGreg Roach     */
4549a243cbSGreg Roach    public function title(): string
46c1010edaSGreg Roach    {
47bbb76c12SGreg Roach        /* I18N: Name of a module. Top=Most common */
48bbb76c12SGreg Roach        return I18N::translate('Top surnames');
498c2e8227SGreg Roach    }
508c2e8227SGreg Roach
5176692c8bSGreg Roach    /**
5276692c8bSGreg Roach     * A sentence describing what this module does.
5376692c8bSGreg Roach     *
5476692c8bSGreg Roach     * @return string
5576692c8bSGreg Roach     */
5649a243cbSGreg Roach    public function description(): string
57c1010edaSGreg Roach    {
58bbb76c12SGreg Roach        /* I18N: Description of the “Top surnames” module */
59bbb76c12SGreg Roach        return I18N::translate('A list of the most popular surnames.');
608c2e8227SGreg Roach    }
618c2e8227SGreg Roach
6276692c8bSGreg Roach    /**
6376692c8bSGreg Roach     * Generate the HTML content of this block.
6476692c8bSGreg Roach     *
65e490cd80SGreg Roach     * @param Tree     $tree
6676692c8bSGreg Roach     * @param int      $block_id
67*3caaa4d2SGreg Roach     * @param string   $context
68*3caaa4d2SGreg Roach     * @param string[] $config
6976692c8bSGreg Roach     *
7076692c8bSGreg Roach     * @return string
7176692c8bSGreg Roach     */
72*3caaa4d2SGreg Roach    public function getBlock(Tree $tree, int $block_id, string $context, array $config = []): string
73c1010edaSGreg Roach    {
7480536c2dSGreg Roach        $num       = (int) $this->getBlockSetting($block_id, 'num', self::DEFAULT_NUMBER);
75c385536dSGreg Roach        $infoStyle = $this->getBlockSetting($block_id, 'infoStyle', self::DEFAULT_STYLE);
768c2e8227SGreg Roach
77*3caaa4d2SGreg Roach        extract($config, EXTR_OVERWRITE);
788c2e8227SGreg Roach
7980536c2dSGreg Roach        // Use the count of base surnames.
808d7ed87eSGreg Roach        $top_surnames = DB::table('name')
818d7ed87eSGreg Roach            ->where('n_file', '=', $tree->id())
828d7ed87eSGreg Roach            ->where('n_type', '<>', '_MARNM')
838d7ed87eSGreg Roach            ->whereNotIn('n_surn', ['@N.N.', ''])
848d7ed87eSGreg Roach            ->groupBy('n_surn')
858d7ed87eSGreg Roach            ->orderByDesc(DB::raw('COUNT(n_surn)'))
868d7ed87eSGreg Roach            ->take($num)
878d7ed87eSGreg Roach            ->pluck('n_surn');
888c2e8227SGreg Roach
8913abd6f3SGreg Roach        $all_surnames = [];
9080536c2dSGreg Roach
918d7ed87eSGreg Roach        foreach ($top_surnames as $top_surname) {
928d7ed87eSGreg Roach            $variants = DB::table('name')
938d7ed87eSGreg Roach                ->where('n_file', '=', $tree->id())
948d7ed87eSGreg Roach                ->where(DB::raw('n_surn /*! COLLATE utf8_bin */'), '=', $top_surname)
958d7ed87eSGreg Roach                ->groupBy('surname')
968d7ed87eSGreg Roach                ->select([DB::raw('n_surname /*! COLLATE utf8_bin */ AS surname'), DB::raw('count(*) AS total')])
978d7ed87eSGreg Roach                ->pluck('total', 'surname')
988d7ed87eSGreg Roach                ->all();
99f8c9edfeSGreg Roach
10080536c2dSGreg Roach            $all_surnames[$top_surname] = $variants;
1018c2e8227SGreg Roach        }
1028c2e8227SGreg Roach
10367992b6aSRichard Cissee        //find a module providing individual lists
1040b93976aSGreg Roach        $module = app(ModuleService::class)->findByComponent(ModuleListInterface::class, $tree, Auth::user())->first(static function (ModuleInterface $module): bool {
10567992b6aSRichard Cissee            return $module instanceof IndividualListModule;
10667992b6aSRichard Cissee        });
10767992b6aSRichard Cissee
1088c2e8227SGreg Roach        switch ($infoStyle) {
1098c2e8227SGreg Roach            case 'tagcloud':
11080536c2dSGreg Roach                uksort($all_surnames, [I18N::class, 'strcasecmp']);
11167992b6aSRichard Cissee                $content = FunctionsPrintLists::surnameTagCloud($all_surnames, $module, true, $tree);
1128c2e8227SGreg Roach                break;
1138c2e8227SGreg Roach            case 'list':
114a515be7cSGreg Roach                uasort($all_surnames, [$this, 'surnameCountSort']);
11567992b6aSRichard Cissee                $content = FunctionsPrintLists::surnameList($all_surnames, 1, true, $module, $tree);
1168c2e8227SGreg Roach                break;
1178c2e8227SGreg Roach            case 'array':
118a515be7cSGreg Roach                uasort($all_surnames, [$this, 'surnameCountSort']);
11967992b6aSRichard Cissee                $content = FunctionsPrintLists::surnameList($all_surnames, 2, true, $module, $tree);
1208c2e8227SGreg Roach                break;
1218c2e8227SGreg Roach            case 'table':
1228c2e8227SGreg Roach            default:
123cf184a4dSGreg Roach                $content = view('lists/surnames-table', [
1246ab54c76SGreg Roach                    'surnames' => $all_surnames,
12567992b6aSRichard Cissee                    'module'   => $module,
12667992b6aSRichard Cissee                    'families' => false,
127e490cd80SGreg Roach                    'tree'     => $tree,
1286ab54c76SGreg Roach                ]);
1298c2e8227SGreg Roach                break;
1308c2e8227SGreg Roach        }
1318c2e8227SGreg Roach
132*3caaa4d2SGreg Roach        if ($context !== self::CONTEXT_EMBED) {
13380536c2dSGreg Roach            $num = count($top_surnames);
13480536c2dSGreg Roach            if ($num === 1) {
1358cbbfdceSGreg Roach                // I18N: i.e. most popular surname.
1368cbbfdceSGreg Roach                $title = I18N::translate('Top surname');
1378cbbfdceSGreg Roach            } else {
1388cbbfdceSGreg 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
1398cbbfdceSGreg Roach                $title = I18N::plural('Top %s surname', 'Top %s surnames', $num, I18N::number($num));
1408cbbfdceSGreg Roach            }
1418cbbfdceSGreg Roach
142147e99aaSGreg Roach            return view('modules/block-template', [
1431e7a7a28SGreg Roach                'block'      => Str::kebab($this->name()),
1449c6524dcSGreg Roach                'id'         => $block_id,
145*3caaa4d2SGreg Roach                'config_url' => $this->configUrl($tree, $context, $block_id),
1468cbbfdceSGreg Roach                'title'      => $title,
1479c6524dcSGreg Roach                'content'    => $content,
1489c6524dcSGreg Roach            ]);
1498c2e8227SGreg Roach        }
150b2ce94c6SRico Sonntag
151b2ce94c6SRico Sonntag        return $content;
1528c2e8227SGreg Roach    }
1538c2e8227SGreg Roach
154*3caaa4d2SGreg Roach    /**
155*3caaa4d2SGreg Roach     * Should this block load asynchronously using AJAX?
156*3caaa4d2SGreg Roach     *
157*3caaa4d2SGreg Roach     * Simple blocks are faster in-line, more complex ones can be loaded later.
158*3caaa4d2SGreg Roach     *
159*3caaa4d2SGreg Roach     * @return bool
160*3caaa4d2SGreg Roach     */
161c1010edaSGreg Roach    public function loadAjax(): bool
162c1010edaSGreg Roach    {
1634ecf4f82SGreg Roach        return false;
1648c2e8227SGreg Roach    }
1658c2e8227SGreg Roach
166*3caaa4d2SGreg Roach    /**
167*3caaa4d2SGreg Roach     * Can this block be shown on the user’s home page?
168*3caaa4d2SGreg Roach     *
169*3caaa4d2SGreg Roach     * @return bool
170*3caaa4d2SGreg Roach     */
171c1010edaSGreg Roach    public function isUserBlock(): bool
172c1010edaSGreg Roach    {
1738c2e8227SGreg Roach        return true;
1748c2e8227SGreg Roach    }
1758c2e8227SGreg Roach
176*3caaa4d2SGreg Roach    /**
177*3caaa4d2SGreg Roach     * Can this block be shown on the tree’s home page?
178*3caaa4d2SGreg Roach     *
179*3caaa4d2SGreg Roach     * @return bool
180*3caaa4d2SGreg Roach     */
18163276d8fSGreg Roach    public function isTreeBlock(): bool
182c1010edaSGreg Roach    {
1838c2e8227SGreg Roach        return true;
1848c2e8227SGreg Roach    }
1858c2e8227SGreg Roach
18676692c8bSGreg Roach    /**
187a45f9889SGreg Roach     * Update the configuration for a block.
188a45f9889SGreg Roach     *
1896ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
190a45f9889SGreg Roach     * @param int     $block_id
191a45f9889SGreg Roach     *
192a45f9889SGreg Roach     * @return void
193a45f9889SGreg Roach     */
1946ccdf4f0SGreg Roach    public function saveBlockConfiguration(ServerRequestInterface $request, int $block_id): void
195a45f9889SGreg Roach    {
196c50a90beSGreg Roach        $params = $request->getParsedBody();
197c50a90beSGreg Roach
198c50a90beSGreg Roach        $this->setBlockSetting($block_id, 'num', $params['num']);
199c50a90beSGreg Roach        $this->setBlockSetting($block_id, 'infoStyle', $params['infoStyle']);
200a45f9889SGreg Roach    }
201a45f9889SGreg Roach
202a45f9889SGreg Roach    /**
20376692c8bSGreg Roach     * An HTML form to edit block settings
20476692c8bSGreg Roach     *
205e490cd80SGreg Roach     * @param Tree $tree
20676692c8bSGreg Roach     * @param int  $block_id
207a9430be8SGreg Roach     *
208*3caaa4d2SGreg Roach     * @return string
20976692c8bSGreg Roach     */
210*3caaa4d2SGreg Roach    public function editBlockConfiguration(Tree $tree, int $block_id): string
211c1010edaSGreg Roach    {
212c385536dSGreg Roach        $num       = $this->getBlockSetting($block_id, 'num', self::DEFAULT_NUMBER);
213c385536dSGreg Roach        $infoStyle = $this->getBlockSetting($block_id, 'infoStyle', self::DEFAULT_STYLE);
2148c2e8227SGreg Roach
215c385536dSGreg Roach        $info_styles = [
216bbb76c12SGreg Roach            /* I18N: An option in a list-box */
217bbb76c12SGreg Roach            'list'     => I18N::translate('bullet list'),
218bbb76c12SGreg Roach            /* I18N: An option in a list-box */
219bbb76c12SGreg Roach            'array'    => I18N::translate('compact list'),
220bbb76c12SGreg Roach            /* I18N: An option in a list-box */
221bbb76c12SGreg Roach            'table'    => I18N::translate('table'),
222bbb76c12SGreg Roach            /* I18N: An option in a list-box */
223bbb76c12SGreg Roach            'tagcloud' => I18N::translate('tag cloud'),
224c385536dSGreg Roach        ];
2258c2e8227SGreg Roach
226*3caaa4d2SGreg Roach        return view('modules/top10_surnames/config', [
227c385536dSGreg Roach            'num'         => $num,
228c385536dSGreg Roach            'infoStyle'   => $infoStyle,
229c385536dSGreg Roach            'info_styles' => $info_styles,
230c385536dSGreg Roach        ]);
2318c2e8227SGreg Roach    }
232e15d3b66SRico Sonntag
233e15d3b66SRico Sonntag    /**
234e15d3b66SRico Sonntag     * Sort (lists of counts of similar) surname by total count.
235e15d3b66SRico Sonntag     *
236e15d3b66SRico Sonntag     * @param string[] $a
237e15d3b66SRico Sonntag     * @param string[] $b
238e15d3b66SRico Sonntag     *
239e15d3b66SRico Sonntag     * @return int
240e15d3b66SRico Sonntag     */
241e15d3b66SRico Sonntag    private function surnameCountSort(array $a, array $b): int
242e15d3b66SRico Sonntag    {
243e15d3b66SRico Sonntag        return array_sum($b) - array_sum($a);
244e15d3b66SRico Sonntag    }
2458c2e8227SGreg Roach}
246