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