xref: /webtrees/app/Module/PrivacyPolicy.php (revision 1270d2767576ed4a83917769b0ee3613e3b010bf)
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    public function description(): string
69    {
70        /* I18N: Description of the “Cookie warning” module */
71        return I18N::translate('Show a privacy policy.');
72    }
73
74    /**
75     * The default position for this footer.  It can be changed in the control panel.
76     *
77     * @return int
78     */
79    public function defaultFooterOrder(): int
80    {
81        return 4;
82    }
83
84    /**
85     * A footer, to be added at the bottom of every page.
86     *
87     * @param ServerRequestInterface $request
88     *
89     * @return string
90     */
91    public function getFooter(ServerRequestInterface $request): string
92    {
93        $tree = Validator::attributes($request)->treeOptional();
94        $user = Validator::attributes($request)->user();
95
96        if ($tree === null) {
97            return '';
98        }
99
100        return view('modules/privacy-policy/footer', [
101            'tree'           => $tree,
102            'uses_analytics' => $this->analyticsModules($tree, $user)->isNotEmpty(),
103        ]);
104    }
105
106    /**
107     * @param ServerRequestInterface $request
108     *
109     * @return ResponseInterface
110     */
111    public function getPageAction(ServerRequestInterface $request): ResponseInterface
112    {
113        $tree = Validator::attributes($request)->tree();
114        $user = Validator::attributes($request)->user();
115
116        $title = I18N::translate('Privacy policy');
117
118        return $this->viewResponse('modules/privacy-policy/page', [
119            'administrators' => $this->user_service->administrators(),
120            'analytics'      => $this->analyticsModules($tree, $user),
121            'title'          => $title,
122            'tree'           => $tree,
123        ]);
124    }
125
126    /**
127     * @param Tree          $tree
128     * @param UserInterface $user
129     *
130     * @return Collection<int,ModuleAnalyticsInterface>
131     */
132    protected function analyticsModules(Tree $tree, UserInterface $user): Collection
133    {
134        return $this->module_service
135            ->findByComponent(ModuleAnalyticsInterface::class, $tree, $user)
136            ->filter(static fn (ModuleAnalyticsInterface $module): bool => $module->isTracker());
137    }
138}
139