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)) { 115 $config_url = route('tree-page-block-edit', ['block_id' => $block_id, 'ged' => $WT_TREE->getName()]); 116 } elseif ($ctype === 'user' && Auth::check()) { 117 $config_url = route('user-page-block-edit', ['block_id' => $block_id, 'ged' => $WT_TREE->getName()]); 118 } else { 119 $config_url = ''; 120 } 121 122 return view('blocks/template', [ 123 'block' => str_replace('_', '-', $this->getName()), 124 'id' => $block_id, 125 'config_url' => $config_url, 126 'title' => $title, 127 'content' => $content, 128 ]); 129 } else { 130 return $content; 131 } 132 } 133 134 /** {@inheritdoc} */ 135 public function loadAjax(): bool { 136 return false; 137 } 138 139 /** {@inheritdoc} */ 140 public function isUserBlock(): bool { 141 return true; 142 } 143 144 /** {@inheritdoc} */ 145 public function isGedcomBlock(): bool { 146 return true; 147 } 148 149 /** 150 * An HTML form to edit block settings 151 * 152 * @param int $block_id 153 * 154 * @return void 155 */ 156 public function configureBlock($block_id) { 157 if (Filter::postBool('save') && Filter::checkCsrf()) { 158 $this->setBlockSetting($block_id, 'num', Filter::postInteger('num', 1, 10000, 10)); 159 $this->setBlockSetting($block_id, 'infoStyle', Filter::post('infoStyle', 'list|array|table|tagcloud', 'table')); 160 } 161 162 $num = $this->getBlockSetting($block_id, 'num', '10'); 163 $infoStyle = $this->getBlockSetting($block_id, 'infoStyle', 'table'); 164 165 ?> 166 <div class="form-group row"> 167 <label class="col-sm-3 col-form-label" for="num"> 168 <?= /* I18N: ... to show in a list */ I18N::translate('Number of surnames') ?> 169 </label> 170 <div class="col-sm-9"> 171 <input type="text" name="num" size="2" value="<?= $num ?>"> 172 </div> 173 </div> 174 175 <div class="form-group row"> 176 <label class="col-sm-3 col-form-label" for="infoStyle"> 177 <?= I18N::translate('Presentation style') ?> 178 </label> 179 <div class="col-sm-9"> 180 <?= 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']) ?> 181 </div> 182 </div> 183 <?php 184 } 185 186 /** 187 * Sort (lists of counts of similar) surname by total count. 188 * 189 * @param string[][] $a 190 * @param string[][] $b 191 * 192 * @return int 193 */ 194 private static function surnameCountSort($a, $b) { 195 $counta = 0; 196 foreach ($a as $x) { 197 $counta += count($x); 198 } 199 $countb = 0; 200 foreach ($b as $x) { 201 $countb += count($x); 202 } 203 204 return $countb - $counta; 205 } 206} 207