xref: /webtrees/app/Module/TopGivenNamesModule.php (revision b2ce94c6833c25934b40a7e6bc15985d50b5f42a)
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 */
16namespace Fisharebest\Webtrees\Module;
17
18use Fisharebest\Webtrees\Auth;
19use Fisharebest\Webtrees\I18N;
20use Fisharebest\Webtrees\Stats;
21use Fisharebest\Webtrees\Tree;
22use Symfony\Component\HttpFoundation\Request;
23
24/**
25 * Class TopGivenNamesModule
26 */
27class TopGivenNamesModule extends AbstractModule implements ModuleBlockInterface
28{
29    // Default values for new blocks.
30    const DEFAULT_NUMBER = '10';
31    const DEFAULT_STYLE  = 'table';
32
33    /** {@inheritdoc} */
34    public function getTitle(): string
35    {
36        /* I18N: Name of a module. Top=Most common */
37        return I18N::translate('Top given names');
38    }
39
40    /** {@inheritdoc} */
41    public function getDescription(): string
42    {
43        /* I18N: Description of the “Top given names” module */
44        return I18N::translate('A list of the most popular given names.');
45    }
46
47    /**
48     * Generate the HTML content of this block.
49     *
50     * @param Tree     $tree
51     * @param int      $block_id
52     * @param bool     $template
53     * @param string[] $cfg
54     *
55     * @return string
56     */
57    public function getBlock(Tree $tree, int $block_id, bool $template = true, array $cfg = []): string
58    {
59        global $ctype;
60
61        $num       = (int) $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        $males   = $stats->commonGivenMaleTable(['1', $num, 'rcount']);
68        $females = $stats->commonGivenFemaleTable(['1', $num, 'rcount']);
69
70        switch ($infoStyle) {
71            case 'list':
72                $content = view('modules/top10_givnnames/list', [
73                    'males'   => $males,
74                    'females' => $females,
75                ]);
76                break;
77            default:
78            case 'table':
79                $content = view('modules/top10_givnnames/table', [
80                    'males'   => $males,
81                    'females' => $females,
82                ]);
83                break;
84        }
85
86        if ($template) {
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->getName(),
99                ]);
100            } elseif ($ctype === 'user' && Auth::check()) {
101                $config_url = route('user-page-block-edit', [
102                    'block_id' => $block_id,
103                    'ged'      => $tree->getName(),
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