1<?php 2/** 3 * webtrees: online genealogy 4 * Copyright (C) 2016 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\FunctionsEdit; 22use Fisharebest\Webtrees\Functions\FunctionsPrintLists; 23use Fisharebest\Webtrees\I18N; 24use Fisharebest\Webtrees\Query\QueryName; 25use Fisharebest\Webtrees\Theme; 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 = array()) { 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 (array('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 = array(); 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 $id = $this->getName() . $block_id; 85 $class = $this->getName() . '_block'; 86 if ($ctype === 'gedcom' && Auth::isManager($WT_TREE) || $ctype === 'user' && Auth::check()) { 87 $title = '<a class="icon-admin" title="' . I18N::translate('Configure') . '" href="block_edit.php?block_id=' . $block_id . '&ged=' . $WT_TREE->getNameHtml() . '&ctype=' . $ctype . '"></a>'; 88 } else { 89 $title = ''; 90 } 91 92 if ($num == 1) { 93 // I18N: i.e. most popular surname. 94 $title .= I18N::translate('Top surname'); 95 } else { 96 // I18N: Title for a list of the most common surnames, %s is a number. Note that a separate translation exists when %s is 1 97 $title .= I18N::plural('Top %s surname', 'Top %s surnames', $num, I18N::number($num)); 98 } 99 100 switch ($infoStyle) { 101 case 'tagcloud': 102 uksort($all_surnames, '\Fisharebest\Webtrees\I18N::strcasecmp'); 103 $content = FunctionsPrintLists::surnameTagCloud($all_surnames, 'indilist.php', true, $WT_TREE); 104 break; 105 case 'list': 106 uasort($all_surnames, '\Fisharebest\Webtrees\Module\TopSurnamesModule::surnameCountSort'); 107 $content = FunctionsPrintLists::surnameList($all_surnames, 1, true, 'indilist.php', $WT_TREE); 108 break; 109 case 'array': 110 uasort($all_surnames, '\Fisharebest\Webtrees\Module\TopSurnamesModule::surnameCountSort'); 111 $content = FunctionsPrintLists::surnameList($all_surnames, 2, true, 'indilist.php', $WT_TREE); 112 break; 113 case 'table': 114 default: 115 uasort($all_surnames, '\Fisharebest\Webtrees\Module\TopSurnamesModule::surnameCountSort'); 116 $content = FunctionsPrintLists::surnameTable($all_surnames, 'indilist.php', $WT_TREE); 117 break; 118 } 119 120 if ($template) { 121 return Theme::theme()->formatBlock($id, $title, $class, $content); 122 } else { 123 return $content; 124 } 125 } 126 127 /** {@inheritdoc} */ 128 public function loadAjax() { 129 return true; 130 } 131 132 /** {@inheritdoc} */ 133 public function isUserBlock() { 134 return true; 135 } 136 137 /** {@inheritdoc} */ 138 public function isGedcomBlock() { 139 return true; 140 } 141 142 /** 143 * An HTML form to edit block settings 144 * 145 * @param int $block_id 146 */ 147 public function configureBlock($block_id) { 148 if (Filter::postBool('save') && Filter::checkCsrf()) { 149 $this->setBlockSetting($block_id, 'num', Filter::postInteger('num', 1, 10000, 10)); 150 $this->setBlockSetting($block_id, 'infoStyle', Filter::post('infoStyle', 'list|array|table|tagcloud', 'table')); 151 } 152 153 $num = $this->getBlockSetting($block_id, 'num', '10'); 154 $infoStyle = $this->getBlockSetting($block_id, 'infoStyle', 'table'); 155 156 echo '<tr><td class="descriptionbox wrap width33">'; 157 echo /* I18N: ... to show in a list */ I18N::translate('Number of surnames'); 158 echo '</td><td class="optionbox">'; 159 echo '<input type="text" name="num" size="2" value="', $num, '">'; 160 echo '</td></tr>'; 161 162 echo '<tr><td class="descriptionbox wrap width33">'; 163 echo I18N::translate('Presentation style'); 164 echo '</td><td class="optionbox">'; 165 echo FunctionsEdit::selectEditControl('infoStyle', array('list' => I18N::translate('bullet list'), 'array' => I18N::translate('compact list'), 'table' => I18N::translate('table'), 'tagcloud' => I18N::translate('tag cloud')), null, $infoStyle, ''); 166 echo '</td></tr>'; 167 } 168 169 /** 170 * Sort (lists of counts of similar) surname by total count. 171 * 172 * @param string[] $a 173 * @param string[] $b 174 * 175 * @return int 176 */ 177 private static function surnameCountSort($a, $b) { 178 $counta = 0; 179 foreach ($a as $x) { 180 $counta += count($x); 181 } 182 $countb = 0; 183 foreach ($b as $x) { 184 $countb += count($x); 185 } 186 187 return $countb - $counta; 188 } 189} 190