xref: /webtrees/app/Module/ModuleAnalyticsTrait.php (revision bac83be95cc96bdac6a0a180473e77cbbad83047)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2019 webtrees development team
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18declare(strict_types=1);
19
20namespace Fisharebest\Webtrees\Module;
21
22use Fisharebest\Webtrees\Http\ViewResponseTrait;
23use Fisharebest\Webtrees\I18N;
24use Psr\Http\Message\ResponseInterface;
25use Psr\Http\Message\ServerRequestInterface;
26
27use function app;
28
29/**
30 * Trait ModuleAnalyticsTrait - default implementation of ModuleAnalyticsInterface
31 */
32trait ModuleAnalyticsTrait
33{
34    use ViewResponseTrait;
35
36    /**
37     * Should we add this tracker?
38     *
39     * @return bool
40     */
41    public function analyticsCanShow(): bool
42    {
43        $request = app(ServerRequestInterface::class);
44
45        // If the browser sets the DNT header, then we won't use analytics.
46        $dnt = $request->getServerParams()['HTTP_DNT'] ?? '';
47
48        if ($dnt === '1') {
49            return false;
50        }
51
52        foreach ($this->analyticsParameters() as $parameter) {
53            if ($parameter === '') {
54                return false;
55            }
56        }
57
58        return true;
59    }
60
61    /**
62     * The parameters that need to be embedded in the snippet.
63     *
64     * @return string[]
65     */
66    public function analyticsParameters(): array
67    {
68        return [];
69    }
70
71    /**
72     * A sentence describing what this module does.
73     *
74     * @return string
75     */
76    public function description(): string
77    {
78        return I18N::translate('Tracking and analytics');
79    }
80
81    /**
82     * @param ServerRequestInterface $request
83     *
84     * @return ResponseInterface
85     */
86    public function getAdminAction(ServerRequestInterface $request): ResponseInterface
87    {
88        $this->layout = 'layouts/administration';
89
90        return $this->viewResponse('admin/analytics-edit', [
91            'action'      => route('module', ['module' => $this->name(), 'action' => 'Admin']),
92            'form_fields' => $this->analyticsFormFields(),
93            'preview'     => $this->analyticsSnippet($this->analyticsParameters()),
94            'title'       => $this->title(),
95        ]);
96    }
97
98    /**
99     * Form fields to edit the parameters.
100     *
101     * @return string
102     */
103    public function analyticsFormFields(): string
104    {
105        return '';
106    }
107
108    /**
109     * Embed placeholders in the snippet.
110     *
111     * @param string[] $parameters
112     *
113     * @return string
114     */
115    public function analyticsSnippet(array $parameters): string
116    {
117        return '';
118    }
119
120    /**
121     * Is this a tracker, as opposed to just a site-verification.
122     *
123     * @return bool
124     */
125    public function isTracker(): bool
126    {
127        return true;
128    }
129
130    /**
131     * @param ServerRequestInterface $request
132     *
133     * @return ResponseInterface
134     */
135    public function postAdminAction(ServerRequestInterface $request): ResponseInterface
136    {
137        $params = (array) $request->getParsedBody();
138
139        foreach (array_keys($this->analyticsParameters()) as $parameter) {
140            $new_value = $params[$parameter];
141
142            $this->setPreference($parameter, $new_value);
143        }
144
145        return redirect(route('analytics'));
146    }
147}
148