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\Statistics; 23use Fisharebest\Webtrees\Tree; 24use Symfony\Component\HttpFoundation\Request; 25 26/** 27 * Class TopGivenNamesModule 28 */ 29class TopGivenNamesModule extends AbstractModule implements ModuleBlockInterface 30{ 31 use ModuleBlockTrait; 32 33 // Default values for new blocks. 34 private const DEFAULT_NUMBER = '10'; 35 private const DEFAULT_STYLE = 'table'; 36 37 /** 38 * @var Statistics 39 */ 40 private $statistics; 41 42 /** 43 * TopGivenNamesModule constructor. 44 * 45 * @param Statistics $statistics 46 */ 47 public function __construct(Statistics $statistics) 48 { 49 $this->statistics = $statistics; 50 } 51 52 /** 53 * How should this module be labelled on tabs, menus, etc.? 54 * 55 * @return string 56 */ 57 public function title(): string 58 { 59 /* I18N: Name of a module. Top=Most common */ 60 return I18N::translate('Top given names'); 61 } 62 63 /** 64 * A sentence describing what this module does. 65 * 66 * @return string 67 */ 68 public function description(): string 69 { 70 /* I18N: Description of the “Top given names” module */ 71 return I18N::translate('A list of the most popular given names.'); 72 } 73 74 /** 75 * Generate the HTML content of this block. 76 * 77 * @param Tree $tree 78 * @param int $block_id 79 * @param string $ctype 80 * @param string[] $cfg 81 * 82 * @return string 83 */ 84 public function getBlock(Tree $tree, int $block_id, string $ctype = '', array $cfg = []): string 85 { 86 $num = $this->getBlockSetting($block_id, 'num', self::DEFAULT_NUMBER); 87 $infoStyle = $this->getBlockSetting($block_id, 'infoStyle', self::DEFAULT_STYLE); 88 89 extract($cfg, EXTR_OVERWRITE); 90 91 switch ($infoStyle) { 92 case 'list': 93 $content = view('modules/top10_givnnames/block', [ 94 'males' => $this->statistics->commonGivenMaleListTotals('1', $num), 95 'females' => $this->statistics->commonGivenFemaleListTotals('1', $num), 96 ]); 97 break; 98 default: 99 case 'table': 100 $content = view('modules/top10_givnnames/block', [ 101 'males' => $this->statistics->commonGivenMaleTable('1', $num), 102 'females' => $this->statistics->commonGivenFemaleTable('1', $num), 103 ]); 104 break; 105 } 106 107 if ($ctype !== '') { 108 $num = (int) $num; 109 110 if ($num === 1) { 111 // I18N: i.e. most popular given name. 112 $title = I18N::translate('Top given name'); 113 } else { 114 // 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 115 $title = I18N::plural('Top %s given name', 'Top %s given names', $num, I18N::number($num)); 116 } 117 118 if ($ctype === 'gedcom' && Auth::isManager($tree)) { 119 $config_url = route('tree-page-block-edit', [ 120 'block_id' => $block_id, 121 'ged' => $tree->name(), 122 ]); 123 } elseif ($ctype === 'user' && Auth::check()) { 124 $config_url = route('user-page-block-edit', [ 125 'block_id' => $block_id, 126 'ged' => $tree->name(), 127 ]); 128 } else { 129 $config_url = ''; 130 } 131 132 return view('modules/block-template', [ 133 'block' => str_replace('_', '-', $this->name()), 134 'id' => $block_id, 135 'config_url' => $config_url, 136 'title' => $title, 137 'content' => $content, 138 ]); 139 } 140 141 return $content; 142 } 143 144 /** {@inheritdoc} */ 145 public function loadAjax(): bool 146 { 147 return false; 148 } 149 150 /** {@inheritdoc} */ 151 public function isUserBlock(): bool 152 { 153 return true; 154 } 155 156 /** {@inheritdoc} */ 157 public function isTreeBlock(): bool 158 { 159 return true; 160 } 161 162 /** 163 * Update the configuration for a block. 164 * 165 * @param Request $request 166 * @param int $block_id 167 * 168 * @return void 169 */ 170 public function saveBlockConfiguration(Request $request, int $block_id) 171 { 172 $this->setBlockSetting($block_id, 'num', $request->get('num', self::DEFAULT_NUMBER)); 173 $this->setBlockSetting($block_id, 'infoStyle', $request->get('infoStyle', self::DEFAULT_STYLE)); 174 } 175 176 /** 177 * An HTML form to edit block settings 178 * 179 * @param Tree $tree 180 * @param int $block_id 181 * 182 * @return void 183 */ 184 public function editBlockConfiguration(Tree $tree, int $block_id) 185 { 186 $num = $this->getBlockSetting($block_id, 'num', self::DEFAULT_NUMBER); 187 $infoStyle = $this->getBlockSetting($block_id, 'infoStyle', self::DEFAULT_STYLE); 188 189 $info_styles = [ 190 /* I18N: An option in a list-box */ 191 'list' => I18N::translate('list'), 192 /* I18N: An option in a list-box */ 193 'table' => I18N::translate('table'), 194 ]; 195 196 echo view('modules/top10_givnnames/config', [ 197 'infoStyle' => $infoStyle, 198 'info_styles' => $info_styles, 199 'num' => $num, 200 ]); 201 } 202} 203