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