xref: /webtrees/app/Module/HitCountFooterModule.php (revision f6f7bcfffb8de388b89285e219c3c8090e3d9e52)
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 Closure;
21use Fisharebest\Webtrees\Contracts\UserInterface;
22use Fisharebest\Webtrees\Http\Middleware\MiddlewareInterface;
23use Fisharebest\Webtrees\I18N;
24use Fisharebest\Webtrees\Session;
25use Fisharebest\Webtrees\Tree;
26use Fisharebest\Webtrees\User;
27use Illuminate\Database\Capsule\Manager as DB;
28use Symfony\Component\HttpFoundation\Request;
29use Symfony\Component\HttpFoundation\Response;
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 pages/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 Tree|null $tree
90     *
91     * @return string
92     */
93    public function getFooter(?Tree $tree): 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     * Increment the page count.
108     *
109     * @param Tree   $tree
110     * @param string $page
111     * @param string $parameter
112     *
113     * @return int
114     */
115    protected function countHit(Tree $tree, string $page, string $parameter): int
116    {
117        // Don't increment the counter while we stay on the same page.
118        if (
119            Session::get('last_gedcom_id') === $tree->id() &&
120            Session::get('last_page_name') === $page &&
121            Session::get('last_page_parameter') === $parameter
122        ) {
123            return (int) Session::get('last_count');
124        }
125
126        $count = (int) DB::table('hit_counter')
127            ->where('gedcom_id', '=', $tree->id())
128            ->where('page_name', '=', $page)
129            ->where('page_parameter', '=', $parameter)
130            ->value('page_count');
131
132        $count++;
133
134        DB::table('hit_counter')->updateOrInsert([
135            'gedcom_id'      => $tree->id(),
136            'page_name'      => $page,
137            'page_parameter' => $parameter,
138        ], [
139            'page_count' => $count,
140        ]);
141
142        Session::put('last_gedcom_id', $tree->id());
143        Session::put('last_page_name', $page);
144        Session::put('last_page_parameter', $parameter);
145        Session::put('last_count', $count);
146
147        return $count;
148    }
149
150    /**
151     * @param Request $request
152     * @param Closure $next
153     *
154     * @return Response
155     */
156    public function handle(Request $request, Closure $next): Response
157    {
158        $tree = app()->make(Tree::class);
159
160        if ($tree instanceof Tree && $tree->getPreference('SHOW_COUNTER')) {
161            $route = $request->get('route');
162
163            $page_name = self::PAGE_NAMES[$route] ?? '';
164
165            switch ($route) {
166                case 'family':
167                case 'individual':
168                case 'media':
169                case 'note':
170                case 'repository':
171                case 'source':
172                    $this->page_hits = $this->countHit($tree, $page_name, $request->get('xref', ''));
173                    break;
174
175                case 'tree-page':
176                    $this->page_hits = $this->countHit($tree, $page_name, 'gedcom:' . $tree->id());
177                    break;
178
179                case 'user-page':
180                    $user = app()->make(UserInterface::class);
181                    $this->page_hits = $this->countHit($tree, $page_name, 'user:' . $user->id());
182                    break;
183            }
184        }
185
186        return $next($request);
187    }
188}
189