xref: /webtrees/app/Module/TopSurnamesModule.php (revision 87cca37c8b5ee0f07397179f377cdfde768951bb)
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\Module\ModuleInterface;
2567992b6aSRichard Cisseeuse Fisharebest\Webtrees\Module\IndividualListModule;
2667992b6aSRichard Cisseeuse Fisharebest\Webtrees\Services\ModuleService;
278d7ed87eSGreg Roachuse Illuminate\Database\Capsule\Manager as DB;
28a45f9889SGreg Roachuse Symfony\Component\HttpFoundation\Request;
298c2e8227SGreg Roach
308c2e8227SGreg Roach/**
318c2e8227SGreg Roach * Class TopSurnamesModule
328c2e8227SGreg Roach */
3337eb8894SGreg Roachclass TopSurnamesModule extends AbstractModule implements ModuleBlockInterface
34c1010edaSGreg Roach{
3549a243cbSGreg Roach    use ModuleBlockTrait;
3649a243cbSGreg Roach
37c385536dSGreg Roach    // Default values for new blocks.
3816d6367aSGreg Roach    private const DEFAULT_NUMBER = '10';
3916d6367aSGreg Roach    private const DEFAULT_STYLE  = 'table';
40c385536dSGreg Roach
4176692c8bSGreg Roach    /**
4276692c8bSGreg Roach     * How should this module be labelled on tabs, menus, etc.?
4376692c8bSGreg Roach     *
4476692c8bSGreg Roach     * @return string
4576692c8bSGreg Roach     */
4649a243cbSGreg Roach    public function title(): string
47c1010edaSGreg Roach    {
48bbb76c12SGreg Roach        /* I18N: Name of a module. Top=Most common */
49bbb76c12SGreg Roach        return I18N::translate('Top surnames');
508c2e8227SGreg Roach    }
518c2e8227SGreg Roach
5276692c8bSGreg Roach    /**
5376692c8bSGreg Roach     * A sentence describing what this module does.
5476692c8bSGreg Roach     *
5576692c8bSGreg Roach     * @return string
5676692c8bSGreg Roach     */
5749a243cbSGreg Roach    public function description(): string
58c1010edaSGreg Roach    {
59bbb76c12SGreg Roach        /* I18N: Description of the “Top surnames” module */
60bbb76c12SGreg Roach        return I18N::translate('A list of the most popular surnames.');
618c2e8227SGreg Roach    }
628c2e8227SGreg Roach
6376692c8bSGreg Roach    /**
6476692c8bSGreg Roach     * Generate the HTML content of this block.
6576692c8bSGreg Roach     *
66e490cd80SGreg Roach     * @param Tree     $tree
6776692c8bSGreg Roach     * @param int      $block_id
685f2ae573SGreg Roach     * @param string   $ctype
69727f238cSGreg Roach     * @param string[] $cfg
7076692c8bSGreg Roach     *
7176692c8bSGreg Roach     * @return string
7276692c8bSGreg Roach     */
735f2ae573SGreg Roach    public function getBlock(Tree $tree, int $block_id, string $ctype = '', array $cfg = []): string
74c1010edaSGreg Roach    {
7580536c2dSGreg Roach        $num       = (int) $this->getBlockSetting($block_id, 'num', self::DEFAULT_NUMBER);
76c385536dSGreg Roach        $infoStyle = $this->getBlockSetting($block_id, 'infoStyle', self::DEFAULT_STYLE);
778c2e8227SGreg Roach
78c385536dSGreg Roach        extract($cfg, EXTR_OVERWRITE);
798c2e8227SGreg Roach
8080536c2dSGreg Roach        // Use the count of base surnames.
818d7ed87eSGreg Roach        $top_surnames = DB::table('name')
828d7ed87eSGreg Roach            ->where('n_file', '=', $tree->id())
838d7ed87eSGreg Roach            ->where('n_type', '<>', '_MARNM')
848d7ed87eSGreg Roach            ->whereNotIn('n_surn', ['@N.N.', ''])
858d7ed87eSGreg Roach            ->groupBy('n_surn')
868d7ed87eSGreg Roach            ->orderByDesc(DB::raw('COUNT(n_surn)'))
878d7ed87eSGreg Roach            ->take($num)
888d7ed87eSGreg Roach            ->pluck('n_surn');
898c2e8227SGreg Roach
9013abd6f3SGreg Roach        $all_surnames = [];
9180536c2dSGreg Roach
928d7ed87eSGreg Roach        foreach ($top_surnames as $top_surname) {
938d7ed87eSGreg Roach            $variants = DB::table('name')
948d7ed87eSGreg Roach                ->where('n_file', '=', $tree->id())
958d7ed87eSGreg Roach                ->where(DB::raw('n_surn /*! COLLATE utf8_bin */'), '=', $top_surname)
968d7ed87eSGreg Roach                ->groupBy('surname')
978d7ed87eSGreg Roach                ->select([DB::raw('n_surname /*! COLLATE utf8_bin */ AS surname'), DB::raw('count(*) AS total')])
988d7ed87eSGreg Roach                ->pluck('total', 'surname')
998d7ed87eSGreg Roach                ->all();
100f8c9edfeSGreg Roach
10180536c2dSGreg Roach            $all_surnames[$top_surname] = $variants;
1028c2e8227SGreg Roach        }
1038c2e8227SGreg Roach
10467992b6aSRichard Cissee        //find a module providing individual lists
105*87cca37cSGreg Roach        $module = app(ModuleService::class)->findByComponent(ModuleListInterface::class, $tree, Auth::user())->first(function (ModuleInterface $module) {
10667992b6aSRichard Cissee            return $module instanceof IndividualListModule;
10767992b6aSRichard Cissee        });
10867992b6aSRichard Cissee
1098c2e8227SGreg Roach        switch ($infoStyle) {
1108c2e8227SGreg Roach            case 'tagcloud':
11180536c2dSGreg Roach                uksort($all_surnames, [I18N::class, 'strcasecmp']);
11267992b6aSRichard Cissee                $content = FunctionsPrintLists::surnameTagCloud($all_surnames, $module, true, $tree);
1138c2e8227SGreg Roach                break;
1148c2e8227SGreg Roach            case 'list':
115a515be7cSGreg Roach                uasort($all_surnames, [$this, 'surnameCountSort']);
11667992b6aSRichard Cissee                $content = FunctionsPrintLists::surnameList($all_surnames, 1, true, $module, $tree);
1178c2e8227SGreg Roach                break;
1188c2e8227SGreg Roach            case 'array':
119a515be7cSGreg Roach                uasort($all_surnames, [$this, 'surnameCountSort']);
12067992b6aSRichard Cissee                $content = FunctionsPrintLists::surnameList($all_surnames, 2, true, $module, $tree);
1218c2e8227SGreg Roach                break;
1228c2e8227SGreg Roach            case 'table':
1238c2e8227SGreg Roach            default:
124cf184a4dSGreg Roach                $content = view('lists/surnames-table', [
1256ab54c76SGreg Roach                    'surnames' => $all_surnames,
12667992b6aSRichard Cissee                    'module'   => $module,
12767992b6aSRichard Cissee                    'families' => false,
128e490cd80SGreg Roach                    'tree'     => $tree,
1296ab54c76SGreg Roach                ]);
1308c2e8227SGreg Roach                break;
1318c2e8227SGreg Roach        }
1328c2e8227SGreg Roach
1336a8879feSGreg Roach        if ($ctype !== '') {
13480536c2dSGreg Roach            $num = count($top_surnames);
13580536c2dSGreg Roach            if ($num === 1) {
1368cbbfdceSGreg Roach                // I18N: i.e. most popular surname.
1378cbbfdceSGreg Roach                $title = I18N::translate('Top surname');
1388cbbfdceSGreg Roach            } else {
1398cbbfdceSGreg 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
1408cbbfdceSGreg Roach                $title = I18N::plural('Top %s surname', 'Top %s surnames', $num, I18N::number($num));
1418cbbfdceSGreg Roach            }
1428cbbfdceSGreg Roach
143e490cd80SGreg Roach            if ($ctype === 'gedcom' && Auth::isManager($tree)) {
144c1010edaSGreg Roach                $config_url = route('tree-page-block-edit', [
145c1010edaSGreg Roach                    'block_id' => $block_id,
146aa6f03bbSGreg Roach                    'ged'      => $tree->name(),
147c1010edaSGreg Roach                ]);
148397e599aSGreg Roach            } elseif ($ctype === 'user' && Auth::check()) {
149c1010edaSGreg Roach                $config_url = route('user-page-block-edit', [
150c1010edaSGreg Roach                    'block_id' => $block_id,
151aa6f03bbSGreg Roach                    'ged'      => $tree->name(),
152c1010edaSGreg Roach                ]);
1538cbbfdceSGreg Roach            } else {
1548cbbfdceSGreg Roach                $config_url = '';
1558cbbfdceSGreg Roach            }
1568cbbfdceSGreg Roach
157147e99aaSGreg Roach            return view('modules/block-template', [
15826684e68SGreg Roach                'block'      => str_replace('_', '-', $this->name()),
1599c6524dcSGreg Roach                'id'         => $block_id,
1608cbbfdceSGreg Roach                'config_url' => $config_url,
1618cbbfdceSGreg Roach                'title'      => $title,
1629c6524dcSGreg Roach                'content'    => $content,
1639c6524dcSGreg Roach            ]);
1648c2e8227SGreg Roach        }
165b2ce94c6SRico Sonntag
166b2ce94c6SRico Sonntag        return $content;
1678c2e8227SGreg Roach    }
1688c2e8227SGreg Roach
1698c2e8227SGreg Roach    /** {@inheritdoc} */
170c1010edaSGreg Roach    public function loadAjax(): bool
171c1010edaSGreg Roach    {
1724ecf4f82SGreg Roach        return false;
1738c2e8227SGreg Roach    }
1748c2e8227SGreg Roach
1758c2e8227SGreg Roach    /** {@inheritdoc} */
176c1010edaSGreg Roach    public function isUserBlock(): bool
177c1010edaSGreg Roach    {
1788c2e8227SGreg Roach        return true;
1798c2e8227SGreg Roach    }
1808c2e8227SGreg Roach
1818c2e8227SGreg Roach    /** {@inheritdoc} */
18263276d8fSGreg Roach    public function isTreeBlock(): bool
183c1010edaSGreg Roach    {
1848c2e8227SGreg Roach        return true;
1858c2e8227SGreg Roach    }
1868c2e8227SGreg Roach
18776692c8bSGreg Roach    /**
188a45f9889SGreg Roach     * Update the configuration for a block.
189a45f9889SGreg Roach     *
190a45f9889SGreg Roach     * @param Request $request
191a45f9889SGreg Roach     * @param int     $block_id
192a45f9889SGreg Roach     *
193a45f9889SGreg Roach     * @return void
194a45f9889SGreg Roach     */
195a45f9889SGreg Roach    public function saveBlockConfiguration(Request $request, int $block_id)
196a45f9889SGreg Roach    {
197a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'num', $request->get('num', self::DEFAULT_NUMBER));
198a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'infoStyle', $request->get('infoStyle', self::DEFAULT_STYLE));
199a45f9889SGreg Roach    }
200a45f9889SGreg Roach
201a45f9889SGreg Roach    /**
20276692c8bSGreg Roach     * An HTML form to edit block settings
20376692c8bSGreg Roach     *
204e490cd80SGreg Roach     * @param Tree $tree
20576692c8bSGreg Roach     * @param int  $block_id
206a9430be8SGreg Roach     *
207a9430be8SGreg Roach     * @return void
20876692c8bSGreg Roach     */
209a45f9889SGreg Roach    public function editBlockConfiguration(Tree $tree, int $block_id)
210c1010edaSGreg Roach    {
211c385536dSGreg Roach        $num       = $this->getBlockSetting($block_id, 'num', self::DEFAULT_NUMBER);
212c385536dSGreg Roach        $infoStyle = $this->getBlockSetting($block_id, 'infoStyle', self::DEFAULT_STYLE);
2138c2e8227SGreg Roach
214c385536dSGreg Roach        $info_styles = [
215bbb76c12SGreg Roach            /* I18N: An option in a list-box */
216bbb76c12SGreg Roach            'list'     => I18N::translate('bullet list'),
217bbb76c12SGreg Roach            /* I18N: An option in a list-box */
218bbb76c12SGreg Roach            'array'    => I18N::translate('compact list'),
219bbb76c12SGreg Roach            /* I18N: An option in a list-box */
220bbb76c12SGreg Roach            'table'    => I18N::translate('table'),
221bbb76c12SGreg Roach            /* I18N: An option in a list-box */
222bbb76c12SGreg Roach            'tagcloud' => I18N::translate('tag cloud'),
223c385536dSGreg Roach        ];
2248c2e8227SGreg Roach
225147e99aaSGreg Roach        echo view('modules/top10_surnames/config', [
226c385536dSGreg Roach            'num'         => $num,
227c385536dSGreg Roach            'infoStyle'   => $infoStyle,
228c385536dSGreg Roach            'info_styles' => $info_styles,
229c385536dSGreg Roach        ]);
2308c2e8227SGreg Roach    }
231e15d3b66SRico Sonntag
232e15d3b66SRico Sonntag    /**
233e15d3b66SRico Sonntag     * Sort (lists of counts of similar) surname by total count.
234e15d3b66SRico Sonntag     *
235e15d3b66SRico Sonntag     * @param string[] $a
236e15d3b66SRico Sonntag     * @param string[] $b
237e15d3b66SRico Sonntag     *
238e15d3b66SRico Sonntag     * @return int
239e15d3b66SRico Sonntag     */
240e15d3b66SRico Sonntag    private function surnameCountSort(array $a, array $b): int
241e15d3b66SRico Sonntag    {
242e15d3b66SRico Sonntag        return array_sum($b) - array_sum($a);
243e15d3b66SRico Sonntag    }
2448c2e8227SGreg Roach}
245