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