1<?php 2namespace Fisharebest\Webtrees; 3 4/** 5 * webtrees: online genealogy 6 * Copyright (C) 2015 webtrees development team 7 * This program is free software: you can redistribute it and/or modify 8 * it under the terms of the GNU General Public License as published by 9 * the Free Software Foundation, either version 3 of the License, or 10 * (at your option) any later version. 11 * This program is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU General Public License for more details. 15 * You should have received a copy of the GNU General Public License 16 * along with this program. If not, see <http://www.gnu.org/licenses/>. 17 */ 18 19/** 20 * Class TopSurnamesModule 21 */ 22class TopSurnamesModule extends Module implements ModuleBlockInterface { 23 /** {@inheritdoc} */ 24 public function getTitle() { 25 return /* I18N: Name of a module. Top=Most common */ I18N::translate('Top surnames'); 26 } 27 28 /** {@inheritdoc} */ 29 public function getDescription() { 30 return /* I18N: Description of the “Top surnames” module */ I18N::translate('A list of the most popular surnames.'); 31 } 32 33 /** {@inheritdoc} */ 34 public function getBlock($block_id, $template = true, $cfg = null) { 35 global $WT_TREE, $ctype; 36 37 $COMMON_NAMES_REMOVE = $WT_TREE->getPreference('COMMON_NAMES_REMOVE'); 38 $COMMON_NAMES_THRESHOLD = $WT_TREE->getPreference('COMMON_NAMES_THRESHOLD'); 39 40 $num = get_block_setting($block_id, 'num', '10'); 41 $infoStyle = get_block_setting($block_id, 'infoStyle', 'table'); 42 $block = get_block_setting($block_id, 'block', '0'); 43 44 if ($cfg) { 45 foreach (array('num', 'infoStyle', 'block') as $name) { 46 if (array_key_exists($name, $cfg)) { 47 $$name = $cfg[$name]; 48 } 49 } 50 } 51 52 // This next function is a bit out of date, and doesn't cope well with surname variants 53 $top_surnames = get_top_surnames(WT_GED_ID, $COMMON_NAMES_THRESHOLD, $num); 54 55 // Remove names found in the "Remove Names" list 56 if ($COMMON_NAMES_REMOVE) { 57 foreach (preg_split("/[,; ]+/", $COMMON_NAMES_REMOVE) as $delname) { 58 unset($top_surnames[$delname]); 59 unset($top_surnames[I18N::strtoupper($delname)]); 60 } 61 } 62 63 $all_surnames = array(); 64 $i = 0; 65 foreach (array_keys($top_surnames) as $top_surname) { 66 $all_surnames = array_merge($all_surnames, WT_Query_Name::surnames($top_surname, '', false, false, WT_GED_ID)); 67 if (++$i == $num) { 68 break; 69 } 70 } 71 if ($i < $num) { 72 $num = $i; 73 } 74 $id = $this->getName() . $block_id; 75 $class = $this->getName() . '_block'; 76 if ($ctype === 'gedcom' && WT_USER_GEDCOM_ADMIN || $ctype === 'user' && Auth::check()) { 77 $title = '<i class="icon-admin" title="' . I18N::translate('Configure') . '" onclick="modalDialog(\'block_edit.php?block_id=' . $block_id . '\', \'' . $this->getTitle() . '\');"></i>'; 78 } else { 79 $title = ''; 80 } 81 82 if ($num == 1) { 83 // I18N: i.e. most popular surname. 84 $title .= I18N::translate('Top surname'); 85 } else { 86 // I18N: Title for a list of the most common surnames, %s is a number. Note that a separate translation exists when %s is 1 87 $title .= I18N::plural('Top %s surname', 'Top %s surnames', $num, I18N::number($num)); 88 } 89 90 switch ($infoStyle) { 91 case 'tagcloud': 92 uksort($all_surnames, __NAMESPACE__ . '\I18N::strcasecmp'); 93 $content = format_surname_tagcloud($all_surnames, 'indilist.php', true); 94 break; 95 case 'list': 96 uasort($all_surnames, __NAMESPACE__ . '\\TopSurnamesModule::surnameCountSort'); 97 $content = format_surname_list($all_surnames, '1', true, 'indilist.php', $WT_TREE); 98 break; 99 case 'array': 100 uasort($all_surnames, __NAMESPACE__ . '\\TopSurnamesModule::surnameCountSort'); 101 $content = format_surname_list($all_surnames, '2', true, 'indilist.php', $WT_TREE); 102 break; 103 case 'table': 104 default: 105 uasort($all_surnames, __NAMESPACE__ . '\\TopSurnamesModule::surnameCountSort'); 106 $content = format_surname_table($all_surnames, 'indilist.php'); 107 break; 108 } 109 110 if ($template) { 111 if ($block) { 112 $class .= ' small_inner_block'; 113 } 114 return Theme::theme()->formatBlock($id, $title, $class, $content); 115 } else { 116 117 return $content; 118 } 119 } 120 121 /** {@inheritdoc} */ 122 public function loadAjax() { 123 return true; 124 } 125 126 /** {@inheritdoc} */ 127 public function isUserBlock() { 128 return true; 129 } 130 131 /** {@inheritdoc} */ 132 public function isGedcomBlock() { 133 return true; 134 } 135 136 /** {@inheritdoc} */ 137 public function configureBlock($block_id) { 138 if (Filter::postBool('save') && Filter::checkCsrf()) { 139 set_block_setting($block_id, 'num', Filter::postInteger('num', 1, 10000, 10)); 140 set_block_setting($block_id, 'infoStyle', Filter::post('infoStyle', 'list|array|table|tagcloud', 'table')); 141 set_block_setting($block_id, 'block', Filter::postBool('block')); 142 } 143 144 $num = get_block_setting($block_id, 'num', '10'); 145 $infoStyle = get_block_setting($block_id, 'infoStyle', 'table'); 146 $block = get_block_setting($block_id, 'block', '0'); 147 148 echo '<tr><td class="descriptionbox wrap width33">'; 149 echo I18N::translate('Number of items to show'); 150 echo '</td><td class="optionbox">'; 151 echo '<input type="text" name="num" size="2" value="', $num, '">'; 152 echo '</td></tr>'; 153 154 echo '<tr><td class="descriptionbox wrap width33">'; 155 echo I18N::translate('Presentation style'); 156 echo '</td><td class="optionbox">'; 157 echo select_edit_control('infoStyle', array('list' => I18N::translate('bullet list'), 'array' => I18N::translate('compact list'), 'table' => I18N::translate('table'), 'tagcloud' => I18N::translate('tag cloud')), null, $infoStyle, ''); 158 echo '</td></tr>'; 159 160 echo '<tr><td class="descriptionbox wrap width33">'; 161 echo /* I18N: label for a yes/no option */ I18N::translate('Add a scrollbar when block contents grow'); 162 echo '</td><td class="optionbox">'; 163 echo edit_field_yes_no('block', $block); 164 echo '</td></tr>'; 165 } 166 167 /** 168 * Sort (lists of counts of similar) surname by total count. 169 * 170 * @param string[] $a 171 * @param string[] $b 172 * 173 * @return integer 174 */ 175 private static function surnameCountSort($a, $b) { 176 $counta = 0; 177 foreach ($a as $x) { 178 $counta += count($x); 179 } 180 $countb = 0; 181 foreach ($b as $x) { 182 $countb += count($x); 183 } 184 185 return $countb - $counta; 186 } 187} 188