xref: /webtrees/app/Module/HitCountFooterModule.php (revision a992e8c1f5cff3fba48e8b69ec698e4e21142dc8)
133c34396SGreg Roach<?php
23976b470SGreg Roach
333c34396SGreg Roach/**
433c34396SGreg Roach * webtrees: online genealogy
533c34396SGreg Roach * Copyright (C) 2019 webtrees development team
633c34396SGreg Roach * This program is free software: you can redistribute it and/or modify
733c34396SGreg Roach * it under the terms of the GNU General Public License as published by
833c34396SGreg Roach * the Free Software Foundation, either version 3 of the License, or
933c34396SGreg Roach * (at your option) any later version.
1033c34396SGreg Roach * This program is distributed in the hope that it will be useful,
1133c34396SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
1233c34396SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1333c34396SGreg Roach * GNU General Public License for more details.
1433c34396SGreg Roach * You should have received a copy of the GNU General Public License
1533c34396SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
1633c34396SGreg Roach */
1733c34396SGreg Roachdeclare(strict_types=1);
1833c34396SGreg Roach
1933c34396SGreg Roachnamespace Fisharebest\Webtrees\Module;
2033c34396SGreg Roach
21e5a6b4d4SGreg Roachuse Fisharebest\Webtrees\Contracts\UserInterface;
2233c34396SGreg Roachuse Fisharebest\Webtrees\I18N;
2333c34396SGreg Roachuse Fisharebest\Webtrees\Session;
2433c34396SGreg Roachuse Fisharebest\Webtrees\Tree;
2533c34396SGreg Roachuse Illuminate\Database\Capsule\Manager as DB;
266ccdf4f0SGreg Roachuse Psr\Http\Message\ResponseInterface;
276ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
286ccdf4f0SGreg Roachuse Psr\Http\Server\MiddlewareInterface;
296ccdf4f0SGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
3033c34396SGreg Roach
3133c34396SGreg Roach/**
320c8c69d4SGreg Roach * Class HitCountFooterModule - show the number of page hits in the footer.
3333c34396SGreg Roach */
340c8c69d4SGreg Roachclass HitCountFooterModule extends AbstractModule implements ModuleFooterInterface, MiddlewareInterface
3533c34396SGreg Roach{
3633c34396SGreg Roach    use ModuleFooterTrait;
3733c34396SGreg Roach
3833c34396SGreg Roach    // Which pages/routes do we count?
3933c34396SGreg Roach    // For historical reasons, we record the names of the original webtrees script and parameter.
4033c34396SGreg Roach    protected const PAGE_NAMES = [
4133c34396SGreg Roach        'family'     => 'family.php',
4233c34396SGreg Roach        'individual' => 'individual.php',
4333c34396SGreg Roach        'media'      => 'mediaviewer.php',
4433c34396SGreg Roach        'note'       => 'note.php',
4533c34396SGreg Roach        'repository' => 'repo.php',
4633c34396SGreg Roach        'source'     => 'source.php',
4733c34396SGreg Roach        'tree-page'  => 'index.php',
4833c34396SGreg Roach        'user-page'  => 'index.php',
4933c34396SGreg Roach    ];
5033c34396SGreg Roach
510c8c69d4SGreg Roach    /** @var int Count of visits to the current page */
5233c34396SGreg Roach    protected $page_hits = 0;
5333c34396SGreg Roach
5433c34396SGreg Roach    /**
55d4c04956SGreg Roach     * How should this module be labelled on tabs, footers, etc.?
56d4c04956SGreg Roach     *
57d4c04956SGreg Roach     * @return string
58d4c04956SGreg Roach     */
59d4c04956SGreg Roach    public function title(): string
60d4c04956SGreg Roach    {
61d4c04956SGreg Roach        /* I18N: Name of a module */
62d4c04956SGreg Roach        return I18N::translate('Hit counters');
63d4c04956SGreg Roach    }
64d4c04956SGreg Roach
65d4c04956SGreg Roach    /**
66d4c04956SGreg Roach     * A sentence describing what this module does.
67d4c04956SGreg Roach     *
68d4c04956SGreg Roach     * @return string
69d4c04956SGreg Roach     */
70d4c04956SGreg Roach    public function description(): string
71d4c04956SGreg Roach    {
72d4c04956SGreg Roach        /* I18N: Description of the “Hit counters” module */
73d4c04956SGreg Roach        return I18N::translate('Count the visits to each page');
74d4c04956SGreg Roach    }
75d4c04956SGreg Roach
76d4c04956SGreg Roach    /**
7733c34396SGreg Roach     * The default position for this footer.  It can be changed in the control panel.
7833c34396SGreg Roach     *
7933c34396SGreg Roach     * @return int
8033c34396SGreg Roach     */
8133c34396SGreg Roach    public function defaultFooterOrder(): int
8233c34396SGreg Roach    {
8333c34396SGreg Roach        return 3;
8433c34396SGreg Roach    }
8533c34396SGreg Roach
8633c34396SGreg Roach    /**
8733c34396SGreg Roach     * A footer, to be added at the bottom of every page.
8833c34396SGreg Roach     *
89*a992e8c1SGreg Roach     * @param ServerRequestInterface $request
900c8c69d4SGreg Roach     *
9133c34396SGreg Roach     * @return string
9233c34396SGreg Roach     */
93*a992e8c1SGreg Roach    public function getFooter(ServerRequestInterface $request): string
9433c34396SGreg Roach    {
950c8c69d4SGreg Roach        if ($this->page_hits === 0) {
9633c34396SGreg Roach            return '';
9733c34396SGreg Roach        }
9833c34396SGreg Roach
9933c34396SGreg Roach        $digits = '<span class="odometer">' . I18N::digits($this->page_hits) . '</span>';
10033c34396SGreg Roach
10133c34396SGreg Roach        return view('modules/hit-counter/footer', [
10233c34396SGreg Roach            'hit_counter' => I18N::plural('This page has been viewed %s time.', 'This page has been viewed %s times.', $this->page_hits, $digits),
10333c34396SGreg Roach        ]);
10433c34396SGreg Roach    }
10533c34396SGreg Roach
10633c34396SGreg Roach    /**
1076ccdf4f0SGreg Roach     * @param ServerRequestInterface  $request
1086ccdf4f0SGreg Roach     * @param RequestHandlerInterface $handler
1096ccdf4f0SGreg Roach     *
1106ccdf4f0SGreg Roach     * @return ResponseInterface
1116ccdf4f0SGreg Roach     */
1126ccdf4f0SGreg Roach    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
1136ccdf4f0SGreg Roach    {
1146ccdf4f0SGreg Roach        $tree = app(Tree::class);
1156ccdf4f0SGreg Roach
1166ccdf4f0SGreg Roach        if ($tree instanceof Tree && $tree->getPreference('SHOW_COUNTER')) {
1174f2a4a62SGreg Roach            $route = $request->getQueryParams()['route'] ?? '';
1186ccdf4f0SGreg Roach
1196ccdf4f0SGreg Roach            $page_name = self::PAGE_NAMES[$route] ?? '';
1206ccdf4f0SGreg Roach
1216ccdf4f0SGreg Roach            switch ($route) {
1226ccdf4f0SGreg Roach                case 'family':
1236ccdf4f0SGreg Roach                case 'individual':
1246ccdf4f0SGreg Roach                case 'media':
1256ccdf4f0SGreg Roach                case 'note':
1266ccdf4f0SGreg Roach                case 'repository':
1276ccdf4f0SGreg Roach                case 'source':
1284f2a4a62SGreg Roach                    $this->page_hits = $this->countHit($tree, $page_name, $request->getQueryParams()['xref'] ?? '');
1296ccdf4f0SGreg Roach                    break;
1306ccdf4f0SGreg Roach
1316ccdf4f0SGreg Roach                case 'tree-page':
1326ccdf4f0SGreg Roach                    $this->page_hits = $this->countHit($tree, $page_name, 'gedcom:' . $tree->id());
1336ccdf4f0SGreg Roach                    break;
1346ccdf4f0SGreg Roach
1356ccdf4f0SGreg Roach                case 'user-page':
1366ccdf4f0SGreg Roach                    $user            = app(UserInterface::class);
1376ccdf4f0SGreg Roach                    $this->page_hits = $this->countHit($tree, $page_name, 'user:' . $user->id());
1386ccdf4f0SGreg Roach                    break;
1396ccdf4f0SGreg Roach            }
1406ccdf4f0SGreg Roach        }
1416ccdf4f0SGreg Roach
1426ccdf4f0SGreg Roach        return $handler->handle($request);
1436ccdf4f0SGreg Roach    }
1446ccdf4f0SGreg Roach
1456ccdf4f0SGreg Roach    /**
14633c34396SGreg Roach     * Increment the page count.
14733c34396SGreg Roach     *
1480c8c69d4SGreg Roach     * @param Tree   $tree
14933c34396SGreg Roach     * @param string $page
15033c34396SGreg Roach     * @param string $parameter
15133c34396SGreg Roach     *
15233c34396SGreg Roach     * @return int
15333c34396SGreg Roach     */
1540c8c69d4SGreg Roach    protected function countHit(Tree $tree, string $page, string $parameter): int
15533c34396SGreg Roach    {
15633c34396SGreg Roach        // Don't increment the counter while we stay on the same page.
15733c34396SGreg Roach        if (
15833c34396SGreg Roach            Session::get('last_page_name') === $page &&
159*a992e8c1SGreg Roach            Session::get('last_page_parameter') === $parameter &&
160*a992e8c1SGreg Roach            Session::get('last_gedcom_id') === $tree->id()
16133c34396SGreg Roach        ) {
1620c8c69d4SGreg Roach            return (int) Session::get('last_count');
16333c34396SGreg Roach        }
16433c34396SGreg Roach
16533c34396SGreg Roach        $count = (int) DB::table('hit_counter')
1660c8c69d4SGreg Roach            ->where('gedcom_id', '=', $tree->id())
16733c34396SGreg Roach            ->where('page_name', '=', $page)
16833c34396SGreg Roach            ->where('page_parameter', '=', $parameter)
1698491737aSGreg Roach            ->value('page_count');
17033c34396SGreg Roach
17133c34396SGreg Roach        $count++;
17233c34396SGreg Roach
17333c34396SGreg Roach        DB::table('hit_counter')->updateOrInsert([
1740c8c69d4SGreg Roach            'gedcom_id'      => $tree->id(),
17533c34396SGreg Roach            'page_name'      => $page,
17633c34396SGreg Roach            'page_parameter' => $parameter,
17733c34396SGreg Roach        ], [
17833c34396SGreg Roach            'page_count' => $count,
17933c34396SGreg Roach        ]);
18033c34396SGreg Roach
1810c8c69d4SGreg Roach        Session::put('last_gedcom_id', $tree->id());
18233c34396SGreg Roach        Session::put('last_page_name', $page);
18333c34396SGreg Roach        Session::put('last_page_parameter', $parameter);
18433c34396SGreg Roach        Session::put('last_count', $count);
18533c34396SGreg Roach
18633c34396SGreg Roach        return $count;
18733c34396SGreg Roach    }
18833c34396SGreg Roach}
189