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