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 */ 16declare(strict_types=1); 17 18namespace Fisharebest\Webtrees\Module; 19 20use Fisharebest\Webtrees\Auth; 21use Fisharebest\Webtrees\Database; 22use Fisharebest\Webtrees\GedcomRecord; 23use Fisharebest\Webtrees\I18N; 24use Fisharebest\Webtrees\Tree; 25use Symfony\Component\HttpFoundation\Request; 26 27/** 28 * Class TopPageViewsModule 29 */ 30class TopPageViewsModule extends AbstractModule implements ModuleBlockInterface 31{ 32 /** 33 * How should this module be labelled on tabs, menus, etc.? 34 * 35 * @return string 36 */ 37 public function getTitle(): string 38 { 39 /* I18N: Name of a module */ 40 return I18N::translate('Most viewed pages'); 41 } 42 43 /** 44 * A sentence describing what this module does. 45 * 46 * @return string 47 */ 48 public function getDescription(): string 49 { 50 /* I18N: Description of the “Most visited pages” module */ 51 return I18N::translate('A list of the pages that have been viewed the most number of times.'); 52 } 53 54 /** 55 * Generate the HTML content of this block. 56 * 57 * @param Tree $tree 58 * @param int $block_id 59 * @param bool $template 60 * @param string[] $cfg 61 * 62 * @return string 63 */ 64 public function getBlock(Tree $tree, int $block_id, bool $template = true, array $cfg = []): string 65 { 66 global $ctype; 67 68 $num = $this->getBlockSetting($block_id, 'num', '10'); 69 $count_placement = $this->getBlockSetting($block_id, 'count_placement', 'before'); 70 71 extract($cfg, EXTR_OVERWRITE); 72 73 // load the lines from the file 74 $top10 = Database::prepare( 75 "SELECT page_parameter, page_count" . 76 " FROM `##hit_counter`" . 77 " WHERE gedcom_id = :tree_id AND page_name IN ('individual.php','family.php','source.php','repo.php','note.php','mediaviewer.php')" . 78 " ORDER BY page_count DESC LIMIT :limit" 79 )->execute([ 80 'tree_id' => $tree->id(), 81 'limit' => (int) $num, 82 ])->fetchAssoc(); 83 84 $content = '<table>'; 85 foreach ($top10 as $id => $count) { 86 $record = GedcomRecord::getInstance($id, $tree); 87 if ($record && $record->canShow()) { 88 $content .= '<tr>'; 89 if ($count_placement == 'before') { 90 $content .= '<td dir="ltr" style="text-align:right">[' . $count . ']</td>'; 91 } 92 $content .= '<td class="name2" ><a href="' . e($record->url()) . '">' . $record->getFullName() . '</a></td>'; 93 if ($count_placement == 'after') { 94 $content .= '<td dir="ltr" style="text-align:right">[' . $count . ']</td>'; 95 } 96 $content .= '</tr>'; 97 } 98 } 99 $content .= '</table>'; 100 101 if ($template) { 102 if ($ctype === 'gedcom' && Auth::isManager($tree)) { 103 $config_url = route('tree-page-block-edit', [ 104 'block_id' => $block_id, 105 'ged' => $tree->name(), 106 ]); 107 } elseif ($ctype === 'user' && Auth::check()) { 108 $config_url = route('user-page-block-edit', [ 109 'block_id' => $block_id, 110 'ged' => $tree->name(), 111 ]); 112 } else { 113 $config_url = ''; 114 } 115 116 return view('modules/block-template', [ 117 'block' => str_replace('_', '-', $this->getName()), 118 'id' => $block_id, 119 'config_url' => $config_url, 120 'title' => $this->getTitle(), 121 'content' => $content, 122 ]); 123 } 124 125 return $content; 126 } 127 128 /** 129 * Should this block load asynchronously using AJAX? 130 * 131 * Simple blocks are faster in-line, more comples ones 132 * can be loaded later. 133 * 134 * @return bool 135 */ 136 public function loadAjax(): bool 137 { 138 return true; 139 } 140 141 /** 142 * Can this block be shown on the user’s home page? 143 * 144 * @return bool 145 */ 146 public function isUserBlock(): bool 147 { 148 return false; 149 } 150 151 /** 152 * Can this block be shown on the tree’s home page? 153 * 154 * @return bool 155 */ 156 public function isGedcomBlock(): bool 157 { 158 return true; 159 } 160 161 /** 162 * Update the configuration for a block. 163 * 164 * @param Request $request 165 * @param int $block_id 166 * 167 * @return void 168 */ 169 public function saveBlockConfiguration(Request $request, int $block_id) 170 { 171 $this->setBlockSetting($block_id, 'num', $request->get('num', '10')); 172 $this->setBlockSetting($block_id, 'count_placement', $request->get('count_placement', 'before')); 173 } 174 175 /** 176 * An HTML form to edit block settings 177 * 178 * @param Tree $tree 179 * @param int $block_id 180 * 181 * @return void 182 */ 183 public function editBlockConfiguration(Tree $tree, int $block_id) 184 { 185 $num = $this->getBlockSetting($block_id, 'num', '10'); 186 $count_placement = $this->getBlockSetting($block_id, 'count_placement', 'before'); 187 188 $options = [ 189 /* I18N: An option in a list-box */ 190 'before' => I18N::translate('before'), 191 /* I18N: An option in a list-box */ 192 'after' => I18N::translate('after'), 193 ]; 194 195 echo view('modules/top10_pageviews/config', [ 196 'count_placement' => $count_placement, 197 'num' => $num, 198 'options' => $options, 199 ]); 200 } 201} 202