xref: /webtrees/app/Http/RequestHandlers/TreePrivacyPage.php (revision 992ad95a3b71cdc2194e7663a57b01db9e5e7719)
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\Elements\UnknownElement;
23use Fisharebest\Webtrees\Family;
24use Fisharebest\Webtrees\Http\ViewResponseTrait;
25use Fisharebest\Webtrees\I18N;
26use Fisharebest\Webtrees\Individual;
27use Fisharebest\Webtrees\Registry;
28use Fisharebest\Webtrees\Services\TreeService;
29use Fisharebest\Webtrees\Tree;
30use Illuminate\Database\Capsule\Manager as DB;
31use Psr\Http\Message\ResponseInterface;
32use Psr\Http\Message\ServerRequestInterface;
33use Psr\Http\Server\RequestHandlerInterface;
34
35use function array_merge;
36use function assert;
37use function e;
38use function in_array;
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();
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 (object $row) use ($tree): object {
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 = $row->tag_type;
119
120                    foreach (['', Family::RECORD_TYPE . ':', Individual::RECORD_TYPE . ':'] as $prefix) {
121                        $element = Registry::elementFactory()->make($prefix . $row->tag_type);
122
123                        if (!$element instanceof UnknownElement) {
124                            $row->tag_label = $element->label();
125                            break;
126                        }
127                    }
128                } else {
129                    $row->tag_label = '';
130                }
131
132                return $row;
133            })
134            ->sort(static function (object $x, object $y): int {
135                return I18N::comparator()($x->tag_label, $y->tag_label);
136            })
137            ->all();
138    }
139
140    /**
141     * Generate a list of tags that can be used in privacy settings.
142     *
143     * @return array<string>
144     */
145    private function tagsForPrivacy(): array
146    {
147        $tags = [];
148
149        $exclude = ['SEX'];
150
151        foreach ([Family::RECORD_TYPE, Individual::RECORD_TYPE] as $record_type) {
152            foreach (Registry::elementFactory()->make($record_type)->subtags() as $subtag => $occurrence) {
153                if (!in_array($subtag, $exclude, true)) {
154                    $tags[$subtag] = Registry::elementFactory()->make($record_type . ':' . $subtag)->label();
155                }
156            }
157        }
158
159        // SOUR overwrites INDI:SOUR
160        $include = ['REPO', 'SOUR', 'SUBN'];
161
162        foreach ($include as $tag) {
163            $tags[$tag] = Registry::elementFactory()->make($tag) -> label();
164        }
165
166        uasort($tags, I18N::comparator());
167
168        return array_merge(
169            ['' => I18N::translate('All facts and events')],
170            $tags
171        );
172    }
173}
174