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