18c2e8227SGreg Roach<?php 28c2e8227SGreg Roachnamespace Fisharebest\Webtrees; 38c2e8227SGreg Roach 48c2e8227SGreg Roach/** 58c2e8227SGreg Roach * webtrees: online genealogy 68c2e8227SGreg Roach * Copyright (C) 2015 webtrees development team 78c2e8227SGreg Roach * This program is free software: you can redistribute it and/or modify 88c2e8227SGreg Roach * it under the terms of the GNU General Public License as published by 98c2e8227SGreg Roach * the Free Software Foundation, either version 3 of the License, or 108c2e8227SGreg Roach * (at your option) any later version. 118c2e8227SGreg Roach * This program is distributed in the hope that it will be useful, 128c2e8227SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of 138c2e8227SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 148c2e8227SGreg Roach * GNU General Public License for more details. 158c2e8227SGreg Roach * You should have received a copy of the GNU General Public License 168c2e8227SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>. 178c2e8227SGreg Roach */ 188c2e8227SGreg Roach 198c2e8227SGreg Roach/** 208c2e8227SGreg Roach * Class TopSurnamesModule 218c2e8227SGreg Roach */ 228c2e8227SGreg Roachclass TopSurnamesModule extends Module implements ModuleBlockInterface { 238c2e8227SGreg Roach /** {@inheritdoc} */ 248c2e8227SGreg Roach public function getTitle() { 258c2e8227SGreg Roach return /* I18N: Name of a module. Top=Most common */ I18N::translate('Top surnames'); 268c2e8227SGreg Roach } 278c2e8227SGreg Roach 288c2e8227SGreg Roach /** {@inheritdoc} */ 298c2e8227SGreg Roach public function getDescription() { 308c2e8227SGreg Roach return /* I18N: Description of the “Top surnames” module */ I18N::translate('A list of the most popular surnames.'); 318c2e8227SGreg Roach } 328c2e8227SGreg Roach 338c2e8227SGreg Roach /** {@inheritdoc} */ 348c2e8227SGreg Roach public function getBlock($block_id, $template = true, $cfg = null) { 358c2e8227SGreg Roach global $WT_TREE, $ctype; 368c2e8227SGreg Roach 378c2e8227SGreg Roach $COMMON_NAMES_REMOVE = $WT_TREE->getPreference('COMMON_NAMES_REMOVE'); 388c2e8227SGreg Roach $COMMON_NAMES_THRESHOLD = $WT_TREE->getPreference('COMMON_NAMES_THRESHOLD'); 398c2e8227SGreg Roach 408c2e8227SGreg Roach $num = get_block_setting($block_id, 'num', '10'); 418c2e8227SGreg Roach $infoStyle = get_block_setting($block_id, 'infoStyle', 'table'); 428c2e8227SGreg Roach $block = get_block_setting($block_id, 'block', '0'); 438c2e8227SGreg Roach 448c2e8227SGreg Roach if ($cfg) { 458c2e8227SGreg Roach foreach (array('num', 'infoStyle', 'block') as $name) { 468c2e8227SGreg Roach if (array_key_exists($name, $cfg)) { 478c2e8227SGreg Roach $$name = $cfg[$name]; 488c2e8227SGreg Roach } 498c2e8227SGreg Roach } 508c2e8227SGreg Roach } 518c2e8227SGreg Roach 528c2e8227SGreg Roach // This next function is a bit out of date, and doesn't cope well with surname variants 53*24ec66ceSGreg Roach $top_surnames = get_top_surnames($WT_TREE->getTreeId(), $COMMON_NAMES_THRESHOLD, $num); 548c2e8227SGreg Roach 558c2e8227SGreg Roach // Remove names found in the "Remove Names" list 568c2e8227SGreg Roach if ($COMMON_NAMES_REMOVE) { 578c2e8227SGreg Roach foreach (preg_split("/[,; ]+/", $COMMON_NAMES_REMOVE) as $delname) { 588c2e8227SGreg Roach unset($top_surnames[$delname]); 598c2e8227SGreg Roach unset($top_surnames[I18N::strtoupper($delname)]); 608c2e8227SGreg Roach } 618c2e8227SGreg Roach } 628c2e8227SGreg Roach 638c2e8227SGreg Roach $all_surnames = array(); 648c2e8227SGreg Roach $i = 0; 658c2e8227SGreg Roach foreach (array_keys($top_surnames) as $top_surname) { 66*24ec66ceSGreg Roach $all_surnames = array_merge($all_surnames, QueryName::surnames($top_surname, '', false, false, $WT_TREE->getTreeId())); 678c2e8227SGreg Roach if (++$i == $num) { 688c2e8227SGreg Roach break; 698c2e8227SGreg Roach } 708c2e8227SGreg Roach } 718c2e8227SGreg Roach if ($i < $num) { 728c2e8227SGreg Roach $num = $i; 738c2e8227SGreg Roach } 748c2e8227SGreg Roach $id = $this->getName() . $block_id; 758c2e8227SGreg Roach $class = $this->getName() . '_block'; 764b9ff166SGreg Roach if ($ctype === 'gedcom' && Auth::isManager($WT_TREE) || $ctype === 'user' && Auth::check()) { 778c2e8227SGreg Roach $title = '<i class="icon-admin" title="' . I18N::translate('Configure') . '" onclick="modalDialog(\'block_edit.php?block_id=' . $block_id . '\', \'' . $this->getTitle() . '\');"></i>'; 788c2e8227SGreg Roach } else { 798c2e8227SGreg Roach $title = ''; 808c2e8227SGreg Roach } 818c2e8227SGreg Roach 828c2e8227SGreg Roach if ($num == 1) { 838c2e8227SGreg Roach // I18N: i.e. most popular surname. 848c2e8227SGreg Roach $title .= I18N::translate('Top surname'); 858c2e8227SGreg Roach } else { 868c2e8227SGreg Roach // I18N: Title for a list of the most common surnames, %s is a number. Note that a separate translation exists when %s is 1 878c2e8227SGreg Roach $title .= I18N::plural('Top %s surname', 'Top %s surnames', $num, I18N::number($num)); 888c2e8227SGreg Roach } 898c2e8227SGreg Roach 908c2e8227SGreg Roach switch ($infoStyle) { 918c2e8227SGreg Roach case 'tagcloud': 928c2e8227SGreg Roach uksort($all_surnames, __NAMESPACE__ . '\I18N::strcasecmp'); 938c2e8227SGreg Roach $content = format_surname_tagcloud($all_surnames, 'indilist.php', true); 948c2e8227SGreg Roach break; 958c2e8227SGreg Roach case 'list': 968c2e8227SGreg Roach uasort($all_surnames, __NAMESPACE__ . '\\TopSurnamesModule::surnameCountSort'); 978c2e8227SGreg Roach $content = format_surname_list($all_surnames, '1', true, 'indilist.php', $WT_TREE); 988c2e8227SGreg Roach break; 998c2e8227SGreg Roach case 'array': 1008c2e8227SGreg Roach uasort($all_surnames, __NAMESPACE__ . '\\TopSurnamesModule::surnameCountSort'); 1018c2e8227SGreg Roach $content = format_surname_list($all_surnames, '2', true, 'indilist.php', $WT_TREE); 1028c2e8227SGreg Roach break; 1038c2e8227SGreg Roach case 'table': 1048c2e8227SGreg Roach default: 1058c2e8227SGreg Roach uasort($all_surnames, __NAMESPACE__ . '\\TopSurnamesModule::surnameCountSort'); 1068c2e8227SGreg Roach $content = format_surname_table($all_surnames, 'indilist.php'); 1078c2e8227SGreg Roach break; 1088c2e8227SGreg Roach } 1098c2e8227SGreg Roach 1108c2e8227SGreg Roach if ($template) { 1118c2e8227SGreg Roach if ($block) { 1128c2e8227SGreg Roach $class .= ' small_inner_block'; 1138c2e8227SGreg Roach } 1148c2e8227SGreg Roach return Theme::theme()->formatBlock($id, $title, $class, $content); 1158c2e8227SGreg Roach } else { 1168c2e8227SGreg Roach 1178c2e8227SGreg Roach return $content; 1188c2e8227SGreg Roach } 1198c2e8227SGreg Roach } 1208c2e8227SGreg Roach 1218c2e8227SGreg Roach /** {@inheritdoc} */ 1228c2e8227SGreg Roach public function loadAjax() { 1238c2e8227SGreg Roach return true; 1248c2e8227SGreg Roach } 1258c2e8227SGreg Roach 1268c2e8227SGreg Roach /** {@inheritdoc} */ 1278c2e8227SGreg Roach public function isUserBlock() { 1288c2e8227SGreg Roach return true; 1298c2e8227SGreg Roach } 1308c2e8227SGreg Roach 1318c2e8227SGreg Roach /** {@inheritdoc} */ 1328c2e8227SGreg Roach public function isGedcomBlock() { 1338c2e8227SGreg Roach return true; 1348c2e8227SGreg Roach } 1358c2e8227SGreg Roach 1368c2e8227SGreg Roach /** {@inheritdoc} */ 1378c2e8227SGreg Roach public function configureBlock($block_id) { 1388c2e8227SGreg Roach if (Filter::postBool('save') && Filter::checkCsrf()) { 1398c2e8227SGreg Roach set_block_setting($block_id, 'num', Filter::postInteger('num', 1, 10000, 10)); 1408c2e8227SGreg Roach set_block_setting($block_id, 'infoStyle', Filter::post('infoStyle', 'list|array|table|tagcloud', 'table')); 1418c2e8227SGreg Roach set_block_setting($block_id, 'block', Filter::postBool('block')); 1428c2e8227SGreg Roach } 1438c2e8227SGreg Roach 1448c2e8227SGreg Roach $num = get_block_setting($block_id, 'num', '10'); 1458c2e8227SGreg Roach $infoStyle = get_block_setting($block_id, 'infoStyle', 'table'); 1468c2e8227SGreg Roach $block = get_block_setting($block_id, 'block', '0'); 1478c2e8227SGreg Roach 1488c2e8227SGreg Roach echo '<tr><td class="descriptionbox wrap width33">'; 1498c2e8227SGreg Roach echo I18N::translate('Number of items to show'); 1508c2e8227SGreg Roach echo '</td><td class="optionbox">'; 1518c2e8227SGreg Roach echo '<input type="text" name="num" size="2" value="', $num, '">'; 1528c2e8227SGreg Roach echo '</td></tr>'; 1538c2e8227SGreg Roach 1548c2e8227SGreg Roach echo '<tr><td class="descriptionbox wrap width33">'; 1558c2e8227SGreg Roach echo I18N::translate('Presentation style'); 1568c2e8227SGreg Roach echo '</td><td class="optionbox">'; 1578c2e8227SGreg Roach 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, ''); 1588c2e8227SGreg Roach echo '</td></tr>'; 1598c2e8227SGreg Roach 1608c2e8227SGreg Roach echo '<tr><td class="descriptionbox wrap width33">'; 1618c2e8227SGreg Roach echo /* I18N: label for a yes/no option */ I18N::translate('Add a scrollbar when block contents grow'); 1628c2e8227SGreg Roach echo '</td><td class="optionbox">'; 1638c2e8227SGreg Roach echo edit_field_yes_no('block', $block); 1648c2e8227SGreg Roach echo '</td></tr>'; 1658c2e8227SGreg Roach } 1668c2e8227SGreg Roach 1678c2e8227SGreg Roach /** 1688c2e8227SGreg Roach * Sort (lists of counts of similar) surname by total count. 1698c2e8227SGreg Roach * 1708c2e8227SGreg Roach * @param string[] $a 1718c2e8227SGreg Roach * @param string[] $b 1728c2e8227SGreg Roach * 1738c2e8227SGreg Roach * @return integer 1748c2e8227SGreg Roach */ 1758c2e8227SGreg Roach private static function surnameCountSort($a, $b) { 1768c2e8227SGreg Roach $counta = 0; 1778c2e8227SGreg Roach foreach ($a as $x) { 1788c2e8227SGreg Roach $counta += count($x); 1798c2e8227SGreg Roach } 1808c2e8227SGreg Roach $countb = 0; 1818c2e8227SGreg Roach foreach ($b as $x) { 1828c2e8227SGreg Roach $countb += count($x); 1838c2e8227SGreg Roach } 1848c2e8227SGreg Roach 1858c2e8227SGreg Roach return $countb - $counta; 1868c2e8227SGreg Roach } 1878c2e8227SGreg Roach} 188