xref: /webtrees/app/Statistics/Repository/HitCountRepository.php (revision 6f4ec3cadc983f0a7294108c634bef48846b4311)
18add1155SRico Sonntag<?php
23976b470SGreg Roach
38add1155SRico Sonntag/**
48add1155SRico Sonntag * webtrees: online genealogy
5d11be702SGreg Roach * Copyright (C) 2023 webtrees development team
68add1155SRico Sonntag * This program is free software: you can redistribute it and/or modify
78add1155SRico Sonntag * it under the terms of the GNU General Public License as published by
88add1155SRico Sonntag * the Free Software Foundation, either version 3 of the License, or
98add1155SRico Sonntag * (at your option) any later version.
108add1155SRico Sonntag * This program is distributed in the hope that it will be useful,
118add1155SRico Sonntag * but WITHOUT ANY WARRANTY; without even the implied warranty of
128add1155SRico Sonntag * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
138add1155SRico Sonntag * GNU General Public License for more details.
148add1155SRico Sonntag * You should have received a copy of the GNU General Public License
1589f7189bSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>.
168add1155SRico Sonntag */
17fcfa147eSGreg Roach
188add1155SRico Sonntagdeclare(strict_types=1);
198add1155SRico Sonntag
208add1155SRico Sonntagnamespace Fisharebest\Webtrees\Statistics\Repository;
218add1155SRico Sonntag
228add1155SRico Sonntaguse Fisharebest\Webtrees\Auth;
23ef475b14SGreg Roachuse Fisharebest\Webtrees\Contracts\UserInterface;
24*6f4ec3caSGreg Roachuse Fisharebest\Webtrees\DB;
25e5a6b4d4SGreg Roachuse Fisharebest\Webtrees\Services\UserService;
268add1155SRico Sonntaguse Fisharebest\Webtrees\Statistics\Repository\Interfaces\HitCountRepositoryInterface;
278add1155SRico Sonntaguse Fisharebest\Webtrees\Tree;
288add1155SRico Sonntag
294c78e066SGreg Roachuse function view;
304c78e066SGreg Roach
318add1155SRico Sonntag/**
328add1155SRico Sonntag * A repository providing methods for hit count related statistics.
338add1155SRico Sonntag */
348add1155SRico Sonntagclass HitCountRepository implements HitCountRepositoryInterface
358add1155SRico Sonntag{
364c78e066SGreg Roach    private Tree $tree;
374c78e066SGreg Roach
384c78e066SGreg Roach    private UserService $user_service;
398add1155SRico Sonntag
408add1155SRico Sonntag    /**
418add1155SRico Sonntag     * @param Tree        $tree
42e5a6b4d4SGreg Roach     * @param UserService $user_service
438add1155SRico Sonntag     */
44e5a6b4d4SGreg Roach    public function __construct(Tree $tree, UserService $user_service)
458add1155SRico Sonntag    {
468add1155SRico Sonntag        $this->tree         = $tree;
47e5a6b4d4SGreg Roach        $this->user_service = $user_service;
488add1155SRico Sonntag    }
498add1155SRico Sonntag
508add1155SRico Sonntag    /**
518add1155SRico Sonntag     * These functions provide access to hitcounter for use in the HTML block.
528add1155SRico Sonntag     *
538add1155SRico Sonntag     * @param string $page_name
548add1155SRico Sonntag     * @param string $page_parameter
558add1155SRico Sonntag     *
568add1155SRico Sonntag     * @return string
578add1155SRico Sonntag     */
5824f2a3afSGreg Roach    private function hitCountQuery(string $page_name, string $page_parameter = ''): string
598add1155SRico Sonntag    {
608add1155SRico Sonntag        if ($page_name === '') {
613caaa4d2SGreg Roach            // index.php?context=gedcom
628add1155SRico Sonntag            $page_name      = 'index.php';
638add1155SRico Sonntag            $page_parameter = 'gedcom:' . $this->tree->id();
648add1155SRico Sonntag        } elseif ($page_name === 'index.php') {
653caaa4d2SGreg Roach            // index.php?context=user
66e5a6b4d4SGreg Roach            $user           = $this->user_service->findByIdentifier($page_parameter);
67ef475b14SGreg Roach            $page_parameter = 'user:' . ($user instanceof UserInterface ? $user->id() : Auth::id());
688add1155SRico Sonntag        }
698add1155SRico Sonntag
708add1155SRico Sonntag        $count = (int) DB::table('hit_counter')
718add1155SRico Sonntag            ->where('gedcom_id', '=', $this->tree->id())
728add1155SRico Sonntag            ->where('page_name', '=', $page_name)
738add1155SRico Sonntag            ->where('page_parameter', '=', $page_parameter)
748add1155SRico Sonntag            ->value('page_count');
758add1155SRico Sonntag
768add1155SRico Sonntag        return view(
778add1155SRico Sonntag            'statistics/hit-count',
788add1155SRico Sonntag            [
798add1155SRico Sonntag                'count' => $count,
808add1155SRico Sonntag            ]
818add1155SRico Sonntag        );
828add1155SRico Sonntag    }
838add1155SRico Sonntag
848add1155SRico Sonntag    /**
850dcd9387SGreg Roach     * @param string $page_parameter
860dcd9387SGreg Roach     *
870dcd9387SGreg Roach     * @return string
888add1155SRico Sonntag     */
898add1155SRico Sonntag    public function hitCount(string $page_parameter = ''): string
908add1155SRico Sonntag    {
918add1155SRico Sonntag        return $this->hitCountQuery('', $page_parameter);
928add1155SRico Sonntag    }
938add1155SRico Sonntag
948add1155SRico Sonntag    /**
950dcd9387SGreg Roach     * @param string $page_parameter
960dcd9387SGreg Roach     *
970dcd9387SGreg Roach     * @return string
988add1155SRico Sonntag     */
998add1155SRico Sonntag    public function hitCountUser(string $page_parameter = ''): string
1008add1155SRico Sonntag    {
1018add1155SRico Sonntag        return $this->hitCountQuery('index.php', $page_parameter);
1028add1155SRico Sonntag    }
1038add1155SRico Sonntag
1048add1155SRico Sonntag    /**
1050dcd9387SGreg Roach     * @param string $page_parameter
1060dcd9387SGreg Roach     *
1070dcd9387SGreg Roach     * @return string
1088add1155SRico Sonntag     */
1098add1155SRico Sonntag    public function hitCountIndi(string $page_parameter = ''): string
1108add1155SRico Sonntag    {
1118add1155SRico Sonntag        return $this->hitCountQuery('individual.php', $page_parameter);
1128add1155SRico Sonntag    }
1138add1155SRico Sonntag
1148add1155SRico Sonntag    /**
1150dcd9387SGreg Roach     * @param string $page_parameter
1160dcd9387SGreg Roach     *
1170dcd9387SGreg Roach     * @return string
1188add1155SRico Sonntag     */
1198add1155SRico Sonntag    public function hitCountFam(string $page_parameter = ''): string
1208add1155SRico Sonntag    {
1218add1155SRico Sonntag        return $this->hitCountQuery('family.php', $page_parameter);
1228add1155SRico Sonntag    }
1238add1155SRico Sonntag
1248add1155SRico Sonntag    /**
1250dcd9387SGreg Roach     * @param string $page_parameter
1260dcd9387SGreg Roach     *
1270dcd9387SGreg Roach     * @return string
1288add1155SRico Sonntag     */
1298add1155SRico Sonntag    public function hitCountSour(string $page_parameter = ''): string
1308add1155SRico Sonntag    {
1318add1155SRico Sonntag        return $this->hitCountQuery('source.php', $page_parameter);
1328add1155SRico Sonntag    }
1338add1155SRico Sonntag
1348add1155SRico Sonntag    /**
1350dcd9387SGreg Roach     * @param string $page_parameter
1360dcd9387SGreg Roach     *
1370dcd9387SGreg Roach     * @return string
1388add1155SRico Sonntag     */
1398add1155SRico Sonntag    public function hitCountRepo(string $page_parameter = ''): string
1408add1155SRico Sonntag    {
1418add1155SRico Sonntag        return $this->hitCountQuery('repo.php', $page_parameter);
1428add1155SRico Sonntag    }
1438add1155SRico Sonntag
1448add1155SRico Sonntag    /**
1450dcd9387SGreg Roach     * @param string $page_parameter
1460dcd9387SGreg Roach     *
1470dcd9387SGreg Roach     * @return string
1488add1155SRico Sonntag     */
1498add1155SRico Sonntag    public function hitCountNote(string $page_parameter = ''): string
1508add1155SRico Sonntag    {
1518add1155SRico Sonntag        return $this->hitCountQuery('note.php', $page_parameter);
1528add1155SRico Sonntag    }
1538add1155SRico Sonntag
1548add1155SRico Sonntag    /**
1550dcd9387SGreg Roach     * @param string $page_parameter
1560dcd9387SGreg Roach     *
1570dcd9387SGreg Roach     * @return string
1588add1155SRico Sonntag     */
1598add1155SRico Sonntag    public function hitCountObje(string $page_parameter = ''): string
1608add1155SRico Sonntag    {
1618add1155SRico Sonntag        return $this->hitCountQuery('mediaviewer.php', $page_parameter);
1628add1155SRico Sonntag    }
1638add1155SRico Sonntag}
164