1<?php 2 3/** 4 * webtrees: online genealogy 5 * Copyright (C) 2019 webtrees development team 6 * This program is free software: you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation, either version 3 of the License, or 9 * (at your option) any later version. 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * You should have received a copy of the GNU General Public License 15 * along with this program. If not, see <http://www.gnu.org/licenses/>. 16 */ 17declare(strict_types=1); 18 19namespace Fisharebest\Webtrees\Module; 20 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 $context 63 * @param string[] $config 64 * 65 * @return string 66 */ 67 public function getBlock(Tree $tree, int $block_id, string $context, array $config = []): string 68 { 69 $num = $this->getBlockSetting($block_id, 'num', '10'); 70 $count_placement = $this->getBlockSetting($block_id, 'count_placement', 'before'); 71 72 extract($config, 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 ($context !== self::CONTEXT_EMBED) { 99 return view('modules/block-template', [ 100 'block' => Str::kebab($this->name()), 101 'id' => $block_id, 102 'config_url' => $this->configUrl($tree, $context, $block_id), 103 'title' => $this->title(), 104 'content' => $content, 105 ]); 106 } 107 108 return $content; 109 } 110 111 /** 112 * Should this block load asynchronously using AJAX? 113 * 114 * Simple blocks are faster in-line, more complex ones can be loaded later. 115 * 116 * @return bool 117 */ 118 public function loadAjax(): bool 119 { 120 return true; 121 } 122 123 /** 124 * Can this block be shown on the user’s home page? 125 * 126 * @return bool 127 */ 128 public function isUserBlock(): bool 129 { 130 return false; 131 } 132 133 /** 134 * Can this block be shown on the tree’s home page? 135 * 136 * @return bool 137 */ 138 public function isTreeBlock(): bool 139 { 140 return true; 141 } 142 143 /** 144 * Update the configuration for a block. 145 * 146 * @param ServerRequestInterface $request 147 * @param int $block_id 148 * 149 * @return void 150 */ 151 public function saveBlockConfiguration(ServerRequestInterface $request, int $block_id): void 152 { 153 $params = $request->getParsedBody(); 154 155 $this->setBlockSetting($block_id, 'num', $params['num']); 156 $this->setBlockSetting($block_id, 'count_placement', $params['count_placement']); 157 } 158 159 /** 160 * An HTML form to edit block settings 161 * 162 * @param Tree $tree 163 * @param int $block_id 164 * 165 * @return string 166 */ 167 public function editBlockConfiguration(Tree $tree, int $block_id): string 168 { 169 $num = $this->getBlockSetting($block_id, 'num', '10'); 170 $count_placement = $this->getBlockSetting($block_id, 'count_placement', 'before'); 171 172 $options = [ 173 /* I18N: An option in a list-box */ 174 'before' => I18N::translate('before'), 175 /* I18N: An option in a list-box */ 176 'after' => I18N::translate('after'), 177 ]; 178 179 return view('modules/top10_pageviews/config', [ 180 'count_placement' => $count_placement, 181 'num' => $num, 182 'options' => $options, 183 ]); 184 } 185} 186