xref: /webtrees/app/Module/PrivacyPolicy.php (revision b3a775f6a370b67e80b292212c765673a0177ffc)
1*b3a775f6SGreg Roach<?php
2*b3a775f6SGreg Roach
3*b3a775f6SGreg Roach/**
4*b3a775f6SGreg Roach * webtrees: online genealogy
5*b3a775f6SGreg Roach * Copyright (C) 2019 webtrees development team
6*b3a775f6SGreg Roach * This program is free software: you can redistribute it and/or modify
7*b3a775f6SGreg Roach * it under the terms of the GNU General Public License as published by
8*b3a775f6SGreg Roach * the Free Software Foundation, either version 3 of the License, or
9*b3a775f6SGreg Roach * (at your option) any later version.
10*b3a775f6SGreg Roach * This program is distributed in the hope that it will be useful,
11*b3a775f6SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
12*b3a775f6SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13*b3a775f6SGreg Roach * GNU General Public License for more details.
14*b3a775f6SGreg Roach * You should have received a copy of the GNU General Public License
15*b3a775f6SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
16*b3a775f6SGreg Roach */
17*b3a775f6SGreg Roach
18*b3a775f6SGreg Roachdeclare(strict_types=1);
19*b3a775f6SGreg Roach
20*b3a775f6SGreg Roachnamespace Fisharebest\Webtrees\Module;
21*b3a775f6SGreg Roach
22*b3a775f6SGreg Roachuse Fisharebest\Webtrees\Contracts\UserInterface;
23*b3a775f6SGreg Roachuse Fisharebest\Webtrees\I18N;
24*b3a775f6SGreg Roachuse Fisharebest\Webtrees\Services\ModuleService;
25*b3a775f6SGreg Roachuse Fisharebest\Webtrees\Services\UserService;
26*b3a775f6SGreg Roachuse Fisharebest\Webtrees\Tree;
27*b3a775f6SGreg Roachuse Illuminate\Support\Collection;
28*b3a775f6SGreg Roachuse Psr\Http\Message\ResponseInterface;
29*b3a775f6SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
30*b3a775f6SGreg Roach
31*b3a775f6SGreg Roachuse function assert;
32*b3a775f6SGreg Roachuse function view;
33*b3a775f6SGreg Roach
34*b3a775f6SGreg Roach/**
35*b3a775f6SGreg Roach * Class PrivacyPolicy - to comply with the GDPR and similar local laws.
36*b3a775f6SGreg Roach */
37*b3a775f6SGreg Roachclass PrivacyPolicy extends AbstractModule implements ModuleFooterInterface
38*b3a775f6SGreg Roach{
39*b3a775f6SGreg Roach    use ModuleFooterTrait;
40*b3a775f6SGreg Roach
41*b3a775f6SGreg Roach    /** @var ModuleService */
42*b3a775f6SGreg Roach    private $module_service;
43*b3a775f6SGreg Roach
44*b3a775f6SGreg Roach    /** @var UserService */
45*b3a775f6SGreg Roach    private $user_service;
46*b3a775f6SGreg Roach
47*b3a775f6SGreg Roach    /**
48*b3a775f6SGreg Roach     * Dependency injection.
49*b3a775f6SGreg Roach     *
50*b3a775f6SGreg Roach     * @param ModuleService $module_service
51*b3a775f6SGreg Roach     * @param UserService   $user_service
52*b3a775f6SGreg Roach     */
53*b3a775f6SGreg Roach    public function __construct(ModuleService $module_service, UserService $user_service)
54*b3a775f6SGreg Roach    {
55*b3a775f6SGreg Roach        $this->module_service = $module_service;
56*b3a775f6SGreg Roach        $this->user_service   = $user_service;
57*b3a775f6SGreg Roach    }
58*b3a775f6SGreg Roach
59*b3a775f6SGreg Roach    /**
60*b3a775f6SGreg Roach     * How should this module be labelled on tabs, footers, etc.?
61*b3a775f6SGreg Roach     *
62*b3a775f6SGreg Roach     * @return string
63*b3a775f6SGreg Roach     */
64*b3a775f6SGreg Roach    public function title(): string
65*b3a775f6SGreg Roach    {
66*b3a775f6SGreg Roach        /* I18N: Name of a module */
67*b3a775f6SGreg Roach        return I18N::translate('Privacy policy');
68*b3a775f6SGreg Roach    }
69*b3a775f6SGreg Roach
70*b3a775f6SGreg Roach    /**
71*b3a775f6SGreg Roach     * A sentence describing what this module does.
72*b3a775f6SGreg Roach     *
73*b3a775f6SGreg Roach     * @return string
74*b3a775f6SGreg Roach     */
75*b3a775f6SGreg Roach    public function description(): string
76*b3a775f6SGreg Roach    {
77*b3a775f6SGreg Roach        /* I18N: Description of the “Cookie warning” module */
78*b3a775f6SGreg Roach        return I18N::translate('Show a privacy policy.');
79*b3a775f6SGreg Roach    }
80*b3a775f6SGreg Roach
81*b3a775f6SGreg Roach    /**
82*b3a775f6SGreg Roach     * The default position for this footer.  It can be changed in the control panel.
83*b3a775f6SGreg Roach     *
84*b3a775f6SGreg Roach     * @return int
85*b3a775f6SGreg Roach     */
86*b3a775f6SGreg Roach    public function defaultFooterOrder(): int
87*b3a775f6SGreg Roach    {
88*b3a775f6SGreg Roach        return 4;
89*b3a775f6SGreg Roach    }
90*b3a775f6SGreg Roach
91*b3a775f6SGreg Roach    /**
92*b3a775f6SGreg Roach     * A footer, to be added at the bottom of every page.
93*b3a775f6SGreg Roach     *
94*b3a775f6SGreg Roach     * @param ServerRequestInterface $request
95*b3a775f6SGreg Roach     *
96*b3a775f6SGreg Roach     * @return string
97*b3a775f6SGreg Roach     */
98*b3a775f6SGreg Roach    public function getFooter(ServerRequestInterface $request): string
99*b3a775f6SGreg Roach    {
100*b3a775f6SGreg Roach        $tree = $request->getAttribute('tree');
101*b3a775f6SGreg Roach
102*b3a775f6SGreg Roach        if ($tree === null) {
103*b3a775f6SGreg Roach            return '';
104*b3a775f6SGreg Roach        }
105*b3a775f6SGreg Roach
106*b3a775f6SGreg Roach        $user = $request->getAttribute('user');
107*b3a775f6SGreg Roach        assert($user instanceof UserInterface);
108*b3a775f6SGreg Roach
109*b3a775f6SGreg Roach        return view('modules/privacy-policy/footer', [
110*b3a775f6SGreg Roach            'tree'           => $tree,
111*b3a775f6SGreg Roach            'uses_analytics' => $this->analyticsModules($tree, $user)->isNotEmpty(),
112*b3a775f6SGreg Roach        ]);
113*b3a775f6SGreg Roach    }
114*b3a775f6SGreg Roach
115*b3a775f6SGreg Roach    /**
116*b3a775f6SGreg Roach     * @param ServerRequestInterface $request
117*b3a775f6SGreg Roach     *
118*b3a775f6SGreg Roach     * @return ResponseInterface
119*b3a775f6SGreg Roach     */
120*b3a775f6SGreg Roach    public function getPageAction(ServerRequestInterface $request): ResponseInterface
121*b3a775f6SGreg Roach    {
122*b3a775f6SGreg Roach        $tree = $request->getAttribute('tree');
123*b3a775f6SGreg Roach        assert($tree instanceof Tree);
124*b3a775f6SGreg Roach
125*b3a775f6SGreg Roach        $user = $request->getAttribute('user');
126*b3a775f6SGreg Roach        assert($user instanceof UserInterface);
127*b3a775f6SGreg Roach
128*b3a775f6SGreg Roach        $title = I18N::translate('Privacy policy');
129*b3a775f6SGreg Roach
130*b3a775f6SGreg Roach        return $this->viewResponse('modules/privacy-policy/page', [
131*b3a775f6SGreg Roach            'administrators' => $this->user_service->administrators(),
132*b3a775f6SGreg Roach            'analytics'      => $this->analyticsModules($tree, $user),
133*b3a775f6SGreg Roach            'title'          => $title,
134*b3a775f6SGreg Roach            'tree'           => $tree,
135*b3a775f6SGreg Roach        ]);
136*b3a775f6SGreg Roach    }
137*b3a775f6SGreg Roach
138*b3a775f6SGreg Roach    /**
139*b3a775f6SGreg Roach     * @param Tree          $tree
140*b3a775f6SGreg Roach     * @param UserInterface $user
141*b3a775f6SGreg Roach     *
142*b3a775f6SGreg Roach     * @return Collection
143*b3a775f6SGreg Roach     */
144*b3a775f6SGreg Roach    protected function analyticsModules(Tree $tree, UserInterface $user): Collection
145*b3a775f6SGreg Roach    {
146*b3a775f6SGreg Roach        return $this->module_service
147*b3a775f6SGreg Roach            ->findByComponent(ModuleAnalyticsInterface::class, $tree, $user)
148*b3a775f6SGreg Roach            ->filter(static function (ModuleAnalyticsInterface $module): bool {
149*b3a775f6SGreg Roach                return $module->isTracker();
150*b3a775f6SGreg Roach            });
151*b3a775f6SGreg Roach    }
152*b3a775f6SGreg Roach}
153