xref: /webtrees/app/Module/ModuleAnalyticsTrait.php (revision ad7270802ba57a28d195fc58fcc55f26458a779a)
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 labelled on tabs, menus, 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        foreach ($this->analyticsParameters() as $parameter) {
67            if ($parameter === '') {
68                return false;
69            }
70        }
71
72        return true;
73    }
74
75    /**
76     * A sentence describing what this module does.
77     *
78     * @return string
79     */
80    public function description(): string
81    {
82        return I18N::translate('Tracking and analytics');
83    }
84
85    /**
86     * Form fields to edit the parameters.
87     *
88     * @return string
89     */
90    public function analyticsFormFields(): string
91    {
92        return '';
93    }
94
95    /**
96     * The parameters that need to be embedded in the snippet.
97     *
98     * @return string[]
99     */
100    public function analyticsParameters(): array
101    {
102        return [];
103    }
104
105    /**
106     * Embed placeholders in the snippet.
107     *
108     * @param string[] $parameters
109     *
110     * @return string
111     */
112    public function analyticsSnippet(array $parameters): string
113    {
114        return '';
115    }
116
117
118    /**
119     * @return Response
120     */
121    public function getAdminAction(): Response
122    {
123        $this->layout = 'layouts/administration';
124
125        return $this->viewResponse('admin/analytics-edit', [
126            'form_fields' => $this->analyticsFormFields(),
127            'preview'     => $this->analyticsSnippet($this->analyticsParameters()),
128            'title'       => $this->title(),
129        ]);
130    }
131
132    /**
133     * @param Request $request
134     *
135     * @return RedirectResponse
136     */
137    public function postAdminAction(Request $request): RedirectResponse
138    {
139        foreach (array_keys($this->analyticsParameters()) as $parameter) {
140            $new_value = $request->get($parameter, '');
141            $this->setPreference($parameter, $new_value);
142        }
143
144        return new RedirectResponse(route('analytics'));
145    }
146}
147