xref: /webtrees/app/Module/ModuleAnalyticsTrait.php (revision 5703d1ae5966a5567605edc850c26ddd54062657)
1<?php
2/**
3 * webtrees: online genealogy
4 * Copyright (C) 2019 webtrees development team
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16declare(strict_types=1);
17
18namespace Fisharebest\Webtrees\Module;
19
20use Fisharebest\Webtrees\I18N;
21use Symfony\Component\HttpFoundation\RedirectResponse;
22use Symfony\Component\HttpFoundation\Request;
23use Symfony\Component\HttpFoundation\Response;
24
25/**
26 * Trait ModuleAnalyticsTrait - default implementation of ModuleAnalyticsInterface
27 */
28trait ModuleAnalyticsTrait
29{
30    /**
31     * @param $view_name
32     * @param $view_data
33     * @param $status
34     *
35     * @return Response
36     */
37    abstract protected function viewResponse($view_name, $view_data, $status = Response::HTTP_OK): Response;
38
39    /**
40     * How should this module be identified in the control panel, etc.?
41     *
42     * @return string
43     */
44    abstract public function title(): string;
45
46    /**
47     * Set a module setting.
48     *
49     * Since module settings are NOT NULL, setting a value to NULL will cause
50     * it to be deleted.
51     *
52     * @param string $setting_name
53     * @param string $setting_value
54     *
55     * @return void
56     */
57    abstract public function setPreference(string $setting_name, string $setting_value): void;
58
59    /**
60     * Should we add this tracker?
61     *
62     * @return bool
63     */
64    public function analyticsCanShow(): bool
65    {
66        // If the browser sets the DNT header, then we won't use analytics.
67        $request = app(Request::class);
68
69        if ($request->server->get('HTTP_DNT') === '1') {
70            return false;
71        }
72
73        foreach ($this->analyticsParameters() as $parameter) {
74            if ($parameter === '') {
75                return false;
76            }
77        }
78
79        return true;
80    }
81
82    /**
83     * A sentence describing what this module does.
84     *
85     * @return string
86     */
87    public function description(): string
88    {
89        return I18N::translate('Tracking and analytics');
90    }
91
92    /**
93     * Form fields to edit the parameters.
94     *
95     * @return string
96     */
97    public function analyticsFormFields(): string
98    {
99        return '';
100    }
101
102    /**
103     * The parameters that need to be embedded in the snippet.
104     *
105     * @return string[]
106     */
107    public function analyticsParameters(): array
108    {
109        return [];
110    }
111
112    /**
113     * Embed placeholders in the snippet.
114     *
115     * @param string[] $parameters
116     *
117     * @return string
118     */
119    public function analyticsSnippet(array $parameters): string
120    {
121        return '';
122    }
123
124
125    /**
126     * @return Response
127     */
128    public function getAdminAction(): Response
129    {
130        $this->layout = 'layouts/administration';
131
132        return $this->viewResponse('admin/analytics-edit', [
133            'form_fields' => $this->analyticsFormFields(),
134            'preview'     => $this->analyticsSnippet($this->analyticsParameters()),
135            'title'       => $this->title(),
136        ]);
137    }
138
139    /**
140     * @param Request $request
141     *
142     * @return RedirectResponse
143     */
144    public function postAdminAction(Request $request): RedirectResponse
145    {
146        foreach (array_keys($this->analyticsParameters()) as $parameter) {
147            $new_value = $request->get($parameter, '');
148            $this->setPreference($parameter, $new_value);
149        }
150
151        return new RedirectResponse(route('analytics'));
152    }
153}
154