xref: /webtrees/app/Module/HitCountFooterModule.php (revision 5f52b64188c4f443eebeb985bb53134753a7560f)
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\Module;
21
22use Fisharebest\Webtrees\DB;
23use Fisharebest\Webtrees\Http\RequestHandlers\FamilyPage;
24use Fisharebest\Webtrees\Http\RequestHandlers\IndividualPage;
25use Fisharebest\Webtrees\Http\RequestHandlers\MediaPage;
26use Fisharebest\Webtrees\Http\RequestHandlers\NotePage;
27use Fisharebest\Webtrees\Http\RequestHandlers\RepositoryPage;
28use Fisharebest\Webtrees\Http\RequestHandlers\SourcePage;
29use Fisharebest\Webtrees\Http\RequestHandlers\SubmitterPage;
30use Fisharebest\Webtrees\Http\RequestHandlers\TreePage;
31use Fisharebest\Webtrees\Http\RequestHandlers\UserPage;
32use Fisharebest\Webtrees\I18N;
33use Fisharebest\Webtrees\Session;
34use Fisharebest\Webtrees\Tree;
35use Fisharebest\Webtrees\Validator;
36use Psr\Http\Message\ResponseInterface;
37use Psr\Http\Message\ServerRequestInterface;
38use Psr\Http\Server\MiddlewareInterface;
39use Psr\Http\Server\RequestHandlerInterface;
40
41/**
42 * Class HitCountFooterModule - show the number of page hits in the footer.
43 */
44class HitCountFooterModule extends AbstractModule implements ModuleFooterInterface, MiddlewareInterface
45{
46    use ModuleFooterTrait;
47
48    // Which routes do we count?
49    // For historical reasons, we record the names of the original webtrees script and parameter.
50    protected const PAGE_NAMES = [
51        FamilyPage::class     => 'family.php',
52        IndividualPage::class => 'individual.php',
53        MediaPage::class      => 'mediaviewer.php',
54        NotePage::class       => 'note.php',
55        RepositoryPage::class => 'repo.php',
56        SourcePage::class     => 'source.php',
57        SubmitterPage::class  => 'submitter',
58        TreePage::class       => 'index.php',
59        UserPage::class       => 'index.php',
60    ];
61
62    // Count of visits to the current page
63    protected int $page_hits = 0;
64
65    /**
66     * How should this module be labelled on tabs, footers, etc.?
67     *
68     * @return string
69     */
70    public function title(): string
71    {
72        /* I18N: Name of a module */
73        return I18N::translate('Hit counters');
74    }
75
76    public function description(): string
77    {
78        /* I18N: Description of the “Hit counters” module */
79        return I18N::translate('Count the visits to each page');
80    }
81
82    /**
83     * The default position for this footer.  It can be changed in the control panel.
84     *
85     * @return int
86     */
87    public function defaultFooterOrder(): int
88    {
89        return 3;
90    }
91
92    /**
93     * A footer, to be added at the bottom of every page.
94     *
95     * @param ServerRequestInterface $request
96     *
97     * @return string
98     */
99    public function getFooter(ServerRequestInterface $request): string
100    {
101        if ($this->page_hits === 0) {
102            return '';
103        }
104
105        $digits = '<span class="odometer">' . I18N::digits($this->page_hits) . '</span>';
106
107        return view('modules/hit-counter/footer', [
108            'hit_counter' => I18N::plural('This page has been viewed %s time.', 'This page has been viewed %s times.', $this->page_hits, $digits),
109        ]);
110    }
111
112    /**
113     * @param ServerRequestInterface  $request
114     * @param RequestHandlerInterface $handler
115     *
116     * @return ResponseInterface
117     */
118    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
119    {
120        $route = Validator::attributes($request)->route();
121        $tree  = Validator::attributes($request)->treeOptional();
122        $user  = Validator::attributes($request)->user();
123
124        if ($tree instanceof Tree && $tree->getPreference('SHOW_COUNTER')) {
125            $page_name = self::PAGE_NAMES[$route->name] ?? '';
126
127            switch ($route->name) {
128                case FamilyPage::class:
129                case IndividualPage::class:
130                case MediaPage::class:
131                case NotePage::class:
132                case RepositoryPage::class:
133                case SourcePage::class:
134                case SubmitterPage::class:
135                    $xref = Validator::attributes($request)->isXref()->string('xref');
136                    $this->page_hits = $this->countHit($tree, $page_name, $xref);
137                    break;
138
139                case TreePage::class:
140                    $this->page_hits = $this->countHit($tree, $page_name, 'gedcom:' . $tree->id());
141                    break;
142
143                case UserPage::class:
144                    $this->page_hits = $this->countHit($tree, $page_name, 'user:' . $user->id());
145                    break;
146            }
147        }
148
149        return $handler->handle($request);
150    }
151
152    /**
153     * Increment the page count.
154     *
155     * @param Tree   $tree
156     * @param string $page
157     * @param string $parameter
158     *
159     * @return int
160     */
161    protected function countHit(Tree $tree, string $page, string $parameter): int
162    {
163        // Don't increment the counter while we stay on the same page.
164        if (
165            Session::get('last_page_name') === $page &&
166            Session::get('last_page_parameter') === $parameter &&
167            Session::get('last_gedcom_id') === $tree->id()
168        ) {
169            return (int) Session::get('last_count');
170        }
171
172        $count = (int) DB::table('hit_counter')
173            ->where('gedcom_id', '=', $tree->id())
174            ->where('page_name', '=', $page)
175            ->where('page_parameter', '=', $parameter)
176            ->value('page_count');
177
178        $count++;
179
180        DB::table('hit_counter')->updateOrInsert([
181            'gedcom_id'      => $tree->id(),
182            'page_name'      => $page,
183            'page_parameter' => $parameter,
184        ], [
185            'page_count' => $count,
186        ]);
187
188        Session::put('last_gedcom_id', $tree->id());
189        Session::put('last_page_name', $page);
190        Session::put('last_page_parameter', $parameter);
191        Session::put('last_count', $count);
192
193        return $count;
194    }
195}
196