xref: /webtrees/app/Module/TopGivenNamesModule.php (revision b6ec1ccf23d5adc5a335304551953cfb6f52eef2)
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\Registry;
24use Fisharebest\Webtrees\Statistics;
25use Fisharebest\Webtrees\Tree;
26use Fisharebest\Webtrees\Validator;
27use Illuminate\Support\Str;
28use Psr\Http\Message\ServerRequestInterface;
29
30/**
31 * Class TopGivenNamesModule
32 */
33class TopGivenNamesModule extends AbstractModule implements ModuleBlockInterface
34{
35    use ModuleBlockTrait;
36
37    // Default values for new blocks.
38    private const DEFAULT_NUMBER = '10';
39    private const DEFAULT_STYLE  = 'table';
40
41    /**
42     * How should this module be identified in the control panel, etc.?
43     *
44     * @return string
45     */
46    public function title(): string
47    {
48        /* I18N: Name of a module. Top=Most common */
49        return I18N::translate('Top given names');
50    }
51
52    /**
53     * A sentence describing what this module does.
54     *
55     * @return string
56     */
57    public function description(): string
58    {
59        /* I18N: Description of the “Top given names” module */
60        return I18N::translate('A list of the most popular given names.');
61    }
62
63    /**
64     * Generate the HTML content of this block.
65     *
66     * @param Tree                 $tree
67     * @param int                  $block_id
68     * @param string               $context
69     * @param array<string,string> $config
70     *
71     * @return string
72     */
73    public function getBlock(Tree $tree, int $block_id, string $context, array $config = []): string
74    {
75        $statistics = Registry::container()->get(Statistics::class);
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