18c2e8227SGreg Roach<?php 23976b470SGreg Roach 38c2e8227SGreg Roach/** 48c2e8227SGreg Roach * webtrees: online genealogy 5d11be702SGreg Roach * Copyright (C) 2023 webtrees development team 68c2e8227SGreg Roach * This program is free software: you can redistribute it and/or modify 78c2e8227SGreg Roach * it under the terms of the GNU General Public License as published by 88c2e8227SGreg Roach * the Free Software Foundation, either version 3 of the License, or 98c2e8227SGreg Roach * (at your option) any later version. 108c2e8227SGreg Roach * This program is distributed in the hope that it will be useful, 118c2e8227SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of 128c2e8227SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 138c2e8227SGreg Roach * GNU General Public License for more details. 148c2e8227SGreg Roach * You should have received a copy of the GNU General Public License 1589f7189bSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>. 168c2e8227SGreg Roach */ 17fcfa147eSGreg Roach 18e7f56f2aSGreg Roachdeclare(strict_types=1); 19e7f56f2aSGreg Roach 2076692c8bSGreg Roachnamespace Fisharebest\Webtrees\Module; 2176692c8bSGreg Roach 22*6f4ec3caSGreg Roachuse Fisharebest\Webtrees\DB; 230e62c4b8SGreg Roachuse Fisharebest\Webtrees\GedcomRecord; 240e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N; 2509482a55SGreg Roachuse Fisharebest\Webtrees\Registry; 26e490cd80SGreg Roachuse Fisharebest\Webtrees\Tree; 27748dbe15SGreg Roachuse Fisharebest\Webtrees\Validator; 281e7a7a28SGreg Roachuse Illuminate\Support\Str; 296ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface; 308c2e8227SGreg Roach 3110e06497SGreg Roachuse function count; 3210e06497SGreg Roach 338c2e8227SGreg Roach/** 348c2e8227SGreg Roach * Class TopPageViewsModule 358c2e8227SGreg Roach */ 3637eb8894SGreg Roachclass TopPageViewsModule extends AbstractModule implements ModuleBlockInterface 37c1010edaSGreg Roach{ 3849a243cbSGreg Roach use ModuleBlockTrait; 3949a243cbSGreg Roach 40380303edSGreg Roach private const DEFAULT_NUMBER_TO_SHOW = '10'; 41380303edSGreg Roach 424fedbbe7SGreg Roach private const PAGES = ['individual.php', 'family.php', 'source.php', 'repo.php', 'note.php', 'mediaviewer.php']; 434fedbbe7SGreg Roach 4476692c8bSGreg Roach /** 450cfd6963SGreg Roach * How should this module be identified in the control panel, etc.? 4676692c8bSGreg Roach * 4776692c8bSGreg Roach * @return string 4876692c8bSGreg Roach */ 4949a243cbSGreg Roach public function title(): string 50c1010edaSGreg Roach { 51bbb76c12SGreg Roach /* I18N: Name of a module */ 52bbb76c12SGreg Roach return I18N::translate('Most viewed pages'); 538c2e8227SGreg Roach } 548c2e8227SGreg Roach 5549a243cbSGreg Roach public function description(): string 56c1010edaSGreg Roach { 5786c75e30SGreg Roach /* I18N: Description of the “Most viewed pages” module */ 58bbb76c12SGreg Roach return I18N::translate('A list of the pages that have been viewed the most number of times.'); 598c2e8227SGreg Roach } 608c2e8227SGreg Roach 6176692c8bSGreg Roach /** 6276692c8bSGreg Roach * Generate the HTML content of this block. 6376692c8bSGreg Roach * 64e490cd80SGreg Roach * @param Tree $tree 6576692c8bSGreg Roach * @param int $block_id 663caaa4d2SGreg Roach * @param string $context 6776d39c55SGreg Roach * @param array<string,string> $config 6876692c8bSGreg Roach * 6976692c8bSGreg Roach * @return string 7076692c8bSGreg Roach */ 713caaa4d2SGreg Roach public function getBlock(Tree $tree, int $block_id, string $context, array $config = []): string 72c1010edaSGreg Roach { 73380303edSGreg Roach $num = (int) $this->getBlockSetting($block_id, 'num', self::DEFAULT_NUMBER_TO_SHOW); 748c2e8227SGreg Roach 753caaa4d2SGreg Roach extract($config, EXTR_OVERWRITE); 768c2e8227SGreg Roach 77380303edSGreg Roach $query = DB::table('hit_counter') 7886c75e30SGreg Roach ->where('gedcom_id', '=', $tree->id()) 794fedbbe7SGreg Roach ->whereIn('page_name', self::PAGES) 806f25e671SGreg Roach ->select(['page_parameter', 'page_count']) 81380303edSGreg Roach ->orderByDesc('page_count'); 828c2e8227SGreg Roach 83380303edSGreg Roach $results = []; 84380303edSGreg Roach foreach ($query->cursor() as $row) { 856b9cb339SGreg Roach $record = Registry::gedcomRecordFactory()->make($row->page_parameter, $tree); 86380303edSGreg Roach 87380303edSGreg Roach if ($record instanceof GedcomRecord && $record->canShow()) { 88380303edSGreg Roach $results[] = [ 89380303edSGreg Roach 'record' => $record, 906f25e671SGreg Roach 'count' => (int) $row->page_count, 91380303edSGreg Roach ]; 928c2e8227SGreg Roach } 93380303edSGreg Roach 94380303edSGreg Roach if (count($results) === $num) { 95380303edSGreg Roach break; 968c2e8227SGreg Roach } 978c2e8227SGreg Roach } 98380303edSGreg Roach 99380303edSGreg Roach $content = view('modules/top10_pageviews/list', ['results' => $results]); 1008c2e8227SGreg Roach 1013caaa4d2SGreg Roach if ($context !== self::CONTEXT_EMBED) { 102147e99aaSGreg Roach return view('modules/block-template', [ 1031e7a7a28SGreg Roach 'block' => Str::kebab($this->name()), 1049c6524dcSGreg Roach 'id' => $block_id, 1053caaa4d2SGreg Roach 'config_url' => $this->configUrl($tree, $context, $block_id), 10649a243cbSGreg Roach 'title' => $this->title(), 1079c6524dcSGreg Roach 'content' => $content, 1089c6524dcSGreg Roach ]); 1098c2e8227SGreg Roach } 110b2ce94c6SRico Sonntag 111b2ce94c6SRico Sonntag return $content; 1128c2e8227SGreg Roach } 1138c2e8227SGreg Roach 11476692c8bSGreg Roach /** 11576692c8bSGreg Roach * Should this block load asynchronously using AJAX? 11676692c8bSGreg Roach * 1173caaa4d2SGreg Roach * Simple blocks are faster in-line, more complex ones can be loaded later. 11876692c8bSGreg Roach * 11976692c8bSGreg Roach * @return bool 12076692c8bSGreg Roach */ 121c1010edaSGreg Roach public function loadAjax(): bool 122c1010edaSGreg Roach { 1238c2e8227SGreg Roach return true; 1248c2e8227SGreg Roach } 1258c2e8227SGreg Roach 12676692c8bSGreg Roach /** 12776692c8bSGreg Roach * Can this block be shown on the user’s home page? 12876692c8bSGreg Roach * 12976692c8bSGreg Roach * @return bool 13076692c8bSGreg Roach */ 131c1010edaSGreg Roach public function isUserBlock(): bool 132c1010edaSGreg Roach { 1338c2e8227SGreg Roach return false; 1348c2e8227SGreg Roach } 1358c2e8227SGreg Roach 13676692c8bSGreg Roach /** 13776692c8bSGreg Roach * Can this block be shown on the tree’s home page? 13876692c8bSGreg Roach * 13976692c8bSGreg Roach * @return bool 14076692c8bSGreg Roach */ 14163276d8fSGreg Roach public function isTreeBlock(): bool 142c1010edaSGreg Roach { 1438c2e8227SGreg Roach return true; 1448c2e8227SGreg Roach } 1458c2e8227SGreg Roach 14676692c8bSGreg Roach /** 147a45f9889SGreg Roach * Update the configuration for a block. 148a45f9889SGreg Roach * 1496ccdf4f0SGreg Roach * @param ServerRequestInterface $request 150a45f9889SGreg Roach * @param int $block_id 151a45f9889SGreg Roach * 152a45f9889SGreg Roach * @return void 153a45f9889SGreg Roach */ 1546ccdf4f0SGreg Roach public function saveBlockConfiguration(ServerRequestInterface $request, int $block_id): void 155a45f9889SGreg Roach { 156748dbe15SGreg Roach $num = Validator::parsedBody($request)->integer('num'); 157c50a90beSGreg Roach 158748dbe15SGreg Roach $this->setBlockSetting($block_id, 'num', (string) $num); 159a45f9889SGreg Roach } 160a45f9889SGreg Roach 161a45f9889SGreg Roach /** 16276692c8bSGreg Roach * An HTML form to edit block settings 16376692c8bSGreg Roach * 164e490cd80SGreg Roach * @param Tree $tree 16576692c8bSGreg Roach * @param int $block_id 166a9430be8SGreg Roach * 1673caaa4d2SGreg Roach * @return string 16876692c8bSGreg Roach */ 1693caaa4d2SGreg Roach public function editBlockConfiguration(Tree $tree, int $block_id): string 170c1010edaSGreg Roach { 171380303edSGreg Roach $num = $this->getBlockSetting($block_id, 'num', self::DEFAULT_NUMBER_TO_SHOW); 1728c2e8227SGreg Roach 1733caaa4d2SGreg Roach return view('modules/top10_pageviews/config', [ 174c385536dSGreg Roach 'num' => $num, 175c385536dSGreg Roach ]); 1768c2e8227SGreg Roach } 1778c2e8227SGreg Roach} 178