xref: /webtrees/app/Module/TopGivenNamesModule.php (revision 8634febf6487fcb3ee7b59e88f828071a22cba22)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2023 webtrees development team
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17
18declare(strict_types=1);
19
20namespace Fisharebest\Webtrees\Module;
21
22use Fisharebest\Webtrees\I18N;
23use Fisharebest\Webtrees\Statistics;
24use Fisharebest\Webtrees\Tree;
25use Fisharebest\Webtrees\Validator;
26use Illuminate\Support\Str;
27use Psr\Http\Message\ServerRequestInterface;
28
29/**
30 * Class TopGivenNamesModule
31 */
32class TopGivenNamesModule extends AbstractModule implements ModuleBlockInterface
33{
34    use ModuleBlockTrait;
35
36    // Default values for new blocks.
37    private const DEFAULT_NUMBER = '10';
38    private const DEFAULT_STYLE  = 'table';
39
40    /**
41     * How should this module be identified in the control panel, etc.?
42     *
43     * @return string
44     */
45    public function title(): string
46    {
47        /* I18N: Name of a module. Top=Most common */
48        return I18N::translate('Top given names');
49    }
50
51    /**
52     * A sentence describing what this module does.
53     *
54     * @return string
55     */
56    public function description(): string
57    {
58        /* I18N: Description of the “Top given names” module */
59        return I18N::translate('A list of the most popular given names.');
60    }
61
62    /**
63     * Generate the HTML content of this block.
64     *
65     * @param Tree                 $tree
66     * @param int                  $block_id
67     * @param string               $context
68     * @param array<string,string> $config
69     *
70     * @return string
71     */
72    public function getBlock(Tree $tree, int $block_id, string $context, array $config = []): string
73    {
74        $statistics = app(Statistics::class);
75
76        $num       = $this->getBlockSetting($block_id, 'num', self::DEFAULT_NUMBER);
77        $infoStyle = $this->getBlockSetting($block_id, 'infoStyle', self::DEFAULT_STYLE);
78
79        extract($config, EXTR_OVERWRITE);
80
81        switch ($infoStyle) {
82            case 'list':
83                $content = view('modules/top10_givnnames/block', [
84                    'males'   => $statistics->commonGivenMaleListTotals('1', $num),
85                    'females' => $statistics->commonGivenFemaleListTotals('1', $num),
86                ]);
87                break;
88            default:
89            case 'table':
90                $content = view('modules/top10_givnnames/block', [
91                    'males'   => $statistics->commonGivenMaleTable('1', $num),
92                    'females' => $statistics->commonGivenFemaleTable('1', $num),
93                ]);
94                break;
95        }
96
97        if ($context !== self::CONTEXT_EMBED) {
98            $num = (int) $num;
99
100            if ($num === 1) {
101                // I18N: i.e. most popular given name.
102                $title = I18N::translate('Top given name');
103            } else {
104                // 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
105                $title = I18N::plural('Top %s given name', 'Top %s given names', $num, I18N::number($num));
106            }
107
108            return view('modules/block-template', [
109                'block'      => Str::kebab($this->name()),
110                'id'         => $block_id,
111                'config_url' => $this->configUrl($tree, $context, $block_id),
112                'title'      => $title,
113                'content'    => $content,
114            ]);
115        }
116
117        return $content;
118    }
119
120    /**
121     * Should this block load asynchronously using AJAX?
122     *
123     * Simple blocks are faster in-line, more complex ones can be loaded later.
124     *
125     * @return bool
126     */
127    public function loadAjax(): bool
128    {
129        return false;
130    }
131
132    /**
133     * Can this block be shown on the user’s home page?
134     *
135     * @return bool
136     */
137    public function isUserBlock(): bool
138    {
139        return true;
140    }
141
142    /**
143     * Can this block be shown on the tree’s home page?
144     *
145     * @return bool
146     */
147    public function isTreeBlock(): bool
148    {
149        return true;
150    }
151
152    /**
153     * Update the configuration for a block.
154     *
155     * @param ServerRequestInterface $request
156     * @param int $block_id
157     *
158     * @return void
159     */
160    public function saveBlockConfiguration(ServerRequestInterface $request, int $block_id): void
161    {
162        $num        = Validator::parsedBody($request)->integer('num');
163        $info_style = Validator::parsedBody($request)->string('infoStyle');
164
165        $this->setBlockSetting($block_id, 'num', (string) $num);
166        $this->setBlockSetting($block_id, 'infoStyle', $info_style);
167    }
168
169    /**
170     * An HTML form to edit block settings
171     *
172     * @param Tree $tree
173     * @param int  $block_id
174     *
175     * @return string
176     */
177    public function editBlockConfiguration(Tree $tree, int $block_id): string
178    {
179        $num        = (int) $this->getBlockSetting($block_id, 'num', self::DEFAULT_NUMBER);
180        $info_style = $this->getBlockSetting($block_id, 'infoStyle', self::DEFAULT_STYLE);
181
182        $info_styles = [
183            /* I18N: An option in a list-box */
184            'list'  => I18N::translate('list'),
185            /* I18N: An option in a list-box */
186            'table' => I18N::translate('table'),
187        ];
188
189        return view('modules/top10_givnnames/config', [
190            'info_style'  => $info_style,
191            'info_styles' => $info_styles,
192            'num'         => $num,
193        ]);
194    }
195}
196