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