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