Issues (2564)

app/Module/TopGivenNamesModule.php (1 issue)

Labels
Severity
1
<?php
2
3
/**
4
 * webtrees: online genealogy
5
 * Copyright (C) 2025 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
18
declare(strict_types=1);
19
20
namespace Fisharebest\Webtrees\Module;
21
22
use Fisharebest\Webtrees\I18N;
23
use Fisharebest\Webtrees\Registry;
24
use Fisharebest\Webtrees\Statistics;
25
use Fisharebest\Webtrees\Tree;
26
use Fisharebest\Webtrees\Validator;
27
use Illuminate\Support\Str;
28
use Psr\Http\Message\ServerRequestInterface;
29
30
class TopGivenNamesModule extends AbstractModule implements ModuleBlockInterface
31
{
32
    use ModuleBlockTrait;
33
34
    // Default values for new blocks.
35
    private const string DEFAULT_NUMBER = '10';
0 ignored issues
show
A parse error occurred: Syntax error, unexpected T_STRING, expecting '=' on line 35 at column 25
Loading history...
36
    private const string DEFAULT_STYLE  = 'table';
37
38
    public function title(): string
39
    {
40
        /* I18N: Name of a module. Top=Most common */
41
        return I18N::translate('Top given names');
42
    }
43
44
    public function description(): string
45
    {
46
        /* I18N: Description of the “Top given names” module */
47
        return I18N::translate('A list of the most popular given names.');
48
    }
49
50
    /**
51
     * Generate the HTML content of this block.
52
     *
53
     * @param Tree                 $tree
54
     * @param int                  $block_id
55
     * @param string               $context
56
     * @param array<string,string> $config
57
     *
58
     * @return string
59
     */
60
    public function getBlock(Tree $tree, int $block_id, string $context, array $config = []): string
61
    {
62
        $statistics = Registry::container()->get(Statistics::class);
63
        $num        = $this->getBlockSetting($block_id, 'num', self::DEFAULT_NUMBER);
64
        $infoStyle  = $this->getBlockSetting($block_id, 'infoStyle', self::DEFAULT_STYLE);
65
66
        extract($config, EXTR_OVERWRITE);
67
68
        switch ($infoStyle) {
69
            case 'list':
70
                $content = view('modules/top10_givnnames/block', [
71
                    'males'   => $statistics->commonGivenMaleListTotals('1', $num),
72
                    'females' => $statistics->commonGivenFemaleListTotals('1', $num),
73
                ]);
74
                break;
75
            default:
76
            case 'table':
77
                $content = view('modules/top10_givnnames/block', [
78
                    'males'   => $statistics->commonGivenMaleTable('1', $num),
79
                    'females' => $statistics->commonGivenFemaleTable('1', $num),
80
                ]);
81
                break;
82
        }
83
84
        if ($context !== self::CONTEXT_EMBED) {
85
            $num = (int) $num;
86
87
            if ($num === 1) {
88
                // I18N: i.e. most popular given name.
89
                $title = I18N::translate('Top given name');
90
            } else {
91
                // 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
92
                $title = I18N::plural('Top %s given name', 'Top %s given names', $num, I18N::number($num));
93
            }
94
95
            return view('modules/block-template', [
96
                'block'      => Str::kebab($this->name()),
97
                'id'         => $block_id,
98
                'config_url' => $this->configUrl($tree, $context, $block_id),
99
                'title'      => $title,
100
                'content'    => $content,
101
            ]);
102
        }
103
104
        return $content;
105
    }
106
107
    /**
108
     * Should this block load asynchronously using AJAX?
109
     *
110
     * Simple blocks are faster in-line, more complex ones can be loaded later.
111
     *
112
     * @return bool
113
     */
114
    public function loadAjax(): bool
115
    {
116
        return false;
117
    }
118
119
    /**
120
     * Can this block be shown on the user’s home page?
121
     *
122
     * @return bool
123
     */
124
    public function isUserBlock(): bool
125
    {
126
        return true;
127
    }
128
129
    /**
130
     * Can this block be shown on the tree’s home page?
131
     *
132
     * @return bool
133
     */
134
    public function isTreeBlock(): bool
135
    {
136
        return true;
137
    }
138
139
    /**
140
     * Update the configuration for a block.
141
     *
142
     * @param ServerRequestInterface $request
143
     * @param int $block_id
144
     *
145
     * @return void
146
     */
147
    public function saveBlockConfiguration(ServerRequestInterface $request, int $block_id): void
148
    {
149
        $num        = Validator::parsedBody($request)->integer('num');
150
        $info_style = Validator::parsedBody($request)->string('infoStyle');
151
152
        $this->setBlockSetting($block_id, 'num', (string) $num);
153
        $this->setBlockSetting($block_id, 'infoStyle', $info_style);
154
    }
155
156
    /**
157
     * An HTML form to edit block settings
158
     *
159
     * @param Tree $tree
160
     * @param int  $block_id
161
     *
162
     * @return string
163
     */
164
    public function editBlockConfiguration(Tree $tree, int $block_id): string
165
    {
166
        $num        = (int) $this->getBlockSetting($block_id, 'num', self::DEFAULT_NUMBER);
167
        $info_style = $this->getBlockSetting($block_id, 'infoStyle', self::DEFAULT_STYLE);
168
169
        $info_styles = [
170
            /* I18N: An option in a list-box */
171
            'list'  => I18N::translate('list'),
172
            /* I18N: An option in a list-box */
173
            'table' => I18N::translate('table'),
174
        ];
175
176
        return view('modules/top10_givnnames/config', [
177
            'info_style'  => $info_style,
178
            'info_styles' => $info_styles,
179
            'num'         => $num,
180
        ]);
181
    }
182
}
183