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