xref: /webtrees/app/Http/RequestHandlers/TreePrivacyPage.php (revision b55cbc6b43247e8b2ad14af6f6d24dc6747195ff)
16fd01894SGreg Roach<?php
26fd01894SGreg Roach
36fd01894SGreg Roach/**
46fd01894SGreg Roach * webtrees: online genealogy
589f7189bSGreg Roach * Copyright (C) 2021 webtrees development team
66fd01894SGreg Roach * This program is free software: you can redistribute it and/or modify
76fd01894SGreg Roach * it under the terms of the GNU General Public License as published by
86fd01894SGreg Roach * the Free Software Foundation, either version 3 of the License, or
96fd01894SGreg Roach * (at your option) any later version.
106fd01894SGreg Roach * This program is distributed in the hope that it will be useful,
116fd01894SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
126fd01894SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
136fd01894SGreg Roach * GNU General Public License for more details.
146fd01894SGreg Roach * You should have received a copy of the GNU General Public License
1589f7189bSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>.
166fd01894SGreg Roach */
176fd01894SGreg Roach
186fd01894SGreg Roachdeclare(strict_types=1);
196fd01894SGreg Roach
206fd01894SGreg Roachnamespace Fisharebest\Webtrees\Http\RequestHandlers;
216fd01894SGreg Roach
2297def6bcSGreg Roachuse Fisharebest\Webtrees\Elements\UnknownElement;
2397def6bcSGreg Roachuse Fisharebest\Webtrees\Family;
246fd01894SGreg Roachuse Fisharebest\Webtrees\Http\ViewResponseTrait;
256fd01894SGreg Roachuse Fisharebest\Webtrees\I18N;
2697def6bcSGreg Roachuse Fisharebest\Webtrees\Individual;
276b9cb339SGreg Roachuse Fisharebest\Webtrees\Registry;
286fd01894SGreg Roachuse Fisharebest\Webtrees\Services\TreeService;
296fd01894SGreg Roachuse Fisharebest\Webtrees\Tree;
30*b55cbc6bSGreg Roachuse Fisharebest\Webtrees\Validator;
316fd01894SGreg Roachuse Illuminate\Database\Capsule\Manager as DB;
326fd01894SGreg Roachuse Psr\Http\Message\ResponseInterface;
336fd01894SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
346fd01894SGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
356fd01894SGreg Roach
366fd01894SGreg Roachuse function array_merge;
376fd01894SGreg Roachuse function e;
3897def6bcSGreg Roachuse function in_array;
396fd01894SGreg Roachuse function uasort;
406fd01894SGreg Roach
416fd01894SGreg Roach/**
426fd01894SGreg Roach * Edit the tree privacy.
436fd01894SGreg Roach */
446fd01894SGreg Roachclass TreePrivacyPage implements RequestHandlerInterface
456fd01894SGreg Roach{
466fd01894SGreg Roach    use ViewResponseTrait;
476fd01894SGreg Roach
48c4943cffSGreg Roach    private TreeService $tree_service;
496fd01894SGreg Roach
506fd01894SGreg Roach    public function __construct(TreeService $tree_service)
516fd01894SGreg Roach    {
526fd01894SGreg Roach        $this->tree_service = $tree_service;
536fd01894SGreg Roach    }
546fd01894SGreg Roach
556fd01894SGreg Roach    /**
566fd01894SGreg Roach     * @param ServerRequestInterface $request
576fd01894SGreg Roach     *
586fd01894SGreg Roach     * @return ResponseInterface
596fd01894SGreg Roach     */
606fd01894SGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
616fd01894SGreg Roach    {
626fd01894SGreg Roach        $this->layout = 'layouts/administration';
636fd01894SGreg Roach
64*b55cbc6bSGreg Roach        $tree                 = Validator::attributes($request)->tree();
656fd01894SGreg Roach        $title                = e($tree->name()) . ' — ' . I18N::translate('Privacy');
6697def6bcSGreg Roach        $all_tags             = $this->tagsForPrivacy();
676fd01894SGreg Roach        $privacy_constants    = $this->privacyConstants();
686fd01894SGreg Roach        $privacy_restrictions = $this->privacyRestrictions($tree);
696fd01894SGreg Roach
706fd01894SGreg Roach        return $this->viewResponse('admin/trees-privacy', [
716fd01894SGreg Roach            'all_tags'             => $all_tags,
726fd01894SGreg Roach            'count_trees'          => $this->tree_service->all()->count(),
736fd01894SGreg Roach            'privacy_constants'    => $privacy_constants,
746fd01894SGreg Roach            'privacy_restrictions' => $privacy_restrictions,
756fd01894SGreg Roach            'title'                => $title,
766fd01894SGreg Roach            'tree'                 => $tree,
776fd01894SGreg Roach        ]);
786fd01894SGreg Roach    }
796fd01894SGreg Roach
806fd01894SGreg Roach    /**
816fd01894SGreg Roach     * Names of our privacy levels
826fd01894SGreg Roach     *
836fd01894SGreg Roach     * @return array<string,string>
846fd01894SGreg Roach     */
856fd01894SGreg Roach    private function privacyConstants(): array
866fd01894SGreg Roach    {
876fd01894SGreg Roach        return [
886fd01894SGreg Roach            'none'         => I18N::translate('Show to visitors'),
896fd01894SGreg Roach            'privacy'      => I18N::translate('Show to members'),
906fd01894SGreg Roach            'confidential' => I18N::translate('Show to managers'),
916fd01894SGreg Roach            'hidden'       => I18N::translate('Hide from everyone'),
926fd01894SGreg Roach        ];
936fd01894SGreg Roach    }
946fd01894SGreg Roach
956fd01894SGreg Roach    /**
966fd01894SGreg Roach     * The current privacy restrictions for a tree.
976fd01894SGreg Roach     *
986fd01894SGreg Roach     * @param Tree $tree
996fd01894SGreg Roach     *
10076d39c55SGreg Roach     * @return array<object>
1016fd01894SGreg Roach     */
1026fd01894SGreg Roach    private function privacyRestrictions(Tree $tree): array
1036fd01894SGreg Roach    {
1046fd01894SGreg Roach        return DB::table('default_resn')
1056fd01894SGreg Roach            ->where('gedcom_id', '=', $tree->id())
1066fd01894SGreg Roach            ->get()
107f70bcff5SGreg Roach            ->map(static function (object $row) use ($tree): object {
1086fd01894SGreg Roach                $row->record = null;
1096fd01894SGreg Roach                $row->label  = '';
1106fd01894SGreg Roach
1116fd01894SGreg Roach                if ($row->xref !== null) {
1126b9cb339SGreg Roach                    $row->record = Registry::gedcomRecordFactory()->make($row->xref, $tree);
1136fd01894SGreg Roach                }
1146fd01894SGreg Roach
1156fd01894SGreg Roach                if ($row->tag_type) {
11697def6bcSGreg Roach                    $row->tag_label = $row->tag_type;
11797def6bcSGreg Roach
11897def6bcSGreg Roach                    foreach (['', Family::RECORD_TYPE . ':', Individual::RECORD_TYPE . ':'] as $prefix) {
11997def6bcSGreg Roach                        $element = Registry::elementFactory()->make($prefix . $row->tag_type);
12097def6bcSGreg Roach
12197def6bcSGreg Roach                        if (!$element instanceof UnknownElement) {
12297def6bcSGreg Roach                            $row->tag_label = $element->label();
12397def6bcSGreg Roach                            break;
12497def6bcSGreg Roach                        }
12597def6bcSGreg Roach                    }
1266fd01894SGreg Roach                } else {
1276fd01894SGreg Roach                    $row->tag_label = '';
1286fd01894SGreg Roach                }
1296fd01894SGreg Roach
1306fd01894SGreg Roach                return $row;
1316fd01894SGreg Roach            })
132f70bcff5SGreg Roach            ->sort(static function (object $x, object $y): int {
13337646143SGreg Roach                return I18N::comparator()($x->tag_label, $y->tag_label);
1346fd01894SGreg Roach            })
1356fd01894SGreg Roach            ->all();
1366fd01894SGreg Roach    }
1376fd01894SGreg Roach
1386fd01894SGreg Roach    /**
13997def6bcSGreg Roach     * Generate a list of tags that can be used in privacy settings.
1406fd01894SGreg Roach     *
14124f2a3afSGreg Roach     * @return array<string>
1426fd01894SGreg Roach     */
14397def6bcSGreg Roach    private function tagsForPrivacy(): array
1446fd01894SGreg Roach    {
14597def6bcSGreg Roach        $tags = [];
1466fd01894SGreg Roach
14797def6bcSGreg Roach        $exclude = ['SEX'];
1486fd01894SGreg Roach
14997def6bcSGreg Roach        foreach ([Family::RECORD_TYPE, Individual::RECORD_TYPE] as $record_type) {
15097def6bcSGreg Roach            foreach (Registry::elementFactory()->make($record_type)->subtags() as $subtag => $occurrence) {
15197def6bcSGreg Roach                if (!in_array($subtag, $exclude, true)) {
15297def6bcSGreg Roach                    $tags[$subtag] = Registry::elementFactory()->make($record_type . ':' . $subtag)->label();
15397def6bcSGreg Roach                }
1546fd01894SGreg Roach            }
1556fd01894SGreg Roach        }
1566fd01894SGreg Roach
15797def6bcSGreg Roach        // SOUR overwrites INDI:SOUR
15897def6bcSGreg Roach        $include = ['REPO', 'SOUR', 'SUBN'];
15997def6bcSGreg Roach
16097def6bcSGreg Roach        foreach ($include as $tag) {
16197def6bcSGreg Roach            $tags[$tag] = Registry::elementFactory()->make($tag) -> label();
16297def6bcSGreg Roach        }
16397def6bcSGreg Roach
16497def6bcSGreg Roach        uasort($tags, I18N::comparator());
1656fd01894SGreg Roach
1666fd01894SGreg Roach        return array_merge(
1676fd01894SGreg Roach            ['' => I18N::translate('All facts and events')],
16897def6bcSGreg Roach            $tags
1696fd01894SGreg Roach        );
1706fd01894SGreg Roach    }
1716fd01894SGreg Roach}
172