xref: /webtrees/app/Module/HitCountFooterModule.php (revision 4a1f1d433794f86f893b59883147e57af4e94374)
1<?php
2/**
3 * webtrees: online genealogy
4 * Copyright (C) 2019 webtrees development team
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16declare(strict_types=1);
17
18namespace Fisharebest\Webtrees\Module;
19
20use Fisharebest\Webtrees\Contracts\UserInterface;
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 pages/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 Tree|null $tree
89     *
90     * @return string
91     */
92    public function getFooter(?Tree $tree): 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        $tree = app(Tree::class);
114
115        if ($tree instanceof Tree && $tree->getPreference('SHOW_COUNTER')) {
116            $route = $request->getQueryParams()['route'] ?? '';
117
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                    $user            = app(UserInterface::class);
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_gedcom_id') === $tree->id() &&
158            Session::get('last_page_name') === $page &&
159            Session::get('last_page_parameter') === $parameter
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