xref: /webtrees/app/Module/PrivacyPolicy.php (revision afa67798854828b1edc33dd077960ec2b18e6140)
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    /** @var ModuleService */
42    private $module_service;
43
44    /** @var UserService */
45    private $user_service;
46
47    /**
48     * Dependency injection.
49     *
50     * @param ModuleService $module_service
51     * @param UserService   $user_service
52     */
53    public function __construct(ModuleService $module_service, UserService $user_service)
54    {
55        $this->module_service = $module_service;
56        $this->user_service   = $user_service;
57    }
58
59    /**
60     * How should this module be labelled on tabs, footers, etc.?
61     *
62     * @return string
63     */
64    public function title(): string
65    {
66        /* I18N: Name of a module */
67        return I18N::translate('Privacy policy');
68    }
69
70    /**
71     * A sentence describing what this module does.
72     *
73     * @return string
74     */
75    public function description(): string
76    {
77        /* I18N: Description of the “Cookie warning” module */
78        return I18N::translate('Show a privacy policy.');
79    }
80
81    /**
82     * The default position for this footer.  It can be changed in the control panel.
83     *
84     * @return int
85     */
86    public function defaultFooterOrder(): int
87    {
88        return 4;
89    }
90
91    /**
92     * A footer, to be added at the bottom of every page.
93     *
94     * @param ServerRequestInterface $request
95     *
96     * @return string
97     */
98    public function getFooter(ServerRequestInterface $request): string
99    {
100        $tree = $request->getAttribute('tree');
101
102        if ($tree === null) {
103            return '';
104        }
105
106        $user = $request->getAttribute('user');
107        assert($user instanceof UserInterface);
108
109        return view('modules/privacy-policy/footer', [
110            'tree'           => $tree,
111            'uses_analytics' => $this->analyticsModules($tree, $user)->isNotEmpty(),
112        ]);
113    }
114
115    /**
116     * @param ServerRequestInterface $request
117     *
118     * @return ResponseInterface
119     */
120    public function getPageAction(ServerRequestInterface $request): ResponseInterface
121    {
122        $tree = $request->getAttribute('tree');
123        assert($tree instanceof Tree);
124
125        $user = $request->getAttribute('user');
126        assert($user instanceof UserInterface);
127
128        $title = I18N::translate('Privacy policy');
129
130        return $this->viewResponse('modules/privacy-policy/page', [
131            'administrators' => $this->user_service->administrators(),
132            'analytics'      => $this->analyticsModules($tree, $user),
133            'title'          => $title,
134            'tree'           => $tree,
135        ]);
136    }
137
138    /**
139     * @param Tree          $tree
140     * @param UserInterface $user
141     *
142     * @return Collection<ModuleAnalyticsInterface>
143     */
144    protected function analyticsModules(Tree $tree, UserInterface $user): Collection
145    {
146        return $this->module_service
147            ->findByComponent(ModuleAnalyticsInterface::class, $tree, $user)
148            ->filter(static function (ModuleAnalyticsInterface $module): bool {
149                return $module->isTracker();
150            });
151    }
152}
153