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 string $ctype 60 * @param string[] $cfg 61 * 62 * @return string 63 */ 64 public function getBlock(Tree $tree, int $block_id, string $ctype = '', array $cfg = []): string 65 { 66 $num = $this->getBlockSetting($block_id, 'num', '10'); 67 $count_placement = $this->getBlockSetting($block_id, 'count_placement', 'before'); 68 69 extract($cfg, EXTR_OVERWRITE); 70 71 // load the lines from the file 72 $top10 = Database::prepare( 73 "SELECT page_parameter, page_count" . 74 " FROM `##hit_counter`" . 75 " WHERE gedcom_id = :tree_id AND page_name IN ('individual.php','family.php','source.php','repo.php','note.php','mediaviewer.php')" . 76 " ORDER BY page_count DESC LIMIT :limit" 77 )->execute([ 78 'tree_id' => $tree->id(), 79 'limit' => (int) $num, 80 ])->fetchAssoc(); 81 82 $content = '<table>'; 83 foreach ($top10 as $id => $count) { 84 $record = GedcomRecord::getInstance($id, $tree); 85 if ($record && $record->canShow()) { 86 $content .= '<tr>'; 87 if ($count_placement == 'before') { 88 $content .= '<td dir="ltr" style="text-align:right">[' . $count . ']</td>'; 89 } 90 $content .= '<td class="name2" ><a href="' . e($record->url()) . '">' . $record->getFullName() . '</a></td>'; 91 if ($count_placement == 'after') { 92 $content .= '<td dir="ltr" style="text-align:right">[' . $count . ']</td>'; 93 } 94 $content .= '</tr>'; 95 } 96 } 97 $content .= '</table>'; 98 99 if ($ctype) { 100 if ($ctype === 'gedcom' && Auth::isManager($tree)) { 101 $config_url = route('tree-page-block-edit', [ 102 'block_id' => $block_id, 103 'ged' => $tree->name(), 104 ]); 105 } elseif ($ctype === 'user' && Auth::check()) { 106 $config_url = route('user-page-block-edit', [ 107 'block_id' => $block_id, 108 'ged' => $tree->name(), 109 ]); 110 } else { 111 $config_url = ''; 112 } 113 114 return view('modules/block-template', [ 115 'block' => str_replace('_', '-', $this->getName()), 116 'id' => $block_id, 117 'config_url' => $config_url, 118 'title' => $this->getTitle(), 119 'content' => $content, 120 ]); 121 } 122 123 return $content; 124 } 125 126 /** 127 * Should this block load asynchronously using AJAX? 128 * 129 * Simple blocks are faster in-line, more comples ones 130 * can be loaded later. 131 * 132 * @return bool 133 */ 134 public function loadAjax(): bool 135 { 136 return true; 137 } 138 139 /** 140 * Can this block be shown on the user’s home page? 141 * 142 * @return bool 143 */ 144 public function isUserBlock(): bool 145 { 146 return false; 147 } 148 149 /** 150 * Can this block be shown on the tree’s home page? 151 * 152 * @return bool 153 */ 154 public function isGedcomBlock(): bool 155 { 156 return true; 157 } 158 159 /** 160 * Update the configuration for a block. 161 * 162 * @param Request $request 163 * @param int $block_id 164 * 165 * @return void 166 */ 167 public function saveBlockConfiguration(Request $request, int $block_id) 168 { 169 $this->setBlockSetting($block_id, 'num', $request->get('num', '10')); 170 $this->setBlockSetting($block_id, 'count_placement', $request->get('count_placement', 'before')); 171 } 172 173 /** 174 * An HTML form to edit block settings 175 * 176 * @param Tree $tree 177 * @param int $block_id 178 * 179 * @return void 180 */ 181 public function editBlockConfiguration(Tree $tree, int $block_id) 182 { 183 $num = $this->getBlockSetting($block_id, 'num', '10'); 184 $count_placement = $this->getBlockSetting($block_id, 'count_placement', 'before'); 185 186 $options = [ 187 /* I18N: An option in a list-box */ 188 'before' => I18N::translate('before'), 189 /* I18N: An option in a list-box */ 190 'after' => I18N::translate('after'), 191 ]; 192 193 echo view('modules/top10_pageviews/config', [ 194 'count_placement' => $count_placement, 195 'num' => $num, 196 'options' => $options, 197 ]); 198 } 199} 200