xref: /webtrees/app/Module/TopGivenNamesModule.php (revision 748dbe155a6d19d66918ad136947fa23ee8f8469)
13763c3f2SGreg Roach<?php
23976b470SGreg Roach
33763c3f2SGreg Roach/**
43763c3f2SGreg Roach * webtrees: online genealogy
55bfc6897SGreg Roach * Copyright (C) 2022 webtrees development team
63763c3f2SGreg Roach * This program is free software: you can redistribute it and/or modify
73763c3f2SGreg Roach * it under the terms of the GNU General Public License as published by
83763c3f2SGreg Roach * the Free Software Foundation, either version 3 of the License, or
93763c3f2SGreg Roach * (at your option) any later version.
103763c3f2SGreg Roach * This program is distributed in the hope that it will be useful,
113763c3f2SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
123763c3f2SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
133763c3f2SGreg Roach * GNU General Public License for more details.
143763c3f2SGreg Roach * You should have received a copy of the GNU General Public License
1589f7189bSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>.
163763c3f2SGreg Roach */
17fcfa147eSGreg Roach
18e7f56f2aSGreg Roachdeclare(strict_types=1);
19e7f56f2aSGreg Roach
2076692c8bSGreg Roachnamespace Fisharebest\Webtrees\Module;
2176692c8bSGreg Roach
220e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N;
239219296aSGreg Roachuse Fisharebest\Webtrees\Statistics;
24e490cd80SGreg Roachuse Fisharebest\Webtrees\Tree;
25*748dbe15SGreg Roachuse Fisharebest\Webtrees\Validator;
261e7a7a28SGreg Roachuse Illuminate\Support\Str;
276ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
283763c3f2SGreg Roach
293763c3f2SGreg Roach/**
303763c3f2SGreg Roach * Class TopGivenNamesModule
313763c3f2SGreg Roach */
3237eb8894SGreg Roachclass TopGivenNamesModule extends AbstractModule implements ModuleBlockInterface
33c1010edaSGreg Roach{
3449a243cbSGreg Roach    use ModuleBlockTrait;
3549a243cbSGreg Roach
3655664801SGreg Roach    // Default values for new blocks.
3716d6367aSGreg Roach    private const DEFAULT_NUMBER = '10';
3816d6367aSGreg Roach    private const DEFAULT_STYLE  = 'table';
3955664801SGreg Roach
40961ec755SGreg Roach    /**
410cfd6963SGreg Roach     * How should this module be identified in the control panel, etc.?
42961ec755SGreg Roach     *
43961ec755SGreg Roach     * @return string
44961ec755SGreg Roach     */
4549a243cbSGreg Roach    public function title(): string
46c1010edaSGreg Roach    {
47bbb76c12SGreg Roach        /* I18N: Name of a module. Top=Most common */
48bbb76c12SGreg Roach        return I18N::translate('Top given names');
493763c3f2SGreg Roach    }
503763c3f2SGreg Roach
51961ec755SGreg Roach    /**
52961ec755SGreg Roach     * A sentence describing what this module does.
53961ec755SGreg Roach     *
54961ec755SGreg Roach     * @return string
55961ec755SGreg Roach     */
5649a243cbSGreg Roach    public function description(): string
57c1010edaSGreg Roach    {
58bbb76c12SGreg Roach        /* I18N: Description of the “Top given names” module */
59bbb76c12SGreg Roach        return I18N::translate('A list of the most popular given names.');
603763c3f2SGreg Roach    }
613763c3f2SGreg Roach
6276692c8bSGreg Roach    /**
6376692c8bSGreg Roach     * Generate the HTML content of this block.
6476692c8bSGreg Roach     *
65e490cd80SGreg Roach     * @param Tree                 $tree
6676692c8bSGreg Roach     * @param int                  $block_id
673caaa4d2SGreg Roach     * @param string               $context
6876d39c55SGreg Roach     * @param array<string,string> $config
6976692c8bSGreg Roach     *
7076692c8bSGreg Roach     * @return string
7176692c8bSGreg Roach     */
723caaa4d2SGreg Roach    public function getBlock(Tree $tree, int $block_id, string $context, array $config = []): string
73c1010edaSGreg Roach    {
74cab242e7SGreg Roach        $statistics = app(Statistics::class);
75bf57b580SGreg Roach
76fce4f17fSGreg Roach        $num       = $this->getBlockSetting($block_id, 'num', self::DEFAULT_NUMBER);
7755664801SGreg Roach        $infoStyle = $this->getBlockSetting($block_id, 'infoStyle', self::DEFAULT_STYLE);
783763c3f2SGreg Roach
793caaa4d2SGreg Roach        extract($config, EXTR_OVERWRITE);
803763c3f2SGreg Roach
813763c3f2SGreg Roach        switch ($infoStyle) {
8245831e7fSGreg Roach            case 'list':
8395768326SGreg Roach                $content = view('modules/top10_givnnames/block', [
84bf57b580SGreg Roach                    'males'   => $statistics->commonGivenMaleListTotals('1', $num),
85bf57b580SGreg Roach                    'females' => $statistics->commonGivenFemaleListTotals('1', $num),
8645831e7fSGreg Roach                ]);
873763c3f2SGreg Roach                break;
8845831e7fSGreg Roach            default:
8945831e7fSGreg Roach            case 'table':
9095768326SGreg Roach                $content = view('modules/top10_givnnames/block', [
91bf57b580SGreg Roach                    'males'   => $statistics->commonGivenMaleTable('1', $num),
92bf57b580SGreg Roach                    'females' => $statistics->commonGivenFemaleTable('1', $num),
9345831e7fSGreg Roach                ]);
943763c3f2SGreg Roach                break;
953763c3f2SGreg Roach        }
963763c3f2SGreg Roach
973caaa4d2SGreg Roach        if ($context !== self::CONTEXT_EMBED) {
98fce4f17fSGreg Roach            $num = (int) $num;
99fce4f17fSGreg Roach
10055664801SGreg Roach            if ($num === 1) {
1018cbbfdceSGreg Roach                // I18N: i.e. most popular given name.
1028cbbfdceSGreg Roach                $title = I18N::translate('Top given name');
1038cbbfdceSGreg Roach            } else {
1048cbbfdceSGreg 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
1058cbbfdceSGreg Roach                $title = I18N::plural('Top %s given name', 'Top %s given names', $num, I18N::number($num));
1068cbbfdceSGreg Roach            }
1078cbbfdceSGreg Roach
108147e99aaSGreg Roach            return view('modules/block-template', [
1091e7a7a28SGreg Roach                'block'      => Str::kebab($this->name()),
1109c6524dcSGreg Roach                'id'         => $block_id,
1113caaa4d2SGreg Roach                'config_url' => $this->configUrl($tree, $context, $block_id),
1128cbbfdceSGreg Roach                'title'      => $title,
1139c6524dcSGreg Roach                'content'    => $content,
1149c6524dcSGreg Roach            ]);
1153763c3f2SGreg Roach        }
116b2ce94c6SRico Sonntag
117b2ce94c6SRico Sonntag        return $content;
1183763c3f2SGreg Roach    }
1193763c3f2SGreg Roach
1203caaa4d2SGreg Roach    /**
1213caaa4d2SGreg Roach     * Should this block load asynchronously using AJAX?
1223caaa4d2SGreg Roach     *
1233caaa4d2SGreg Roach     * Simple blocks are faster in-line, more complex ones can be loaded later.
1243caaa4d2SGreg Roach     *
1253caaa4d2SGreg Roach     * @return bool
1263caaa4d2SGreg Roach     */
127c1010edaSGreg Roach    public function loadAjax(): bool
128c1010edaSGreg Roach    {
1294ecf4f82SGreg Roach        return false;
1303763c3f2SGreg Roach    }
1313763c3f2SGreg Roach
1323caaa4d2SGreg Roach    /**
1333caaa4d2SGreg Roach     * Can this block be shown on the user’s home page?
1343caaa4d2SGreg Roach     *
1353caaa4d2SGreg Roach     * @return bool
1363caaa4d2SGreg Roach     */
137c1010edaSGreg Roach    public function isUserBlock(): bool
138c1010edaSGreg Roach    {
1393763c3f2SGreg Roach        return true;
1403763c3f2SGreg Roach    }
1413763c3f2SGreg Roach
1423caaa4d2SGreg Roach    /**
1433caaa4d2SGreg Roach     * Can this block be shown on the tree’s home page?
1443caaa4d2SGreg Roach     *
1453caaa4d2SGreg Roach     * @return bool
1463caaa4d2SGreg Roach     */
14763276d8fSGreg Roach    public function isTreeBlock(): bool
148c1010edaSGreg Roach    {
1493763c3f2SGreg Roach        return true;
1503763c3f2SGreg Roach    }
1513763c3f2SGreg Roach
15276692c8bSGreg Roach    /**
153a45f9889SGreg Roach     * Update the configuration for a block.
154a45f9889SGreg Roach     *
1556ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
156a45f9889SGreg Roach     * @param int $block_id
157a45f9889SGreg Roach     *
158a45f9889SGreg Roach     * @return void
159a45f9889SGreg Roach     */
1606ccdf4f0SGreg Roach    public function saveBlockConfiguration(ServerRequestInterface $request, int $block_id): void
161a45f9889SGreg Roach    {
162*748dbe15SGreg Roach        $num        = Validator::parsedBody($request)->integer('num');
163*748dbe15SGreg Roach        $info_style = Validator::parsedBody($request)->string('infoStyle');
164c50a90beSGreg Roach
165*748dbe15SGreg Roach        $this->setBlockSetting($block_id, 'num', (string) $num);
166*748dbe15SGreg Roach        $this->setBlockSetting($block_id, 'infoStyle', $info_style);
167a45f9889SGreg Roach    }
168a45f9889SGreg Roach
169a45f9889SGreg Roach    /**
17076692c8bSGreg Roach     * An HTML form to edit block settings
17176692c8bSGreg Roach     *
172e490cd80SGreg Roach     * @param Tree $tree
17376692c8bSGreg Roach     * @param int  $block_id
174a9430be8SGreg Roach     *
1753caaa4d2SGreg Roach     * @return string
17676692c8bSGreg Roach     */
1773caaa4d2SGreg Roach    public function editBlockConfiguration(Tree $tree, int $block_id): string
178c1010edaSGreg Roach    {
179*748dbe15SGreg Roach        $num        = (int) $this->getBlockSetting($block_id, 'num', self::DEFAULT_NUMBER);
1807c2c99faSGreg Roach        $info_style = $this->getBlockSetting($block_id, 'infoStyle', self::DEFAULT_STYLE);
1813763c3f2SGreg Roach
182c385536dSGreg Roach        $info_styles = [
183bbb76c12SGreg Roach            /* I18N: An option in a list-box */
184bbb76c12SGreg Roach            'list'  => I18N::translate('list'),
185bbb76c12SGreg Roach            /* I18N: An option in a list-box */
186bbb76c12SGreg Roach            'table' => I18N::translate('table'),
187c385536dSGreg Roach        ];
1883763c3f2SGreg Roach
1893caaa4d2SGreg Roach        return view('modules/top10_givnnames/config', [
190e5a38573SGreg Roach            'info_style'  => $info_style,
191c385536dSGreg Roach            'info_styles' => $info_styles,
192c385536dSGreg Roach            'num'         => $num,
193c385536dSGreg Roach        ]);
1943763c3f2SGreg Roach    }
1953763c3f2SGreg Roach}
196