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