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