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