1<?php 2namespace Fisharebest\Webtrees\Module; 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 */ 18use Fisharebest\Webtrees\Auth; 19use Fisharebest\Webtrees\Database; 20use Fisharebest\Webtrees\Filter; 21use Fisharebest\Webtrees\Functions\FunctionsEdit; 22use Fisharebest\Webtrees\GedcomRecord; 23use Fisharebest\Webtrees\I18N; 24use Fisharebest\Webtrees\Theme; 25 26/** 27 * Class TopPageViewsModule 28 */ 29class TopPageViewsModule extends AbstractModule implements ModuleBlockInterface { 30 /** {@inheritdoc} */ 31 public function getTitle() { 32 return /* I18N: Name of a module */ I18N::translate('Most viewed pages'); 33 } 34 35 /** {@inheritdoc} */ 36 public function getDescription() { 37 return /* I18N: Description of the “Most visited pages” module */ I18N::translate('A list of the pages that have been viewed the most number of times.'); 38 } 39 40 /** {@inheritdoc} */ 41 public function getBlock($block_id, $template = true, $cfg = null) { 42 global $ctype, $WT_TREE; 43 44 $num = $this->getBlockSetting($block_id, 'num', '10'); 45 $count_placement = $this->getBlockSetting($block_id, 'count_placement', 'before'); 46 $block = $this->getBlockSetting($block_id, 'block', '0'); 47 48 if ($cfg) { 49 foreach (array('count_placement', 'num', 'block') as $name) { 50 if (array_key_exists($name, $cfg)) { 51 $$name = $cfg[$name]; 52 } 53 } 54 } 55 56 $id = $this->getName() . $block_id; 57 $class = $this->getName() . '_block'; 58 if ($ctype === 'gedcom' && Auth::isManager($WT_TREE) || $ctype === 'user' && Auth::check()) { 59 $title = '<a class="icon-admin" title="' . I18N::translate('Configure') . '" href="block_edit.php?block_id=' . $block_id . '&ged=' . $WT_TREE->getNameHtml() . '&ctype=' . $ctype . '"></a>'; 60 } else { 61 $title = ''; 62 } 63 $title .= $this->getTitle(); 64 65 $content = ""; 66 // load the lines from the file 67 $top10 = Database::prepare( 68 "SELECT page_parameter, page_count" . 69 " FROM `##hit_counter`" . 70 " WHERE gedcom_id = :tree_id AND page_name IN ('individual.php','family.php','source.php','repo.php','note.php','mediaviewer.php')" . 71 " ORDER BY page_count DESC LIMIT :limit" 72 )->execute(array( 73 'tree_id' => $WT_TREE->getTreeId(), 74 'limit' => (int) $num, 75 ))->FetchAssoc(); 76 77 if ($block) { 78 $content .= "<table width=\"90%\">"; 79 } else { 80 $content .= "<table>"; 81 } 82 foreach ($top10 as $id => $count) { 83 $record = GedcomRecord::getInstance($id, $WT_TREE); 84 if ($record && $record->canShow()) { 85 $content .= '<tr valign="top">'; 86 if ($count_placement == 'before') { 87 $content .= '<td dir="ltr" align="right">[' . $count . ']</td>'; 88 } 89 $content .= '<td class="name2" ><a href="' . $record->getHtmlUrl() . '">' . $record->getFullName() . '</a></td>'; 90 if ($count_placement == 'after') { 91 $content .= '<td dir="ltr" align="right">[' . $count . ']</td>'; 92 } 93 $content .= '</tr>'; 94 } 95 } 96 $content .= "</table>"; 97 98 if ($template) { 99 if ($block) { 100 $class .= ' small_inner_block'; 101 } 102 103 return Theme::theme()->formatBlock($id, $title, $class, $content); 104 } else { 105 return $content; 106 } 107 } 108 109 /** {@inheritdoc} */ 110 public function loadAjax() { 111 return true; 112 } 113 114 /** {@inheritdoc} */ 115 public function isUserBlock() { 116 return false; 117 } 118 119 /** {@inheritdoc} */ 120 public function isGedcomBlock() { 121 return true; 122 } 123 124 /** {@inheritdoc} */ 125 public function configureBlock($block_id) { 126 if (Filter::postBool('save') && Filter::checkCsrf()) { 127 $this->setBlockSetting($block_id, 'num', Filter::postInteger('num', 1, 10000, 10)); 128 $this->setBlockSetting($block_id, 'count_placement', Filter::post('count_placement', 'before|after', 'before')); 129 $this->setBlockSetting($block_id, 'block', Filter::postBool('block')); 130 } 131 132 $num = $this->getBlockSetting($block_id, 'num', '10'); 133 $count_placement = $this->getBlockSetting($block_id, 'count_placement', 'before'); 134 $block = $this->getBlockSetting($block_id, 'block', '0'); 135 136 echo '<tr><td class="descriptionbox wrap width33">'; 137 echo I18N::translate('Number of items to show'); 138 echo '</td><td class="optionbox">'; 139 echo '<input type="text" name="num" size="2" value="', $num, '">'; 140 echo '</td></tr>'; 141 142 echo "<tr><td class=\"descriptionbox wrap width33\">"; 143 echo I18N::translate('Place counts before or after name?'); 144 echo "</td><td class=\"optionbox\">"; 145 echo FunctionsEdit::selectEditControl('count_placement', array('before' => I18N::translate('before'), 'after' => I18N::translate('after')), null, $count_placement, ''); 146 echo '</td></tr>'; 147 148 echo '<tr><td class="descriptionbox wrap width33">'; 149 echo /* I18N: label for a yes/no option */ I18N::translate('Add a scrollbar when block contents grow'); 150 echo '</td><td class="optionbox">'; 151 echo FunctionsEdit::editFieldYesNo('block', $block); 152 echo '</td></tr>'; 153 } 154} 155