xref: /webtrees/app/Module/TopSurnamesModule.php (revision 8fcd0d32e56ee262912bbdb593202cfd1cbc1615)
18c2e8227SGreg Roach<?php
28c2e8227SGreg Roach/**
38c2e8227SGreg Roach * webtrees: online genealogy
4*8fcd0d32SGreg 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;
2180536c2dSGreg Roachuse Fisharebest\Webtrees\Database;
223d7a8a4cSGreg Roachuse Fisharebest\Webtrees\Functions\FunctionsPrintLists;
230e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N;
24e490cd80SGreg Roachuse Fisharebest\Webtrees\Tree;
25a45f9889SGreg Roachuse Symfony\Component\HttpFoundation\Request;
268c2e8227SGreg Roach
278c2e8227SGreg Roach/**
288c2e8227SGreg Roach * Class TopSurnamesModule
298c2e8227SGreg Roach */
30c1010edaSGreg Roachclass TopSurnamesModule extends AbstractModule implements ModuleBlockInterface
31c1010edaSGreg Roach{
32c385536dSGreg Roach    // Default values for new blocks.
33a45f9889SGreg Roach    const DEFAULT_NUMBER = '10';
34c385536dSGreg Roach    const DEFAULT_STYLE  = 'table';
35c385536dSGreg Roach
3676692c8bSGreg Roach    /**
3776692c8bSGreg Roach     * How should this module be labelled on tabs, menus, etc.?
3876692c8bSGreg Roach     *
3976692c8bSGreg Roach     * @return string
4076692c8bSGreg Roach     */
418f53f488SRico Sonntag    public function getTitle(): string
42c1010edaSGreg Roach    {
43bbb76c12SGreg Roach        /* I18N: Name of a module. Top=Most common */
44bbb76c12SGreg Roach        return I18N::translate('Top surnames');
458c2e8227SGreg Roach    }
468c2e8227SGreg Roach
4776692c8bSGreg Roach    /**
4876692c8bSGreg Roach     * A sentence describing what this module does.
4976692c8bSGreg Roach     *
5076692c8bSGreg Roach     * @return string
5176692c8bSGreg Roach     */
528f53f488SRico Sonntag    public function getDescription(): string
53c1010edaSGreg Roach    {
54bbb76c12SGreg Roach        /* I18N: Description of the “Top surnames” module */
55bbb76c12SGreg Roach        return I18N::translate('A list of the most popular surnames.');
568c2e8227SGreg Roach    }
578c2e8227SGreg Roach
5876692c8bSGreg Roach    /**
5976692c8bSGreg Roach     * Generate the HTML content of this block.
6076692c8bSGreg Roach     *
61e490cd80SGreg Roach     * @param Tree     $tree
6276692c8bSGreg Roach     * @param int      $block_id
635f2ae573SGreg Roach     * @param string   $ctype
64727f238cSGreg Roach     * @param string[] $cfg
6576692c8bSGreg Roach     *
6676692c8bSGreg Roach     * @return string
6776692c8bSGreg Roach     */
685f2ae573SGreg Roach    public function getBlock(Tree $tree, int $block_id, string $ctype = '', array $cfg = []): string
69c1010edaSGreg Roach    {
7080536c2dSGreg Roach        $num       = (int) $this->getBlockSetting($block_id, 'num', self::DEFAULT_NUMBER);
71c385536dSGreg Roach        $infoStyle = $this->getBlockSetting($block_id, 'infoStyle', self::DEFAULT_STYLE);
728c2e8227SGreg Roach
73c385536dSGreg Roach        extract($cfg, EXTR_OVERWRITE);
748c2e8227SGreg Roach
7580536c2dSGreg Roach        // Use the count of base surnames.
7680536c2dSGreg Roach        $top_surnames = Database::prepare(
7780536c2dSGreg Roach            "SELECT n_surn FROM `##name`" .
7880536c2dSGreg Roach            " WHERE n_file = :tree_id AND n_type != '_MARNM' AND n_surn NOT IN ('@N.N.', '')" .
7980536c2dSGreg Roach            " GROUP BY n_surn" .
8080536c2dSGreg Roach            " ORDER BY COUNT(n_surn) DESC" .
8180536c2dSGreg Roach            " LIMIT :limit"
8280536c2dSGreg Roach        )->execute([
8372cf66d4SGreg Roach            'tree_id' => $tree->id(),
8480536c2dSGreg Roach            'limit'   => $num,
8580536c2dSGreg Roach        ])->fetchOneColumn();
868c2e8227SGreg Roach
8713abd6f3SGreg Roach        $all_surnames = [];
8880536c2dSGreg Roach        foreach ($top_surnames as $top_surname) {
8980536c2dSGreg Roach            $variants = Database::prepare(
9080536c2dSGreg Roach                "SELECT n_surname COLLATE utf8_bin, COUNT(*) FROM `##name` WHERE n_file = :tree_id AND n_surn COLLATE :collate = :surname GROUP BY 1"
9180536c2dSGreg Roach            )->execute([
9280536c2dSGreg Roach                'collate' => I18N::collation(),
9380536c2dSGreg Roach                'surname' => $top_surname,
9472cf66d4SGreg Roach                'tree_id' => $tree->id(),
9580536c2dSGreg Roach            ])->fetchAssoc();
9680536c2dSGreg Roach
97f8c9edfeSGreg Roach            $variants = array_map(function (string $count): int {
98f8c9edfeSGreg Roach                return (int) $count;
99f8c9edfeSGreg Roach            }, $variants);
100f8c9edfeSGreg Roach
10180536c2dSGreg Roach            $all_surnames[$top_surname] = $variants;
1028c2e8227SGreg Roach        }
1038c2e8227SGreg Roach
1048c2e8227SGreg Roach        switch ($infoStyle) {
1058c2e8227SGreg Roach            case 'tagcloud':
10680536c2dSGreg Roach                uksort($all_surnames, [I18N::class, 'strcasecmp']);
107e490cd80SGreg Roach                $content = FunctionsPrintLists::surnameTagCloud($all_surnames, 'individual-list', true, $tree);
1088c2e8227SGreg Roach                break;
1098c2e8227SGreg Roach            case 'list':
110a515be7cSGreg Roach                uasort($all_surnames, [$this, 'surnameCountSort']);
111e490cd80SGreg Roach                $content = FunctionsPrintLists::surnameList($all_surnames, 1, true, 'individual-list', $tree);
1128c2e8227SGreg Roach                break;
1138c2e8227SGreg Roach            case 'array':
114a515be7cSGreg Roach                uasort($all_surnames, [$this, 'surnameCountSort']);
115e490cd80SGreg Roach                $content = FunctionsPrintLists::surnameList($all_surnames, 2, true, 'individual-list', $tree);
1168c2e8227SGreg Roach                break;
1178c2e8227SGreg Roach            case 'table':
1188c2e8227SGreg Roach            default:
119cf184a4dSGreg Roach                $content = view('lists/surnames-table', [
1206ab54c76SGreg Roach                    'surnames' => $all_surnames,
1216ab54c76SGreg Roach                    'route'    => 'individual-list',
122e490cd80SGreg Roach                    'tree'     => $tree,
1236ab54c76SGreg Roach                ]);
1248c2e8227SGreg Roach                break;
1258c2e8227SGreg Roach        }
1268c2e8227SGreg Roach
1276a8879feSGreg Roach        if ($ctype !== '') {
12880536c2dSGreg Roach            $num = count($top_surnames);
12980536c2dSGreg Roach            if ($num === 1) {
1308cbbfdceSGreg Roach                // I18N: i.e. most popular surname.
1318cbbfdceSGreg Roach                $title = I18N::translate('Top surname');
1328cbbfdceSGreg Roach            } else {
1338cbbfdceSGreg 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
1348cbbfdceSGreg Roach                $title = I18N::plural('Top %s surname', 'Top %s surnames', $num, I18N::number($num));
1358cbbfdceSGreg Roach            }
1368cbbfdceSGreg Roach
137e490cd80SGreg Roach            if ($ctype === 'gedcom' && Auth::isManager($tree)) {
138c1010edaSGreg Roach                $config_url = route('tree-page-block-edit', [
139c1010edaSGreg Roach                    'block_id' => $block_id,
140aa6f03bbSGreg Roach                    'ged'      => $tree->name(),
141c1010edaSGreg Roach                ]);
142397e599aSGreg Roach            } elseif ($ctype === 'user' && Auth::check()) {
143c1010edaSGreg Roach                $config_url = route('user-page-block-edit', [
144c1010edaSGreg Roach                    'block_id' => $block_id,
145aa6f03bbSGreg Roach                    'ged'      => $tree->name(),
146c1010edaSGreg Roach                ]);
1478cbbfdceSGreg Roach            } else {
1488cbbfdceSGreg Roach                $config_url = '';
1498cbbfdceSGreg Roach            }
1508cbbfdceSGreg Roach
151147e99aaSGreg Roach            return view('modules/block-template', [
1529c6524dcSGreg Roach                'block'      => str_replace('_', '-', $this->getName()),
1539c6524dcSGreg Roach                'id'         => $block_id,
1548cbbfdceSGreg Roach                'config_url' => $config_url,
1558cbbfdceSGreg Roach                'title'      => $title,
1569c6524dcSGreg Roach                'content'    => $content,
1579c6524dcSGreg Roach            ]);
1588c2e8227SGreg Roach        }
159b2ce94c6SRico Sonntag
160b2ce94c6SRico Sonntag        return $content;
1618c2e8227SGreg Roach    }
1628c2e8227SGreg Roach
1638c2e8227SGreg Roach    /** {@inheritdoc} */
164c1010edaSGreg Roach    public function loadAjax(): bool
165c1010edaSGreg Roach    {
1664ecf4f82SGreg Roach        return false;
1678c2e8227SGreg Roach    }
1688c2e8227SGreg Roach
1698c2e8227SGreg Roach    /** {@inheritdoc} */
170c1010edaSGreg Roach    public function isUserBlock(): bool
171c1010edaSGreg Roach    {
1728c2e8227SGreg Roach        return true;
1738c2e8227SGreg Roach    }
1748c2e8227SGreg Roach
1758c2e8227SGreg Roach    /** {@inheritdoc} */
176c1010edaSGreg Roach    public function isGedcomBlock(): bool
177c1010edaSGreg Roach    {
1788c2e8227SGreg Roach        return true;
1798c2e8227SGreg Roach    }
1808c2e8227SGreg Roach
18176692c8bSGreg Roach    /**
182a45f9889SGreg Roach     * Update the configuration for a block.
183a45f9889SGreg Roach     *
184a45f9889SGreg Roach     * @param Request $request
185a45f9889SGreg Roach     * @param int     $block_id
186a45f9889SGreg Roach     *
187a45f9889SGreg Roach     * @return void
188a45f9889SGreg Roach     */
189a45f9889SGreg Roach    public function saveBlockConfiguration(Request $request, int $block_id)
190a45f9889SGreg Roach    {
191a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'num', $request->get('num', self::DEFAULT_NUMBER));
192a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'infoStyle', $request->get('infoStyle', self::DEFAULT_STYLE));
193a45f9889SGreg Roach    }
194a45f9889SGreg Roach
195a45f9889SGreg Roach    /**
19676692c8bSGreg Roach     * An HTML form to edit block settings
19776692c8bSGreg Roach     *
198e490cd80SGreg Roach     * @param Tree $tree
19976692c8bSGreg Roach     * @param int  $block_id
200a9430be8SGreg Roach     *
201a9430be8SGreg Roach     * @return void
20276692c8bSGreg Roach     */
203a45f9889SGreg Roach    public function editBlockConfiguration(Tree $tree, int $block_id)
204c1010edaSGreg Roach    {
205c385536dSGreg Roach        $num       = $this->getBlockSetting($block_id, 'num', self::DEFAULT_NUMBER);
206c385536dSGreg Roach        $infoStyle = $this->getBlockSetting($block_id, 'infoStyle', self::DEFAULT_STYLE);
2078c2e8227SGreg Roach
208c385536dSGreg Roach        $info_styles = [
209bbb76c12SGreg Roach            /* I18N: An option in a list-box */
210bbb76c12SGreg Roach            'list'     => I18N::translate('bullet list'),
211bbb76c12SGreg Roach            /* I18N: An option in a list-box */
212bbb76c12SGreg Roach            'array'    => I18N::translate('compact list'),
213bbb76c12SGreg Roach            /* I18N: An option in a list-box */
214bbb76c12SGreg Roach            'table'    => I18N::translate('table'),
215bbb76c12SGreg Roach            /* I18N: An option in a list-box */
216bbb76c12SGreg Roach            'tagcloud' => I18N::translate('tag cloud'),
217c385536dSGreg Roach        ];
2188c2e8227SGreg Roach
219147e99aaSGreg Roach        echo view('modules/top10_surnames/config', [
220c385536dSGreg Roach            'num'         => $num,
221c385536dSGreg Roach            'infoStyle'   => $infoStyle,
222c385536dSGreg Roach            'info_styles' => $info_styles,
223c385536dSGreg Roach        ]);
2248c2e8227SGreg Roach    }
225e15d3b66SRico Sonntag
226e15d3b66SRico Sonntag    /**
227e15d3b66SRico Sonntag     * Sort (lists of counts of similar) surname by total count.
228e15d3b66SRico Sonntag     *
229e15d3b66SRico Sonntag     * @param string[] $a
230e15d3b66SRico Sonntag     * @param string[] $b
231e15d3b66SRico Sonntag     *
232e15d3b66SRico Sonntag     * @return int
233e15d3b66SRico Sonntag     */
234e15d3b66SRico Sonntag    private function surnameCountSort(array $a, array $b): int
235e15d3b66SRico Sonntag    {
236e15d3b66SRico Sonntag        return array_sum($b) - array_sum($a);
237e15d3b66SRico Sonntag    }
2388c2e8227SGreg Roach}
239