xref: /webtrees/app/Http/RequestHandlers/TreePrivacyPage.php (revision ee51991c90edae1dd008c6d75dbe69b6d78db9c5)
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\Http\RequestHandlers;
21
22use Fisharebest\Webtrees\GedcomTag;
23use Fisharebest\Webtrees\Http\ViewResponseTrait;
24use Fisharebest\Webtrees\I18N;
25use Fisharebest\Webtrees\Registry;
26use Fisharebest\Webtrees\Services\TreeService;
27use Fisharebest\Webtrees\Tree;
28use Illuminate\Database\Capsule\Manager as DB;
29use Psr\Http\Message\ResponseInterface;
30use Psr\Http\Message\ServerRequestInterface;
31use Psr\Http\Server\RequestHandlerInterface;
32use stdClass;
33
34use function array_merge;
35use function array_unique;
36use function assert;
37use function e;
38use function explode;
39use function uasort;
40
41/**
42 * Edit the tree privacy.
43 */
44class TreePrivacyPage implements RequestHandlerInterface
45{
46    use ViewResponseTrait;
47
48    private TreeService $tree_service;
49
50    public function __construct(TreeService $tree_service)
51    {
52        $this->tree_service = $tree_service;
53    }
54
55    /**
56     * @param ServerRequestInterface $request
57     *
58     * @return ResponseInterface
59     */
60    public function handle(ServerRequestInterface $request): ResponseInterface
61    {
62        $this->layout = 'layouts/administration';
63
64        $tree = $request->getAttribute('tree');
65        assert($tree instanceof Tree);
66
67        $title                = e($tree->name()) . ' — ' . I18N::translate('Privacy');
68        $all_tags             = $this->tagsForPrivacy($tree);
69        $privacy_constants    = $this->privacyConstants();
70        $privacy_restrictions = $this->privacyRestrictions($tree);
71
72        return $this->viewResponse('admin/trees-privacy', [
73            'all_tags'             => $all_tags,
74            'count_trees'          => $this->tree_service->all()->count(),
75            'privacy_constants'    => $privacy_constants,
76            'privacy_restrictions' => $privacy_restrictions,
77            'title'                => $title,
78            'tree'                 => $tree,
79        ]);
80    }
81
82    /**
83     * Names of our privacy levels
84     *
85     * @return array<string,string>
86     */
87    private function privacyConstants(): array
88    {
89        return [
90            'none'         => I18N::translate('Show to visitors'),
91            'privacy'      => I18N::translate('Show to members'),
92            'confidential' => I18N::translate('Show to managers'),
93            'hidden'       => I18N::translate('Hide from everyone'),
94        ];
95    }
96
97    /**
98     * The current privacy restrictions for a tree.
99     *
100     * @param Tree $tree
101     *
102     * @return array<string,string>
103     */
104    private function privacyRestrictions(Tree $tree): array
105    {
106        return DB::table('default_resn')
107            ->where('gedcom_id', '=', $tree->id())
108            ->get()
109            ->map(static function (stdClass $row) use ($tree): stdClass {
110                $row->record = null;
111                $row->label  = '';
112
113                if ($row->xref !== null) {
114                    $row->record = Registry::gedcomRecordFactory()->make($row->xref, $tree);
115                }
116
117                if ($row->tag_type) {
118                    $row->tag_label = GedcomTag::getLabel($row->tag_type);
119                } else {
120                    $row->tag_label = '';
121                }
122
123                return $row;
124            })
125            ->sort(static function (stdClass $x, stdClass $y): int {
126                return I18N::comparator()($x->tag_label, $y->tag_label);
127            })
128            ->all();
129    }
130
131    /**
132     * Generate a list of potential problems with the server.
133     *
134     * @param Tree $tree
135     *
136     * @return array<string>
137     */
138    private function tagsForPrivacy(Tree $tree): array
139    {
140        $tags = array_unique(array_merge(
141            explode(',', $tree->getPreference('INDI_FACTS_ADD')),
142            explode(',', $tree->getPreference('INDI_FACTS_UNIQUE')),
143            explode(',', $tree->getPreference('FAM_FACTS_ADD')),
144            explode(',', $tree->getPreference('FAM_FACTS_UNIQUE')),
145            [
146                'SOUR',
147                'REPO',
148                'OBJE',
149                '_PRIM',
150                'NOTE',
151                'SUBM',
152                'SUBN',
153                '_UID',
154                'CHAN',
155            ]
156        ));
157
158        $all_tags = [];
159
160        foreach ($tags as $tag) {
161            if ($tag) {
162                $all_tags[$tag] = GedcomTag::getLabel($tag);
163            }
164        }
165
166        uasort($all_tags, I18N::comparator());
167
168        return array_merge(
169            ['' => I18N::translate('All facts and events')],
170            $all_tags
171        );
172    }
173}
174