xref: /webtrees/app/Module/TopSurnamesModule.php (revision aa6f03bb51be5a55a78fe8aed18753116689a6d3)
18c2e8227SGreg Roach<?php
28c2e8227SGreg Roach/**
38c2e8227SGreg Roach * webtrees: online genealogy
41062a142SGreg Roach * Copyright (C) 2018 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
6376692c8bSGreg Roach     * @param bool     $template
64727f238cSGreg Roach     * @param string[] $cfg
6576692c8bSGreg Roach     *
6676692c8bSGreg Roach     * @return string
6776692c8bSGreg Roach     */
68c1010edaSGreg Roach    public function getBlock(Tree $tree, int $block_id, bool $template = true, array $cfg = []): string
69c1010edaSGreg Roach    {
70e490cd80SGreg Roach        global $ctype;
718c2e8227SGreg Roach
7280536c2dSGreg Roach        $num       = (int) $this->getBlockSetting($block_id, 'num', self::DEFAULT_NUMBER);
73c385536dSGreg Roach        $infoStyle = $this->getBlockSetting($block_id, 'infoStyle', self::DEFAULT_STYLE);
748c2e8227SGreg Roach
75c385536dSGreg Roach        extract($cfg, EXTR_OVERWRITE);
768c2e8227SGreg Roach
7780536c2dSGreg Roach        // Use the count of base surnames.
7880536c2dSGreg Roach        $top_surnames = Database::prepare(
7980536c2dSGreg Roach            "SELECT n_surn FROM `##name`" .
8080536c2dSGreg Roach            " WHERE n_file = :tree_id AND n_type != '_MARNM' AND n_surn NOT IN ('@N.N.', '')" .
8180536c2dSGreg Roach            " GROUP BY n_surn" .
8280536c2dSGreg Roach            " ORDER BY COUNT(n_surn) DESC" .
8380536c2dSGreg Roach            " LIMIT :limit"
8480536c2dSGreg Roach        )->execute([
8572cf66d4SGreg Roach            'tree_id' => $tree->id(),
8680536c2dSGreg Roach            'limit'   => $num,
8780536c2dSGreg Roach        ])->fetchOneColumn();
888c2e8227SGreg Roach
8913abd6f3SGreg Roach        $all_surnames = [];
9080536c2dSGreg Roach        foreach ($top_surnames as $top_surname) {
9180536c2dSGreg Roach            $variants = Database::prepare(
9280536c2dSGreg Roach                "SELECT n_surname COLLATE utf8_bin, COUNT(*) FROM `##name` WHERE n_file = :tree_id AND n_surn COLLATE :collate = :surname GROUP BY 1"
9380536c2dSGreg Roach            )->execute([
9480536c2dSGreg Roach                'collate' => I18N::collation(),
9580536c2dSGreg Roach                'surname' => $top_surname,
9672cf66d4SGreg Roach                'tree_id' => $tree->id(),
9780536c2dSGreg Roach            ])->fetchAssoc();
9880536c2dSGreg Roach
99f8c9edfeSGreg Roach            $variants = array_map(function (string $count): int {
100f8c9edfeSGreg Roach                return (int) $count;
101f8c9edfeSGreg Roach            }, $variants);
102f8c9edfeSGreg Roach
10380536c2dSGreg Roach            $all_surnames[$top_surname] = $variants;
1048c2e8227SGreg Roach        }
1058c2e8227SGreg Roach
1068c2e8227SGreg Roach        switch ($infoStyle) {
1078c2e8227SGreg Roach            case 'tagcloud':
10880536c2dSGreg Roach                uksort($all_surnames, [I18N::class, 'strcasecmp']);
109e490cd80SGreg Roach                $content = FunctionsPrintLists::surnameTagCloud($all_surnames, 'individual-list', true, $tree);
1108c2e8227SGreg Roach                break;
1118c2e8227SGreg Roach            case 'list':
112a515be7cSGreg Roach                uasort($all_surnames, [$this, 'surnameCountSort']);
113e490cd80SGreg Roach                $content = FunctionsPrintLists::surnameList($all_surnames, 1, true, 'individual-list', $tree);
1148c2e8227SGreg Roach                break;
1158c2e8227SGreg Roach            case 'array':
116a515be7cSGreg Roach                uasort($all_surnames, [$this, 'surnameCountSort']);
117e490cd80SGreg Roach                $content = FunctionsPrintLists::surnameList($all_surnames, 2, true, 'individual-list', $tree);
1188c2e8227SGreg Roach                break;
1198c2e8227SGreg Roach            case 'table':
1208c2e8227SGreg Roach            default:
121cf184a4dSGreg Roach                $content = view('lists/surnames-table', [
1226ab54c76SGreg Roach                    'surnames' => $all_surnames,
1236ab54c76SGreg Roach                    'route'    => 'individual-list',
124e490cd80SGreg Roach                    'tree'     => $tree,
1256ab54c76SGreg Roach                ]);
1268c2e8227SGreg Roach                break;
1278c2e8227SGreg Roach        }
1288c2e8227SGreg Roach
1298c2e8227SGreg Roach        if ($template) {
13080536c2dSGreg Roach            $num = count($top_surnames);
13180536c2dSGreg Roach            if ($num === 1) {
1328cbbfdceSGreg Roach                // I18N: i.e. most popular surname.
1338cbbfdceSGreg Roach                $title = I18N::translate('Top surname');
1348cbbfdceSGreg Roach            } else {
1358cbbfdceSGreg 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
1368cbbfdceSGreg Roach                $title = I18N::plural('Top %s surname', 'Top %s surnames', $num, I18N::number($num));
1378cbbfdceSGreg Roach            }
1388cbbfdceSGreg Roach
139e490cd80SGreg Roach            if ($ctype === 'gedcom' && Auth::isManager($tree)) {
140c1010edaSGreg Roach                $config_url = route('tree-page-block-edit', [
141c1010edaSGreg Roach                    'block_id' => $block_id,
142*aa6f03bbSGreg Roach                    'ged'      => $tree->name(),
143c1010edaSGreg Roach                ]);
144397e599aSGreg Roach            } elseif ($ctype === 'user' && Auth::check()) {
145c1010edaSGreg Roach                $config_url = route('user-page-block-edit', [
146c1010edaSGreg Roach                    'block_id' => $block_id,
147*aa6f03bbSGreg Roach                    'ged'      => $tree->name(),
148c1010edaSGreg Roach                ]);
1498cbbfdceSGreg Roach            } else {
1508cbbfdceSGreg Roach                $config_url = '';
1518cbbfdceSGreg Roach            }
1528cbbfdceSGreg Roach
153147e99aaSGreg Roach            return view('modules/block-template', [
1549c6524dcSGreg Roach                'block'      => str_replace('_', '-', $this->getName()),
1559c6524dcSGreg Roach                'id'         => $block_id,
1568cbbfdceSGreg Roach                'config_url' => $config_url,
1578cbbfdceSGreg Roach                'title'      => $title,
1589c6524dcSGreg Roach                'content'    => $content,
1599c6524dcSGreg Roach            ]);
1608c2e8227SGreg Roach        }
161b2ce94c6SRico Sonntag
162b2ce94c6SRico Sonntag        return $content;
1638c2e8227SGreg Roach    }
1648c2e8227SGreg Roach
1658c2e8227SGreg Roach    /** {@inheritdoc} */
166c1010edaSGreg Roach    public function loadAjax(): bool
167c1010edaSGreg Roach    {
1684ecf4f82SGreg Roach        return false;
1698c2e8227SGreg Roach    }
1708c2e8227SGreg Roach
1718c2e8227SGreg Roach    /** {@inheritdoc} */
172c1010edaSGreg Roach    public function isUserBlock(): bool
173c1010edaSGreg Roach    {
1748c2e8227SGreg Roach        return true;
1758c2e8227SGreg Roach    }
1768c2e8227SGreg Roach
1778c2e8227SGreg Roach    /** {@inheritdoc} */
178c1010edaSGreg Roach    public function isGedcomBlock(): bool
179c1010edaSGreg Roach    {
1808c2e8227SGreg Roach        return true;
1818c2e8227SGreg Roach    }
1828c2e8227SGreg Roach
18376692c8bSGreg Roach    /**
184a45f9889SGreg Roach     * Update the configuration for a block.
185a45f9889SGreg Roach     *
186a45f9889SGreg Roach     * @param Request $request
187a45f9889SGreg Roach     * @param int     $block_id
188a45f9889SGreg Roach     *
189a45f9889SGreg Roach     * @return void
190a45f9889SGreg Roach     */
191a45f9889SGreg Roach    public function saveBlockConfiguration(Request $request, int $block_id)
192a45f9889SGreg Roach    {
193a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'num', $request->get('num', self::DEFAULT_NUMBER));
194a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'infoStyle', $request->get('infoStyle', self::DEFAULT_STYLE));
195a45f9889SGreg Roach    }
196a45f9889SGreg Roach
197a45f9889SGreg Roach    /**
19876692c8bSGreg Roach     * An HTML form to edit block settings
19976692c8bSGreg Roach     *
200e490cd80SGreg Roach     * @param Tree $tree
20176692c8bSGreg Roach     * @param int  $block_id
202a9430be8SGreg Roach     *
203a9430be8SGreg Roach     * @return void
20476692c8bSGreg Roach     */
205a45f9889SGreg Roach    public function editBlockConfiguration(Tree $tree, int $block_id)
206c1010edaSGreg Roach    {
207c385536dSGreg Roach        $num       = $this->getBlockSetting($block_id, 'num', self::DEFAULT_NUMBER);
208c385536dSGreg Roach        $infoStyle = $this->getBlockSetting($block_id, 'infoStyle', self::DEFAULT_STYLE);
2098c2e8227SGreg Roach
210c385536dSGreg Roach        $info_styles = [
211bbb76c12SGreg Roach            /* I18N: An option in a list-box */
212bbb76c12SGreg Roach            'list'     => I18N::translate('bullet list'),
213bbb76c12SGreg Roach            /* I18N: An option in a list-box */
214bbb76c12SGreg Roach            'array'    => I18N::translate('compact list'),
215bbb76c12SGreg Roach            /* I18N: An option in a list-box */
216bbb76c12SGreg Roach            'table'    => I18N::translate('table'),
217bbb76c12SGreg Roach            /* I18N: An option in a list-box */
218bbb76c12SGreg Roach            'tagcloud' => I18N::translate('tag cloud'),
219c385536dSGreg Roach        ];
2208c2e8227SGreg Roach
221147e99aaSGreg Roach        echo view('modules/top10_surnames/config', [
222c385536dSGreg Roach            'num'         => $num,
223c385536dSGreg Roach            'infoStyle'   => $infoStyle,
224c385536dSGreg Roach            'info_styles' => $info_styles,
225c385536dSGreg Roach        ]);
2268c2e8227SGreg Roach    }
227e15d3b66SRico Sonntag
228e15d3b66SRico Sonntag    /**
229e15d3b66SRico Sonntag     * Sort (lists of counts of similar) surname by total count.
230e15d3b66SRico Sonntag     *
231e15d3b66SRico Sonntag     * @param string[] $a
232e15d3b66SRico Sonntag     * @param string[] $b
233e15d3b66SRico Sonntag     *
234e15d3b66SRico Sonntag     * @return int
235e15d3b66SRico Sonntag     */
236e15d3b66SRico Sonntag    private function surnameCountSort(array $a, array $b): int
237e15d3b66SRico Sonntag    {
238e15d3b66SRico Sonntag        return array_sum($b) - array_sum($a);
239e15d3b66SRico Sonntag    }
2408c2e8227SGreg Roach}
241