xref: /webtrees/app/Module/ModuleAnalyticsTrait.php (revision b46c87bda4b592cf9252f1db48552a820b1e3d97)
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 */
17fcfa147eSGreg Roach
1837eb8894SGreg Roachdeclare(strict_types=1);
1937eb8894SGreg Roach
2037eb8894SGreg Roachnamespace Fisharebest\Webtrees\Module;
2137eb8894SGreg Roach
22f4917837SGreg Roachuse Fisharebest\Webtrees\Http\ViewResponseTrait;
2337eb8894SGreg Roachuse Fisharebest\Webtrees\I18N;
246ccdf4f0SGreg Roachuse Psr\Http\Message\ResponseInterface;
256ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
263976b470SGreg Roach
276ccdf4f0SGreg Roachuse function app;
2837eb8894SGreg Roach
2937eb8894SGreg Roach/**
3037eb8894SGreg Roach * Trait ModuleAnalyticsTrait - default implementation of ModuleAnalyticsInterface
3137eb8894SGreg Roach */
3237eb8894SGreg Roachtrait ModuleAnalyticsTrait
3337eb8894SGreg Roach{
34f4917837SGreg Roach    use ViewResponseTrait;
35f4917837SGreg Roach
3637eb8894SGreg Roach    /**
37962ead51SGreg Roach     * @return string
38962ead51SGreg Roach     */
39962ead51SGreg Roach    abstract public function name(): string;
40962ead51SGreg Roach
41962ead51SGreg Roach    /**
4237eb8894SGreg Roach     * Should we add this tracker?
4337eb8894SGreg Roach     *
4437eb8894SGreg Roach     * @return bool
4537eb8894SGreg Roach     */
4637eb8894SGreg Roach    public function analyticsCanShow(): bool
4737eb8894SGreg Roach    {
486ccdf4f0SGreg Roach        $request = app(ServerRequestInterface::class);
49abafa13cSGreg Roach
506ccdf4f0SGreg Roach        // If the browser sets the DNT header, then we won't use analytics.
516ccdf4f0SGreg Roach        $dnt = $request->getServerParams()['HTTP_DNT'] ?? '';
526ccdf4f0SGreg Roach
536ccdf4f0SGreg Roach        if ($dnt === '1') {
54abafa13cSGreg Roach            return false;
55abafa13cSGreg Roach        }
56abafa13cSGreg Roach
5737eb8894SGreg Roach        foreach ($this->analyticsParameters() as $parameter) {
5837eb8894SGreg Roach            if ($parameter === '') {
5937eb8894SGreg Roach                return false;
6037eb8894SGreg Roach            }
6137eb8894SGreg Roach        }
6237eb8894SGreg Roach
6337eb8894SGreg Roach        return true;
6437eb8894SGreg Roach    }
6537eb8894SGreg Roach
6637eb8894SGreg Roach    /**
676ccdf4f0SGreg Roach     * The parameters that need to be embedded in the snippet.
686ccdf4f0SGreg Roach     *
696ccdf4f0SGreg Roach     * @return string[]
706ccdf4f0SGreg Roach     */
716ccdf4f0SGreg Roach    public function analyticsParameters(): array
726ccdf4f0SGreg Roach    {
736ccdf4f0SGreg Roach        return [];
746ccdf4f0SGreg Roach    }
756ccdf4f0SGreg Roach
766ccdf4f0SGreg Roach    /**
7737eb8894SGreg Roach     * A sentence describing what this module does.
7837eb8894SGreg Roach     *
7937eb8894SGreg Roach     * @return string
8037eb8894SGreg Roach     */
8137eb8894SGreg Roach    public function description(): string
8237eb8894SGreg Roach    {
8337eb8894SGreg Roach        return I18N::translate('Tracking and analytics');
8437eb8894SGreg Roach    }
8537eb8894SGreg Roach
8637eb8894SGreg Roach    /**
8757ab2231SGreg Roach     * @param ServerRequestInterface $request
8857ab2231SGreg Roach     *
896ccdf4f0SGreg Roach     * @return ResponseInterface
906ccdf4f0SGreg Roach     */
9157ab2231SGreg Roach    public function getAdminAction(ServerRequestInterface $request): ResponseInterface
926ccdf4f0SGreg Roach    {
936ccdf4f0SGreg Roach        $this->layout = 'layouts/administration';
946ccdf4f0SGreg Roach
956ccdf4f0SGreg Roach        return $this->viewResponse('admin/analytics-edit', [
9683615acfSGreg Roach            'action'      => route('module', ['module' => $this->name(), 'action' => 'Admin']),
976ccdf4f0SGreg Roach            'form_fields' => $this->analyticsFormFields(),
986ccdf4f0SGreg Roach            'preview'     => $this->analyticsSnippet($this->analyticsParameters()),
996ccdf4f0SGreg Roach            'title'       => $this->title(),
1006ccdf4f0SGreg Roach        ]);
1016ccdf4f0SGreg Roach    }
1026ccdf4f0SGreg Roach
1036ccdf4f0SGreg Roach    /**
10437eb8894SGreg Roach     * Form fields to edit the parameters.
10537eb8894SGreg Roach     *
10637eb8894SGreg Roach     * @return string
10737eb8894SGreg Roach     */
10837eb8894SGreg Roach    public function analyticsFormFields(): string
10937eb8894SGreg Roach    {
11037eb8894SGreg Roach        return '';
11137eb8894SGreg Roach    }
11237eb8894SGreg Roach
11337eb8894SGreg Roach    /**
11437eb8894SGreg Roach     * Embed placeholders in the snippet.
11537eb8894SGreg Roach     *
11637eb8894SGreg Roach     * @param string[] $parameters
11737eb8894SGreg Roach     *
11837eb8894SGreg Roach     * @return string
11937eb8894SGreg Roach     */
12037eb8894SGreg Roach    public function analyticsSnippet(array $parameters): string
12137eb8894SGreg Roach    {
12237eb8894SGreg Roach        return '';
12337eb8894SGreg Roach    }
1248e5c5efeSGreg Roach
1258e5c5efeSGreg Roach    /**
1266ccdf4f0SGreg Roach     * How should this module be identified in the control panel, etc.?
1278e5c5efeSGreg Roach     *
1286ccdf4f0SGreg Roach     * @return string
1298e5c5efeSGreg Roach     */
1306ccdf4f0SGreg Roach    abstract public function title(): string;
1316ccdf4f0SGreg Roach
1326ccdf4f0SGreg Roach    /**
133b3a775f6SGreg Roach     * Is this a tracker, as opposed to just a site-verification.
134b3a775f6SGreg Roach     *
135b3a775f6SGreg Roach     * @return bool
136b3a775f6SGreg Roach     */
137b3a775f6SGreg Roach    public function isTracker(): bool
138b3a775f6SGreg Roach    {
139b3a775f6SGreg Roach        return true;
140b3a775f6SGreg Roach    }
141b3a775f6SGreg Roach
142b3a775f6SGreg Roach    /**
1436ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
1446ccdf4f0SGreg Roach     *
1456ccdf4f0SGreg Roach     * @return ResponseInterface
1466ccdf4f0SGreg Roach     */
1476ccdf4f0SGreg Roach    public function postAdminAction(ServerRequestInterface $request): ResponseInterface
1488e5c5efeSGreg Roach    {
149*b46c87bdSGreg Roach        $params = (array) $request->getParsedBody();
150*b46c87bdSGreg Roach
1516b32f095SGreg Roach        foreach (array_keys($this->analyticsParameters()) as $parameter) {
152*b46c87bdSGreg Roach            $new_value = $params[$parameter];
153e106ff43SGreg Roach
1546b32f095SGreg Roach            $this->setPreference($parameter, $new_value);
1556b32f095SGreg Roach        }
1566b32f095SGreg Roach
1576ccdf4f0SGreg Roach        return redirect(route('analytics'));
1588e5c5efeSGreg Roach    }
1596ccdf4f0SGreg Roach
1606ccdf4f0SGreg Roach    /**
1616ccdf4f0SGreg Roach     * Set a module setting.
1626ccdf4f0SGreg Roach     * Since module settings are NOT NULL, setting a value to NULL will cause
1636ccdf4f0SGreg Roach     * it to be deleted.
1646ccdf4f0SGreg Roach     *
1656ccdf4f0SGreg Roach     * @param string $setting_name
1666ccdf4f0SGreg Roach     * @param string $setting_value
1676ccdf4f0SGreg Roach     *
1686ccdf4f0SGreg Roach     * @return void
1696ccdf4f0SGreg Roach     */
1706ccdf4f0SGreg Roach    abstract public function setPreference(string $setting_name, string $setting_value): void;
17137eb8894SGreg Roach}
172