xref: /webtrees/app/Module/TopGivenNamesModule.php (revision bed27cedea507c28d3a20c0fce49bfb8580f832f)
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\Auth;
21use Fisharebest\Webtrees\I18N;
22use Fisharebest\Webtrees\Statistics;
23use Fisharebest\Webtrees\Tree;
24use Illuminate\Support\Str;
25use Psr\Http\Message\ServerRequestInterface;
26
27/**
28 * Class TopGivenNamesModule
29 */
30class TopGivenNamesModule extends AbstractModule implements ModuleBlockInterface
31{
32    use ModuleBlockTrait;
33
34    // Default values for new blocks.
35    private const DEFAULT_NUMBER = '10';
36    private const DEFAULT_STYLE  = 'table';
37
38    /**
39     * How should this module be identified in the control panel, etc.?
40     *
41     * @return string
42     */
43    public function title(): string
44    {
45        /* I18N: Name of a module. Top=Most common */
46        return I18N::translate('Top given names');
47    }
48
49    /**
50     * A sentence describing what this module does.
51     *
52     * @return string
53     */
54    public function description(): string
55    {
56        /* I18N: Description of the “Top given names” module */
57        return I18N::translate('A list of the most popular given names.');
58    }
59
60    /**
61     * Generate the HTML content of this block.
62     *
63     * @param Tree     $tree
64     * @param int      $block_id
65     * @param string   $ctype
66     * @param string[] $cfg
67     *
68     * @return string
69     */
70    public function getBlock(Tree $tree, int $block_id, string $ctype = '', array $cfg = []): string
71    {
72        $statistics = app(Statistics::class);
73
74        $num       = $this->getBlockSetting($block_id, 'num', self::DEFAULT_NUMBER);
75        $infoStyle = $this->getBlockSetting($block_id, 'infoStyle', self::DEFAULT_STYLE);
76
77        extract($cfg, EXTR_OVERWRITE);
78
79        switch ($infoStyle) {
80            case 'list':
81                $content = view('modules/top10_givnnames/block', [
82                    'males'   => $statistics->commonGivenMaleListTotals('1', $num),
83                    'females' => $statistics->commonGivenFemaleListTotals('1', $num),
84                ]);
85                break;
86            default:
87            case 'table':
88                $content = view('modules/top10_givnnames/block', [
89                    'males'   => $statistics->commonGivenMaleTable('1', $num),
90                    'females' => $statistics->commonGivenFemaleTable('1', $num),
91                ]);
92                break;
93        }
94
95        if ($ctype !== '') {
96            $num = (int) $num;
97
98            if ($num === 1) {
99                // I18N: i.e. most popular given name.
100                $title = I18N::translate('Top given name');
101            } else {
102                // 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
103                $title = I18N::plural('Top %s given name', 'Top %s given names', $num, I18N::number($num));
104            }
105
106            if ($ctype === 'gedcom' && Auth::isManager($tree)) {
107                $config_url = route('tree-page-block-edit', [
108                    'block_id' => $block_id,
109                    'ged'      => $tree->name(),
110                ]);
111            } elseif ($ctype === 'user' && Auth::check()) {
112                $config_url = route('user-page-block-edit', [
113                    'block_id' => $block_id,
114                    'ged'      => $tree->name(),
115                ]);
116            } else {
117                $config_url = '';
118            }
119
120            return view('modules/block-template', [
121                'block'      => Str::kebab($this->name()),
122                'id'         => $block_id,
123                'config_url' => $config_url,
124                'title'      => $title,
125                'content'    => $content,
126            ]);
127        }
128
129        return $content;
130    }
131
132    /** {@inheritdoc} */
133    public function loadAjax(): bool
134    {
135        return false;
136    }
137
138    /** {@inheritdoc} */
139    public function isUserBlock(): bool
140    {
141        return true;
142    }
143
144    /** {@inheritdoc} */
145    public function isTreeBlock(): bool
146    {
147        return true;
148    }
149
150    /**
151     * Update the configuration for a block.
152     *
153     * @param ServerRequestInterface $request
154     * @param int $block_id
155     *
156     * @return void
157     */
158    public function saveBlockConfiguration(ServerRequestInterface $request, int $block_id): void
159    {
160        $params = $request->getParsedBody();
161
162        $this->setBlockSetting($block_id, 'num', $params['num']);
163        $this->setBlockSetting($block_id, 'infoStyle', $params['infoStyle']);
164    }
165
166    /**
167     * An HTML form to edit block settings
168     *
169     * @param Tree $tree
170     * @param int  $block_id
171     *
172     * @return void
173     */
174    public function editBlockConfiguration(Tree $tree, int $block_id): void
175    {
176        $num       = $this->getBlockSetting($block_id, 'num', self::DEFAULT_NUMBER);
177        $infoStyle = $this->getBlockSetting($block_id, 'infoStyle', self::DEFAULT_STYLE);
178
179        $info_styles = [
180            /* I18N: An option in a list-box */
181            'list'  => I18N::translate('list'),
182            /* I18N: An option in a list-box */
183            'table' => I18N::translate('table'),
184        ];
185
186        echo view('modules/top10_givnnames/config', [
187            'infoStyle'   => $infoStyle,
188            'info_styles' => $info_styles,
189            'num'         => $num,
190        ]);
191    }
192}
193