xref: /webtrees/app/Module/TopGivenNamesModule.php (revision 17907095d917ef2b56d7bdf08f08ea42db03cb32)
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\Stats;
23use Fisharebest\Webtrees\Tree;
24use Symfony\Component\HttpFoundation\Request;
25
26/**
27 * Class TopGivenNamesModule
28 */
29class TopGivenNamesModule extends AbstractModule implements ModuleBlockInterface
30{
31    // Default values for new blocks.
32    private const DEFAULT_NUMBER = '10';
33    private const DEFAULT_STYLE  = 'table';
34
35    /** {@inheritdoc} */
36    public function getTitle(): string
37    {
38        /* I18N: Name of a module. Top=Most common */
39        return I18N::translate('Top given names');
40    }
41
42    /** {@inheritdoc} */
43    public function getDescription(): string
44    {
45        /* I18N: Description of the “Top given names” module */
46        return I18N::translate('A list of the most popular given names.');
47    }
48
49    /**
50     * Generate the HTML content of this block.
51     *
52     * @param Tree     $tree
53     * @param int      $block_id
54     * @param string   $ctype
55     * @param string[] $cfg
56     *
57     * @return string
58     */
59    public function getBlock(Tree $tree, int $block_id, string $ctype = '', array $cfg = []): string
60    {
61        $num       = $this->getBlockSetting($block_id, 'num', self::DEFAULT_NUMBER);
62        $infoStyle = $this->getBlockSetting($block_id, 'infoStyle', self::DEFAULT_STYLE);
63
64        extract($cfg, EXTR_OVERWRITE);
65
66        $stats   = new Stats($tree);
67
68        switch ($infoStyle) {
69            case 'list':
70                $content = view('modules/top10_givnnames/block', [
71                    'males'   => $stats->commonGivenMaleListTotals('1', $num),
72                    'females' => $stats->commonGivenFemaleListTotals('1', $num),
73                ]);
74                break;
75            default:
76            case 'table':
77                $content = view('modules/top10_givnnames/block', [
78                    'males'   => $stats->commonGivenMaleTable('1', $num),
79                    'females' => $stats->commonGivenFemaleTable('1', $num),
80                ]);
81                break;
82        }
83
84        if ($ctype !== '') {
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            if ($ctype === 'gedcom' && Auth::isManager($tree)) {
96                $config_url = route('tree-page-block-edit', [
97                    'block_id' => $block_id,
98                    'ged'      => $tree->name(),
99                ]);
100            } elseif ($ctype === 'user' && Auth::check()) {
101                $config_url = route('user-page-block-edit', [
102                    'block_id' => $block_id,
103                    'ged'      => $tree->name(),
104                ]);
105            } else {
106                $config_url = '';
107            }
108
109            return view('modules/block-template', [
110                'block'      => str_replace('_', '-', $this->getName()),
111                'id'         => $block_id,
112                'config_url' => $config_url,
113                'title'      => $title,
114                'content'    => $content,
115            ]);
116        }
117
118        return $content;
119    }
120
121    /** {@inheritdoc} */
122    public function loadAjax(): bool
123    {
124        return false;
125    }
126
127    /** {@inheritdoc} */
128    public function isUserBlock(): bool
129    {
130        return true;
131    }
132
133    /** {@inheritdoc} */
134    public function isGedcomBlock(): bool
135    {
136        return true;
137    }
138
139    /**
140     * Update the configuration for a block.
141     *
142     * @param Request $request
143     * @param int     $block_id
144     *
145     * @return void
146     */
147    public function saveBlockConfiguration(Request $request, int $block_id)
148    {
149        $this->setBlockSetting($block_id, 'num', $request->get('num', self::DEFAULT_NUMBER));
150        $this->setBlockSetting($block_id, 'infoStyle', $request->get('infoStyle', self::DEFAULT_STYLE));
151    }
152
153    /**
154     * An HTML form to edit block settings
155     *
156     * @param Tree $tree
157     * @param int  $block_id
158     *
159     * @return void
160     */
161    public function editBlockConfiguration(Tree $tree, int $block_id)
162    {
163        $num       = $this->getBlockSetting($block_id, 'num', self::DEFAULT_NUMBER);
164        $infoStyle = $this->getBlockSetting($block_id, 'infoStyle', self::DEFAULT_STYLE);
165
166        $info_styles = [
167            /* I18N: An option in a list-box */
168            'list'  => I18N::translate('list'),
169            /* I18N: An option in a list-box */
170            'table' => I18N::translate('table'),
171        ];
172
173        echo view('modules/top10_givnnames/config', [
174            'infoStyle'   => $infoStyle,
175            'info_styles' => $info_styles,
176            'num'         => $num,
177        ]);
178    }
179}
180