1<?php 2/** 3 * webtrees: online genealogy 4 * Copyright (C) 2018 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\Statistics\Repository; 19 20use Fisharebest\Webtrees\Auth; 21use Fisharebest\Webtrees\Statistics\Repository\Interfaces\HitCountRepositoryInterface; 22use Fisharebest\Webtrees\Tree; 23use Fisharebest\Webtrees\User; 24use Illuminate\Database\Capsule\Manager as DB; 25 26/** 27 * A repository providing methods for hit count related statistics. 28 */ 29class HitCountRepository implements HitCountRepositoryInterface 30{ 31 /** 32 * @var Tree 33 */ 34 private $tree; 35 36 /** 37 * Constructor. 38 * 39 * @param Tree $tree 40 */ 41 public function __construct(Tree $tree) 42 { 43 $this->tree = $tree; 44 } 45 46 /** 47 * These functions provide access to hitcounter for use in the HTML block. 48 * 49 * @param string $page_name 50 * @param string $page_parameter 51 * 52 * @return string 53 */ 54 private function hitCountQuery($page_name, string $page_parameter = ''): string 55 { 56 if ($page_name === '') { 57 // index.php?ctype=gedcom 58 $page_name = 'index.php'; 59 $page_parameter = 'gedcom:' . $this->tree->id(); 60 } elseif ($page_name === 'index.php') { 61 // index.php?ctype=user 62 $user = User::findByIdentifier($page_parameter); 63 $page_parameter = 'user:' . ($user ? $user->id() : Auth::id()); 64 } 65 66 $count = (int) DB::table('hit_counter') 67 ->where('gedcom_id', '=', $this->tree->id()) 68 ->where('page_name', '=', $page_name) 69 ->where('page_parameter', '=', $page_parameter) 70 ->value('page_count'); 71 72 return view( 73 'statistics/hit-count', 74 [ 75 'count' => $count, 76 ] 77 ); 78 } 79 80 /** 81 * @inheritDoc 82 */ 83 public function hitCount(string $page_parameter = ''): string 84 { 85 return $this->hitCountQuery('', $page_parameter); 86 } 87 88 /** 89 * @inheritDoc 90 */ 91 public function hitCountUser(string $page_parameter = ''): string 92 { 93 return $this->hitCountQuery('index.php', $page_parameter); 94 } 95 96 /** 97 * @inheritDoc 98 */ 99 public function hitCountIndi(string $page_parameter = ''): string 100 { 101 return $this->hitCountQuery('individual.php', $page_parameter); 102 } 103 104 /** 105 * @inheritDoc 106 */ 107 public function hitCountFam(string $page_parameter = ''): string 108 { 109 return $this->hitCountQuery('family.php', $page_parameter); 110 } 111 112 /** 113 * @inheritDoc 114 */ 115 public function hitCountSour(string $page_parameter = ''): string 116 { 117 return $this->hitCountQuery('source.php', $page_parameter); 118 } 119 120 /** 121 * @inheritDoc 122 */ 123 public function hitCountRepo(string $page_parameter = ''): string 124 { 125 return $this->hitCountQuery('repo.php', $page_parameter); 126 } 127 128 /** 129 * @inheritDoc 130 */ 131 public function hitCountNote(string $page_parameter = ''): string 132 { 133 return $this->hitCountQuery('note.php', $page_parameter); 134 } 135 136 /** 137 * @inheritDoc 138 */ 139 public function hitCountObje(string $page_parameter = ''): string 140 { 141 return $this->hitCountQuery('mediaviewer.php', $page_parameter); 142 } 143} 144