18c2e8227SGreg Roach<?php 28c2e8227SGreg Roach/** 38c2e8227SGreg Roach * webtrees: online genealogy 4*369c0ce6SGreg Roach * Copyright (C) 2016 webtrees development team 58c2e8227SGreg Roach * This program is free software: you can redistribute it and/or modify 68c2e8227SGreg Roach * it under the terms of the GNU General Public License as published by 78c2e8227SGreg Roach * the Free Software Foundation, either version 3 of the License, or 88c2e8227SGreg Roach * (at your option) any later version. 98c2e8227SGreg Roach * This program is distributed in the hope that it will be useful, 108c2e8227SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of 118c2e8227SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 128c2e8227SGreg Roach * GNU General Public License for more details. 138c2e8227SGreg Roach * You should have received a copy of the GNU General Public License 148c2e8227SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>. 158c2e8227SGreg Roach */ 1676692c8bSGreg Roachnamespace Fisharebest\Webtrees\Module; 1776692c8bSGreg Roach 180e62c4b8SGreg Roachuse Fisharebest\Webtrees\Auth; 190e62c4b8SGreg Roachuse Fisharebest\Webtrees\Filter; 203d7a8a4cSGreg Roachuse Fisharebest\Webtrees\Functions\FunctionsDb; 213d7a8a4cSGreg Roachuse Fisharebest\Webtrees\Functions\FunctionsEdit; 223d7a8a4cSGreg Roachuse Fisharebest\Webtrees\Functions\FunctionsPrintLists; 230e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N; 240e62c4b8SGreg Roachuse Fisharebest\Webtrees\Query\QueryName; 250e62c4b8SGreg Roachuse Fisharebest\Webtrees\Theme; 268c2e8227SGreg Roach 278c2e8227SGreg Roach/** 288c2e8227SGreg Roach * Class TopSurnamesModule 298c2e8227SGreg Roach */ 30e2a378d3SGreg Roachclass TopSurnamesModule extends AbstractModule implements ModuleBlockInterface { 3176692c8bSGreg Roach /** 3276692c8bSGreg Roach * How should this module be labelled on tabs, menus, etc.? 3376692c8bSGreg Roach * 3476692c8bSGreg Roach * @return string 3576692c8bSGreg Roach */ 368c2e8227SGreg Roach public function getTitle() { 378c2e8227SGreg Roach return /* I18N: Name of a module. Top=Most common */ I18N::translate('Top surnames'); 388c2e8227SGreg Roach } 398c2e8227SGreg Roach 4076692c8bSGreg Roach /** 4176692c8bSGreg Roach * A sentence describing what this module does. 4276692c8bSGreg Roach * 4376692c8bSGreg Roach * @return string 4476692c8bSGreg Roach */ 458c2e8227SGreg Roach public function getDescription() { 468c2e8227SGreg Roach return /* I18N: Description of the “Top surnames” module */ I18N::translate('A list of the most popular surnames.'); 478c2e8227SGreg Roach } 488c2e8227SGreg Roach 4976692c8bSGreg Roach /** 5076692c8bSGreg Roach * Generate the HTML content of this block. 5176692c8bSGreg Roach * 5276692c8bSGreg Roach * @param int $block_id 5376692c8bSGreg Roach * @param bool $template 54727f238cSGreg Roach * @param string[] $cfg 5576692c8bSGreg Roach * 5676692c8bSGreg Roach * @return string 5776692c8bSGreg Roach */ 5873726173SGreg Roach public function getBlock($block_id, $template = true, $cfg = array()) { 598c2e8227SGreg Roach global $WT_TREE, $ctype; 608c2e8227SGreg Roach 618c2e8227SGreg Roach $COMMON_NAMES_REMOVE = $WT_TREE->getPreference('COMMON_NAMES_REMOVE'); 628c2e8227SGreg Roach $COMMON_NAMES_THRESHOLD = $WT_TREE->getPreference('COMMON_NAMES_THRESHOLD'); 638c2e8227SGreg Roach 64e2a378d3SGreg Roach $num = $this->getBlockSetting($block_id, 'num', '10'); 65e2a378d3SGreg Roach $infoStyle = $this->getBlockSetting($block_id, 'infoStyle', 'table'); 66e2a378d3SGreg Roach $block = $this->getBlockSetting($block_id, 'block', '0'); 678c2e8227SGreg Roach 688c2e8227SGreg Roach foreach (array('num', 'infoStyle', 'block') as $name) { 698c2e8227SGreg Roach if (array_key_exists($name, $cfg)) { 708c2e8227SGreg Roach $$name = $cfg[$name]; 718c2e8227SGreg Roach } 728c2e8227SGreg Roach } 738c2e8227SGreg Roach 748c2e8227SGreg Roach // This next function is a bit out of date, and doesn't cope well with surname variants 753d7a8a4cSGreg Roach $top_surnames = FunctionsDb::getTopSurnames($WT_TREE->getTreeId(), $COMMON_NAMES_THRESHOLD, $num); 768c2e8227SGreg Roach 778c2e8227SGreg Roach // Remove names found in the "Remove Names" list 788c2e8227SGreg Roach if ($COMMON_NAMES_REMOVE) { 798c2e8227SGreg Roach foreach (preg_split("/[,; ]+/", $COMMON_NAMES_REMOVE) as $delname) { 808c2e8227SGreg Roach unset($top_surnames[$delname]); 818c2e8227SGreg Roach unset($top_surnames[I18N::strtoupper($delname)]); 828c2e8227SGreg Roach } 838c2e8227SGreg Roach } 848c2e8227SGreg Roach 858c2e8227SGreg Roach $all_surnames = array(); 868c2e8227SGreg Roach $i = 0; 878c2e8227SGreg Roach foreach (array_keys($top_surnames) as $top_surname) { 88e06aff55SGreg Roach $all_surnames = array_merge($all_surnames, QueryName::surnames($WT_TREE, $top_surname, '', false, false)); 898c2e8227SGreg Roach if (++$i == $num) { 908c2e8227SGreg Roach break; 918c2e8227SGreg Roach } 928c2e8227SGreg Roach } 938c2e8227SGreg Roach if ($i < $num) { 948c2e8227SGreg Roach $num = $i; 958c2e8227SGreg Roach } 968c2e8227SGreg Roach $id = $this->getName() . $block_id; 978c2e8227SGreg Roach $class = $this->getName() . '_block'; 984b9ff166SGreg Roach if ($ctype === 'gedcom' && Auth::isManager($WT_TREE) || $ctype === 'user' && Auth::check()) { 999353052eSGreg Roach $title = '<a class="icon-admin" title="' . I18N::translate('Configure') . '" href="block_edit.php?block_id=' . $block_id . '&ged=' . $WT_TREE->getNameHtml() . '&ctype=' . $ctype . '"></a>'; 1008c2e8227SGreg Roach } else { 1018c2e8227SGreg Roach $title = ''; 1028c2e8227SGreg Roach } 1038c2e8227SGreg Roach 1048c2e8227SGreg Roach if ($num == 1) { 1058c2e8227SGreg Roach // I18N: i.e. most popular surname. 1068c2e8227SGreg Roach $title .= I18N::translate('Top surname'); 1078c2e8227SGreg Roach } else { 1088c2e8227SGreg 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 1098c2e8227SGreg Roach $title .= I18N::plural('Top %s surname', 'Top %s surnames', $num, I18N::number($num)); 1108c2e8227SGreg Roach } 1118c2e8227SGreg Roach 1128c2e8227SGreg Roach switch ($infoStyle) { 1138c2e8227SGreg Roach case 'tagcloud': 1140e62c4b8SGreg Roach uksort($all_surnames, '\Fisharebest\Webtrees\I18N::strcasecmp'); 1153d7a8a4cSGreg Roach $content = FunctionsPrintLists::surnameTagCloud($all_surnames, 'indilist.php', true, $WT_TREE); 1168c2e8227SGreg Roach break; 1178c2e8227SGreg Roach case 'list': 1180e62c4b8SGreg Roach uasort($all_surnames, '\Fisharebest\Webtrees\Module\TopSurnamesModule::surnameCountSort'); 1193d7a8a4cSGreg Roach $content = FunctionsPrintLists::surnameList($all_surnames, '1', true, 'indilist.php', $WT_TREE); 1208c2e8227SGreg Roach break; 1218c2e8227SGreg Roach case 'array': 1220e62c4b8SGreg Roach uasort($all_surnames, '\Fisharebest\Webtrees\Module\TopSurnamesModule::surnameCountSort'); 1233d7a8a4cSGreg Roach $content = FunctionsPrintLists::surnameList($all_surnames, '2', true, 'indilist.php', $WT_TREE); 1248c2e8227SGreg Roach break; 1258c2e8227SGreg Roach case 'table': 1268c2e8227SGreg Roach default: 1270e62c4b8SGreg Roach uasort($all_surnames, '\Fisharebest\Webtrees\Module\TopSurnamesModule::surnameCountSort'); 1283d7a8a4cSGreg Roach $content = FunctionsPrintLists::surnameTable($all_surnames, 'indilist.php', $WT_TREE); 1298c2e8227SGreg Roach break; 1308c2e8227SGreg Roach } 1318c2e8227SGreg Roach 1328c2e8227SGreg Roach if ($template) { 1338c2e8227SGreg Roach if ($block) { 1348c2e8227SGreg Roach $class .= ' small_inner_block'; 1358c2e8227SGreg Roach } 136cbc1590aSGreg Roach 1378c2e8227SGreg Roach return Theme::theme()->formatBlock($id, $title, $class, $content); 1388c2e8227SGreg Roach } else { 1398c2e8227SGreg Roach 1408c2e8227SGreg Roach return $content; 1418c2e8227SGreg Roach } 1428c2e8227SGreg Roach } 1438c2e8227SGreg Roach 1448c2e8227SGreg Roach /** {@inheritdoc} */ 1458c2e8227SGreg Roach public function loadAjax() { 1468c2e8227SGreg Roach return true; 1478c2e8227SGreg Roach } 1488c2e8227SGreg Roach 1498c2e8227SGreg Roach /** {@inheritdoc} */ 1508c2e8227SGreg Roach public function isUserBlock() { 1518c2e8227SGreg Roach return true; 1528c2e8227SGreg Roach } 1538c2e8227SGreg Roach 1548c2e8227SGreg Roach /** {@inheritdoc} */ 1558c2e8227SGreg Roach public function isGedcomBlock() { 1568c2e8227SGreg Roach return true; 1578c2e8227SGreg Roach } 1588c2e8227SGreg Roach 15976692c8bSGreg Roach /** 16076692c8bSGreg Roach * An HTML form to edit block settings 16176692c8bSGreg Roach * 16276692c8bSGreg Roach * @param int $block_id 16376692c8bSGreg Roach */ 1648c2e8227SGreg Roach public function configureBlock($block_id) { 1658c2e8227SGreg Roach if (Filter::postBool('save') && Filter::checkCsrf()) { 166e2a378d3SGreg Roach $this->setBlockSetting($block_id, 'num', Filter::postInteger('num', 1, 10000, 10)); 167e2a378d3SGreg Roach $this->setBlockSetting($block_id, 'infoStyle', Filter::post('infoStyle', 'list|array|table|tagcloud', 'table')); 168e2a378d3SGreg Roach $this->setBlockSetting($block_id, 'block', Filter::postBool('block')); 1698c2e8227SGreg Roach } 1708c2e8227SGreg Roach 171e2a378d3SGreg Roach $num = $this->getBlockSetting($block_id, 'num', '10'); 172e2a378d3SGreg Roach $infoStyle = $this->getBlockSetting($block_id, 'infoStyle', 'table'); 173e2a378d3SGreg Roach $block = $this->getBlockSetting($block_id, 'block', '0'); 1748c2e8227SGreg Roach 1758c2e8227SGreg Roach echo '<tr><td class="descriptionbox wrap width33">'; 1768c2e8227SGreg Roach echo I18N::translate('Number of items to show'); 1778c2e8227SGreg Roach echo '</td><td class="optionbox">'; 1788c2e8227SGreg Roach echo '<input type="text" name="num" size="2" value="', $num, '">'; 1798c2e8227SGreg Roach echo '</td></tr>'; 1808c2e8227SGreg Roach 1818c2e8227SGreg Roach echo '<tr><td class="descriptionbox wrap width33">'; 1828c2e8227SGreg Roach echo I18N::translate('Presentation style'); 1838c2e8227SGreg Roach echo '</td><td class="optionbox">'; 1843d7a8a4cSGreg Roach 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, ''); 1858c2e8227SGreg Roach echo '</td></tr>'; 1868c2e8227SGreg Roach 1878c2e8227SGreg Roach echo '<tr><td class="descriptionbox wrap width33">'; 1888c2e8227SGreg Roach echo /* I18N: label for a yes/no option */ I18N::translate('Add a scrollbar when block contents grow'); 1898c2e8227SGreg Roach echo '</td><td class="optionbox">'; 1903d7a8a4cSGreg Roach echo FunctionsEdit::editFieldYesNo('block', $block); 1918c2e8227SGreg Roach echo '</td></tr>'; 1928c2e8227SGreg Roach } 1938c2e8227SGreg Roach 1948c2e8227SGreg Roach /** 1958c2e8227SGreg Roach * Sort (lists of counts of similar) surname by total count. 1968c2e8227SGreg Roach * 1978c2e8227SGreg Roach * @param string[] $a 1988c2e8227SGreg Roach * @param string[] $b 1998c2e8227SGreg Roach * 200cbc1590aSGreg Roach * @return int 2018c2e8227SGreg Roach */ 2028c2e8227SGreg Roach private static function surnameCountSort($a, $b) { 2038c2e8227SGreg Roach $counta = 0; 2048c2e8227SGreg Roach foreach ($a as $x) { 2058c2e8227SGreg Roach $counta += count($x); 2068c2e8227SGreg Roach } 2078c2e8227SGreg Roach $countb = 0; 2088c2e8227SGreg Roach foreach ($b as $x) { 2098c2e8227SGreg Roach $countb += count($x); 2108c2e8227SGreg Roach } 2118c2e8227SGreg Roach 2128c2e8227SGreg Roach return $countb - $counta; 2138c2e8227SGreg Roach } 2148c2e8227SGreg Roach} 215