xref: /webtrees/app/Module/ModuleAnalyticsTrait.php (revision 962ead5100d09d236084c90178436e83af34e4b2)
137eb8894SGreg Roach<?php
23976b470SGreg Roach
337eb8894SGreg Roach/**
437eb8894SGreg Roach * webtrees: online genealogy
537eb8894SGreg Roach * Copyright (C) 2019 webtrees development team
637eb8894SGreg Roach * This program is free software: you can redistribute it and/or modify
737eb8894SGreg Roach * it under the terms of the GNU General Public License as published by
837eb8894SGreg Roach * the Free Software Foundation, either version 3 of the License, or
937eb8894SGreg Roach * (at your option) any later version.
1037eb8894SGreg Roach * This program is distributed in the hope that it will be useful,
1137eb8894SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
1237eb8894SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1337eb8894SGreg Roach * GNU General Public License for more details.
1437eb8894SGreg Roach * You should have received a copy of the GNU General Public License
1537eb8894SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
1637eb8894SGreg Roach */
1737eb8894SGreg Roachdeclare(strict_types=1);
1837eb8894SGreg Roach
1937eb8894SGreg Roachnamespace Fisharebest\Webtrees\Module;
2037eb8894SGreg Roach
216ccdf4f0SGreg Roachuse Fig\Http\Message\StatusCodeInterface;
2237eb8894SGreg Roachuse Fisharebest\Webtrees\I18N;
236ccdf4f0SGreg Roachuse Psr\Http\Message\ResponseInterface;
246ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
253976b470SGreg Roach
266ccdf4f0SGreg Roachuse function app;
2737eb8894SGreg Roach
2837eb8894SGreg Roach/**
2937eb8894SGreg Roach * Trait ModuleAnalyticsTrait - default implementation of ModuleAnalyticsInterface
3037eb8894SGreg Roach */
3137eb8894SGreg Roachtrait ModuleAnalyticsTrait
3237eb8894SGreg Roach{
3337eb8894SGreg Roach    /**
34*962ead51SGreg Roach     * @return string
35*962ead51SGreg Roach     */
36*962ead51SGreg Roach    abstract public function name(): string;
37*962ead51SGreg Roach
38*962ead51SGreg Roach    /**
3937eb8894SGreg Roach     * Should we add this tracker?
4037eb8894SGreg Roach     *
4137eb8894SGreg Roach     * @return bool
4237eb8894SGreg Roach     */
4337eb8894SGreg Roach    public function analyticsCanShow(): bool
4437eb8894SGreg Roach    {
456ccdf4f0SGreg Roach        $request = app(ServerRequestInterface::class);
46abafa13cSGreg Roach
476ccdf4f0SGreg Roach        // If the browser sets the DNT header, then we won't use analytics.
486ccdf4f0SGreg Roach        $dnt = $request->getServerParams()['HTTP_DNT'] ?? '';
496ccdf4f0SGreg Roach
506ccdf4f0SGreg Roach        if ($dnt === '1') {
51abafa13cSGreg Roach            return false;
52abafa13cSGreg Roach        }
53abafa13cSGreg Roach
5437eb8894SGreg Roach        foreach ($this->analyticsParameters() as $parameter) {
5537eb8894SGreg Roach            if ($parameter === '') {
5637eb8894SGreg Roach                return false;
5737eb8894SGreg Roach            }
5837eb8894SGreg Roach        }
5937eb8894SGreg Roach
6037eb8894SGreg Roach        return true;
6137eb8894SGreg Roach    }
6237eb8894SGreg Roach
6337eb8894SGreg Roach    /**
646ccdf4f0SGreg Roach     * The parameters that need to be embedded in the snippet.
656ccdf4f0SGreg Roach     *
666ccdf4f0SGreg Roach     * @return string[]
676ccdf4f0SGreg Roach     */
686ccdf4f0SGreg Roach    public function analyticsParameters(): array
696ccdf4f0SGreg Roach    {
706ccdf4f0SGreg Roach        return [];
716ccdf4f0SGreg Roach    }
726ccdf4f0SGreg Roach
736ccdf4f0SGreg Roach    /**
7437eb8894SGreg Roach     * A sentence describing what this module does.
7537eb8894SGreg Roach     *
7637eb8894SGreg Roach     * @return string
7737eb8894SGreg Roach     */
7837eb8894SGreg Roach    public function description(): string
7937eb8894SGreg Roach    {
8037eb8894SGreg Roach        return I18N::translate('Tracking and analytics');
8137eb8894SGreg Roach    }
8237eb8894SGreg Roach
8337eb8894SGreg Roach    /**
8457ab2231SGreg Roach     * @param ServerRequestInterface $request
8557ab2231SGreg Roach     *
866ccdf4f0SGreg Roach     * @return ResponseInterface
876ccdf4f0SGreg Roach     */
8857ab2231SGreg Roach    public function getAdminAction(ServerRequestInterface $request): ResponseInterface
896ccdf4f0SGreg Roach    {
906ccdf4f0SGreg Roach        $this->layout = 'layouts/administration';
916ccdf4f0SGreg Roach
926ccdf4f0SGreg Roach        return $this->viewResponse('admin/analytics-edit', [
9383615acfSGreg Roach            'action'      => route('module', ['module' => $this->name(), 'action' => 'Admin']),
946ccdf4f0SGreg Roach            'form_fields' => $this->analyticsFormFields(),
956ccdf4f0SGreg Roach            'preview'     => $this->analyticsSnippet($this->analyticsParameters()),
966ccdf4f0SGreg Roach            'title'       => $this->title(),
976ccdf4f0SGreg Roach        ]);
986ccdf4f0SGreg Roach    }
996ccdf4f0SGreg Roach
1006ccdf4f0SGreg Roach    /**
101606471d5SGreg Roach     * @param string  $view_name
102606471d5SGreg Roach     * @param mixed[] $view_data
1036ccdf4f0SGreg Roach     * @param int     $status
1046ccdf4f0SGreg Roach     *
1056ccdf4f0SGreg Roach     * @return ResponseInterface
1066ccdf4f0SGreg Roach     */
107606471d5SGreg Roach    abstract protected function viewResponse(string $view_name, array $view_data, $status = StatusCodeInterface::STATUS_OK): ResponseInterface;
1086ccdf4f0SGreg Roach
1096ccdf4f0SGreg Roach    /**
11037eb8894SGreg Roach     * Form fields to edit the parameters.
11137eb8894SGreg Roach     *
11237eb8894SGreg Roach     * @return string
11337eb8894SGreg Roach     */
11437eb8894SGreg Roach    public function analyticsFormFields(): string
11537eb8894SGreg Roach    {
11637eb8894SGreg Roach        return '';
11737eb8894SGreg Roach    }
11837eb8894SGreg Roach
11937eb8894SGreg Roach    /**
12037eb8894SGreg Roach     * Embed placeholders in the snippet.
12137eb8894SGreg Roach     *
12237eb8894SGreg Roach     * @param string[] $parameters
12337eb8894SGreg Roach     *
12437eb8894SGreg Roach     * @return string
12537eb8894SGreg Roach     */
12637eb8894SGreg Roach    public function analyticsSnippet(array $parameters): string
12737eb8894SGreg Roach    {
12837eb8894SGreg Roach        return '';
12937eb8894SGreg Roach    }
1308e5c5efeSGreg Roach
1318e5c5efeSGreg Roach    /**
1326ccdf4f0SGreg Roach     * How should this module be identified in the control panel, etc.?
1338e5c5efeSGreg Roach     *
1346ccdf4f0SGreg Roach     * @return string
1358e5c5efeSGreg Roach     */
1366ccdf4f0SGreg Roach    abstract public function title(): string;
1376ccdf4f0SGreg Roach
1386ccdf4f0SGreg Roach    /**
1396ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
1406ccdf4f0SGreg Roach     *
1416ccdf4f0SGreg Roach     * @return ResponseInterface
1426ccdf4f0SGreg Roach     */
1436ccdf4f0SGreg Roach    public function postAdminAction(ServerRequestInterface $request): ResponseInterface
1448e5c5efeSGreg Roach    {
1456b32f095SGreg Roach        foreach (array_keys($this->analyticsParameters()) as $parameter) {
146e106ff43SGreg Roach            $new_value = $request->getParsedBody()[$parameter];
147e106ff43SGreg Roach
1486b32f095SGreg Roach            $this->setPreference($parameter, $new_value);
1496b32f095SGreg Roach        }
1506b32f095SGreg Roach
1516ccdf4f0SGreg Roach        return redirect(route('analytics'));
1528e5c5efeSGreg Roach    }
1536ccdf4f0SGreg Roach
1546ccdf4f0SGreg Roach    /**
1556ccdf4f0SGreg Roach     * Set a module setting.
1566ccdf4f0SGreg Roach     * Since module settings are NOT NULL, setting a value to NULL will cause
1576ccdf4f0SGreg Roach     * it to be deleted.
1586ccdf4f0SGreg Roach     *
1596ccdf4f0SGreg Roach     * @param string $setting_name
1606ccdf4f0SGreg Roach     * @param string $setting_value
1616ccdf4f0SGreg Roach     *
1626ccdf4f0SGreg Roach     * @return void
1636ccdf4f0SGreg Roach     */
1646ccdf4f0SGreg Roach    abstract public function setPreference(string $setting_name, string $setting_value): void;
16537eb8894SGreg Roach}
166