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