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\Bootstrap4; 20use Fisharebest\Webtrees\Filter; 21use Fisharebest\Webtrees\Functions\FunctionsDb; 22use Fisharebest\Webtrees\Functions\FunctionsPrintLists; 23use Fisharebest\Webtrees\Html; 24use Fisharebest\Webtrees\I18N; 25use Fisharebest\Webtrees\Query\QueryName; 26 27/** 28 * Class TopSurnamesModule 29 */ 30class TopSurnamesModule extends AbstractModule implements ModuleBlockInterface { 31 /** 32 * How should this module be labelled on tabs, menus, etc.? 33 * 34 * @return string 35 */ 36 public function getTitle() { 37 return /* I18N: Name of a module. Top=Most common */ I18N::translate('Top surnames'); 38 } 39 40 /** 41 * A sentence describing what this module does. 42 * 43 * @return string 44 */ 45 public function getDescription() { 46 return /* I18N: Description of the “Top surnames” module */ I18N::translate('A list of the most popular surnames.'); 47 } 48 49 /** 50 * Generate the HTML content of this block. 51 * 52 * @param int $block_id 53 * @param bool $template 54 * @param string[] $cfg 55 * 56 * @return string 57 */ 58 public function getBlock($block_id, $template = true, $cfg = []): string { 59 global $WT_TREE, $ctype; 60 61 $num = $this->getBlockSetting($block_id, 'num', '10'); 62 $infoStyle = $this->getBlockSetting($block_id, 'infoStyle', 'table'); 63 64 foreach (['num', 'infoStyle'] as $name) { 65 if (array_key_exists($name, $cfg)) { 66 $$name = $cfg[$name]; 67 } 68 } 69 70 // This next function is a bit out of date, and doesn't cope well with surname variants 71 $top_surnames = FunctionsDb::getTopSurnames($WT_TREE->getTreeId(), 0, $num); 72 73 $all_surnames = []; 74 $i = 0; 75 foreach (array_keys($top_surnames) as $top_surname) { 76 $all_surnames = array_merge($all_surnames, QueryName::surnames($WT_TREE, $top_surname, '', false, false)); 77 if (++$i == $num) { 78 break; 79 } 80 } 81 if ($i < $num) { 82 $num = $i; 83 } 84 85 switch ($infoStyle) { 86 case 'tagcloud': 87 uksort($all_surnames, '\Fisharebest\Webtrees\I18N::strcasecmp'); 88 $content = FunctionsPrintLists::surnameTagCloud($all_surnames, 'indilist.php', true, $WT_TREE); 89 break; 90 case 'list': 91 uasort($all_surnames, '\Fisharebest\Webtrees\Module\TopSurnamesModule::surnameCountSort'); 92 $content = FunctionsPrintLists::surnameList($all_surnames, 1, true, 'indilist.php', $WT_TREE); 93 break; 94 case 'array': 95 uasort($all_surnames, '\Fisharebest\Webtrees\Module\TopSurnamesModule::surnameCountSort'); 96 $content = FunctionsPrintLists::surnameList($all_surnames, 2, true, 'indilist.php', $WT_TREE); 97 break; 98 case 'table': 99 default: 100 uasort($all_surnames, '\Fisharebest\Webtrees\Module\TopSurnamesModule::surnameCountSort'); 101 $content = FunctionsPrintLists::surnameTable($all_surnames, 'indilist.php', $WT_TREE); 102 break; 103 } 104 105 if ($template) { 106 if ($num == 1) { 107 // I18N: i.e. most popular surname. 108 $title = I18N::translate('Top surname'); 109 } else { 110 // I18N: Title for a list of the most common surnames, %s is a number. Note that a separate translation exists when %s is 1 111 $title = I18N::plural('Top %s surname', 'Top %s surnames', $num, I18N::number($num)); 112 } 113 114 if ($ctype === 'gedcom' && Auth::isManager($WT_TREE) || $ctype === 'user' && Auth::check()) { 115 $config_url = Html::url('block_edit.php', ['block_id' => $block_id, 'ged' => $WT_TREE->getName()]); 116 } else { 117 $config_url = ''; 118 } 119 120 return view('blocks/template', [ 121 'block' => str_replace('_', '-', $this->getName()), 122 'id' => $block_id, 123 'config_url' => $config_url, 124 'title' => $title, 125 'content' => $content, 126 ]); 127 } else { 128 return $content; 129 } 130 } 131 132 /** {@inheritdoc} */ 133 public function loadAjax(): bool { 134 return false; 135 } 136 137 /** {@inheritdoc} */ 138 public function isUserBlock(): bool { 139 return true; 140 } 141 142 /** {@inheritdoc} */ 143 public function isGedcomBlock(): bool { 144 return true; 145 } 146 147 /** 148 * An HTML form to edit block settings 149 * 150 * @param int $block_id 151 * 152 * @return void 153 */ 154 public function configureBlock($block_id) { 155 if (Filter::postBool('save') && Filter::checkCsrf()) { 156 $this->setBlockSetting($block_id, 'num', Filter::postInteger('num', 1, 10000, 10)); 157 $this->setBlockSetting($block_id, 'infoStyle', Filter::post('infoStyle', 'list|array|table|tagcloud', 'table')); 158 } 159 160 $num = $this->getBlockSetting($block_id, 'num', '10'); 161 $infoStyle = $this->getBlockSetting($block_id, 'infoStyle', 'table'); 162 163 ?> 164 <div class="form-group row"> 165 <label class="col-sm-3 col-form-label" for="num"> 166 <?= /* I18N: ... to show in a list */ I18N::translate('Number of surnames') ?> 167 </label> 168 <div class="col-sm-9"> 169 <input type="text" name="num" size="2" value="<?= $num ?>"> 170 </div> 171 </div> 172 173 <div class="form-group row"> 174 <label class="col-sm-3 col-form-label" for="infoStyle"> 175 <?= I18N::translate('Presentation style') ?> 176 </label> 177 <div class="col-sm-9"> 178 <?= Bootstrap4::select(['list' => I18N::translate('bullet list'), 'array' => I18N::translate('compact list'), 'table' => I18N::translate('table'), 'tagcloud' => I18N::translate('tag cloud')], $infoStyle, ['id' => 'infoStyle', 'name' => 'infoStyle']) ?> 179 </div> 180 </div> 181 <?php 182 } 183 184 /** 185 * Sort (lists of counts of similar) surname by total count. 186 * 187 * @param string[][] $a 188 * @param string[][] $b 189 * 190 * @return int 191 */ 192 private static function surnameCountSort($a, $b) { 193 $counta = 0; 194 foreach ($a as $x) { 195 $counta += count($x); 196 } 197 $countb = 0; 198 foreach ($b as $x) { 199 $countb += count($x); 200 } 201 202 return $countb - $counta; 203 } 204} 205