133c34396SGreg Roach<?php 233c34396SGreg Roach/** 333c34396SGreg Roach * webtrees: online genealogy 433c34396SGreg Roach * Copyright (C) 2019 webtrees development team 533c34396SGreg Roach * This program is free software: you can redistribute it and/or modify 633c34396SGreg Roach * it under the terms of the GNU General Public License as published by 733c34396SGreg Roach * the Free Software Foundation, either version 3 of the License, or 833c34396SGreg Roach * (at your option) any later version. 933c34396SGreg Roach * This program is distributed in the hope that it will be useful, 1033c34396SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of 1133c34396SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 1233c34396SGreg Roach * GNU General Public License for more details. 1333c34396SGreg Roach * You should have received a copy of the GNU General Public License 1433c34396SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>. 1533c34396SGreg Roach */ 1633c34396SGreg Roachdeclare(strict_types=1); 1733c34396SGreg Roach 1833c34396SGreg Roachnamespace Fisharebest\Webtrees\Module; 1933c34396SGreg Roach 2033c34396SGreg Roachuse Fisharebest\Webtrees\I18N; 2133c34396SGreg Roachuse Fisharebest\Webtrees\Session; 2233c34396SGreg Roachuse Fisharebest\Webtrees\Tree; 2333c34396SGreg Roachuse Fisharebest\Webtrees\User; 2433c34396SGreg Roachuse Illuminate\Database\Capsule\Manager as DB; 2533c34396SGreg Roachuse Symfony\Component\HttpFoundation\Request; 2633c34396SGreg Roach 2733c34396SGreg Roach/** 2833c34396SGreg Roach * Class HitCountFooterModule - show the popular sites in the footer. 2933c34396SGreg Roach */ 3033c34396SGreg Roachclass HitCountFooterModule extends AbstractModule implements ModuleFooterInterface 3133c34396SGreg Roach{ 3233c34396SGreg Roach use ModuleFooterTrait; 3333c34396SGreg Roach 3433c34396SGreg Roach // Which pages/routes do we count? 3533c34396SGreg Roach // For historical reasons, we record the names of the original webtrees script and parameter. 3633c34396SGreg Roach protected const PAGE_NAMES = [ 3733c34396SGreg Roach 'family' => 'family.php', 3833c34396SGreg Roach 'individual' => 'individual.php', 3933c34396SGreg Roach 'media' => 'mediaviewer.php', 4033c34396SGreg Roach 'note' => 'note.php', 4133c34396SGreg Roach 'repository' => 'repo.php', 4233c34396SGreg Roach 'source' => 'source.php', 4333c34396SGreg Roach 'tree-page' => 'index.php', 4433c34396SGreg Roach 'user-page' => 'index.php', 4533c34396SGreg Roach ]; 4633c34396SGreg Roach 4733c34396SGreg Roach /** @var Request */ 4833c34396SGreg Roach protected $request; 4933c34396SGreg Roach 5033c34396SGreg Roach /** @var Tree|null */ 5133c34396SGreg Roach protected $tree; 5233c34396SGreg Roach 5333c34396SGreg Roach /** @var User */ 5433c34396SGreg Roach protected $user; 5533c34396SGreg Roach 5633c34396SGreg Roach /** @var int */ 5733c34396SGreg Roach protected $page_hits = 0; 5833c34396SGreg Roach 5933c34396SGreg Roach /** 6033c34396SGreg Roach * How should this module be labelled on tabs, footers, etc.? 6133c34396SGreg Roach * 6233c34396SGreg Roach * @return string 6333c34396SGreg Roach */ 6433c34396SGreg Roach public function title(): string 6533c34396SGreg Roach { 6633c34396SGreg Roach /* I18N: Name of a module */ 6733c34396SGreg Roach return I18N::translate('Hit counters'); 6833c34396SGreg Roach } 6933c34396SGreg Roach 7033c34396SGreg Roach /** 7133c34396SGreg Roach * A sentence describing what this module does. 7233c34396SGreg Roach * 7333c34396SGreg Roach * @return string 7433c34396SGreg Roach */ 7533c34396SGreg Roach public function description(): string 7633c34396SGreg Roach { 7733c34396SGreg Roach /* I18N: Description of the “Hit counters” module */ 7833c34396SGreg Roach return I18N::translate('Count the visits to each page'); 7933c34396SGreg Roach } 8033c34396SGreg Roach 8133c34396SGreg Roach /** 8233c34396SGreg Roach * Dependency injection. 8333c34396SGreg Roach * 8433c34396SGreg Roach * @param Tree|null $tree 8533c34396SGreg Roach * @param User $user 8633c34396SGreg Roach * @param Request $request 8733c34396SGreg Roach */ 8833c34396SGreg Roach public function boot(?Tree $tree, User $user, Request $request): void 8933c34396SGreg Roach { 9033c34396SGreg Roach $this->tree = $tree; 9133c34396SGreg Roach $this->user = $user; 9233c34396SGreg Roach $this->request = $request; 9333c34396SGreg Roach 9433c34396SGreg Roach if ($this->tree !== null && $this->tree->getPreference('SHOW_COUNTER')) { 9533c34396SGreg Roach $route = $request->get('route'); 9633c34396SGreg Roach 9733c34396SGreg Roach $page_name = self::PAGE_NAMES[$route] ?? ''; 9833c34396SGreg Roach 9933c34396SGreg Roach switch ($route) { 10033c34396SGreg Roach case 'family': 10133c34396SGreg Roach case 'individual': 10233c34396SGreg Roach case 'media': 10333c34396SGreg Roach case 'note': 10433c34396SGreg Roach case 'repository': 10533c34396SGreg Roach case 'source': 10633c34396SGreg Roach $this->page_hits = $this->countHit($page_name, $request->get('xref', '')); 10733c34396SGreg Roach break; 10833c34396SGreg Roach 10933c34396SGreg Roach case 'tree-page': 11033c34396SGreg Roach $this->page_hits = $this->countHit($page_name, 'gedcom:' . $this->tree->id()); 11133c34396SGreg Roach break; 11233c34396SGreg Roach 11333c34396SGreg Roach case 'user-page': 11433c34396SGreg Roach $this->page_hits = $this->countHit($page_name, 'user:' . $this->user->id()); 11533c34396SGreg Roach break; 11633c34396SGreg Roach } 11733c34396SGreg Roach } 11833c34396SGreg Roach } 11933c34396SGreg Roach 12033c34396SGreg Roach /** 12133c34396SGreg Roach * The default position for this footer. It can be changed in the control panel. 12233c34396SGreg Roach * 12333c34396SGreg Roach * @return int 12433c34396SGreg Roach */ 12533c34396SGreg Roach public function defaultFooterOrder(): int 12633c34396SGreg Roach { 12733c34396SGreg Roach return 3; 12833c34396SGreg Roach } 12933c34396SGreg Roach 13033c34396SGreg Roach /** 13133c34396SGreg Roach * A footer, to be added at the bottom of every page. 13233c34396SGreg Roach * 13333c34396SGreg Roach * @return string 13433c34396SGreg Roach */ 13533c34396SGreg Roach public function getFooter(): string 13633c34396SGreg Roach { 13733c34396SGreg Roach if ($this->tree === null || $this->page_hits === 0) { 13833c34396SGreg Roach return ''; 13933c34396SGreg Roach } 14033c34396SGreg Roach 14133c34396SGreg Roach $digits = '<span class="odometer">' . I18N::digits($this->page_hits) . '</span>'; 14233c34396SGreg Roach 14333c34396SGreg Roach return view('modules/hit-counter/footer', [ 14433c34396SGreg Roach 'hit_counter' => I18N::plural('This page has been viewed %s time.', 'This page has been viewed %s times.', $this->page_hits, $digits), 14533c34396SGreg Roach ]); 14633c34396SGreg Roach } 14733c34396SGreg Roach 14833c34396SGreg Roach /** 14933c34396SGreg Roach * Increment the page count. 15033c34396SGreg Roach * 15133c34396SGreg Roach * @param string $page 15233c34396SGreg Roach * @param string $parameter 15333c34396SGreg Roach * 15433c34396SGreg Roach * @return int 15533c34396SGreg Roach */ 15633c34396SGreg Roach protected function countHit($page, $parameter): int 15733c34396SGreg Roach { 158*97350b3fSGreg Roach if ($this->tree === null) { 159*97350b3fSGreg Roach return 0; 160*97350b3fSGreg Roach } 161*97350b3fSGreg Roach 16233c34396SGreg Roach $gedcom_id = $this->tree->id(); 16333c34396SGreg Roach 16433c34396SGreg Roach // Don't increment the counter while we stay on the same page. 16533c34396SGreg Roach if ( 16633c34396SGreg Roach Session::get('last_gedcom_id') === $gedcom_id && 16733c34396SGreg Roach Session::get('last_page_name') === $page && 16833c34396SGreg Roach Session::get('last_page_parameter') === $parameter 16933c34396SGreg Roach ) { 17033c34396SGreg Roach return Session::get('last_count'); 17133c34396SGreg Roach } 17233c34396SGreg Roach 17333c34396SGreg Roach $count = (int) DB::table('hit_counter') 17433c34396SGreg Roach ->where('gedcom_id', '=', $this->tree->id()) 17533c34396SGreg Roach ->where('page_name', '=', $page) 17633c34396SGreg Roach ->where('page_parameter', '=', $parameter) 1778491737aSGreg Roach ->value('page_count'); 17833c34396SGreg Roach 17933c34396SGreg Roach $count++; 18033c34396SGreg Roach 18133c34396SGreg Roach DB::table('hit_counter')->updateOrInsert([ 18233c34396SGreg Roach 'gedcom_id' => $this->tree->id(), 18333c34396SGreg Roach 'page_name' => $page, 18433c34396SGreg Roach 'page_parameter' => $parameter, 18533c34396SGreg Roach ], [ 18633c34396SGreg Roach 'page_count' => $count, 18733c34396SGreg Roach ]); 18833c34396SGreg Roach 18933c34396SGreg Roach Session::put('last_gedcom_id', $gedcom_id); 19033c34396SGreg Roach Session::put('last_page_name', $page); 19133c34396SGreg Roach Session::put('last_page_parameter', $parameter); 19233c34396SGreg Roach Session::put('last_count', $count); 19333c34396SGreg Roach 19433c34396SGreg Roach return $count; 19533c34396SGreg Roach } 19633c34396SGreg Roach} 197