xref: /webtrees/app/Module/TopGivenNamesModule.php (revision dd6b2bfcc550270bb6d6778e11576148f71e4330)
1<?php
2/**
3 * webtrees: online genealogy
4 * Copyright (C) 2018 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    const DEFAULT_NUMBER = '10';
33    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 bool     $template
55     * @param string[] $cfg
56     *
57     * @return string
58     */
59    public function getBlock(Tree $tree, int $block_id, bool $template = true, array $cfg = []): string
60    {
61        global $ctype;
62
63        $num       = $this->getBlockSetting($block_id, 'num', self::DEFAULT_NUMBER);
64        $infoStyle = $this->getBlockSetting($block_id, 'infoStyle', self::DEFAULT_STYLE);
65
66        extract($cfg, EXTR_OVERWRITE);
67
68        $stats   = new Stats($tree);
69        $males   = $stats->commonGivenMaleTable('1', $num);
70        $females = $stats->commonGivenFemaleTable('1', $num);
71
72        switch ($infoStyle) {
73            case 'list':
74                $content = view('modules/top10_givnnames/list', [
75                    'males'   => $males,
76                    'females' => $females,
77                ]);
78                break;
79            default:
80            case 'table':
81                $content = view('modules/top10_givnnames/table', [
82                    'males'   => $males,
83                    'females' => $females,
84                ]);
85                break;
86        }
87
88        if ($template) {
89            $num = (int) $num;
90
91            if ($num === 1) {
92                // I18N: i.e. most popular given name.
93                $title = I18N::translate('Top given name');
94            } else {
95                // 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
96                $title = I18N::plural('Top %s given name', 'Top %s given names', $num, I18N::number($num));
97            }
98
99            if ($ctype === 'gedcom' && Auth::isManager($tree)) {
100                $config_url = route('tree-page-block-edit', [
101                    'block_id' => $block_id,
102                    'ged'      => $tree->getName(),
103                ]);
104            } elseif ($ctype === 'user' && Auth::check()) {
105                $config_url = route('user-page-block-edit', [
106                    'block_id' => $block_id,
107                    'ged'      => $tree->getName(),
108                ]);
109            } else {
110                $config_url = '';
111            }
112
113            return view('modules/block-template', [
114                'block'      => str_replace('_', '-', $this->getName()),
115                'id'         => $block_id,
116                'config_url' => $config_url,
117                'title'      => $title,
118                'content'    => $content,
119            ]);
120        }
121
122        return $content;
123    }
124
125    /** {@inheritdoc} */
126    public function loadAjax(): bool
127    {
128        return false;
129    }
130
131    /** {@inheritdoc} */
132    public function isUserBlock(): bool
133    {
134        return true;
135    }
136
137    /** {@inheritdoc} */
138    public function isGedcomBlock(): bool
139    {
140        return true;
141    }
142
143    /**
144     * Update the configuration for a block.
145     *
146     * @param Request $request
147     * @param int     $block_id
148     *
149     * @return void
150     */
151    public function saveBlockConfiguration(Request $request, int $block_id)
152    {
153        $this->setBlockSetting($block_id, 'num', $request->get('num', self::DEFAULT_NUMBER));
154        $this->setBlockSetting($block_id, 'infoStyle', $request->get('infoStyle', self::DEFAULT_STYLE));
155    }
156
157    /**
158     * An HTML form to edit block settings
159     *
160     * @param Tree $tree
161     * @param int  $block_id
162     *
163     * @return void
164     */
165    public function editBlockConfiguration(Tree $tree, int $block_id)
166    {
167        $num       = $this->getBlockSetting($block_id, 'num', self::DEFAULT_NUMBER);
168        $infoStyle = $this->getBlockSetting($block_id, 'infoStyle', self::DEFAULT_STYLE);
169
170        $info_styles = [
171            /* I18N: An option in a list-box */
172            'list'  => I18N::translate('list'),
173            /* I18N: An option in a list-box */
174            'table' => I18N::translate('table'),
175        ];
176
177        echo view('modules/top10_givnnames/config', [
178            'infoStyle'   => $infoStyle,
179            'info_styles' => $info_styles,
180            'num'         => $num,
181        ]);
182    }
183}
184