13763c3f2SGreg Roach<?php 23763c3f2SGreg Roach/** 33763c3f2SGreg Roach * webtrees: online genealogy 48fcd0d32SGreg Roach * Copyright (C) 2019 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; 22*9219296aSGreg Roachuse Fisharebest\Webtrees\Statistics; 23e490cd80SGreg Roachuse Fisharebest\Webtrees\Tree; 24a45f9889SGreg Roachuse Symfony\Component\HttpFoundation\Request; 253763c3f2SGreg Roach 263763c3f2SGreg Roach/** 273763c3f2SGreg Roach * Class TopGivenNamesModule 283763c3f2SGreg Roach */ 2937eb8894SGreg Roachclass TopGivenNamesModule extends AbstractModule implements ModuleBlockInterface 30c1010edaSGreg Roach{ 3149a243cbSGreg Roach use ModuleBlockTrait; 3249a243cbSGreg Roach 3355664801SGreg Roach // Default values for new blocks. 3416d6367aSGreg Roach private const DEFAULT_NUMBER = '10'; 3516d6367aSGreg Roach private const DEFAULT_STYLE = 'table'; 3655664801SGreg Roach 37961ec755SGreg Roach /** 38*9219296aSGreg Roach * @var Statistics 39*9219296aSGreg Roach */ 40*9219296aSGreg Roach private $statistics; 41*9219296aSGreg Roach 42*9219296aSGreg Roach /** 43*9219296aSGreg Roach * TopGivenNamesModule constructor. 44*9219296aSGreg Roach * 45*9219296aSGreg Roach * @param Statistics $statistics 46*9219296aSGreg Roach */ 47*9219296aSGreg Roach public function __construct(Statistics $statistics) { 48*9219296aSGreg Roach $this->statistics = $statistics; 49*9219296aSGreg Roach } 50*9219296aSGreg Roach 51*9219296aSGreg Roach /** 52961ec755SGreg Roach * How should this module be labelled on tabs, menus, etc.? 53961ec755SGreg Roach * 54961ec755SGreg Roach * @return string 55961ec755SGreg Roach */ 5649a243cbSGreg Roach public function title(): string 57c1010edaSGreg Roach { 58bbb76c12SGreg Roach /* I18N: Name of a module. Top=Most common */ 59bbb76c12SGreg Roach return I18N::translate('Top given names'); 603763c3f2SGreg Roach } 613763c3f2SGreg Roach 62961ec755SGreg Roach /** 63961ec755SGreg Roach * A sentence describing what this module does. 64961ec755SGreg Roach * 65961ec755SGreg Roach * @return string 66961ec755SGreg Roach */ 6749a243cbSGreg Roach public function description(): string 68c1010edaSGreg Roach { 69bbb76c12SGreg Roach /* I18N: Description of the “Top given names” module */ 70bbb76c12SGreg Roach return I18N::translate('A list of the most popular given names.'); 713763c3f2SGreg Roach } 723763c3f2SGreg Roach 7376692c8bSGreg Roach /** 7476692c8bSGreg Roach * Generate the HTML content of this block. 7576692c8bSGreg Roach * 76e490cd80SGreg Roach * @param Tree $tree 7776692c8bSGreg Roach * @param int $block_id 785f2ae573SGreg Roach * @param string $ctype 79727f238cSGreg Roach * @param string[] $cfg 8076692c8bSGreg Roach * 8176692c8bSGreg Roach * @return string 8276692c8bSGreg Roach */ 835f2ae573SGreg Roach public function getBlock(Tree $tree, int $block_id, string $ctype = '', array $cfg = []): string 84c1010edaSGreg Roach { 85fce4f17fSGreg Roach $num = $this->getBlockSetting($block_id, 'num', self::DEFAULT_NUMBER); 8655664801SGreg Roach $infoStyle = $this->getBlockSetting($block_id, 'infoStyle', self::DEFAULT_STYLE); 873763c3f2SGreg Roach 88c385536dSGreg Roach extract($cfg, EXTR_OVERWRITE); 893763c3f2SGreg Roach 903763c3f2SGreg Roach switch ($infoStyle) { 9145831e7fSGreg Roach case 'list': 9295768326SGreg Roach $content = view('modules/top10_givnnames/block', [ 93*9219296aSGreg Roach 'males' => $this->statistics->commonGivenMaleListTotals('1', $num), 94*9219296aSGreg Roach 'females' => $this->statistics->commonGivenFemaleListTotals('1', $num), 9545831e7fSGreg Roach ]); 963763c3f2SGreg Roach break; 9745831e7fSGreg Roach default: 9845831e7fSGreg Roach case 'table': 9995768326SGreg Roach $content = view('modules/top10_givnnames/block', [ 100*9219296aSGreg Roach 'males' => $this->statistics->commonGivenMaleTable('1', $num), 101*9219296aSGreg Roach 'females' => $this->statistics->commonGivenFemaleTable('1', $num), 10245831e7fSGreg Roach ]); 1033763c3f2SGreg Roach break; 1043763c3f2SGreg Roach } 1053763c3f2SGreg Roach 1066a8879feSGreg Roach if ($ctype !== '') { 107fce4f17fSGreg Roach $num = (int) $num; 108fce4f17fSGreg Roach 10955664801SGreg Roach if ($num === 1) { 1108cbbfdceSGreg Roach // I18N: i.e. most popular given name. 1118cbbfdceSGreg Roach $title = I18N::translate('Top given name'); 1128cbbfdceSGreg Roach } else { 1138cbbfdceSGreg 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 1148cbbfdceSGreg Roach $title = I18N::plural('Top %s given name', 'Top %s given names', $num, I18N::number($num)); 1158cbbfdceSGreg Roach } 1168cbbfdceSGreg Roach 117e490cd80SGreg Roach if ($ctype === 'gedcom' && Auth::isManager($tree)) { 118c1010edaSGreg Roach $config_url = route('tree-page-block-edit', [ 119c1010edaSGreg Roach 'block_id' => $block_id, 120aa6f03bbSGreg Roach 'ged' => $tree->name(), 121c1010edaSGreg Roach ]); 122397e599aSGreg Roach } elseif ($ctype === 'user' && Auth::check()) { 123c1010edaSGreg Roach $config_url = route('user-page-block-edit', [ 124c1010edaSGreg Roach 'block_id' => $block_id, 125aa6f03bbSGreg Roach 'ged' => $tree->name(), 126c1010edaSGreg Roach ]); 1278cbbfdceSGreg Roach } else { 1288cbbfdceSGreg Roach $config_url = ''; 1298cbbfdceSGreg Roach } 1308cbbfdceSGreg Roach 131147e99aaSGreg Roach return view('modules/block-template', [ 13226684e68SGreg Roach 'block' => str_replace('_', '-', $this->name()), 1339c6524dcSGreg Roach 'id' => $block_id, 1348cbbfdceSGreg Roach 'config_url' => $config_url, 1358cbbfdceSGreg Roach 'title' => $title, 1369c6524dcSGreg Roach 'content' => $content, 1379c6524dcSGreg Roach ]); 1383763c3f2SGreg Roach } 139b2ce94c6SRico Sonntag 140b2ce94c6SRico Sonntag return $content; 1413763c3f2SGreg Roach } 1423763c3f2SGreg Roach 1433763c3f2SGreg Roach /** {@inheritdoc} */ 144c1010edaSGreg Roach public function loadAjax(): bool 145c1010edaSGreg Roach { 1464ecf4f82SGreg Roach return false; 1473763c3f2SGreg Roach } 1483763c3f2SGreg Roach 1493763c3f2SGreg Roach /** {@inheritdoc} */ 150c1010edaSGreg Roach public function isUserBlock(): bool 151c1010edaSGreg Roach { 1523763c3f2SGreg Roach return true; 1533763c3f2SGreg Roach } 1543763c3f2SGreg Roach 1553763c3f2SGreg Roach /** {@inheritdoc} */ 15663276d8fSGreg Roach public function isTreeBlock(): bool 157c1010edaSGreg Roach { 1583763c3f2SGreg Roach return true; 1593763c3f2SGreg Roach } 1603763c3f2SGreg Roach 16176692c8bSGreg Roach /** 162a45f9889SGreg Roach * Update the configuration for a block. 163a45f9889SGreg Roach * 164a45f9889SGreg Roach * @param Request $request 165a45f9889SGreg Roach * @param int $block_id 166a45f9889SGreg Roach * 167a45f9889SGreg Roach * @return void 168a45f9889SGreg Roach */ 169a45f9889SGreg Roach public function saveBlockConfiguration(Request $request, int $block_id) 170a45f9889SGreg Roach { 17155664801SGreg Roach $this->setBlockSetting($block_id, 'num', $request->get('num', self::DEFAULT_NUMBER)); 17255664801SGreg Roach $this->setBlockSetting($block_id, 'infoStyle', $request->get('infoStyle', self::DEFAULT_STYLE)); 173a45f9889SGreg Roach } 174a45f9889SGreg Roach 175a45f9889SGreg Roach /** 17676692c8bSGreg Roach * An HTML form to edit block settings 17776692c8bSGreg Roach * 178e490cd80SGreg Roach * @param Tree $tree 17976692c8bSGreg Roach * @param int $block_id 180a9430be8SGreg Roach * 181a9430be8SGreg Roach * @return void 18276692c8bSGreg Roach */ 183a45f9889SGreg Roach public function editBlockConfiguration(Tree $tree, int $block_id) 184c1010edaSGreg Roach { 18555664801SGreg Roach $num = $this->getBlockSetting($block_id, 'num', self::DEFAULT_NUMBER); 18655664801SGreg Roach $infoStyle = $this->getBlockSetting($block_id, 'infoStyle', self::DEFAULT_STYLE); 1873763c3f2SGreg Roach 188c385536dSGreg Roach $info_styles = [ 189bbb76c12SGreg Roach /* I18N: An option in a list-box */ 190bbb76c12SGreg Roach 'list' => I18N::translate('list'), 191bbb76c12SGreg Roach /* I18N: An option in a list-box */ 192bbb76c12SGreg Roach 'table' => I18N::translate('table'), 193c385536dSGreg Roach ]; 1943763c3f2SGreg Roach 195147e99aaSGreg Roach echo view('modules/top10_givnnames/config', [ 196c385536dSGreg Roach 'infoStyle' => $infoStyle, 197c385536dSGreg Roach 'info_styles' => $info_styles, 198c385536dSGreg Roach 'num' => $num, 199c385536dSGreg Roach ]); 2003763c3f2SGreg Roach } 2013763c3f2SGreg Roach} 202