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