xref: /webtrees/app/Module/ModuleAnalyticsTrait.php (revision 990350cffdd7cbd842d2c5f8ecbca59ed9f7376d)
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     * @return string
38     */
39    abstract public function name(): string;
40
41    /**
42     * Should we add this tracker?
43     *
44     * @return bool
45     */
46    public function analyticsCanShow(): bool
47    {
48        $request = app(ServerRequestInterface::class);
49
50        // If the browser sets the DNT header, then we won't use analytics.
51        $dnt = $request->getServerParams()['HTTP_DNT'] ?? '';
52
53        if ($dnt === '1') {
54            return false;
55        }
56
57        foreach ($this->analyticsParameters() as $parameter) {
58            if ($parameter === '') {
59                return false;
60            }
61        }
62
63        return true;
64    }
65
66    /**
67     * The parameters that need to be embedded in the snippet.
68     *
69     * @return string[]
70     */
71    public function analyticsParameters(): array
72    {
73        return [];
74    }
75
76    /**
77     * A sentence describing what this module does.
78     *
79     * @return string
80     */
81    public function description(): string
82    {
83        return I18N::translate('Tracking and analytics');
84    }
85
86    /**
87     * @param ServerRequestInterface $request
88     *
89     * @return ResponseInterface
90     */
91    public function getAdminAction(ServerRequestInterface $request): ResponseInterface
92    {
93        $this->layout = 'layouts/administration';
94
95        return $this->viewResponse('admin/analytics-edit', [
96            'action'      => route('module', ['module' => $this->name(), 'action' => 'Admin']),
97            'form_fields' => $this->analyticsFormFields(),
98            'preview'     => $this->analyticsSnippet($this->analyticsParameters()),
99            'title'       => $this->title(),
100        ]);
101    }
102
103    /**
104     * Form fields to edit the parameters.
105     *
106     * @return string
107     */
108    public function analyticsFormFields(): string
109    {
110        return '';
111    }
112
113    /**
114     * Embed placeholders in the snippet.
115     *
116     * @param string[] $parameters
117     *
118     * @return string
119     */
120    public function analyticsSnippet(array $parameters): string
121    {
122        return '';
123    }
124
125    /**
126     * How should this module be identified in the control panel, etc.?
127     *
128     * @return string
129     */
130    abstract public function title(): string;
131
132    /**
133     * Is this a tracker, as opposed to just a site-verification.
134     *
135     * @return bool
136     */
137    public function isTracker(): bool
138    {
139        return true;
140    }
141
142    /**
143     * @param ServerRequestInterface $request
144     *
145     * @return ResponseInterface
146     */
147    public function postAdminAction(ServerRequestInterface $request): ResponseInterface
148    {
149        $params = (array) $request->getParsedBody();
150
151        foreach (array_keys($this->analyticsParameters()) as $parameter) {
152            $new_value = $params[$parameter];
153
154            $this->setPreference($parameter, $new_value);
155        }
156
157        return redirect(route('analytics'));
158    }
159
160    /**
161     * Set a module setting.
162     * Since module settings are NOT NULL, setting a value to NULL will cause
163     * it to be deleted.
164     *
165     * @param string $setting_name
166     * @param string $setting_value
167     *
168     * @return void
169     */
170    abstract public function setPreference(string $setting_name, string $setting_value): void;
171}
172