xref: /webtrees/app/Module/ModuleAnalyticsTrait.php (revision 8e5c5efec485305c8b0de7a7a21cb0e31bb2e11d)
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 function viewResponse($view_name, $view_data, $status = Response::HTTP_OK): Response;
38
39    /**
40     * Should we add this tracker?
41     *
42     * @return bool
43     */
44    public function analyticsCanShow(): bool
45    {
46        foreach ($this->analyticsParameters() as $parameter) {
47            if ($parameter === '') {
48                return false;
49            }
50        }
51
52        return true;
53    }
54
55    /**
56     * A sentence describing what this module does.
57     *
58     * @return string
59     */
60    public function description(): string
61    {
62        return I18N::translate('Tracking and analytics');
63    }
64
65    /**
66     * Form fields to edit the parameters.
67     *
68     * @return string
69     */
70    public function analyticsFormFields(): string
71    {
72        return '';
73    }
74
75    /**
76     * The parameters that need to be embedded in the snippet.
77     *
78     * @return string[]
79     */
80    public function analyticsParameters(): array
81    {
82        return [];
83    }
84
85    /**
86     * Embed placeholders in the snippet.
87     *
88     * @param string[] $parameters
89     *
90     * @return string
91     */
92    public function analyticsSnippet(array $parameters): string
93    {
94        return '';
95    }
96
97
98    /**
99     * @return Response
100     */
101    public function getAdminAction(): Response
102    {
103        $this->layout = 'layouts/administration';
104
105        return $this->viewResponse('admin/analytics-edit', [
106            'form_fields' => $this->analyticsFormFields(),
107            'preview'     => $this->analyticsSnippet($this->analyticsParameters()),
108            'title'       => $this->title(),
109        ]);
110    }
111
112    /**
113     * @param Request $request
114     *
115     * @return RedirectResponse
116     */
117    public function postAdminAction(Request $request): RedirectResponse
118    {
119        return new RedirectResponse(route('analytics'));
120    }
121}
122