xref: /webtrees/app/Module/TopGivenNamesModule.php (revision 8f53f488f13e53e44dc48778e8f51ec9f91352dd)
13763c3f2SGreg Roach<?php
23763c3f2SGreg Roach/**
33763c3f2SGreg Roach * webtrees: online genealogy
41062a142SGreg Roach * Copyright (C) 2018 webtrees development team
53763c3f2SGreg Roach * This program is free software: you can redistribute it and/or modify
63763c3f2SGreg Roach * it under the terms of the GNU General Public License as published by
73763c3f2SGreg Roach * the Free Software Foundation, either version 3 of the License, or
83763c3f2SGreg Roach * (at your option) any later version.
93763c3f2SGreg Roach * This program is distributed in the hope that it will be useful,
103763c3f2SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
113763c3f2SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
123763c3f2SGreg Roach * GNU General Public License for more details.
133763c3f2SGreg Roach * You should have received a copy of the GNU General Public License
143763c3f2SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
153763c3f2SGreg Roach */
1676692c8bSGreg Roachnamespace Fisharebest\Webtrees\Module;
1776692c8bSGreg Roach
180e62c4b8SGreg Roachuse Fisharebest\Webtrees\Auth;
190e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N;
200e62c4b8SGreg Roachuse Fisharebest\Webtrees\Stats;
21e490cd80SGreg Roachuse Fisharebest\Webtrees\Tree;
22a45f9889SGreg Roachuse Symfony\Component\HttpFoundation\Request;
233763c3f2SGreg Roach
243763c3f2SGreg Roach/**
253763c3f2SGreg Roach * Class TopGivenNamesModule
263763c3f2SGreg Roach */
27c1010edaSGreg Roachclass TopGivenNamesModule extends AbstractModule implements ModuleBlockInterface
28c1010edaSGreg Roach{
293763c3f2SGreg Roach    /** {@inheritdoc} */
30*8f53f488SRico Sonntag    public function getTitle(): string
31c1010edaSGreg Roach    {
32bbb76c12SGreg Roach        /* I18N: Name of a module. Top=Most common */
33bbb76c12SGreg Roach        return I18N::translate('Top given names');
343763c3f2SGreg Roach    }
353763c3f2SGreg Roach
363763c3f2SGreg Roach    /** {@inheritdoc} */
37*8f53f488SRico Sonntag    public function getDescription(): string
38c1010edaSGreg Roach    {
39bbb76c12SGreg Roach        /* I18N: Description of the “Top given names” module */
40bbb76c12SGreg Roach        return I18N::translate('A list of the most popular given names.');
413763c3f2SGreg Roach    }
423763c3f2SGreg Roach
4376692c8bSGreg Roach    /**
4476692c8bSGreg Roach     * Generate the HTML content of this block.
4576692c8bSGreg Roach     *
46e490cd80SGreg Roach     * @param Tree     $tree
4776692c8bSGreg Roach     * @param int      $block_id
4876692c8bSGreg Roach     * @param bool     $template
49727f238cSGreg Roach     * @param string[] $cfg
5076692c8bSGreg Roach     *
5176692c8bSGreg Roach     * @return string
5276692c8bSGreg Roach     */
53c1010edaSGreg Roach    public function getBlock(Tree $tree, int $block_id, bool $template = true, array $cfg = []): string
54c1010edaSGreg Roach    {
55e490cd80SGreg Roach        global $ctype;
563763c3f2SGreg Roach
57e2a378d3SGreg Roach        $num       = $this->getBlockSetting($block_id, 'num', '10');
58e2a378d3SGreg Roach        $infoStyle = $this->getBlockSetting($block_id, 'infoStyle', 'table');
593763c3f2SGreg Roach
60c385536dSGreg Roach        extract($cfg, EXTR_OVERWRITE);
613763c3f2SGreg Roach
62e490cd80SGreg Roach        $stats = new Stats($tree);
633763c3f2SGreg Roach
643763c3f2SGreg Roach        switch ($infoStyle) {
6545831e7fSGreg Roach            case 'list':
66c1010edaSGreg Roach                $males   = $stats->commonGivenMaleTotals([
67c1010edaSGreg Roach                    1,
68c1010edaSGreg Roach                    $num,
69c1010edaSGreg Roach                    'rcount',
70c1010edaSGreg Roach                ]);
71c1010edaSGreg Roach                $females = $stats->commonGivenFemaleTotals([
72c1010edaSGreg Roach                    1,
73c1010edaSGreg Roach                    $num,
74c1010edaSGreg Roach                    'rcount',
75c1010edaSGreg Roach                ]);
76147e99aaSGreg Roach                $content = view('modules/top10_givnnames/list', [
7745831e7fSGreg Roach                    'males'   => $males,
7845831e7fSGreg Roach                    'females' => $females,
7945831e7fSGreg Roach                ]);
803763c3f2SGreg Roach                break;
8145831e7fSGreg Roach            default:
8245831e7fSGreg Roach            case 'table':
83c1010edaSGreg Roach                $males   = $stats->commonGivenMaleTable([
84c1010edaSGreg Roach                    1,
85c1010edaSGreg Roach                    $num,
86c1010edaSGreg Roach                    'rcount',
87c1010edaSGreg Roach                ]);
88c1010edaSGreg Roach                $females = $stats->commonGivenFemaleTable([
89c1010edaSGreg Roach                    1,
90c1010edaSGreg Roach                    $num,
91c1010edaSGreg Roach                    'rcount',
92c1010edaSGreg Roach                ]);
93147e99aaSGreg Roach                $content = view('modules/top10_givnnames/table', [
9445831e7fSGreg Roach                    'males'   => $males,
9545831e7fSGreg Roach                    'females' => $females,
9645831e7fSGreg Roach                ]);
973763c3f2SGreg Roach                break;
983763c3f2SGreg Roach        }
993763c3f2SGreg Roach
1003763c3f2SGreg Roach        if ($template) {
1018cbbfdceSGreg Roach            if ($num == 1) {
1028cbbfdceSGreg Roach                // I18N: i.e. most popular given name.
1038cbbfdceSGreg Roach                $title = I18N::translate('Top given name');
1048cbbfdceSGreg Roach            } else {
1058cbbfdceSGreg Roach                // I18N: Title for a list of the most common given names, %s is a number. Note that a separate translation exists when %s is 1
1068cbbfdceSGreg Roach                $title = I18N::plural('Top %s given name', 'Top %s given names', $num, I18N::number($num));
1078cbbfdceSGreg Roach            }
1088cbbfdceSGreg Roach
109e490cd80SGreg Roach            if ($ctype === 'gedcom' && Auth::isManager($tree)) {
110c1010edaSGreg Roach                $config_url = route('tree-page-block-edit', [
111c1010edaSGreg Roach                    'block_id' => $block_id,
112c1010edaSGreg Roach                    'ged'      => $tree->getName(),
113c1010edaSGreg Roach                ]);
114397e599aSGreg Roach            } elseif ($ctype === 'user' && Auth::check()) {
115c1010edaSGreg Roach                $config_url = route('user-page-block-edit', [
116c1010edaSGreg Roach                    'block_id' => $block_id,
117c1010edaSGreg Roach                    'ged'      => $tree->getName(),
118c1010edaSGreg Roach                ]);
1198cbbfdceSGreg Roach            } else {
1208cbbfdceSGreg Roach                $config_url = '';
1218cbbfdceSGreg Roach            }
1228cbbfdceSGreg Roach
123147e99aaSGreg Roach            return view('modules/block-template', [
1249c6524dcSGreg Roach                'block'      => str_replace('_', '-', $this->getName()),
1259c6524dcSGreg Roach                'id'         => $block_id,
1268cbbfdceSGreg Roach                'config_url' => $config_url,
1278cbbfdceSGreg Roach                'title'      => $title,
1289c6524dcSGreg Roach                'content'    => $content,
1299c6524dcSGreg Roach            ]);
1303763c3f2SGreg Roach        } else {
1313763c3f2SGreg Roach            return $content;
1323763c3f2SGreg Roach        }
1333763c3f2SGreg Roach    }
1343763c3f2SGreg Roach
1353763c3f2SGreg Roach    /** {@inheritdoc} */
136c1010edaSGreg Roach    public function loadAjax(): bool
137c1010edaSGreg Roach    {
1384ecf4f82SGreg Roach        return false;
1393763c3f2SGreg Roach    }
1403763c3f2SGreg Roach
1413763c3f2SGreg Roach    /** {@inheritdoc} */
142c1010edaSGreg Roach    public function isUserBlock(): bool
143c1010edaSGreg Roach    {
1443763c3f2SGreg Roach        return true;
1453763c3f2SGreg Roach    }
1463763c3f2SGreg Roach
1473763c3f2SGreg Roach    /** {@inheritdoc} */
148c1010edaSGreg Roach    public function isGedcomBlock(): bool
149c1010edaSGreg Roach    {
1503763c3f2SGreg Roach        return true;
1513763c3f2SGreg Roach    }
1523763c3f2SGreg Roach
15376692c8bSGreg Roach    /**
154a45f9889SGreg Roach     * Update the configuration for a block.
155a45f9889SGreg Roach     *
156a45f9889SGreg Roach     * @param Request $request
157a45f9889SGreg Roach     * @param int     $block_id
158a45f9889SGreg Roach     *
159a45f9889SGreg Roach     * @return void
160a45f9889SGreg Roach     */
161a45f9889SGreg Roach    public function saveBlockConfiguration(Request $request, int $block_id)
162a45f9889SGreg Roach    {
163a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'num', $request->get('num', '10'));
164a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'infoStyle', $request->get('infoStyle', 'table'));
165a45f9889SGreg Roach    }
166a45f9889SGreg Roach
167a45f9889SGreg Roach    /**
16876692c8bSGreg Roach     * An HTML form to edit block settings
16976692c8bSGreg Roach     *
170e490cd80SGreg Roach     * @param Tree $tree
17176692c8bSGreg Roach     * @param int  $block_id
172a9430be8SGreg Roach     *
173a9430be8SGreg Roach     * @return void
17476692c8bSGreg Roach     */
175a45f9889SGreg Roach    public function editBlockConfiguration(Tree $tree, int $block_id)
176c1010edaSGreg Roach    {
177e2a378d3SGreg Roach        $num       = $this->getBlockSetting($block_id, 'num', '10');
178e2a378d3SGreg Roach        $infoStyle = $this->getBlockSetting($block_id, 'infoStyle', 'table');
1793763c3f2SGreg Roach
180c385536dSGreg Roach        $info_styles = [
181bbb76c12SGreg Roach            /* I18N: An option in a list-box */
182bbb76c12SGreg Roach            'list'  => I18N::translate('list'),
183bbb76c12SGreg Roach            /* I18N: An option in a list-box */
184bbb76c12SGreg Roach            'table' => I18N::translate('table'),
185c385536dSGreg Roach        ];
1863763c3f2SGreg Roach
187147e99aaSGreg Roach        echo view('modules/top10_givnnames/config', [
188c385536dSGreg Roach            'infoStyle'   => $infoStyle,
189c385536dSGreg Roach            'info_styles' => $info_styles,
190c385536dSGreg Roach            'num'         => $num,
191c385536dSGreg Roach        ]);
1923763c3f2SGreg Roach    }
1933763c3f2SGreg Roach}
194