xref: /webtrees/app/Module/PrivacyPolicy.php (revision 43f2f523bcb6d4090564d23802872c0679ede6bc)
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\Contracts\UserInterface;
23use Fisharebest\Webtrees\I18N;
24use Fisharebest\Webtrees\Services\ModuleService;
25use Fisharebest\Webtrees\Services\UserService;
26use Fisharebest\Webtrees\Tree;
27use Illuminate\Support\Collection;
28use Psr\Http\Message\ResponseInterface;
29use Psr\Http\Message\ServerRequestInterface;
30
31use function assert;
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 = $request->getAttribute('tree');
99
100        if ($tree === null) {
101            return '';
102        }
103
104        $user = $request->getAttribute('user');
105        assert($user instanceof UserInterface);
106
107        return view('modules/privacy-policy/footer', [
108            'tree'           => $tree,
109            'uses_analytics' => $this->analyticsModules($tree, $user)->isNotEmpty(),
110        ]);
111    }
112
113    /**
114     * @param ServerRequestInterface $request
115     *
116     * @return ResponseInterface
117     */
118    public function getPageAction(ServerRequestInterface $request): ResponseInterface
119    {
120        $tree = $request->getAttribute('tree');
121        assert($tree instanceof Tree);
122
123        $user = $request->getAttribute('user');
124        assert($user instanceof UserInterface);
125
126        $title = I18N::translate('Privacy policy');
127
128        return $this->viewResponse('modules/privacy-policy/page', [
129            'administrators' => $this->user_service->administrators(),
130            'analytics'      => $this->analyticsModules($tree, $user),
131            'title'          => $title,
132            'tree'           => $tree,
133        ]);
134    }
135
136    /**
137     * @param Tree          $tree
138     * @param UserInterface $user
139     *
140     * @return Collection<ModuleAnalyticsInterface>
141     */
142    protected function analyticsModules(Tree $tree, UserInterface $user): Collection
143    {
144        return $this->module_service
145            ->findByComponent(ModuleAnalyticsInterface::class, $tree, $user)
146            ->filter(static function (ModuleAnalyticsInterface $module): bool {
147                return $module->isTracker();
148            });
149    }
150}
151