1<?php 2 3/** 4 * webtrees: online genealogy 5 * Copyright (C) 2019 webtrees development team 6 * This program is free software: you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation, either version 3 of the License, or 9 * (at your option) any later version. 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * You should have received a copy of the GNU General Public License 15 * along with this program. If not, see <http://www.gnu.org/licenses/>. 16 */ 17declare(strict_types=1); 18 19namespace Fisharebest\Webtrees\Module; 20 21use Fisharebest\Webtrees\I18N; 22use Fisharebest\Webtrees\Statistics; 23use Fisharebest\Webtrees\Tree; 24use Illuminate\Support\Str; 25use Psr\Http\Message\ServerRequestInterface; 26 27/** 28 * Class TopGivenNamesModule 29 */ 30class TopGivenNamesModule extends AbstractModule implements ModuleBlockInterface 31{ 32 use ModuleBlockTrait; 33 34 // Default values for new blocks. 35 private const DEFAULT_NUMBER = '10'; 36 private const DEFAULT_STYLE = 'table'; 37 38 /** 39 * How should this module be identified in the control panel, etc.? 40 * 41 * @return string 42 */ 43 public function title(): string 44 { 45 /* I18N: Name of a module. Top=Most common */ 46 return I18N::translate('Top given names'); 47 } 48 49 /** 50 * A sentence describing what this module does. 51 * 52 * @return string 53 */ 54 public function description(): string 55 { 56 /* I18N: Description of the “Top given names” module */ 57 return I18N::translate('A list of the most popular given names.'); 58 } 59 60 /** 61 * Generate the HTML content of this block. 62 * 63 * @param Tree $tree 64 * @param int $block_id 65 * @param string $context 66 * @param string[] $config 67 * 68 * @return string 69 */ 70 public function getBlock(Tree $tree, int $block_id, string $context, array $config = []): string 71 { 72 $statistics = app(Statistics::class); 73 74 $num = $this->getBlockSetting($block_id, 'num', self::DEFAULT_NUMBER); 75 $infoStyle = $this->getBlockSetting($block_id, 'infoStyle', self::DEFAULT_STYLE); 76 77 extract($config, EXTR_OVERWRITE); 78 79 switch ($infoStyle) { 80 case 'list': 81 $content = view('modules/top10_givnnames/block', [ 82 'males' => $statistics->commonGivenMaleListTotals('1', $num), 83 'females' => $statistics->commonGivenFemaleListTotals('1', $num), 84 ]); 85 break; 86 default: 87 case 'table': 88 $content = view('modules/top10_givnnames/block', [ 89 'males' => $statistics->commonGivenMaleTable('1', $num), 90 'females' => $statistics->commonGivenFemaleTable('1', $num), 91 ]); 92 break; 93 } 94 95 if ($context !== self::CONTEXT_EMBED) { 96 $num = (int) $num; 97 98 if ($num === 1) { 99 // I18N: i.e. most popular given name. 100 $title = I18N::translate('Top given name'); 101 } else { 102 // 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 103 $title = I18N::plural('Top %s given name', 'Top %s given names', $num, I18N::number($num)); 104 } 105 106 return view('modules/block-template', [ 107 'block' => Str::kebab($this->name()), 108 'id' => $block_id, 109 'config_url' => $this->configUrl($tree, $context, $block_id), 110 'title' => $title, 111 'content' => $content, 112 ]); 113 } 114 115 return $content; 116 } 117 118 /** 119 * Should this block load asynchronously using AJAX? 120 * 121 * Simple blocks are faster in-line, more complex ones can be loaded later. 122 * 123 * @return bool 124 */ 125 public function loadAjax(): bool 126 { 127 return false; 128 } 129 130 /** 131 * Can this block be shown on the user’s home page? 132 * 133 * @return bool 134 */ 135 public function isUserBlock(): bool 136 { 137 return true; 138 } 139 140 /** 141 * Can this block be shown on the tree’s home page? 142 * 143 * @return bool 144 */ 145 public function isTreeBlock(): bool 146 { 147 return true; 148 } 149 150 /** 151 * Update the configuration for a block. 152 * 153 * @param ServerRequestInterface $request 154 * @param int $block_id 155 * 156 * @return void 157 */ 158 public function saveBlockConfiguration(ServerRequestInterface $request, int $block_id): void 159 { 160 $params = $request->getParsedBody(); 161 162 $this->setBlockSetting($block_id, 'num', $params['num']); 163 $this->setBlockSetting($block_id, 'infoStyle', $params['infoStyle']); 164 } 165 166 /** 167 * An HTML form to edit block settings 168 * 169 * @param Tree $tree 170 * @param int $block_id 171 * 172 * @return string 173 */ 174 public function editBlockConfiguration(Tree $tree, int $block_id): string 175 { 176 $num = $this->getBlockSetting($block_id, 'num', self::DEFAULT_NUMBER); 177 $infoStyle = $this->getBlockSetting($block_id, 'infoStyle', self::DEFAULT_STYLE); 178 179 $info_styles = [ 180 /* I18N: An option in a list-box */ 181 'list' => I18N::translate('list'), 182 /* I18N: An option in a list-box */ 183 'table' => I18N::translate('table'), 184 ]; 185 186 return view('modules/top10_givnnames/config', [ 187 'infoStyle' => $infoStyle, 188 'info_styles' => $info_styles, 189 'num' => $num, 190 ]); 191 } 192} 193