xref: /webtrees/app/Module/TopGivenNamesModule.php (revision aa6f03bb51be5a55a78fe8aed18753116689a6d3)
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 */
16e7f56f2aSGreg Roachdeclare(strict_types=1);
17e7f56f2aSGreg Roach
1876692c8bSGreg Roachnamespace Fisharebest\Webtrees\Module;
1976692c8bSGreg Roach
200e62c4b8SGreg Roachuse Fisharebest\Webtrees\Auth;
210e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N;
220e62c4b8SGreg Roachuse Fisharebest\Webtrees\Stats;
23e490cd80SGreg Roachuse Fisharebest\Webtrees\Tree;
24a45f9889SGreg Roachuse Symfony\Component\HttpFoundation\Request;
253763c3f2SGreg Roach
263763c3f2SGreg Roach/**
273763c3f2SGreg Roach * Class TopGivenNamesModule
283763c3f2SGreg Roach */
29c1010edaSGreg Roachclass TopGivenNamesModule extends AbstractModule implements ModuleBlockInterface
30c1010edaSGreg Roach{
3155664801SGreg Roach    // Default values for new blocks.
3255664801SGreg Roach    const DEFAULT_NUMBER = '10';
3355664801SGreg Roach    const DEFAULT_STYLE  = 'table';
3455664801SGreg Roach
353763c3f2SGreg Roach    /** {@inheritdoc} */
368f53f488SRico Sonntag    public function getTitle(): string
37c1010edaSGreg Roach    {
38bbb76c12SGreg Roach        /* I18N: Name of a module. Top=Most common */
39bbb76c12SGreg Roach        return I18N::translate('Top given names');
403763c3f2SGreg Roach    }
413763c3f2SGreg Roach
423763c3f2SGreg Roach    /** {@inheritdoc} */
438f53f488SRico Sonntag    public function getDescription(): string
44c1010edaSGreg Roach    {
45bbb76c12SGreg Roach        /* I18N: Description of the “Top given names” module */
46bbb76c12SGreg Roach        return I18N::translate('A list of the most popular given names.');
473763c3f2SGreg Roach    }
483763c3f2SGreg Roach
4976692c8bSGreg Roach    /**
5076692c8bSGreg Roach     * Generate the HTML content of this block.
5176692c8bSGreg Roach     *
52e490cd80SGreg Roach     * @param Tree     $tree
5376692c8bSGreg Roach     * @param int      $block_id
5476692c8bSGreg Roach     * @param bool     $template
55727f238cSGreg Roach     * @param string[] $cfg
5676692c8bSGreg Roach     *
5776692c8bSGreg Roach     * @return string
5876692c8bSGreg Roach     */
59c1010edaSGreg Roach    public function getBlock(Tree $tree, int $block_id, bool $template = true, array $cfg = []): string
60c1010edaSGreg Roach    {
61e490cd80SGreg Roach        global $ctype;
623763c3f2SGreg Roach
63fce4f17fSGreg Roach        $num       = $this->getBlockSetting($block_id, 'num', self::DEFAULT_NUMBER);
6455664801SGreg Roach        $infoStyle = $this->getBlockSetting($block_id, 'infoStyle', self::DEFAULT_STYLE);
653763c3f2SGreg Roach
66c385536dSGreg Roach        extract($cfg, EXTR_OVERWRITE);
673763c3f2SGreg Roach
68e490cd80SGreg Roach        $stats   = new Stats($tree);
69fce4f17fSGreg Roach        $males   = $stats->commonGivenMaleTable('1', $num);
70fce4f17fSGreg Roach        $females = $stats->commonGivenFemaleTable('1', $num);
713763c3f2SGreg Roach
723763c3f2SGreg Roach        switch ($infoStyle) {
7345831e7fSGreg Roach            case 'list':
74147e99aaSGreg Roach                $content = view('modules/top10_givnnames/list', [
7545831e7fSGreg Roach                    'males'   => $males,
7645831e7fSGreg Roach                    'females' => $females,
7745831e7fSGreg Roach                ]);
783763c3f2SGreg Roach                break;
7945831e7fSGreg Roach            default:
8045831e7fSGreg Roach            case 'table':
81147e99aaSGreg Roach                $content = view('modules/top10_givnnames/table', [
8245831e7fSGreg Roach                    'males'   => $males,
8345831e7fSGreg Roach                    'females' => $females,
8445831e7fSGreg Roach                ]);
853763c3f2SGreg Roach                break;
863763c3f2SGreg Roach        }
873763c3f2SGreg Roach
883763c3f2SGreg Roach        if ($template) {
89fce4f17fSGreg Roach            $num = (int) $num;
90fce4f17fSGreg Roach
9155664801SGreg Roach            if ($num === 1) {
928cbbfdceSGreg Roach                // I18N: i.e. most popular given name.
938cbbfdceSGreg Roach                $title = I18N::translate('Top given name');
948cbbfdceSGreg Roach            } else {
958cbbfdceSGreg 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
968cbbfdceSGreg Roach                $title = I18N::plural('Top %s given name', 'Top %s given names', $num, I18N::number($num));
978cbbfdceSGreg Roach            }
988cbbfdceSGreg Roach
99e490cd80SGreg Roach            if ($ctype === 'gedcom' && Auth::isManager($tree)) {
100c1010edaSGreg Roach                $config_url = route('tree-page-block-edit', [
101c1010edaSGreg Roach                    'block_id' => $block_id,
102*aa6f03bbSGreg Roach                    'ged'      => $tree->name(),
103c1010edaSGreg Roach                ]);
104397e599aSGreg Roach            } elseif ($ctype === 'user' && Auth::check()) {
105c1010edaSGreg Roach                $config_url = route('user-page-block-edit', [
106c1010edaSGreg Roach                    'block_id' => $block_id,
107*aa6f03bbSGreg Roach                    'ged'      => $tree->name(),
108c1010edaSGreg Roach                ]);
1098cbbfdceSGreg Roach            } else {
1108cbbfdceSGreg Roach                $config_url = '';
1118cbbfdceSGreg Roach            }
1128cbbfdceSGreg Roach
113147e99aaSGreg Roach            return view('modules/block-template', [
1149c6524dcSGreg Roach                'block'      => str_replace('_', '-', $this->getName()),
1159c6524dcSGreg Roach                'id'         => $block_id,
1168cbbfdceSGreg Roach                'config_url' => $config_url,
1178cbbfdceSGreg Roach                'title'      => $title,
1189c6524dcSGreg Roach                'content'    => $content,
1199c6524dcSGreg Roach            ]);
1203763c3f2SGreg Roach        }
121b2ce94c6SRico Sonntag
122b2ce94c6SRico Sonntag        return $content;
1233763c3f2SGreg Roach    }
1243763c3f2SGreg Roach
1253763c3f2SGreg Roach    /** {@inheritdoc} */
126c1010edaSGreg Roach    public function loadAjax(): bool
127c1010edaSGreg Roach    {
1284ecf4f82SGreg Roach        return false;
1293763c3f2SGreg Roach    }
1303763c3f2SGreg Roach
1313763c3f2SGreg Roach    /** {@inheritdoc} */
132c1010edaSGreg Roach    public function isUserBlock(): bool
133c1010edaSGreg Roach    {
1343763c3f2SGreg Roach        return true;
1353763c3f2SGreg Roach    }
1363763c3f2SGreg Roach
1373763c3f2SGreg Roach    /** {@inheritdoc} */
138c1010edaSGreg Roach    public function isGedcomBlock(): bool
139c1010edaSGreg Roach    {
1403763c3f2SGreg Roach        return true;
1413763c3f2SGreg Roach    }
1423763c3f2SGreg Roach
14376692c8bSGreg Roach    /**
144a45f9889SGreg Roach     * Update the configuration for a block.
145a45f9889SGreg Roach     *
146a45f9889SGreg Roach     * @param Request $request
147a45f9889SGreg Roach     * @param int     $block_id
148a45f9889SGreg Roach     *
149a45f9889SGreg Roach     * @return void
150a45f9889SGreg Roach     */
151a45f9889SGreg Roach    public function saveBlockConfiguration(Request $request, int $block_id)
152a45f9889SGreg Roach    {
15355664801SGreg Roach        $this->setBlockSetting($block_id, 'num', $request->get('num', self::DEFAULT_NUMBER));
15455664801SGreg Roach        $this->setBlockSetting($block_id, 'infoStyle', $request->get('infoStyle', self::DEFAULT_STYLE));
155a45f9889SGreg Roach    }
156a45f9889SGreg Roach
157a45f9889SGreg Roach    /**
15876692c8bSGreg Roach     * An HTML form to edit block settings
15976692c8bSGreg Roach     *
160e490cd80SGreg Roach     * @param Tree $tree
16176692c8bSGreg Roach     * @param int  $block_id
162a9430be8SGreg Roach     *
163a9430be8SGreg Roach     * @return void
16476692c8bSGreg Roach     */
165a45f9889SGreg Roach    public function editBlockConfiguration(Tree $tree, int $block_id)
166c1010edaSGreg Roach    {
16755664801SGreg Roach        $num       = $this->getBlockSetting($block_id, 'num', self::DEFAULT_NUMBER);
16855664801SGreg Roach        $infoStyle = $this->getBlockSetting($block_id, 'infoStyle', self::DEFAULT_STYLE);
1693763c3f2SGreg Roach
170c385536dSGreg Roach        $info_styles = [
171bbb76c12SGreg Roach            /* I18N: An option in a list-box */
172bbb76c12SGreg Roach            'list'  => I18N::translate('list'),
173bbb76c12SGreg Roach            /* I18N: An option in a list-box */
174bbb76c12SGreg Roach            'table' => I18N::translate('table'),
175c385536dSGreg Roach        ];
1763763c3f2SGreg Roach
177147e99aaSGreg Roach        echo view('modules/top10_givnnames/config', [
178c385536dSGreg Roach            'infoStyle'   => $infoStyle,
179c385536dSGreg Roach            'info_styles' => $info_styles,
180c385536dSGreg Roach            'num'         => $num,
181c385536dSGreg Roach        ]);
1823763c3f2SGreg Roach    }
1833763c3f2SGreg Roach}
184