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