xref: /webtrees/app/Module/TopGivenNamesModule.php (revision 5f2ae57322ecd2d23660294256ca8376fd889348)
13763c3f2SGreg Roach<?php
23763c3f2SGreg Roach/**
33763c3f2SGreg Roach * webtrees: online genealogy
41062a142SGreg Roach * Copyright (C) 2018 webtrees development team
53763c3f2SGreg Roach * This program is free software: you can redistribute it and/or modify
63763c3f2SGreg Roach * it under the terms of the GNU General Public License as published by
73763c3f2SGreg Roach * the Free Software Foundation, either version 3 of the License, or
83763c3f2SGreg Roach * (at your option) any later version.
93763c3f2SGreg Roach * This program is distributed in the hope that it will be useful,
103763c3f2SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
113763c3f2SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
123763c3f2SGreg Roach * GNU General Public License for more details.
133763c3f2SGreg Roach * You should have received a copy of the GNU General Public License
143763c3f2SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
153763c3f2SGreg Roach */
16e7f56f2aSGreg Roachdeclare(strict_types=1);
17e7f56f2aSGreg Roach
1876692c8bSGreg Roachnamespace Fisharebest\Webtrees\Module;
1976692c8bSGreg Roach
200e62c4b8SGreg Roachuse Fisharebest\Webtrees\Auth;
210e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N;
220e62c4b8SGreg Roachuse Fisharebest\Webtrees\Stats;
23e490cd80SGreg Roachuse Fisharebest\Webtrees\Tree;
24a45f9889SGreg Roachuse Symfony\Component\HttpFoundation\Request;
253763c3f2SGreg Roach
263763c3f2SGreg Roach/**
273763c3f2SGreg Roach * Class TopGivenNamesModule
283763c3f2SGreg Roach */
29c1010edaSGreg Roachclass TopGivenNamesModule extends AbstractModule implements ModuleBlockInterface
30c1010edaSGreg Roach{
3155664801SGreg Roach    // Default values for new blocks.
3255664801SGreg Roach    const DEFAULT_NUMBER = '10';
3355664801SGreg Roach    const DEFAULT_STYLE  = 'table';
3455664801SGreg Roach
353763c3f2SGreg Roach    /** {@inheritdoc} */
368f53f488SRico Sonntag    public function getTitle(): string
37c1010edaSGreg Roach    {
38bbb76c12SGreg Roach        /* I18N: Name of a module. Top=Most common */
39bbb76c12SGreg Roach        return I18N::translate('Top given names');
403763c3f2SGreg Roach    }
413763c3f2SGreg Roach
423763c3f2SGreg Roach    /** {@inheritdoc} */
438f53f488SRico Sonntag    public function getDescription(): string
44c1010edaSGreg Roach    {
45bbb76c12SGreg Roach        /* I18N: Description of the “Top given names” module */
46bbb76c12SGreg Roach        return I18N::translate('A list of the most popular given names.');
473763c3f2SGreg Roach    }
483763c3f2SGreg Roach
4976692c8bSGreg Roach    /**
5076692c8bSGreg Roach     * Generate the HTML content of this block.
5176692c8bSGreg Roach     *
52e490cd80SGreg Roach     * @param Tree     $tree
5376692c8bSGreg Roach     * @param int      $block_id
54*5f2ae573SGreg Roach     * @param string   $ctype
55727f238cSGreg Roach     * @param string[] $cfg
5676692c8bSGreg Roach     *
5776692c8bSGreg Roach     * @return string
5876692c8bSGreg Roach     */
59*5f2ae573SGreg Roach    public function getBlock(Tree $tree, int $block_id, string $ctype = '', array $cfg = []): string
60c1010edaSGreg Roach    {
61fce4f17fSGreg Roach        $num       = $this->getBlockSetting($block_id, 'num', self::DEFAULT_NUMBER);
6255664801SGreg Roach        $infoStyle = $this->getBlockSetting($block_id, 'infoStyle', self::DEFAULT_STYLE);
633763c3f2SGreg Roach
64c385536dSGreg Roach        extract($cfg, EXTR_OVERWRITE);
653763c3f2SGreg Roach
66e490cd80SGreg Roach        $stats   = new Stats($tree);
673763c3f2SGreg Roach
683763c3f2SGreg Roach        switch ($infoStyle) {
6945831e7fSGreg Roach            case 'list':
7095768326SGreg Roach                $content = view('modules/top10_givnnames/block', [
7195768326SGreg Roach                    'males'   => $stats->commonGivenMaleListTotals('1', $num),
7295768326SGreg Roach                    'females' => $stats->commonGivenFemaleListTotals('1', $num),
7345831e7fSGreg Roach                ]);
743763c3f2SGreg Roach                break;
7545831e7fSGreg Roach            default:
7645831e7fSGreg Roach            case 'table':
7795768326SGreg Roach                $content = view('modules/top10_givnnames/block', [
7895768326SGreg Roach                    'males'   => $stats->commonGivenMaleTable('1', $num),
7995768326SGreg Roach                    'females' => $stats->commonGivenFemaleTable('1', $num),
8045831e7fSGreg Roach                ]);
813763c3f2SGreg Roach                break;
823763c3f2SGreg Roach        }
833763c3f2SGreg Roach
84*5f2ae573SGreg Roach        if ($ctype) {
85fce4f17fSGreg Roach            $num = (int) $num;
86fce4f17fSGreg Roach
8755664801SGreg Roach            if ($num === 1) {
888cbbfdceSGreg Roach                // I18N: i.e. most popular given name.
898cbbfdceSGreg Roach                $title = I18N::translate('Top given name');
908cbbfdceSGreg Roach            } else {
918cbbfdceSGreg Roach                // 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
928cbbfdceSGreg Roach                $title = I18N::plural('Top %s given name', 'Top %s given names', $num, I18N::number($num));
938cbbfdceSGreg Roach            }
948cbbfdceSGreg Roach
95e490cd80SGreg Roach            if ($ctype === 'gedcom' && Auth::isManager($tree)) {
96c1010edaSGreg Roach                $config_url = route('tree-page-block-edit', [
97c1010edaSGreg Roach                    'block_id' => $block_id,
98aa6f03bbSGreg Roach                    'ged'      => $tree->name(),
99c1010edaSGreg Roach                ]);
100397e599aSGreg Roach            } elseif ($ctype === 'user' && Auth::check()) {
101c1010edaSGreg Roach                $config_url = route('user-page-block-edit', [
102c1010edaSGreg Roach                    'block_id' => $block_id,
103aa6f03bbSGreg Roach                    'ged'      => $tree->name(),
104c1010edaSGreg Roach                ]);
1058cbbfdceSGreg Roach            } else {
1068cbbfdceSGreg Roach                $config_url = '';
1078cbbfdceSGreg Roach            }
1088cbbfdceSGreg Roach
109147e99aaSGreg Roach            return view('modules/block-template', [
1109c6524dcSGreg Roach                'block'      => str_replace('_', '-', $this->getName()),
1119c6524dcSGreg Roach                'id'         => $block_id,
1128cbbfdceSGreg Roach                'config_url' => $config_url,
1138cbbfdceSGreg Roach                'title'      => $title,
1149c6524dcSGreg Roach                'content'    => $content,
1159c6524dcSGreg Roach            ]);
1163763c3f2SGreg Roach        }
117b2ce94c6SRico Sonntag
118b2ce94c6SRico Sonntag        return $content;
1193763c3f2SGreg Roach    }
1203763c3f2SGreg Roach
1213763c3f2SGreg Roach    /** {@inheritdoc} */
122c1010edaSGreg Roach    public function loadAjax(): bool
123c1010edaSGreg Roach    {
1244ecf4f82SGreg Roach        return false;
1253763c3f2SGreg Roach    }
1263763c3f2SGreg Roach
1273763c3f2SGreg Roach    /** {@inheritdoc} */
128c1010edaSGreg Roach    public function isUserBlock(): bool
129c1010edaSGreg Roach    {
1303763c3f2SGreg Roach        return true;
1313763c3f2SGreg Roach    }
1323763c3f2SGreg Roach
1333763c3f2SGreg Roach    /** {@inheritdoc} */
134c1010edaSGreg Roach    public function isGedcomBlock(): bool
135c1010edaSGreg Roach    {
1363763c3f2SGreg Roach        return true;
1373763c3f2SGreg Roach    }
1383763c3f2SGreg Roach
13976692c8bSGreg Roach    /**
140a45f9889SGreg Roach     * Update the configuration for a block.
141a45f9889SGreg Roach     *
142a45f9889SGreg Roach     * @param Request $request
143a45f9889SGreg Roach     * @param int     $block_id
144a45f9889SGreg Roach     *
145a45f9889SGreg Roach     * @return void
146a45f9889SGreg Roach     */
147a45f9889SGreg Roach    public function saveBlockConfiguration(Request $request, int $block_id)
148a45f9889SGreg Roach    {
14955664801SGreg Roach        $this->setBlockSetting($block_id, 'num', $request->get('num', self::DEFAULT_NUMBER));
15055664801SGreg Roach        $this->setBlockSetting($block_id, 'infoStyle', $request->get('infoStyle', self::DEFAULT_STYLE));
151a45f9889SGreg Roach    }
152a45f9889SGreg Roach
153a45f9889SGreg Roach    /**
15476692c8bSGreg Roach     * An HTML form to edit block settings
15576692c8bSGreg Roach     *
156e490cd80SGreg Roach     * @param Tree $tree
15776692c8bSGreg Roach     * @param int  $block_id
158a9430be8SGreg Roach     *
159a9430be8SGreg Roach     * @return void
16076692c8bSGreg Roach     */
161a45f9889SGreg Roach    public function editBlockConfiguration(Tree $tree, int $block_id)
162c1010edaSGreg Roach    {
16355664801SGreg Roach        $num       = $this->getBlockSetting($block_id, 'num', self::DEFAULT_NUMBER);
16455664801SGreg Roach        $infoStyle = $this->getBlockSetting($block_id, 'infoStyle', self::DEFAULT_STYLE);
1653763c3f2SGreg Roach
166c385536dSGreg Roach        $info_styles = [
167bbb76c12SGreg Roach            /* I18N: An option in a list-box */
168bbb76c12SGreg Roach            'list'  => I18N::translate('list'),
169bbb76c12SGreg Roach            /* I18N: An option in a list-box */
170bbb76c12SGreg Roach            'table' => I18N::translate('table'),
171c385536dSGreg Roach        ];
1723763c3f2SGreg Roach
173147e99aaSGreg Roach        echo view('modules/top10_givnnames/config', [
174c385536dSGreg Roach            'infoStyle'   => $infoStyle,
175c385536dSGreg Roach            'info_styles' => $info_styles,
176c385536dSGreg Roach            'num'         => $num,
177c385536dSGreg Roach        ]);
1783763c3f2SGreg Roach    }
1793763c3f2SGreg Roach}
180