xref: /webtrees/app/Statistics/Repository/HitCountRepository.php (revision f7ab47b1fce1949e7b44a4f7d92d527a70deb743)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2019 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 <http://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($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     * @inheritDoc
90     */
91    public function hitCount(string $page_parameter = ''): string
92    {
93        return $this->hitCountQuery('', $page_parameter);
94    }
95
96    /**
97     * @inheritDoc
98     */
99    public function hitCountUser(string $page_parameter = ''): string
100    {
101        return $this->hitCountQuery('index.php', $page_parameter);
102    }
103
104    /**
105     * @inheritDoc
106     */
107    public function hitCountIndi(string $page_parameter = ''): string
108    {
109        return $this->hitCountQuery('individual.php', $page_parameter);
110    }
111
112    /**
113     * @inheritDoc
114     */
115    public function hitCountFam(string $page_parameter = ''): string
116    {
117        return $this->hitCountQuery('family.php', $page_parameter);
118    }
119
120    /**
121     * @inheritDoc
122     */
123    public function hitCountSour(string $page_parameter = ''): string
124    {
125        return $this->hitCountQuery('source.php', $page_parameter);
126    }
127
128    /**
129     * @inheritDoc
130     */
131    public function hitCountRepo(string $page_parameter = ''): string
132    {
133        return $this->hitCountQuery('repo.php', $page_parameter);
134    }
135
136    /**
137     * @inheritDoc
138     */
139    public function hitCountNote(string $page_parameter = ''): string
140    {
141        return $this->hitCountQuery('note.php', $page_parameter);
142    }
143
144    /**
145     * @inheritDoc
146     */
147    public function hitCountObje(string $page_parameter = ''): string
148    {
149        return $this->hitCountQuery('mediaviewer.php', $page_parameter);
150    }
151}
152