xref: /webtrees/app/Module/TopGivenNamesModule.php (revision cab242e7d7a773b0a6dab130048696e26fd6612c)
13763c3f2SGreg Roach<?php
23763c3f2SGreg Roach/**
33763c3f2SGreg Roach * webtrees: online genealogy
48fcd0d32SGreg Roach * Copyright (C) 2019 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 */
16e7f56f2aSGreg Roachdeclare(strict_types=1);
17e7f56f2aSGreg Roach
1876692c8bSGreg Roachnamespace Fisharebest\Webtrees\Module;
1976692c8bSGreg Roach
200e62c4b8SGreg Roachuse Fisharebest\Webtrees\Auth;
210e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N;
229219296aSGreg Roachuse Fisharebest\Webtrees\Statistics;
23e490cd80SGreg Roachuse Fisharebest\Webtrees\Tree;
24a45f9889SGreg Roachuse Symfony\Component\HttpFoundation\Request;
253763c3f2SGreg Roach
263763c3f2SGreg Roach/**
273763c3f2SGreg Roach * Class TopGivenNamesModule
283763c3f2SGreg Roach */
2937eb8894SGreg Roachclass TopGivenNamesModule extends AbstractModule implements ModuleBlockInterface
30c1010edaSGreg Roach{
3149a243cbSGreg Roach    use ModuleBlockTrait;
3249a243cbSGreg Roach
3355664801SGreg Roach    // Default values for new blocks.
3416d6367aSGreg Roach    private const DEFAULT_NUMBER = '10';
3516d6367aSGreg Roach    private const DEFAULT_STYLE  = 'table';
3655664801SGreg Roach
37961ec755SGreg Roach    /**
38961ec755SGreg Roach     * How should this module be labelled on tabs, menus, etc.?
39961ec755SGreg Roach     *
40961ec755SGreg Roach     * @return string
41961ec755SGreg Roach     */
4249a243cbSGreg Roach    public function title(): string
43c1010edaSGreg Roach    {
44bbb76c12SGreg Roach        /* I18N: Name of a module. Top=Most common */
45bbb76c12SGreg Roach        return I18N::translate('Top given names');
463763c3f2SGreg Roach    }
473763c3f2SGreg Roach
48961ec755SGreg Roach    /**
49961ec755SGreg Roach     * A sentence describing what this module does.
50961ec755SGreg Roach     *
51961ec755SGreg Roach     * @return string
52961ec755SGreg Roach     */
5349a243cbSGreg Roach    public function description(): string
54c1010edaSGreg Roach    {
55bbb76c12SGreg Roach        /* I18N: Description of the “Top given names” module */
56bbb76c12SGreg Roach        return I18N::translate('A list of the most popular given names.');
573763c3f2SGreg Roach    }
583763c3f2SGreg Roach
5976692c8bSGreg Roach    /**
6076692c8bSGreg Roach     * Generate the HTML content of this block.
6176692c8bSGreg Roach     *
62e490cd80SGreg Roach     * @param Tree     $tree
6376692c8bSGreg Roach     * @param int      $block_id
645f2ae573SGreg Roach     * @param string   $ctype
65727f238cSGreg Roach     * @param string[] $cfg
6676692c8bSGreg Roach     *
6776692c8bSGreg Roach     * @return string
6876692c8bSGreg Roach     */
695f2ae573SGreg Roach    public function getBlock(Tree $tree, int $block_id, string $ctype = '', array $cfg = []): string
70c1010edaSGreg Roach    {
71*cab242e7SGreg Roach        $statistics = app(Statistics::class);
72bf57b580SGreg Roach
73fce4f17fSGreg Roach        $num       = $this->getBlockSetting($block_id, 'num', self::DEFAULT_NUMBER);
7455664801SGreg Roach        $infoStyle = $this->getBlockSetting($block_id, 'infoStyle', self::DEFAULT_STYLE);
753763c3f2SGreg Roach
76c385536dSGreg Roach        extract($cfg, EXTR_OVERWRITE);
773763c3f2SGreg Roach
783763c3f2SGreg Roach        switch ($infoStyle) {
7945831e7fSGreg Roach            case 'list':
8095768326SGreg Roach                $content = view('modules/top10_givnnames/block', [
81bf57b580SGreg Roach                    'males'   => $statistics->commonGivenMaleListTotals('1', $num),
82bf57b580SGreg Roach                    'females' => $statistics->commonGivenFemaleListTotals('1', $num),
8345831e7fSGreg Roach                ]);
843763c3f2SGreg Roach                break;
8545831e7fSGreg Roach            default:
8645831e7fSGreg Roach            case 'table':
8795768326SGreg Roach                $content = view('modules/top10_givnnames/block', [
88bf57b580SGreg Roach                    'males'   => $statistics->commonGivenMaleTable('1', $num),
89bf57b580SGreg Roach                    'females' => $statistics->commonGivenFemaleTable('1', $num),
9045831e7fSGreg Roach                ]);
913763c3f2SGreg Roach                break;
923763c3f2SGreg Roach        }
933763c3f2SGreg Roach
946a8879feSGreg Roach        if ($ctype !== '') {
95fce4f17fSGreg Roach            $num = (int) $num;
96fce4f17fSGreg Roach
9755664801SGreg Roach            if ($num === 1) {
988cbbfdceSGreg Roach                // I18N: i.e. most popular given name.
998cbbfdceSGreg Roach                $title = I18N::translate('Top given name');
1008cbbfdceSGreg Roach            } else {
1018cbbfdceSGreg 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
1028cbbfdceSGreg Roach                $title = I18N::plural('Top %s given name', 'Top %s given names', $num, I18N::number($num));
1038cbbfdceSGreg Roach            }
1048cbbfdceSGreg Roach
105e490cd80SGreg Roach            if ($ctype === 'gedcom' && Auth::isManager($tree)) {
106c1010edaSGreg Roach                $config_url = route('tree-page-block-edit', [
107c1010edaSGreg Roach                    'block_id' => $block_id,
108aa6f03bbSGreg Roach                    'ged'      => $tree->name(),
109c1010edaSGreg Roach                ]);
110397e599aSGreg Roach            } elseif ($ctype === 'user' && Auth::check()) {
111c1010edaSGreg Roach                $config_url = route('user-page-block-edit', [
112c1010edaSGreg Roach                    'block_id' => $block_id,
113aa6f03bbSGreg Roach                    'ged'      => $tree->name(),
114c1010edaSGreg Roach                ]);
1158cbbfdceSGreg Roach            } else {
1168cbbfdceSGreg Roach                $config_url = '';
1178cbbfdceSGreg Roach            }
1188cbbfdceSGreg Roach
119147e99aaSGreg Roach            return view('modules/block-template', [
12026684e68SGreg Roach                'block'      => str_replace('_', '-', $this->name()),
1219c6524dcSGreg Roach                'id'         => $block_id,
1228cbbfdceSGreg Roach                'config_url' => $config_url,
1238cbbfdceSGreg Roach                'title'      => $title,
1249c6524dcSGreg Roach                'content'    => $content,
1259c6524dcSGreg Roach            ]);
1263763c3f2SGreg Roach        }
127b2ce94c6SRico Sonntag
128b2ce94c6SRico Sonntag        return $content;
1293763c3f2SGreg Roach    }
1303763c3f2SGreg Roach
1313763c3f2SGreg Roach    /** {@inheritdoc} */
132c1010edaSGreg Roach    public function loadAjax(): bool
133c1010edaSGreg Roach    {
1344ecf4f82SGreg Roach        return false;
1353763c3f2SGreg Roach    }
1363763c3f2SGreg Roach
1373763c3f2SGreg Roach    /** {@inheritdoc} */
138c1010edaSGreg Roach    public function isUserBlock(): bool
139c1010edaSGreg Roach    {
1403763c3f2SGreg Roach        return true;
1413763c3f2SGreg Roach    }
1423763c3f2SGreg Roach
1433763c3f2SGreg Roach    /** {@inheritdoc} */
14463276d8fSGreg Roach    public function isTreeBlock(): bool
145c1010edaSGreg Roach    {
1463763c3f2SGreg Roach        return true;
1473763c3f2SGreg Roach    }
1483763c3f2SGreg Roach
14976692c8bSGreg Roach    /**
150a45f9889SGreg Roach     * Update the configuration for a block.
151a45f9889SGreg Roach     *
152a45f9889SGreg Roach     * @param Request $request
153a45f9889SGreg Roach     * @param int     $block_id
154a45f9889SGreg Roach     *
155a45f9889SGreg Roach     * @return void
156a45f9889SGreg Roach     */
157a45f9889SGreg Roach    public function saveBlockConfiguration(Request $request, int $block_id)
158a45f9889SGreg Roach    {
15955664801SGreg Roach        $this->setBlockSetting($block_id, 'num', $request->get('num', self::DEFAULT_NUMBER));
16055664801SGreg Roach        $this->setBlockSetting($block_id, 'infoStyle', $request->get('infoStyle', self::DEFAULT_STYLE));
161a45f9889SGreg Roach    }
162a45f9889SGreg Roach
163a45f9889SGreg Roach    /**
16476692c8bSGreg Roach     * An HTML form to edit block settings
16576692c8bSGreg Roach     *
166e490cd80SGreg Roach     * @param Tree $tree
16776692c8bSGreg Roach     * @param int  $block_id
168a9430be8SGreg Roach     *
169a9430be8SGreg Roach     * @return void
17076692c8bSGreg Roach     */
171a45f9889SGreg Roach    public function editBlockConfiguration(Tree $tree, int $block_id)
172c1010edaSGreg Roach    {
17355664801SGreg Roach        $num       = $this->getBlockSetting($block_id, 'num', self::DEFAULT_NUMBER);
17455664801SGreg Roach        $infoStyle = $this->getBlockSetting($block_id, 'infoStyle', self::DEFAULT_STYLE);
1753763c3f2SGreg Roach
176c385536dSGreg Roach        $info_styles = [
177bbb76c12SGreg Roach            /* I18N: An option in a list-box */
178bbb76c12SGreg Roach            'list'  => I18N::translate('list'),
179bbb76c12SGreg Roach            /* I18N: An option in a list-box */
180bbb76c12SGreg Roach            'table' => I18N::translate('table'),
181c385536dSGreg Roach        ];
1823763c3f2SGreg Roach
183147e99aaSGreg Roach        echo view('modules/top10_givnnames/config', [
184c385536dSGreg Roach            'infoStyle'   => $infoStyle,
185c385536dSGreg Roach            'info_styles' => $info_styles,
186c385536dSGreg Roach            'num'         => $num,
187c385536dSGreg Roach        ]);
1883763c3f2SGreg Roach    }
1893763c3f2SGreg Roach}
190