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