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