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