xref: /webtrees/app/Module/TopSurnamesModule.php (revision e364afe4ae4e316fc4ebd53eccbaff2d29e419a5)
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;
26a45f9889SGreg Roachuse Symfony\Component\HttpFoundation\Request;
278c2e8227SGreg Roach
288c2e8227SGreg Roach/**
298c2e8227SGreg Roach * Class TopSurnamesModule
308c2e8227SGreg Roach */
3137eb8894SGreg Roachclass TopSurnamesModule extends AbstractModule implements ModuleBlockInterface
32c1010edaSGreg Roach{
3349a243cbSGreg Roach    use ModuleBlockTrait;
3449a243cbSGreg Roach
35c385536dSGreg Roach    // Default values for new blocks.
3616d6367aSGreg Roach    private const DEFAULT_NUMBER = '10';
3716d6367aSGreg Roach    private const DEFAULT_STYLE  = 'table';
38c385536dSGreg Roach
3976692c8bSGreg Roach    /**
400cfd6963SGreg Roach     * How should this module be identified in the control panel, etc.?
4176692c8bSGreg Roach     *
4276692c8bSGreg Roach     * @return string
4376692c8bSGreg Roach     */
4449a243cbSGreg Roach    public function title(): string
45c1010edaSGreg Roach    {
46bbb76c12SGreg Roach        /* I18N: Name of a module. Top=Most common */
47bbb76c12SGreg Roach        return I18N::translate('Top surnames');
488c2e8227SGreg Roach    }
498c2e8227SGreg Roach
5076692c8bSGreg Roach    /**
5176692c8bSGreg Roach     * A sentence describing what this module does.
5276692c8bSGreg Roach     *
5376692c8bSGreg Roach     * @return string
5476692c8bSGreg Roach     */
5549a243cbSGreg Roach    public function description(): string
56c1010edaSGreg Roach    {
57bbb76c12SGreg Roach        /* I18N: Description of the “Top surnames” module */
58bbb76c12SGreg Roach        return I18N::translate('A list of the most popular surnames.');
598c2e8227SGreg Roach    }
608c2e8227SGreg Roach
6176692c8bSGreg Roach    /**
6276692c8bSGreg Roach     * Generate the HTML content of this block.
6376692c8bSGreg Roach     *
64e490cd80SGreg Roach     * @param Tree     $tree
6576692c8bSGreg Roach     * @param int      $block_id
665f2ae573SGreg Roach     * @param string   $ctype
67727f238cSGreg Roach     * @param string[] $cfg
6876692c8bSGreg Roach     *
6976692c8bSGreg Roach     * @return string
7076692c8bSGreg Roach     */
715f2ae573SGreg Roach    public function getBlock(Tree $tree, int $block_id, string $ctype = '', array $cfg = []): string
72c1010edaSGreg Roach    {
7380536c2dSGreg Roach        $num       = (int) $this->getBlockSetting($block_id, 'num', self::DEFAULT_NUMBER);
74c385536dSGreg Roach        $infoStyle = $this->getBlockSetting($block_id, 'infoStyle', self::DEFAULT_STYLE);
758c2e8227SGreg Roach
76c385536dSGreg Roach        extract($cfg, EXTR_OVERWRITE);
778c2e8227SGreg Roach
7880536c2dSGreg Roach        // Use the count of base surnames.
798d7ed87eSGreg Roach        $top_surnames = DB::table('name')
808d7ed87eSGreg Roach            ->where('n_file', '=', $tree->id())
818d7ed87eSGreg Roach            ->where('n_type', '<>', '_MARNM')
828d7ed87eSGreg Roach            ->whereNotIn('n_surn', ['@N.N.', ''])
838d7ed87eSGreg Roach            ->groupBy('n_surn')
848d7ed87eSGreg Roach            ->orderByDesc(DB::raw('COUNT(n_surn)'))
858d7ed87eSGreg Roach            ->take($num)
868d7ed87eSGreg Roach            ->pluck('n_surn');
878c2e8227SGreg Roach
8813abd6f3SGreg Roach        $all_surnames = [];
8980536c2dSGreg Roach
908d7ed87eSGreg Roach        foreach ($top_surnames as $top_surname) {
918d7ed87eSGreg Roach            $variants = DB::table('name')
928d7ed87eSGreg Roach                ->where('n_file', '=', $tree->id())
938d7ed87eSGreg Roach                ->where(DB::raw('n_surn /*! COLLATE utf8_bin */'), '=', $top_surname)
948d7ed87eSGreg Roach                ->groupBy('surname')
958d7ed87eSGreg Roach                ->select([DB::raw('n_surname /*! COLLATE utf8_bin */ AS surname'), DB::raw('count(*) AS total')])
968d7ed87eSGreg Roach                ->pluck('total', 'surname')
978d7ed87eSGreg Roach                ->all();
98f8c9edfeSGreg Roach
9980536c2dSGreg Roach            $all_surnames[$top_surname] = $variants;
1008c2e8227SGreg Roach        }
1018c2e8227SGreg Roach
10267992b6aSRichard Cissee        //find a module providing individual lists
10387cca37cSGreg Roach        $module = app(ModuleService::class)->findByComponent(ModuleListInterface::class, $tree, Auth::user())->first(function (ModuleInterface $module) {
10467992b6aSRichard Cissee            return $module instanceof IndividualListModule;
10567992b6aSRichard Cissee        });
10667992b6aSRichard Cissee
1078c2e8227SGreg Roach        switch ($infoStyle) {
1088c2e8227SGreg Roach            case 'tagcloud':
10980536c2dSGreg Roach                uksort($all_surnames, [I18N::class, 'strcasecmp']);
11067992b6aSRichard Cissee                $content = FunctionsPrintLists::surnameTagCloud($all_surnames, $module, true, $tree);
1118c2e8227SGreg Roach                break;
1128c2e8227SGreg Roach            case 'list':
113a515be7cSGreg Roach                uasort($all_surnames, [$this, 'surnameCountSort']);
11467992b6aSRichard Cissee                $content = FunctionsPrintLists::surnameList($all_surnames, 1, true, $module, $tree);
1158c2e8227SGreg Roach                break;
1168c2e8227SGreg Roach            case 'array':
117a515be7cSGreg Roach                uasort($all_surnames, [$this, 'surnameCountSort']);
11867992b6aSRichard Cissee                $content = FunctionsPrintLists::surnameList($all_surnames, 2, true, $module, $tree);
1198c2e8227SGreg Roach                break;
1208c2e8227SGreg Roach            case 'table':
1218c2e8227SGreg Roach            default:
122cf184a4dSGreg Roach                $content = view('lists/surnames-table', [
1236ab54c76SGreg Roach                    'surnames' => $all_surnames,
12467992b6aSRichard Cissee                    'module'   => $module,
12567992b6aSRichard Cissee                    'families' => false,
126e490cd80SGreg Roach                    'tree'     => $tree,
1276ab54c76SGreg Roach                ]);
1288c2e8227SGreg Roach                break;
1298c2e8227SGreg Roach        }
1308c2e8227SGreg Roach
1316a8879feSGreg Roach        if ($ctype !== '') {
13280536c2dSGreg Roach            $num = count($top_surnames);
13380536c2dSGreg Roach            if ($num === 1) {
1348cbbfdceSGreg Roach                // I18N: i.e. most popular surname.
1358cbbfdceSGreg Roach                $title = I18N::translate('Top surname');
1368cbbfdceSGreg Roach            } else {
1378cbbfdceSGreg 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
1388cbbfdceSGreg Roach                $title = I18N::plural('Top %s surname', 'Top %s surnames', $num, I18N::number($num));
1398cbbfdceSGreg Roach            }
1408cbbfdceSGreg Roach
141e490cd80SGreg Roach            if ($ctype === 'gedcom' && Auth::isManager($tree)) {
142c1010edaSGreg Roach                $config_url = route('tree-page-block-edit', [
143c1010edaSGreg Roach                    'block_id' => $block_id,
144aa6f03bbSGreg Roach                    'ged'      => $tree->name(),
145c1010edaSGreg Roach                ]);
146397e599aSGreg Roach            } elseif ($ctype === 'user' && Auth::check()) {
147c1010edaSGreg Roach                $config_url = route('user-page-block-edit', [
148c1010edaSGreg Roach                    'block_id' => $block_id,
149aa6f03bbSGreg Roach                    'ged'      => $tree->name(),
150c1010edaSGreg Roach                ]);
1518cbbfdceSGreg Roach            } else {
1528cbbfdceSGreg Roach                $config_url = '';
1538cbbfdceSGreg Roach            }
1548cbbfdceSGreg Roach
155147e99aaSGreg Roach            return view('modules/block-template', [
15626684e68SGreg Roach                'block'      => str_replace('_', '-', $this->name()),
1579c6524dcSGreg Roach                'id'         => $block_id,
1588cbbfdceSGreg Roach                'config_url' => $config_url,
1598cbbfdceSGreg Roach                'title'      => $title,
1609c6524dcSGreg Roach                'content'    => $content,
1619c6524dcSGreg Roach            ]);
1628c2e8227SGreg Roach        }
163b2ce94c6SRico Sonntag
164b2ce94c6SRico Sonntag        return $content;
1658c2e8227SGreg Roach    }
1668c2e8227SGreg Roach
1678c2e8227SGreg Roach    /** {@inheritdoc} */
168c1010edaSGreg Roach    public function loadAjax(): bool
169c1010edaSGreg Roach    {
1704ecf4f82SGreg Roach        return false;
1718c2e8227SGreg Roach    }
1728c2e8227SGreg Roach
1738c2e8227SGreg Roach    /** {@inheritdoc} */
174c1010edaSGreg Roach    public function isUserBlock(): bool
175c1010edaSGreg Roach    {
1768c2e8227SGreg Roach        return true;
1778c2e8227SGreg Roach    }
1788c2e8227SGreg Roach
1798c2e8227SGreg Roach    /** {@inheritdoc} */
18063276d8fSGreg Roach    public function isTreeBlock(): bool
181c1010edaSGreg Roach    {
1828c2e8227SGreg Roach        return true;
1838c2e8227SGreg Roach    }
1848c2e8227SGreg Roach
18576692c8bSGreg Roach    /**
186a45f9889SGreg Roach     * Update the configuration for a block.
187a45f9889SGreg Roach     *
188a45f9889SGreg Roach     * @param Request $request
189a45f9889SGreg Roach     * @param int     $block_id
190a45f9889SGreg Roach     *
191a45f9889SGreg Roach     * @return void
192a45f9889SGreg Roach     */
193*e364afe4SGreg Roach    public function saveBlockConfiguration(Request $request, int $block_id): void
194a45f9889SGreg Roach    {
195a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'num', $request->get('num', self::DEFAULT_NUMBER));
196a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'infoStyle', $request->get('infoStyle', self::DEFAULT_STYLE));
197a45f9889SGreg Roach    }
198a45f9889SGreg Roach
199a45f9889SGreg Roach    /**
20076692c8bSGreg Roach     * An HTML form to edit block settings
20176692c8bSGreg Roach     *
202e490cd80SGreg Roach     * @param Tree $tree
20376692c8bSGreg Roach     * @param int  $block_id
204a9430be8SGreg Roach     *
205a9430be8SGreg Roach     * @return void
20676692c8bSGreg Roach     */
207*e364afe4SGreg Roach    public function editBlockConfiguration(Tree $tree, int $block_id): void
208c1010edaSGreg Roach    {
209c385536dSGreg Roach        $num       = $this->getBlockSetting($block_id, 'num', self::DEFAULT_NUMBER);
210c385536dSGreg Roach        $infoStyle = $this->getBlockSetting($block_id, 'infoStyle', self::DEFAULT_STYLE);
2118c2e8227SGreg Roach
212c385536dSGreg Roach        $info_styles = [
213bbb76c12SGreg Roach            /* I18N: An option in a list-box */
214bbb76c12SGreg Roach            'list'     => I18N::translate('bullet list'),
215bbb76c12SGreg Roach            /* I18N: An option in a list-box */
216bbb76c12SGreg Roach            'array'    => I18N::translate('compact list'),
217bbb76c12SGreg Roach            /* I18N: An option in a list-box */
218bbb76c12SGreg Roach            'table'    => I18N::translate('table'),
219bbb76c12SGreg Roach            /* I18N: An option in a list-box */
220bbb76c12SGreg Roach            'tagcloud' => I18N::translate('tag cloud'),
221c385536dSGreg Roach        ];
2228c2e8227SGreg Roach
223147e99aaSGreg Roach        echo view('modules/top10_surnames/config', [
224c385536dSGreg Roach            'num'         => $num,
225c385536dSGreg Roach            'infoStyle'   => $infoStyle,
226c385536dSGreg Roach            'info_styles' => $info_styles,
227c385536dSGreg Roach        ]);
2288c2e8227SGreg Roach    }
229e15d3b66SRico Sonntag
230e15d3b66SRico Sonntag    /**
231e15d3b66SRico Sonntag     * Sort (lists of counts of similar) surname by total count.
232e15d3b66SRico Sonntag     *
233e15d3b66SRico Sonntag     * @param string[] $a
234e15d3b66SRico Sonntag     * @param string[] $b
235e15d3b66SRico Sonntag     *
236e15d3b66SRico Sonntag     * @return int
237e15d3b66SRico Sonntag     */
238e15d3b66SRico Sonntag    private function surnameCountSort(array $a, array $b): int
239e15d3b66SRico Sonntag    {
240e15d3b66SRico Sonntag        return array_sum($b) - array_sum($a);
241e15d3b66SRico Sonntag    }
2428c2e8227SGreg Roach}
243