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