xref: /webtrees/app/Module/TopGivenNamesModule.php (revision 7413816e6dd2d50e569034fb804f3dce7471bb94)
13763c3f2SGreg Roach<?php
23976b470SGreg Roach
33763c3f2SGreg Roach/**
43763c3f2SGreg Roach * webtrees: online genealogy
5d11be702SGreg Roach * Copyright (C) 2023 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;
23*d35568b4SGreg Roachuse Fisharebest\Webtrees\Registry;
249219296aSGreg Roachuse Fisharebest\Webtrees\Statistics;
25e490cd80SGreg Roachuse Fisharebest\Webtrees\Tree;
26748dbe15SGreg Roachuse Fisharebest\Webtrees\Validator;
271e7a7a28SGreg Roachuse Illuminate\Support\Str;
286ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
293763c3f2SGreg Roach
303763c3f2SGreg Roach/**
313763c3f2SGreg Roach * Class TopGivenNamesModule
323763c3f2SGreg Roach */
3337eb8894SGreg Roachclass TopGivenNamesModule extends AbstractModule implements ModuleBlockInterface
34c1010edaSGreg Roach{
3549a243cbSGreg Roach    use ModuleBlockTrait;
3649a243cbSGreg Roach
3755664801SGreg Roach    // Default values for new blocks.
3816d6367aSGreg Roach    private const DEFAULT_NUMBER = '10';
3916d6367aSGreg Roach    private const DEFAULT_STYLE  = 'table';
4055664801SGreg Roach
41961ec755SGreg Roach    /**
420cfd6963SGreg Roach     * How should this module be identified in the control panel, etc.?
43961ec755SGreg Roach     *
44961ec755SGreg Roach     * @return string
45961ec755SGreg Roach     */
4649a243cbSGreg Roach    public function title(): string
47c1010edaSGreg Roach    {
48bbb76c12SGreg Roach        /* I18N: Name of a module. Top=Most common */
49bbb76c12SGreg Roach        return I18N::translate('Top given names');
503763c3f2SGreg Roach    }
513763c3f2SGreg Roach
5249a243cbSGreg Roach    public function description(): string
53c1010edaSGreg Roach    {
54bbb76c12SGreg Roach        /* I18N: Description of the “Top given names” module */
55bbb76c12SGreg Roach        return I18N::translate('A list of the most popular given names.');
563763c3f2SGreg Roach    }
573763c3f2SGreg Roach
5876692c8bSGreg Roach    /**
5976692c8bSGreg Roach     * Generate the HTML content of this block.
6076692c8bSGreg Roach     *
61e490cd80SGreg Roach     * @param Tree                 $tree
6276692c8bSGreg Roach     * @param int                  $block_id
633caaa4d2SGreg Roach     * @param string               $context
6476d39c55SGreg Roach     * @param array<string,string> $config
6576692c8bSGreg Roach     *
6676692c8bSGreg Roach     * @return string
6776692c8bSGreg Roach     */
683caaa4d2SGreg Roach    public function getBlock(Tree $tree, int $block_id, string $context, array $config = []): string
69c1010edaSGreg Roach    {
70*d35568b4SGreg Roach        $statistics = Registry::container()->get(Statistics::class);
71fce4f17fSGreg Roach        $num        = $this->getBlockSetting($block_id, 'num', self::DEFAULT_NUMBER);
7255664801SGreg Roach        $infoStyle  = $this->getBlockSetting($block_id, 'infoStyle', self::DEFAULT_STYLE);
733763c3f2SGreg Roach
743caaa4d2SGreg Roach        extract($config, EXTR_OVERWRITE);
753763c3f2SGreg Roach
763763c3f2SGreg Roach        switch ($infoStyle) {
7745831e7fSGreg Roach            case 'list':
7895768326SGreg Roach                $content = view('modules/top10_givnnames/block', [
79bf57b580SGreg Roach                    'males'   => $statistics->commonGivenMaleListTotals('1', $num),
80bf57b580SGreg Roach                    'females' => $statistics->commonGivenFemaleListTotals('1', $num),
8145831e7fSGreg Roach                ]);
823763c3f2SGreg Roach                break;
8345831e7fSGreg Roach            default:
8445831e7fSGreg Roach            case 'table':
8595768326SGreg Roach                $content = view('modules/top10_givnnames/block', [
86bf57b580SGreg Roach                    'males'   => $statistics->commonGivenMaleTable('1', $num),
87bf57b580SGreg Roach                    'females' => $statistics->commonGivenFemaleTable('1', $num),
8845831e7fSGreg Roach                ]);
893763c3f2SGreg Roach                break;
903763c3f2SGreg Roach        }
913763c3f2SGreg Roach
923caaa4d2SGreg Roach        if ($context !== self::CONTEXT_EMBED) {
93fce4f17fSGreg Roach            $num = (int) $num;
94fce4f17fSGreg Roach
9555664801SGreg Roach            if ($num === 1) {
968cbbfdceSGreg Roach                // I18N: i.e. most popular given name.
978cbbfdceSGreg Roach                $title = I18N::translate('Top given name');
988cbbfdceSGreg Roach            } else {
998cbbfdceSGreg 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
1008cbbfdceSGreg Roach                $title = I18N::plural('Top %s given name', 'Top %s given names', $num, I18N::number($num));
1018cbbfdceSGreg Roach            }
1028cbbfdceSGreg Roach
103147e99aaSGreg Roach            return view('modules/block-template', [
1041e7a7a28SGreg Roach                'block'      => Str::kebab($this->name()),
1059c6524dcSGreg Roach                'id'         => $block_id,
1063caaa4d2SGreg Roach                'config_url' => $this->configUrl($tree, $context, $block_id),
1078cbbfdceSGreg Roach                'title'      => $title,
1089c6524dcSGreg Roach                'content'    => $content,
1099c6524dcSGreg Roach            ]);
1103763c3f2SGreg Roach        }
111b2ce94c6SRico Sonntag
112b2ce94c6SRico Sonntag        return $content;
1133763c3f2SGreg Roach    }
1143763c3f2SGreg Roach
1153caaa4d2SGreg Roach    /**
1163caaa4d2SGreg Roach     * Should this block load asynchronously using AJAX?
1173caaa4d2SGreg Roach     *
1183caaa4d2SGreg Roach     * Simple blocks are faster in-line, more complex ones can be loaded later.
1193caaa4d2SGreg Roach     *
1203caaa4d2SGreg Roach     * @return bool
1213caaa4d2SGreg Roach     */
122c1010edaSGreg Roach    public function loadAjax(): bool
123c1010edaSGreg Roach    {
1244ecf4f82SGreg Roach        return false;
1253763c3f2SGreg Roach    }
1263763c3f2SGreg Roach
1273caaa4d2SGreg Roach    /**
1283caaa4d2SGreg Roach     * Can this block be shown on the user’s home page?
1293caaa4d2SGreg Roach     *
1303caaa4d2SGreg Roach     * @return bool
1313caaa4d2SGreg Roach     */
132c1010edaSGreg Roach    public function isUserBlock(): bool
133c1010edaSGreg Roach    {
1343763c3f2SGreg Roach        return true;
1353763c3f2SGreg Roach    }
1363763c3f2SGreg Roach
1373caaa4d2SGreg Roach    /**
1383caaa4d2SGreg Roach     * Can this block be shown on the tree’s home page?
1393caaa4d2SGreg Roach     *
1403caaa4d2SGreg Roach     * @return bool
1413caaa4d2SGreg Roach     */
14263276d8fSGreg Roach    public function isTreeBlock(): bool
143c1010edaSGreg Roach    {
1443763c3f2SGreg Roach        return true;
1453763c3f2SGreg Roach    }
1463763c3f2SGreg Roach
14776692c8bSGreg Roach    /**
148a45f9889SGreg Roach     * Update the configuration for a block.
149a45f9889SGreg Roach     *
1506ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
151a45f9889SGreg Roach     * @param int $block_id
152a45f9889SGreg Roach     *
153a45f9889SGreg Roach     * @return void
154a45f9889SGreg Roach     */
1556ccdf4f0SGreg Roach    public function saveBlockConfiguration(ServerRequestInterface $request, int $block_id): void
156a45f9889SGreg Roach    {
157748dbe15SGreg Roach        $num        = Validator::parsedBody($request)->integer('num');
158748dbe15SGreg Roach        $info_style = Validator::parsedBody($request)->string('infoStyle');
159c50a90beSGreg Roach
160748dbe15SGreg Roach        $this->setBlockSetting($block_id, 'num', (string) $num);
161748dbe15SGreg Roach        $this->setBlockSetting($block_id, 'infoStyle', $info_style);
162a45f9889SGreg Roach    }
163a45f9889SGreg Roach
164a45f9889SGreg Roach    /**
16576692c8bSGreg Roach     * An HTML form to edit block settings
16676692c8bSGreg Roach     *
167e490cd80SGreg Roach     * @param Tree $tree
16876692c8bSGreg Roach     * @param int  $block_id
169a9430be8SGreg Roach     *
1703caaa4d2SGreg Roach     * @return string
17176692c8bSGreg Roach     */
1723caaa4d2SGreg Roach    public function editBlockConfiguration(Tree $tree, int $block_id): string
173c1010edaSGreg Roach    {
174748dbe15SGreg Roach        $num        = (int) $this->getBlockSetting($block_id, 'num', self::DEFAULT_NUMBER);
1757c2c99faSGreg Roach        $info_style = $this->getBlockSetting($block_id, 'infoStyle', self::DEFAULT_STYLE);
1763763c3f2SGreg Roach
177c385536dSGreg Roach        $info_styles = [
178bbb76c12SGreg Roach            /* I18N: An option in a list-box */
179bbb76c12SGreg Roach            'list'  => I18N::translate('list'),
180bbb76c12SGreg Roach            /* I18N: An option in a list-box */
181bbb76c12SGreg Roach            'table' => I18N::translate('table'),
182c385536dSGreg Roach        ];
1833763c3f2SGreg Roach
1843caaa4d2SGreg Roach        return view('modules/top10_givnnames/config', [
185e5a38573SGreg Roach            'info_style'  => $info_style,
186c385536dSGreg Roach            'info_styles' => $info_styles,
187c385536dSGreg Roach            'num'         => $num,
188c385536dSGreg Roach        ]);
1893763c3f2SGreg Roach    }
1903763c3f2SGreg Roach}
191