xref: /webtrees/app/Http/RequestHandlers/TreePrivacyPage.php (revision 202c018b592d5a516e4a465dc6dc515f3be37399)
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\Http\RequestHandlers;
21
22use Fisharebest\Webtrees\DB;
23use Fisharebest\Webtrees\Elements\UnknownElement;
24use Fisharebest\Webtrees\Family;
25use Fisharebest\Webtrees\Http\ViewResponseTrait;
26use Fisharebest\Webtrees\I18N;
27use Fisharebest\Webtrees\Individual;
28use Fisharebest\Webtrees\Registry;
29use Fisharebest\Webtrees\Services\TreeService;
30use Fisharebest\Webtrees\Tree;
31use Fisharebest\Webtrees\Validator;
32use Psr\Http\Message\ResponseInterface;
33use Psr\Http\Message\ServerRequestInterface;
34use Psr\Http\Server\RequestHandlerInterface;
35
36use function array_merge;
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    /**
51     * @param TreeService $tree_service
52     */
53    public function __construct(TreeService $tree_service)
54    {
55        $this->tree_service = $tree_service;
56    }
57
58    /**
59     * @param ServerRequestInterface $request
60     *
61     * @return ResponseInterface
62     */
63    public function handle(ServerRequestInterface $request): ResponseInterface
64    {
65        $this->layout = 'layouts/administration';
66
67        $tree                 = Validator::attributes($request)->tree();
68        $title                = e($tree->name()) . ' — ' . I18N::translate('Privacy');
69        $all_tags             = $this->tagsForPrivacy();
70        $privacy_constants    = $this->privacyConstants();
71        $privacy_restrictions = $this->privacyRestrictions($tree);
72
73        return $this->viewResponse('admin/trees-privacy', [
74            'all_tags'             => $all_tags,
75            'count_trees'          => $this->tree_service->all()->count(),
76            'privacy_constants'    => $privacy_constants,
77            'privacy_restrictions' => $privacy_restrictions,
78            'title'                => $title,
79            'tree'                 => $tree,
80        ]);
81    }
82
83    /**
84     * Names of our privacy levels
85     *
86     * @return array<string,string>
87     */
88    private function privacyConstants(): array
89    {
90        return [
91            'none'         => I18N::translate('Show to visitors'),
92            'privacy'      => I18N::translate('Show to members'),
93            'confidential' => I18N::translate('Show to managers'),
94            'hidden'       => I18N::translate('Hide from everyone'),
95        ];
96    }
97
98    /**
99     * The current privacy restrictions for a tree.
100     *
101     * @param Tree $tree
102     *
103     * @return array<object>
104     */
105    private function privacyRestrictions(Tree $tree): array
106    {
107        return DB::table('default_resn')
108            ->where('gedcom_id', '=', $tree->id())
109            ->get()
110            ->map(static function (object $row) use ($tree): object {
111                $row->record = null;
112                $row->label  = '';
113
114                if ($row->xref !== null) {
115                    $row->record = Registry::gedcomRecordFactory()->make($row->xref, $tree);
116                }
117
118                if ($row->tag_type) {
119                    $row->tag_label = $row->tag_type;
120
121                    foreach (['', Family::RECORD_TYPE . ':', Individual::RECORD_TYPE . ':'] as $prefix) {
122                        $element = Registry::elementFactory()->make($prefix . $row->tag_type);
123
124                        if (!$element instanceof UnknownElement) {
125                            $row->tag_label = $element->label();
126                            break;
127                        }
128                    }
129                } else {
130                    $row->tag_label = '';
131                }
132
133                return $row;
134            })
135            ->sort(static fn(object $x, object $y): int => I18N::comparator()($x->tag_label, $y->tag_label))
136            ->all();
137    }
138
139    /**
140     * Generate a list of tags that can be used in privacy settings.
141     *
142     * @return array<string>
143     */
144    private function tagsForPrivacy(): array
145    {
146        $tags = [];
147
148        $exclude = ['SEX'];
149
150        foreach ([Family::RECORD_TYPE, Individual::RECORD_TYPE] as $record_type) {
151            foreach (Registry::elementFactory()->make($record_type)->subtags() as $subtag => $occurrence) {
152                if (!in_array($subtag, $exclude, true)) {
153                    $tags[$subtag] = Registry::elementFactory()->make($record_type . ':' . $subtag)->label();
154                }
155            }
156        }
157
158        // SOUR overwrites INDI:SOUR
159        $include = ['REPO', 'SOUR', 'SUBN'];
160
161        foreach ($include as $tag) {
162            $tags[$tag] = Registry::elementFactory()->make($tag) -> label();
163        }
164
165        uasort($tags, I18N::comparator());
166
167        return array_merge(
168            ['' => I18N::translate('All facts and events')],
169            $tags
170        );
171    }
172}
173