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