xref: /webtrees/app/Module/TopSurnamesModule.php (revision 8f53f488f13e53e44dc48778e8f51ec9f91352dd)
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 */
1676692c8bSGreg Roachnamespace Fisharebest\Webtrees\Module;
1776692c8bSGreg Roach
180e62c4b8SGreg Roachuse Fisharebest\Webtrees\Auth;
1980536c2dSGreg Roachuse Fisharebest\Webtrees\Database;
203d7a8a4cSGreg Roachuse Fisharebest\Webtrees\Functions\FunctionsPrintLists;
210e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N;
22e490cd80SGreg Roachuse Fisharebest\Webtrees\Tree;
23a45f9889SGreg Roachuse Symfony\Component\HttpFoundation\Request;
248c2e8227SGreg Roach
258c2e8227SGreg Roach/**
268c2e8227SGreg Roach * Class TopSurnamesModule
278c2e8227SGreg Roach */
28c1010edaSGreg Roachclass TopSurnamesModule extends AbstractModule implements ModuleBlockInterface
29c1010edaSGreg Roach{
30c385536dSGreg Roach    // Default values for new blocks.
31a45f9889SGreg Roach    const DEFAULT_NUMBER = '10';
32c385536dSGreg Roach    const DEFAULT_STYLE  = 'table';
33c385536dSGreg Roach
3476692c8bSGreg Roach    /**
3576692c8bSGreg Roach     * How should this module be labelled on tabs, menus, etc.?
3676692c8bSGreg Roach     *
3776692c8bSGreg Roach     * @return string
3876692c8bSGreg Roach     */
39*8f53f488SRico Sonntag    public function getTitle(): string
40c1010edaSGreg Roach    {
41bbb76c12SGreg Roach        /* I18N: Name of a module. Top=Most common */
42bbb76c12SGreg Roach        return I18N::translate('Top surnames');
438c2e8227SGreg Roach    }
448c2e8227SGreg Roach
4576692c8bSGreg Roach    /**
4676692c8bSGreg Roach     * A sentence describing what this module does.
4776692c8bSGreg Roach     *
4876692c8bSGreg Roach     * @return string
4976692c8bSGreg Roach     */
50*8f53f488SRico Sonntag    public function getDescription(): string
51c1010edaSGreg Roach    {
52bbb76c12SGreg Roach        /* I18N: Description of the “Top surnames” module */
53bbb76c12SGreg Roach        return I18N::translate('A list of the most popular surnames.');
548c2e8227SGreg Roach    }
558c2e8227SGreg Roach
5676692c8bSGreg Roach    /**
5776692c8bSGreg Roach     * Generate the HTML content of this block.
5876692c8bSGreg Roach     *
59e490cd80SGreg Roach     * @param Tree     $tree
6076692c8bSGreg Roach     * @param int      $block_id
6176692c8bSGreg Roach     * @param bool     $template
62727f238cSGreg Roach     * @param string[] $cfg
6376692c8bSGreg Roach     *
6476692c8bSGreg Roach     * @return string
6576692c8bSGreg Roach     */
66c1010edaSGreg Roach    public function getBlock(Tree $tree, int $block_id, bool $template = true, array $cfg = []): string
67c1010edaSGreg Roach    {
68e490cd80SGreg Roach        global $ctype;
698c2e8227SGreg 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([
8380536c2dSGreg Roach            'tree_id' => $tree->getTreeId(),
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,
9480536c2dSGreg Roach                'tree_id' => $tree->getTreeId(),
9580536c2dSGreg Roach            ])->fetchAssoc();
9680536c2dSGreg Roach
9780536c2dSGreg Roach            $all_surnames[$top_surname] = $variants;
988c2e8227SGreg Roach        }
998c2e8227SGreg Roach
1008c2e8227SGreg Roach        switch ($infoStyle) {
1018c2e8227SGreg Roach            case 'tagcloud':
10280536c2dSGreg Roach                uksort($all_surnames, [I18N::class, 'strcasecmp']);
103e490cd80SGreg Roach                $content = FunctionsPrintLists::surnameTagCloud($all_surnames, 'individual-list', true, $tree);
1048c2e8227SGreg Roach                break;
1058c2e8227SGreg Roach            case 'list':
106e490cd80SGreg Roach                $content = FunctionsPrintLists::surnameList($all_surnames, 1, true, 'individual-list', $tree);
1078c2e8227SGreg Roach                break;
1088c2e8227SGreg Roach            case 'array':
109e490cd80SGreg Roach                $content = FunctionsPrintLists::surnameList($all_surnames, 2, true, 'individual-list', $tree);
1108c2e8227SGreg Roach                break;
1118c2e8227SGreg Roach            case 'table':
1128c2e8227SGreg Roach            default:
113cf184a4dSGreg Roach                $content = view('lists/surnames-table', [
1146ab54c76SGreg Roach                    'surnames' => $all_surnames,
1156ab54c76SGreg Roach                    'route'    => 'individual-list',
116e490cd80SGreg Roach                    'tree'     => $tree,
1176ab54c76SGreg Roach                ]);
1188c2e8227SGreg Roach                break;
1198c2e8227SGreg Roach        }
1208c2e8227SGreg Roach
1218c2e8227SGreg Roach        if ($template) {
12280536c2dSGreg Roach            $num = count($top_surnames);
12380536c2dSGreg Roach            if ($num === 1) {
1248cbbfdceSGreg Roach                // I18N: i.e. most popular surname.
1258cbbfdceSGreg Roach                $title = I18N::translate('Top surname');
1268cbbfdceSGreg Roach            } else {
1278cbbfdceSGreg 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
1288cbbfdceSGreg Roach                $title = I18N::plural('Top %s surname', 'Top %s surnames', $num, I18N::number($num));
1298cbbfdceSGreg Roach            }
1308cbbfdceSGreg Roach
131e490cd80SGreg Roach            if ($ctype === 'gedcom' && Auth::isManager($tree)) {
132c1010edaSGreg Roach                $config_url = route('tree-page-block-edit', [
133c1010edaSGreg Roach                    'block_id' => $block_id,
134c1010edaSGreg Roach                    'ged'      => $tree->getName(),
135c1010edaSGreg Roach                ]);
136397e599aSGreg Roach            } elseif ($ctype === 'user' && Auth::check()) {
137c1010edaSGreg Roach                $config_url = route('user-page-block-edit', [
138c1010edaSGreg Roach                    'block_id' => $block_id,
139c1010edaSGreg Roach                    'ged'      => $tree->getName(),
140c1010edaSGreg Roach                ]);
1418cbbfdceSGreg Roach            } else {
1428cbbfdceSGreg Roach                $config_url = '';
1438cbbfdceSGreg Roach            }
1448cbbfdceSGreg Roach
145147e99aaSGreg Roach            return view('modules/block-template', [
1469c6524dcSGreg Roach                'block'      => str_replace('_', '-', $this->getName()),
1479c6524dcSGreg Roach                'id'         => $block_id,
1488cbbfdceSGreg Roach                'config_url' => $config_url,
1498cbbfdceSGreg Roach                'title'      => $title,
1509c6524dcSGreg Roach                'content'    => $content,
1519c6524dcSGreg Roach            ]);
1528c2e8227SGreg Roach        } else {
1538c2e8227SGreg Roach            return $content;
1548c2e8227SGreg Roach        }
1558c2e8227SGreg Roach    }
1568c2e8227SGreg Roach
1578c2e8227SGreg Roach    /** {@inheritdoc} */
158c1010edaSGreg Roach    public function loadAjax(): bool
159c1010edaSGreg Roach    {
1604ecf4f82SGreg Roach        return false;
1618c2e8227SGreg Roach    }
1628c2e8227SGreg Roach
1638c2e8227SGreg Roach    /** {@inheritdoc} */
164c1010edaSGreg Roach    public function isUserBlock(): bool
165c1010edaSGreg Roach    {
1668c2e8227SGreg Roach        return true;
1678c2e8227SGreg Roach    }
1688c2e8227SGreg Roach
1698c2e8227SGreg Roach    /** {@inheritdoc} */
170c1010edaSGreg Roach    public function isGedcomBlock(): bool
171c1010edaSGreg Roach    {
1728c2e8227SGreg Roach        return true;
1738c2e8227SGreg Roach    }
1748c2e8227SGreg Roach
17576692c8bSGreg Roach    /**
176a45f9889SGreg Roach     * Update the configuration for a block.
177a45f9889SGreg Roach     *
178a45f9889SGreg Roach     * @param Request $request
179a45f9889SGreg Roach     * @param int     $block_id
180a45f9889SGreg Roach     *
181a45f9889SGreg Roach     * @return void
182a45f9889SGreg Roach     */
183a45f9889SGreg Roach    public function saveBlockConfiguration(Request $request, int $block_id)
184a45f9889SGreg Roach    {
185a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'num', $request->get('num', self::DEFAULT_NUMBER));
186a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'infoStyle', $request->get('infoStyle', self::DEFAULT_STYLE));
187a45f9889SGreg Roach    }
188a45f9889SGreg Roach
189a45f9889SGreg Roach    /**
19076692c8bSGreg Roach     * An HTML form to edit block settings
19176692c8bSGreg Roach     *
192e490cd80SGreg Roach     * @param Tree $tree
19376692c8bSGreg Roach     * @param int  $block_id
194a9430be8SGreg Roach     *
195a9430be8SGreg Roach     * @return void
19676692c8bSGreg Roach     */
197a45f9889SGreg Roach    public function editBlockConfiguration(Tree $tree, int $block_id)
198c1010edaSGreg Roach    {
199c385536dSGreg Roach        $num       = $this->getBlockSetting($block_id, 'num', self::DEFAULT_NUMBER);
200c385536dSGreg Roach        $infoStyle = $this->getBlockSetting($block_id, 'infoStyle', self::DEFAULT_STYLE);
2018c2e8227SGreg Roach
202c385536dSGreg Roach        $info_styles = [
203bbb76c12SGreg Roach            /* I18N: An option in a list-box */
204bbb76c12SGreg Roach            'list'     => I18N::translate('bullet list'),
205bbb76c12SGreg Roach            /* I18N: An option in a list-box */
206bbb76c12SGreg Roach            'array'    => I18N::translate('compact list'),
207bbb76c12SGreg Roach            /* I18N: An option in a list-box */
208bbb76c12SGreg Roach            'table'    => I18N::translate('table'),
209bbb76c12SGreg Roach            /* I18N: An option in a list-box */
210bbb76c12SGreg Roach            'tagcloud' => I18N::translate('tag cloud'),
211c385536dSGreg Roach        ];
2128c2e8227SGreg Roach
213147e99aaSGreg Roach        echo view('modules/top10_surnames/config', [
214c385536dSGreg Roach            'num'         => $num,
215c385536dSGreg Roach            'infoStyle'   => $infoStyle,
216c385536dSGreg Roach            'info_styles' => $info_styles,
217c385536dSGreg Roach        ]);
2188c2e8227SGreg Roach    }
2198c2e8227SGreg Roach}
220