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 */ 17 18declare(strict_types=1); 19 20namespace Fisharebest\Webtrees\Module; 21 22use Fisharebest\Webtrees\GedcomRecord; 23use Fisharebest\Webtrees\I18N; 24use Fisharebest\Webtrees\Tree; 25use Illuminate\Database\Capsule\Manager as DB; 26use Illuminate\Support\Str; 27use Psr\Http\Message\ServerRequestInterface; 28 29/** 30 * Class TopPageViewsModule 31 */ 32class TopPageViewsModule extends AbstractModule implements ModuleBlockInterface 33{ 34 use ModuleBlockTrait; 35 36 private const DEFAULT_NUMBER_TO_SHOW = '10'; 37 38 /** 39 * How should this module be identified in the control panel, etc.? 40 * 41 * @return string 42 */ 43 public function title(): string 44 { 45 /* I18N: Name of a module */ 46 return I18N::translate('Most viewed pages'); 47 } 48 49 /** 50 * A sentence describing what this module does. 51 * 52 * @return string 53 */ 54 public function description(): string 55 { 56 /* I18N: Description of the “Most viewed pages” module */ 57 return I18N::translate('A list of the pages that have been viewed the most number of times.'); 58 } 59 60 /** 61 * Generate the HTML content of this block. 62 * 63 * @param Tree $tree 64 * @param int $block_id 65 * @param string $context 66 * @param string[] $config 67 * 68 * @return string 69 */ 70 public function getBlock(Tree $tree, int $block_id, string $context, array $config = []): string 71 { 72 $num = (int) $this->getBlockSetting($block_id, 'num', self::DEFAULT_NUMBER_TO_SHOW); 73 74 extract($config, EXTR_OVERWRITE); 75 76 $query = DB::table('hit_counter') 77 ->where('gedcom_id', '=', $tree->id()) 78 ->whereIn('page_name', ['individual.php','family.php','source.php','repo.php','note.php','mediaviewer.php']) 79 ->orderByDesc('page_count'); 80 81 $results = []; 82 foreach ($query->cursor() as $row) { 83 $record = GedcomRecord::getInstance($row->page_parameter, $tree); 84 85 if ($record instanceof GedcomRecord && $record->canShow()) { 86 $results[] = [ 87 'record' => $record, 88 'count' => $row->page_count, 89 ]; 90 } 91 92 if (count($results) === $num) { 93 break; 94 } 95 } 96 97 $content = view('modules/top10_pageviews/list', ['results' => $results]); 98 99 if ($context !== self::CONTEXT_EMBED) { 100 return view('modules/block-template', [ 101 'block' => Str::kebab($this->name()), 102 'id' => $block_id, 103 'config_url' => $this->configUrl($tree, $context, $block_id), 104 'title' => $this->title(), 105 'content' => $content, 106 ]); 107 } 108 109 return $content; 110 } 111 112 /** 113 * Should this block load asynchronously using AJAX? 114 * 115 * Simple blocks are faster in-line, more complex ones can be loaded later. 116 * 117 * @return bool 118 */ 119 public function loadAjax(): bool 120 { 121 return true; 122 } 123 124 /** 125 * Can this block be shown on the user’s home page? 126 * 127 * @return bool 128 */ 129 public function isUserBlock(): bool 130 { 131 return false; 132 } 133 134 /** 135 * Can this block be shown on the tree’s home page? 136 * 137 * @return bool 138 */ 139 public function isTreeBlock(): bool 140 { 141 return true; 142 } 143 144 /** 145 * Update the configuration for a block. 146 * 147 * @param ServerRequestInterface $request 148 * @param int $block_id 149 * 150 * @return void 151 */ 152 public function saveBlockConfiguration(ServerRequestInterface $request, int $block_id): void 153 { 154 $params = $request->getParsedBody(); 155 156 $this->setBlockSetting($block_id, 'num', $params['num']); 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', self::DEFAULT_NUMBER_TO_SHOW); 170 171 return view('modules/top10_pageviews/config', [ 172 'num' => $num, 173 ]); 174 } 175} 176