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