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