xref: /webtrees/app/Module/TopGivenNamesModule.php (revision 3976b4703df669696105ed6b024b96d433c8fbdb)
13763c3f2SGreg Roach<?php
2*3976b470SGreg Roach
33763c3f2SGreg Roach/**
43763c3f2SGreg Roach * webtrees: online genealogy
58fcd0d32SGreg Roach * Copyright (C) 2019 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
153763c3f2SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
163763c3f2SGreg Roach */
17e7f56f2aSGreg Roachdeclare(strict_types=1);
18e7f56f2aSGreg Roach
1976692c8bSGreg Roachnamespace Fisharebest\Webtrees\Module;
2076692c8bSGreg Roach
210e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N;
229219296aSGreg Roachuse Fisharebest\Webtrees\Statistics;
23e490cd80SGreg Roachuse Fisharebest\Webtrees\Tree;
241e7a7a28SGreg Roachuse Illuminate\Support\Str;
256ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
263763c3f2SGreg Roach
273763c3f2SGreg Roach/**
283763c3f2SGreg Roach * Class TopGivenNamesModule
293763c3f2SGreg Roach */
3037eb8894SGreg Roachclass TopGivenNamesModule extends AbstractModule implements ModuleBlockInterface
31c1010edaSGreg Roach{
3249a243cbSGreg Roach    use ModuleBlockTrait;
3349a243cbSGreg Roach
3455664801SGreg Roach    // Default values for new blocks.
3516d6367aSGreg Roach    private const DEFAULT_NUMBER = '10';
3616d6367aSGreg Roach    private const DEFAULT_STYLE  = 'table';
3755664801SGreg Roach
38961ec755SGreg Roach    /**
390cfd6963SGreg Roach     * How should this module be identified in the control panel, etc.?
40961ec755SGreg Roach     *
41961ec755SGreg Roach     * @return string
42961ec755SGreg Roach     */
4349a243cbSGreg Roach    public function title(): string
44c1010edaSGreg Roach    {
45bbb76c12SGreg Roach        /* I18N: Name of a module. Top=Most common */
46bbb76c12SGreg Roach        return I18N::translate('Top given names');
473763c3f2SGreg Roach    }
483763c3f2SGreg Roach
49961ec755SGreg Roach    /**
50961ec755SGreg Roach     * A sentence describing what this module does.
51961ec755SGreg Roach     *
52961ec755SGreg Roach     * @return string
53961ec755SGreg Roach     */
5449a243cbSGreg Roach    public function description(): string
55c1010edaSGreg Roach    {
56bbb76c12SGreg Roach        /* I18N: Description of the “Top given names” module */
57bbb76c12SGreg Roach        return I18N::translate('A list of the most popular given names.');
583763c3f2SGreg Roach    }
593763c3f2SGreg Roach
6076692c8bSGreg Roach    /**
6176692c8bSGreg Roach     * Generate the HTML content of this block.
6276692c8bSGreg Roach     *
63e490cd80SGreg Roach     * @param Tree     $tree
6476692c8bSGreg Roach     * @param int      $block_id
653caaa4d2SGreg Roach     * @param string   $context
663caaa4d2SGreg Roach     * @param string[] $config
6776692c8bSGreg Roach     *
6876692c8bSGreg Roach     * @return string
6976692c8bSGreg Roach     */
703caaa4d2SGreg Roach    public function getBlock(Tree $tree, int $block_id, string $context, array $config = []): string
71c1010edaSGreg Roach    {
72cab242e7SGreg Roach        $statistics = app(Statistics::class);
73bf57b580SGreg Roach
74fce4f17fSGreg Roach        $num       = $this->getBlockSetting($block_id, 'num', self::DEFAULT_NUMBER);
7555664801SGreg Roach        $infoStyle = $this->getBlockSetting($block_id, 'infoStyle', self::DEFAULT_STYLE);
763763c3f2SGreg Roach
773caaa4d2SGreg Roach        extract($config, EXTR_OVERWRITE);
783763c3f2SGreg Roach
793763c3f2SGreg Roach        switch ($infoStyle) {
8045831e7fSGreg Roach            case 'list':
8195768326SGreg Roach                $content = view('modules/top10_givnnames/block', [
82bf57b580SGreg Roach                    'males'   => $statistics->commonGivenMaleListTotals('1', $num),
83bf57b580SGreg Roach                    'females' => $statistics->commonGivenFemaleListTotals('1', $num),
8445831e7fSGreg Roach                ]);
853763c3f2SGreg Roach                break;
8645831e7fSGreg Roach            default:
8745831e7fSGreg Roach            case 'table':
8895768326SGreg Roach                $content = view('modules/top10_givnnames/block', [
89bf57b580SGreg Roach                    'males'   => $statistics->commonGivenMaleTable('1', $num),
90bf57b580SGreg Roach                    'females' => $statistics->commonGivenFemaleTable('1', $num),
9145831e7fSGreg Roach                ]);
923763c3f2SGreg Roach                break;
933763c3f2SGreg Roach        }
943763c3f2SGreg Roach
953caaa4d2SGreg Roach        if ($context !== self::CONTEXT_EMBED) {
96fce4f17fSGreg Roach            $num = (int) $num;
97fce4f17fSGreg Roach
9855664801SGreg Roach            if ($num === 1) {
998cbbfdceSGreg Roach                // I18N: i.e. most popular given name.
1008cbbfdceSGreg Roach                $title = I18N::translate('Top given name');
1018cbbfdceSGreg Roach            } else {
1028cbbfdceSGreg 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
1038cbbfdceSGreg Roach                $title = I18N::plural('Top %s given name', 'Top %s given names', $num, I18N::number($num));
1048cbbfdceSGreg Roach            }
1058cbbfdceSGreg Roach
106147e99aaSGreg Roach            return view('modules/block-template', [
1071e7a7a28SGreg Roach                'block'      => Str::kebab($this->name()),
1089c6524dcSGreg Roach                'id'         => $block_id,
1093caaa4d2SGreg Roach                'config_url' => $this->configUrl($tree, $context, $block_id),
1108cbbfdceSGreg Roach                'title'      => $title,
1119c6524dcSGreg Roach                'content'    => $content,
1129c6524dcSGreg Roach            ]);
1133763c3f2SGreg Roach        }
114b2ce94c6SRico Sonntag
115b2ce94c6SRico Sonntag        return $content;
1163763c3f2SGreg Roach    }
1173763c3f2SGreg Roach
1183caaa4d2SGreg Roach    /**
1193caaa4d2SGreg Roach     * Should this block load asynchronously using AJAX?
1203caaa4d2SGreg Roach     *
1213caaa4d2SGreg Roach     * Simple blocks are faster in-line, more complex ones can be loaded later.
1223caaa4d2SGreg Roach     *
1233caaa4d2SGreg Roach     * @return bool
1243caaa4d2SGreg Roach     */
125c1010edaSGreg Roach    public function loadAjax(): bool
126c1010edaSGreg Roach    {
1274ecf4f82SGreg Roach        return false;
1283763c3f2SGreg Roach    }
1293763c3f2SGreg Roach
1303caaa4d2SGreg Roach    /**
1313caaa4d2SGreg Roach     * Can this block be shown on the user’s home page?
1323caaa4d2SGreg Roach     *
1333caaa4d2SGreg Roach     * @return bool
1343caaa4d2SGreg Roach     */
135c1010edaSGreg Roach    public function isUserBlock(): bool
136c1010edaSGreg Roach    {
1373763c3f2SGreg Roach        return true;
1383763c3f2SGreg Roach    }
1393763c3f2SGreg Roach
1403caaa4d2SGreg Roach    /**
1413caaa4d2SGreg Roach     * Can this block be shown on the tree’s home page?
1423caaa4d2SGreg Roach     *
1433caaa4d2SGreg Roach     * @return bool
1443caaa4d2SGreg Roach     */
14563276d8fSGreg Roach    public function isTreeBlock(): bool
146c1010edaSGreg Roach    {
1473763c3f2SGreg Roach        return true;
1483763c3f2SGreg Roach    }
1493763c3f2SGreg Roach
15076692c8bSGreg Roach    /**
151a45f9889SGreg Roach     * Update the configuration for a block.
152a45f9889SGreg Roach     *
1536ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
154a45f9889SGreg Roach     * @param int $block_id
155a45f9889SGreg Roach     *
156a45f9889SGreg Roach     * @return void
157a45f9889SGreg Roach     */
1586ccdf4f0SGreg Roach    public function saveBlockConfiguration(ServerRequestInterface $request, int $block_id): void
159a45f9889SGreg Roach    {
160c50a90beSGreg Roach        $params = $request->getParsedBody();
161c50a90beSGreg Roach
162c50a90beSGreg Roach        $this->setBlockSetting($block_id, 'num', $params['num']);
163c50a90beSGreg Roach        $this->setBlockSetting($block_id, 'infoStyle', $params['infoStyle']);
164a45f9889SGreg Roach    }
165a45f9889SGreg Roach
166a45f9889SGreg Roach    /**
16776692c8bSGreg Roach     * An HTML form to edit block settings
16876692c8bSGreg Roach     *
169e490cd80SGreg Roach     * @param Tree $tree
17076692c8bSGreg Roach     * @param int  $block_id
171a9430be8SGreg Roach     *
1723caaa4d2SGreg Roach     * @return string
17376692c8bSGreg Roach     */
1743caaa4d2SGreg Roach    public function editBlockConfiguration(Tree $tree, int $block_id): string
175c1010edaSGreg Roach    {
17655664801SGreg Roach        $num       = $this->getBlockSetting($block_id, 'num', self::DEFAULT_NUMBER);
17755664801SGreg Roach        $infoStyle = $this->getBlockSetting($block_id, 'infoStyle', self::DEFAULT_STYLE);
1783763c3f2SGreg Roach
179c385536dSGreg Roach        $info_styles = [
180bbb76c12SGreg Roach            /* I18N: An option in a list-box */
181bbb76c12SGreg Roach            'list'  => I18N::translate('list'),
182bbb76c12SGreg Roach            /* I18N: An option in a list-box */
183bbb76c12SGreg Roach            'table' => I18N::translate('table'),
184c385536dSGreg Roach        ];
1853763c3f2SGreg Roach
1863caaa4d2SGreg Roach        return view('modules/top10_givnnames/config', [
187c385536dSGreg Roach            'infoStyle'   => $infoStyle,
188c385536dSGreg Roach            'info_styles' => $info_styles,
189c385536dSGreg Roach            'num'         => $num,
190c385536dSGreg Roach        ]);
1913763c3f2SGreg Roach    }
1923763c3f2SGreg Roach}
193