1<?php 2/** 3 * webtrees: online genealogy 4 * Copyright (C) 2019 webtrees development team 5 * This program is free software: you can redistribute it and/or modify 6 * it under the terms of the GNU General Public License as published by 7 * the Free Software Foundation, either version 3 of the License, or 8 * (at your option) any later version. 9 * This program is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 * You should have received a copy of the GNU General Public License 14 * along with this program. If not, see <http://www.gnu.org/licenses/>. 15 */ 16declare(strict_types=1); 17 18namespace Fisharebest\Webtrees\Module; 19 20use Closure; 21use Fisharebest\Webtrees\Contracts\UserInterface; 22use Fisharebest\Webtrees\Http\Middleware\MiddlewareInterface; 23use Fisharebest\Webtrees\I18N; 24use Fisharebest\Webtrees\Session; 25use Fisharebest\Webtrees\Tree; 26use Illuminate\Database\Capsule\Manager as DB; 27use Symfony\Component\HttpFoundation\Request; 28use Symfony\Component\HttpFoundation\Response; 29 30/** 31 * Class HitCountFooterModule - show the number of page hits in the footer. 32 */ 33class HitCountFooterModule extends AbstractModule implements ModuleFooterInterface, MiddlewareInterface 34{ 35 use ModuleFooterTrait; 36 37 // Which pages/routes do we count? 38 // For historical reasons, we record the names of the original webtrees script and parameter. 39 protected const PAGE_NAMES = [ 40 'family' => 'family.php', 41 'individual' => 'individual.php', 42 'media' => 'mediaviewer.php', 43 'note' => 'note.php', 44 'repository' => 'repo.php', 45 'source' => 'source.php', 46 'tree-page' => 'index.php', 47 'user-page' => 'index.php', 48 ]; 49 50 /** @var int Count of visits to the current page */ 51 protected $page_hits = 0; 52 53 /** 54 * How should this module be labelled on tabs, footers, etc.? 55 * 56 * @return string 57 */ 58 public function title(): string 59 { 60 /* I18N: Name of a module */ 61 return I18N::translate('Hit counters'); 62 } 63 64 /** 65 * A sentence describing what this module does. 66 * 67 * @return string 68 */ 69 public function description(): string 70 { 71 /* I18N: Description of the “Hit counters” module */ 72 return I18N::translate('Count the visits to each page'); 73 } 74 75 /** 76 * The default position for this footer. It can be changed in the control panel. 77 * 78 * @return int 79 */ 80 public function defaultFooterOrder(): int 81 { 82 return 3; 83 } 84 85 /** 86 * A footer, to be added at the bottom of every page. 87 * 88 * @param Tree|null $tree 89 * 90 * @return string 91 */ 92 public function getFooter(?Tree $tree): string 93 { 94 if ($this->page_hits === 0) { 95 return ''; 96 } 97 98 $digits = '<span class="odometer">' . I18N::digits($this->page_hits) . '</span>'; 99 100 return view('modules/hit-counter/footer', [ 101 'hit_counter' => I18N::plural('This page has been viewed %s time.', 'This page has been viewed %s times.', $this->page_hits, $digits), 102 ]); 103 } 104 105 /** 106 * Increment the page count. 107 * 108 * @param Tree $tree 109 * @param string $page 110 * @param string $parameter 111 * 112 * @return int 113 */ 114 protected function countHit(Tree $tree, string $page, string $parameter): int 115 { 116 // Don't increment the counter while we stay on the same page. 117 if ( 118 Session::get('last_gedcom_id') === $tree->id() && 119 Session::get('last_page_name') === $page && 120 Session::get('last_page_parameter') === $parameter 121 ) { 122 return (int) Session::get('last_count'); 123 } 124 125 $count = (int) DB::table('hit_counter') 126 ->where('gedcom_id', '=', $tree->id()) 127 ->where('page_name', '=', $page) 128 ->where('page_parameter', '=', $parameter) 129 ->value('page_count'); 130 131 $count++; 132 133 DB::table('hit_counter')->updateOrInsert([ 134 'gedcom_id' => $tree->id(), 135 'page_name' => $page, 136 'page_parameter' => $parameter, 137 ], [ 138 'page_count' => $count, 139 ]); 140 141 Session::put('last_gedcom_id', $tree->id()); 142 Session::put('last_page_name', $page); 143 Session::put('last_page_parameter', $parameter); 144 Session::put('last_count', $count); 145 146 return $count; 147 } 148 149 /** 150 * @param Request $request 151 * @param Closure $next 152 * 153 * @return Response 154 */ 155 public function handle(Request $request, Closure $next): Response 156 { 157 $tree = app(Tree::class); 158 159 if ($tree instanceof Tree && $tree->getPreference('SHOW_COUNTER')) { 160 $route = $request->get('route'); 161 162 $page_name = self::PAGE_NAMES[$route] ?? ''; 163 164 switch ($route) { 165 case 'family': 166 case 'individual': 167 case 'media': 168 case 'note': 169 case 'repository': 170 case 'source': 171 $this->page_hits = $this->countHit($tree, $page_name, $request->get('xref', '')); 172 break; 173 174 case 'tree-page': 175 $this->page_hits = $this->countHit($tree, $page_name, 'gedcom:' . $tree->id()); 176 break; 177 178 case 'user-page': 179 $user = app(UserInterface::class); 180 $this->page_hits = $this->countHit($tree, $page_name, 'user:' . $user->id()); 181 break; 182 } 183 } 184 185 return $next($request); 186 } 187} 188