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