1<?php 2/** 3 * webtrees: online genealogy 4 * Copyright (C) 2018 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\Bootstrap4; 20use Fisharebest\Webtrees\Database; 21use Fisharebest\Webtrees\Filter; 22use Fisharebest\Webtrees\GedcomRecord; 23use Fisharebest\Webtrees\Html; 24use Fisharebest\Webtrees\I18N; 25 26/** 27 * Class TopPageViewsModule 28 */ 29class TopPageViewsModule extends AbstractModule implements ModuleBlockInterface { 30 /** 31 * How should this module be labelled on tabs, menus, etc.? 32 * 33 * @return string 34 */ 35 public function getTitle() { 36 return /* I18N: Name of a module */ I18N::translate('Most viewed pages'); 37 } 38 39 /** 40 * A sentence describing what this module does. 41 * 42 * @return string 43 */ 44 public function getDescription() { 45 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.'); 46 } 47 48 /** 49 * Generate the HTML content of this block. 50 * 51 * @param int $block_id 52 * @param bool $template 53 * @param string[] $cfg 54 * 55 * @return string 56 */ 57 public function getBlock($block_id, $template = true, $cfg = []): string { 58 global $ctype, $WT_TREE; 59 60 $num = $this->getBlockSetting($block_id, 'num', '10'); 61 $count_placement = $this->getBlockSetting($block_id, 'count_placement', 'before'); 62 63 foreach (['count_placement', 'num'] as $name) { 64 if (array_key_exists($name, $cfg)) { 65 $$name = $cfg[$name]; 66 } 67 } 68 69 // load the lines from the file 70 $top10 = Database::prepare( 71 "SELECT page_parameter, page_count" . 72 " FROM `##hit_counter`" . 73 " WHERE gedcom_id = :tree_id AND page_name IN ('individual.php','family.php','source.php','repo.php','note.php','mediaviewer.php')" . 74 " ORDER BY page_count DESC LIMIT :limit" 75 )->execute([ 76 'tree_id' => $WT_TREE->getTreeId(), 77 'limit' => (int) $num, 78 ])->fetchAssoc(); 79 80 $content = '<table>'; 81 foreach ($top10 as $id => $count) { 82 $record = GedcomRecord::getInstance($id, $WT_TREE); 83 if ($record && $record->canShow()) { 84 $content .= '<tr>'; 85 if ($count_placement == 'before') { 86 $content .= '<td dir="ltr" style="text-align:right">[' . $count . ']</td>'; 87 } 88 $content .= '<td class="name2" ><a href="' . e($record->url()) . '">' . $record->getFullName() . '</a></td>'; 89 if ($count_placement == 'after') { 90 $content .= '<td dir="ltr" style="text-align:right">[' . $count . ']</td>'; 91 } 92 $content .= '</tr>'; 93 } 94 } 95 $content .= '</table>'; 96 97 if ($template) { 98 if ($ctype === 'gedcom' && Auth::isManager($WT_TREE)) { 99 $config_url = route('tree-page-block-edit', ['block_id' => $block_id, 'ged' => $WT_TREE->getName()]); 100 } elseif ($ctype === 'user' && Auth::check()) { 101 $config_url = route('user-page-block-edit', ['block_id' => $block_id, 'ged' => $WT_TREE->getName()]); 102 } else { 103 $config_url = ''; 104 } 105 106 return view('blocks/template', [ 107 'block' => str_replace('_', '-', $this->getName()), 108 'id' => $block_id, 109 'config_url' => $config_url, 110 'title' => $this->getTitle(), 111 'content' => $content, 112 ]); 113 } else { 114 return $content; 115 } 116 } 117 118 /** 119 * Should this block load asynchronously using AJAX? 120 * 121 * Simple blocks are faster in-line, more comples ones 122 * can be loaded later. 123 * 124 * @return bool 125 */ 126 public function loadAjax(): bool { 127 return true; 128 } 129 130 /** 131 * Can this block be shown on the user’s home page? 132 * 133 * @return bool 134 */ 135 public function isUserBlock(): bool { 136 return false; 137 } 138 139 /** 140 * Can this block be shown on the tree’s home page? 141 * 142 * @return bool 143 */ 144 public function isGedcomBlock(): bool { 145 return true; 146 } 147 148 /** 149 * An HTML form to edit block settings 150 * 151 * @param int $block_id 152 * 153 * @return void 154 */ 155 public function configureBlock($block_id) { 156 if (Filter::postBool('save') && Filter::checkCsrf()) { 157 $this->setBlockSetting($block_id, 'num', Filter::postInteger('num', 1, 10000, 10)); 158 $this->setBlockSetting($block_id, 'count_placement', Filter::post('count_placement', 'before|after', 'before')); 159 } 160 161 $num = $this->getBlockSetting($block_id, 'num', '10'); 162 $count_placement = $this->getBlockSetting($block_id, 'count_placement', 'before'); 163 164 echo '<div class="form-group row"><label class="col-sm-3 col-form-label" for="num">'; 165 echo /* I18N: ... to show in a list */ I18N::translate('Number of pages'); 166 echo '</label><div class="col-sm-9">'; 167 echo '<input type="text" name="num" size="2" value="', $num, '">'; 168 echo '</div></div>'; 169 170 echo '<div class="form-group row"><label class="col-sm-3 col-form-label" for="count_placement">'; 171 echo /* I18N: Label for a configuration option */ I18N::translate('Show counts before or after name'); 172 echo '</label><div class="col-sm-9">'; 173 echo Bootstrap4::select(['before' => I18N::translate('before'), 'after' => I18N::translate('after')], $count_placement, ['id' => 'count_placement', 'name' => 'count_placement']); 174 echo '</div></div>'; 175 } 176} 177