xref: /webtrees/app/Module/ModuleAnalyticsTrait.php (revision 3976b4703df669696105ed6b024b96d433c8fbdb)
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            'form_fields' => $this->analyticsFormFields(),
89            'preview'     => $this->analyticsSnippet($this->analyticsParameters()),
90            'title'       => $this->title(),
91        ]);
92    }
93
94    /**
95     * @param string  $view_name
96     * @param mixed[] $view_data
97     * @param int     $status
98     *
99     * @return ResponseInterface
100     */
101    abstract protected function viewResponse(string $view_name, array $view_data, $status = StatusCodeInterface::STATUS_OK): ResponseInterface;
102
103    /**
104     * Form fields to edit the parameters.
105     *
106     * @return string
107     */
108    public function analyticsFormFields(): string
109    {
110        return '';
111    }
112
113    /**
114     * Embed placeholders in the snippet.
115     *
116     * @param string[] $parameters
117     *
118     * @return string
119     */
120    public function analyticsSnippet(array $parameters): string
121    {
122        return '';
123    }
124
125    /**
126     * How should this module be identified in the control panel, etc.?
127     *
128     * @return string
129     */
130    abstract public function title(): string;
131
132    /**
133     * @param ServerRequestInterface $request
134     *
135     * @return ResponseInterface
136     */
137    public function postAdminAction(ServerRequestInterface $request): ResponseInterface
138    {
139        foreach (array_keys($this->analyticsParameters()) as $parameter) {
140            $new_value = $request->getParsedBody()[$parameter];
141
142            $this->setPreference($parameter, $new_value);
143        }
144
145        return redirect(route('analytics'));
146    }
147
148    /**
149     * Set a module setting.
150     * Since module settings are NOT NULL, setting a value to NULL will cause
151     * it to be deleted.
152     *
153     * @param string $setting_name
154     * @param string $setting_value
155     *
156     * @return void
157     */
158    abstract public function setPreference(string $setting_name, string $setting_value): void;
159}
160