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\FunctionsEdit; 21use Fisharebest\Webtrees\I18N; 22use Fisharebest\Webtrees\Stats; 23use Fisharebest\Webtrees\Theme; 24 25/** 26 * Class TopGivenNamesModule 27 */ 28class TopGivenNamesModule extends AbstractModule implements ModuleBlockInterface { 29 /** {@inheritdoc} */ 30 public function getTitle() { 31 return /* I18N: Name of a module. Top=Most common */ I18N::translate('Top given names'); 32 } 33 34 /** {@inheritdoc} */ 35 public function getDescription() { 36 return /* I18N: Description of the “Top given names” module */ I18N::translate('A list of the most popular given names.'); 37 } 38 39 /** 40 * Generate the HTML content of this block. 41 * 42 * @param int $block_id 43 * @param bool $template 44 * @param string[] $cfg 45 * 46 * @return string 47 */ 48 public function getBlock($block_id, $template = true, $cfg = array()) { 49 global $ctype, $WT_TREE; 50 51 $num = $this->getBlockSetting($block_id, 'num', '10'); 52 $infoStyle = $this->getBlockSetting($block_id, 'infoStyle', 'table'); 53 $block = $this->getBlockSetting($block_id, 'block', '0'); 54 55 foreach (array('num', 'infoStyle', 'block') as $name) { 56 if (array_key_exists($name, $cfg)) { 57 $$name = $cfg[$name]; 58 } 59 } 60 61 $stats = new Stats($WT_TREE); 62 63 $id = $this->getName() . $block_id; 64 $class = $this->getName() . '_block'; 65 if ($ctype === 'gedcom' && Auth::isManager($WT_TREE) || $ctype === 'user' && Auth::check()) { 66 $title = '<a class="icon-admin" title="' . I18N::translate('Configure') . '" href="block_edit.php?block_id=' . $block_id . '&ged=' . $WT_TREE->getNameHtml() . '&ctype=' . $ctype . '"></a>'; 67 } else { 68 $title = ''; 69 } 70 if ($num == 1) { 71 // I18N: i.e. most popular given name. 72 $title .= I18N::translate('Top given name'); 73 } else { 74 // I18N: Title for a list of the most common given names, %s is a number. Note that a separate translation exists when %s is 1 75 $title .= I18N::plural('Top %s given name', 'Top %s given names', $num, I18N::number($num)); 76 } 77 78 $content = '<div class="normal_inner_block">'; 79 //Select List or Table 80 switch ($infoStyle) { 81 case "list": // Output style 1: Simple list style. Better suited to left side of page. 82 if (I18N::direction() === 'ltr') { 83 $padding = 'padding-left: 15px'; 84 } else { 85 $padding = 'padding-right: 15px'; 86 } 87 $params = array(1, $num, 'rcount'); 88 // List Female names 89 $totals = $stats->commonGivenFemaleTotals($params); 90 if ($totals) { 91 $content .= '<b>' . I18N::translate('Females') . '</b><div class="wrap" style="' . $padding . '">' . $totals . '</div><br>'; 92 } 93 // List Male names 94 $totals = $stats->commonGivenMaleTotals($params); 95 if ($totals) { 96 $content .= '<b>' . I18N::translate('Males') . '</b><div class="wrap" style="' . $padding . '">' . $totals . '</div><br>'; 97 } 98 break; 99 case "table": // Style 2: Tabular format. Narrow, 2 or 3 column table, good on right side of page 100 $params = array(1, $num, 'rcount'); 101 $content .= '<table style="margin:auto;"> 102 <tr> 103 <td>' . $stats->commonGivenFemaleTable($params) . '</td> 104 <td>' . $stats->commonGivenMaleTable($params) . '</td>'; 105 $content .= '</tr></table>'; 106 break; 107 } 108 $content .= "</div>"; 109 110 if ($template) { 111 if ($block) { 112 $class .= ' small_inner_block'; 113 } 114 115 return Theme::theme()->formatBlock($id, $title, $class, $content); 116 } else { 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 /** 137 * An HTML form to edit block settings 138 * 139 * @param int $block_id 140 */ 141 public function configureBlock($block_id) { 142 if (Filter::postBool('save') && Filter::checkCsrf()) { 143 $this->setBlockSetting($block_id, 'num', Filter::postInteger('num', 1, 10000, 10)); 144 $this->setBlockSetting($block_id, 'infoStyle', Filter::post('infoStyle', 'list|table', 'table')); 145 $this->setBlockSetting($block_id, 'block', Filter::postBool('block')); 146 } 147 148 $num = $this->getBlockSetting($block_id, 'num', '10'); 149 $infoStyle = $this->getBlockSetting($block_id, 'infoStyle', 'table'); 150 $block = $this->getBlockSetting($block_id, 'block', '0'); 151 152 echo '<tr><td class="descriptionbox wrap width33">'; 153 echo I18N::translate('Number of items to show'); 154 echo '</td><td class="optionbox">'; 155 echo '<input type="text" name="num" size="2" value="', $num, '">'; 156 echo '</td></tr>'; 157 158 echo '<tr><td class="descriptionbox wrap width33">'; 159 echo I18N::translate('Presentation style'); 160 echo '</td><td class="optionbox">'; 161 echo FunctionsEdit::selectEditControl('infoStyle', array('list' => I18N::translate('list'), 'table' => I18N::translate('table')), null, $infoStyle, ''); 162 echo '</td></tr>'; 163 164 echo '<tr><td class="descriptionbox wrap width33">'; 165 echo /* I18N: label for a yes/no option */ I18N::translate('Add a scrollbar when block contents grow'); 166 echo '</td><td class="optionbox">'; 167 echo FunctionsEdit::editFieldYesNo('block', $block); 168 echo '</td></tr>'; 169 } 170} 171