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