xref: /webtrees/app/Module/ModuleAnalyticsTrait.php (revision 76d39c55735cfa9ad0972b0dd530e96b051f9ebe)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2021 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 Psr\Http\Message\ResponseInterface;
26use Psr\Http\Message\ServerRequestInterface;
27
28use function app;
29
30/**
31 * Trait ModuleAnalyticsTrait - default implementation of ModuleAnalyticsInterface
32 */
33trait ModuleAnalyticsTrait
34{
35    use ViewResponseTrait;
36
37    /**
38     * Should we add this tracker?
39     *
40     * @return bool
41     */
42    public function analyticsCanShow(): bool
43    {
44        $request = app(ServerRequestInterface::class);
45
46        // If the browser sets the DNT header, then we won't use analytics.
47        $dnt = $request->getServerParams()['HTTP_DNT'] ?? '';
48
49        if ($dnt === '1') {
50            return false;
51        }
52
53        foreach ($this->analyticsParameters() as $parameter) {
54            if ($parameter === '') {
55                return false;
56            }
57        }
58
59        return true;
60    }
61
62    /**
63     * The parameters that need to be embedded in the snippet.
64     *
65     * @return array<string>
66     */
67    public function analyticsParameters(): array
68    {
69        return [];
70    }
71
72    /**
73     * A sentence describing what this module does.
74     *
75     * @return string
76     */
77    public function description(): string
78    {
79        return I18N::translate('Tracking and analytics');
80    }
81
82    /**
83     * @param ServerRequestInterface $request
84     *
85     * @return ResponseInterface
86     */
87    public function getAdminAction(/** @scrutinizer ignore-unused */ ServerRequestInterface $request): ResponseInterface
88    {
89        $this->layout = 'layouts/administration';
90
91        return $this->viewResponse('admin/analytics-edit', [
92            'action'      => route('module', ['module' => $this->name(), 'action' => 'Admin']),
93            'form_fields' => $this->analyticsFormFields(),
94            'preview'     => $this->analyticsSnippet($this->analyticsParameters()),
95            'title'       => $this->title(),
96        ]);
97    }
98
99    /**
100     * Form fields to edit the parameters.
101     *
102     * @return string
103     */
104    public function analyticsFormFields(): string
105    {
106        return '';
107    }
108
109    /**
110     * Embed placeholders in the snippet.
111     *
112     * @param array<string> $parameters
113     *
114     * @return string
115     */
116    public function analyticsSnippet(/** @scrutinizer ignore-unused */ array $parameters): string
117    {
118        return '';
119    }
120
121    /**
122     * Is this a tracker, as opposed to just a site-verification.
123     *
124     * @return bool
125     */
126    public function isTracker(): bool
127    {
128        return true;
129    }
130
131    /**
132     * @param ServerRequestInterface $request
133     *
134     * @return ResponseInterface
135     */
136    public function postAdminAction(ServerRequestInterface $request): ResponseInterface
137    {
138        $params = (array) $request->getParsedBody();
139
140        foreach (array_keys($this->analyticsParameters()) as $parameter) {
141            $new_value = $params[$parameter];
142
143            $this->setPreference($parameter, $new_value);
144        }
145
146        return redirect(route(ModulesAnalyticsPage::class));
147    }
148}
149