1<?php 2/** 3 * webtrees: online genealogy 4 * Copyright (C) 2019 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\GedcomRecord; 22use Fisharebest\Webtrees\I18N; 23use Fisharebest\Webtrees\Tree; 24use Illuminate\Database\Capsule\Manager as DB; 25use Symfony\Component\HttpFoundation\Request; 26 27/** 28 * Class TopPageViewsModule 29 */ 30class TopPageViewsModule extends AbstractModule implements ModuleInterface, ModuleBlockInterface 31{ 32 use ModuleBlockTrait; 33 34 /** 35 * How should this module be labelled on tabs, menus, etc.? 36 * 37 * @return string 38 */ 39 public function title(): string 40 { 41 /* I18N: Name of a module */ 42 return I18N::translate('Most viewed pages'); 43 } 44 45 /** 46 * A sentence describing what this module does. 47 * 48 * @return string 49 */ 50 public function description(): string 51 { 52 /* I18N: Description of the “Most viewed pages” module */ 53 return I18N::translate('A list of the pages that have been viewed the most number of times.'); 54 } 55 56 /** 57 * Generate the HTML content of this block. 58 * 59 * @param Tree $tree 60 * @param int $block_id 61 * @param string $ctype 62 * @param string[] $cfg 63 * 64 * @return string 65 */ 66 public function getBlock(Tree $tree, int $block_id, string $ctype = '', array $cfg = []): string 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 $top10 = DB::table('hit_counter') 74 ->where('gedcom_id', '=', $tree->id()) 75 ->whereIn('page_name', ['individual.php','family.php','source.php','repo.php','note.php','mediaviewer.php']) 76 ->orderByDesc('page_count') 77 ->limit((int) $num) 78 ->pluck('page_count', 'page_parameter'); 79 80 $content = '<table>'; 81 foreach ($top10 as $id => $count) { 82 $record = GedcomRecord::getInstance($id, $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 ($ctype !== '') { 98 if ($ctype === 'gedcom' && Auth::isManager($tree)) { 99 $config_url = route('tree-page-block-edit', [ 100 'block_id' => $block_id, 101 'ged' => $tree->name(), 102 ]); 103 } elseif ($ctype === 'user' && Auth::check()) { 104 $config_url = route('user-page-block-edit', [ 105 'block_id' => $block_id, 106 'ged' => $tree->name(), 107 ]); 108 } else { 109 $config_url = ''; 110 } 111 112 return view('modules/block-template', [ 113 'block' => str_replace('_', '-', $this->name()), 114 'id' => $block_id, 115 'config_url' => $config_url, 116 'title' => $this->title(), 117 'content' => $content, 118 ]); 119 } 120 121 return $content; 122 } 123 124 /** 125 * Should this block load asynchronously using AJAX? 126 * 127 * Simple blocks are faster in-line, more comples ones 128 * can be loaded later. 129 * 130 * @return bool 131 */ 132 public function loadAjax(): bool 133 { 134 return true; 135 } 136 137 /** 138 * Can this block be shown on the user’s home page? 139 * 140 * @return bool 141 */ 142 public function isUserBlock(): bool 143 { 144 return false; 145 } 146 147 /** 148 * Can this block be shown on the tree’s home page? 149 * 150 * @return bool 151 */ 152 public function isGedcomBlock(): bool 153 { 154 return true; 155 } 156 157 /** 158 * Update the configuration for a block. 159 * 160 * @param Request $request 161 * @param int $block_id 162 * 163 * @return void 164 */ 165 public function saveBlockConfiguration(Request $request, int $block_id) 166 { 167 $this->setBlockSetting($block_id, 'num', $request->get('num', '10')); 168 $this->setBlockSetting($block_id, 'count_placement', $request->get('count_placement', 'before')); 169 } 170 171 /** 172 * An HTML form to edit block settings 173 * 174 * @param Tree $tree 175 * @param int $block_id 176 * 177 * @return void 178 */ 179 public function editBlockConfiguration(Tree $tree, int $block_id) 180 { 181 $num = $this->getBlockSetting($block_id, 'num', '10'); 182 $count_placement = $this->getBlockSetting($block_id, 'count_placement', 'before'); 183 184 $options = [ 185 /* I18N: An option in a list-box */ 186 'before' => I18N::translate('before'), 187 /* I18N: An option in a list-box */ 188 'after' => I18N::translate('after'), 189 ]; 190 191 echo view('modules/top10_pageviews/config', [ 192 'count_placement' => $count_placement, 193 'num' => $num, 194 'options' => $options, 195 ]); 196 } 197} 198