xref: /webtrees/app/Module/HitCountFooterModule.php (revision 1afbbc50a65bb73de1d13dc18d669efe80119058)
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\Http\RequestHandlers\FamilyPage;
23use Fisharebest\Webtrees\Http\RequestHandlers\IndividualPage;
24use Fisharebest\Webtrees\Http\RequestHandlers\MediaPage;
25use Fisharebest\Webtrees\Http\RequestHandlers\NotePage;
26use Fisharebest\Webtrees\Http\RequestHandlers\RepositoryPage;
27use Fisharebest\Webtrees\Http\RequestHandlers\SourcePage;
28use Fisharebest\Webtrees\I18N;
29use Fisharebest\Webtrees\Session;
30use Fisharebest\Webtrees\Tree;
31use Illuminate\Database\Capsule\Manager as DB;
32use Psr\Http\Message\ResponseInterface;
33use Psr\Http\Message\ServerRequestInterface;
34use Psr\Http\Server\MiddlewareInterface;
35use Psr\Http\Server\RequestHandlerInterface;
36
37/**
38 * Class HitCountFooterModule - show the number of page hits in the footer.
39 */
40class HitCountFooterModule extends AbstractModule implements ModuleFooterInterface, MiddlewareInterface
41{
42    use ModuleFooterTrait;
43
44    // Which routes do we count?
45    // For historical reasons, we record the names of the original webtrees script and parameter.
46    protected const PAGE_NAMES = [
47        FamilyPage::class     => 'family.php',
48        IndividualPage::class => 'individual.php',
49        MediaPage::class      => 'mediaviewer.php',
50        NotePage::class       => 'note.php',
51        RepositoryPage::class => 'repo.php',
52        SourcePage::class     => 'source.php',
53        'tree-page'  => 'index.php',
54        'user-page'  => 'index.php',
55    ];
56
57    /** @var int Count of visits to the current page */
58    protected $page_hits = 0;
59
60    /**
61     * How should this module be labelled on tabs, footers, etc.?
62     *
63     * @return string
64     */
65    public function title(): string
66    {
67        /* I18N: Name of a module */
68        return I18N::translate('Hit counters');
69    }
70
71    /**
72     * A sentence describing what this module does.
73     *
74     * @return string
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 = $request->getAttribute('route');
121        $tree  = $request->getAttribute('tree');
122        $user  = $request->getAttribute('user');
123
124        if ($tree instanceof Tree && $tree->getPreference('SHOW_COUNTER')) {
125            $page_name = self::PAGE_NAMES[$route] ?? '';
126
127            switch ($route) {
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                    $this->page_hits = $this->countHit($tree, $page_name, $request->getQueryParams()['xref'] ?? '');
135                    break;
136
137                case 'tree-page':
138                    $this->page_hits = $this->countHit($tree, $page_name, 'gedcom:' . $tree->id());
139                    break;
140
141                case 'user-page':
142                    $this->page_hits = $this->countHit($tree, $page_name, 'user:' . $user->id());
143                    break;
144            }
145        }
146
147        return $handler->handle($request);
148    }
149
150    /**
151     * Increment the page count.
152     *
153     * @param Tree   $tree
154     * @param string $page
155     * @param string $parameter
156     *
157     * @return int
158     */
159    protected function countHit(Tree $tree, string $page, string $parameter): int
160    {
161        // Don't increment the counter while we stay on the same page.
162        if (
163            Session::get('last_page_name') === $page &&
164            Session::get('last_page_parameter') === $parameter &&
165            Session::get('last_gedcom_id') === $tree->id()
166        ) {
167            return (int) Session::get('last_count');
168        }
169
170        $count = (int) DB::table('hit_counter')
171            ->where('gedcom_id', '=', $tree->id())
172            ->where('page_name', '=', $page)
173            ->where('page_parameter', '=', $parameter)
174            ->value('page_count');
175
176        $count++;
177
178        DB::table('hit_counter')->updateOrInsert([
179            'gedcom_id'      => $tree->id(),
180            'page_name'      => $page,
181            'page_parameter' => $parameter,
182        ], [
183            'page_count' => $count,
184        ]);
185
186        Session::put('last_gedcom_id', $tree->id());
187        Session::put('last_page_name', $page);
188        Session::put('last_page_parameter', $parameter);
189        Session::put('last_count', $count);
190
191        return $count;
192    }
193}
194