xref: /webtrees/app/Module/ModuleAnalyticsTrait.php (revision fd6c003f26d8770d21ea893811f0fc20a190c323)
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     * Should we add this tracker?
35     *
36     * @return bool
37     */
38    public function analyticsCanShow(): bool
39    {
40        $request = app(ServerRequestInterface::class);
41
42        // If the browser sets the DNT header, then we won't use analytics.
43        $dnt = $request->getServerParams()['HTTP_DNT'] ?? '';
44
45        if ($dnt === '1') {
46            return false;
47        }
48
49        foreach ($this->analyticsParameters() as $parameter) {
50            if ($parameter === '') {
51                return false;
52            }
53        }
54
55        return true;
56    }
57
58    /**
59     * The parameters that need to be embedded in the snippet.
60     *
61     * @return string[]
62     */
63    public function analyticsParameters(): array
64    {
65        return [];
66    }
67
68    /**
69     * A sentence describing what this module does.
70     *
71     * @return string
72     */
73    public function description(): string
74    {
75        return I18N::translate('Tracking and analytics');
76    }
77
78    /**
79     * @param ServerRequestInterface $request
80     *
81     * @return ResponseInterface
82     */
83    public function getAdminAction(ServerRequestInterface $request): ResponseInterface
84    {
85        $this->layout = 'layouts/administration';
86
87        return $this->viewResponse('admin/analytics-edit', [
88            'action'      => route('module', ['module' => $this->name(), 'action' => 'Admin']),
89            'form_fields' => $this->analyticsFormFields(),
90            'preview'     => $this->analyticsSnippet($this->analyticsParameters()),
91            'title'       => $this->title(),
92        ]);
93    }
94
95    /**
96     * @param string  $view_name
97     * @param mixed[] $view_data
98     * @param int     $status
99     *
100     * @return ResponseInterface
101     */
102    abstract protected function viewResponse(string $view_name, array $view_data, $status = StatusCodeInterface::STATUS_OK): ResponseInterface;
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