xref: /webtrees/app/Module/PrivacyPolicy.php (revision 52550490b7095dd69811f3ec21ed5a3ca1a8968d)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2023 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\Contracts\UserInterface;
23use Fisharebest\Webtrees\I18N;
24use Fisharebest\Webtrees\Services\ModuleService;
25use Fisharebest\Webtrees\Services\UserService;
26use Fisharebest\Webtrees\Tree;
27use Fisharebest\Webtrees\Validator;
28use Illuminate\Support\Collection;
29use Psr\Http\Message\ResponseInterface;
30use Psr\Http\Message\ServerRequestInterface;
31
32use function view;
33
34/**
35 * Class PrivacyPolicy - to comply with the GDPR and similar local laws.
36 */
37class PrivacyPolicy extends AbstractModule implements ModuleFooterInterface
38{
39    use ModuleFooterTrait;
40
41    private ModuleService $module_service;
42
43    private UserService $user_service;
44
45    /**
46     * Dependency injection.
47     *
48     * @param ModuleService $module_service
49     * @param UserService   $user_service
50     */
51    public function __construct(ModuleService $module_service, UserService $user_service)
52    {
53        $this->module_service = $module_service;
54        $this->user_service   = $user_service;
55    }
56
57    /**
58     * How should this module be labelled on tabs, footers, etc.?
59     *
60     * @return string
61     */
62    public function title(): string
63    {
64        /* I18N: Name of a module */
65        return I18N::translate('Privacy policy');
66    }
67
68    /**
69     * A sentence describing what this module does.
70     *
71     * @return string
72     */
73    public function description(): string
74    {
75        /* I18N: Description of the “Cookie warning” module */
76        return I18N::translate('Show a privacy policy.');
77    }
78
79    /**
80     * The default position for this footer.  It can be changed in the control panel.
81     *
82     * @return int
83     */
84    public function defaultFooterOrder(): int
85    {
86        return 4;
87    }
88
89    /**
90     * A footer, to be added at the bottom of every page.
91     *
92     * @param ServerRequestInterface $request
93     *
94     * @return string
95     */
96    public function getFooter(ServerRequestInterface $request): string
97    {
98        $tree = Validator::attributes($request)->treeOptional();
99        $user = Validator::attributes($request)->user();
100
101        if ($tree === null) {
102            return '';
103        }
104
105        return view('modules/privacy-policy/footer', [
106            'tree'           => $tree,
107            'uses_analytics' => $this->analyticsModules($tree, $user)->isNotEmpty(),
108        ]);
109    }
110
111    /**
112     * @param ServerRequestInterface $request
113     *
114     * @return ResponseInterface
115     */
116    public function getPageAction(ServerRequestInterface $request): ResponseInterface
117    {
118        $tree = Validator::attributes($request)->tree();
119        $user = Validator::attributes($request)->user();
120
121        $title = I18N::translate('Privacy policy');
122
123        return $this->viewResponse('modules/privacy-policy/page', [
124            'administrators' => $this->user_service->administrators(),
125            'analytics'      => $this->analyticsModules($tree, $user),
126            'title'          => $title,
127            'tree'           => $tree,
128        ]);
129    }
130
131    /**
132     * @param Tree          $tree
133     * @param UserInterface $user
134     *
135     * @return Collection<int,ModuleAnalyticsInterface>
136     */
137    protected function analyticsModules(Tree $tree, UserInterface $user): Collection
138    {
139        return $this->module_service
140            ->findByComponent(ModuleAnalyticsInterface::class, $tree, $user)
141            ->filter(static fn (ModuleAnalyticsInterface $module): bool => $module->isTracker());
142    }
143}
144