xref: /webtrees/app/Http/RequestHandlers/TreePrivacyPage.php (revision 449b311ecf65f677a2595e1e29f712d11ef22f34)
16fd01894SGreg Roach<?php
26fd01894SGreg Roach
36fd01894SGreg Roach/**
46fd01894SGreg Roach * webtrees: online genealogy
5d11be702SGreg Roach * Copyright (C) 2023 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
226f4ec3caSGreg Roachuse Fisharebest\Webtrees\DB;
2397def6bcSGreg Roachuse Fisharebest\Webtrees\Elements\UnknownElement;
2497def6bcSGreg Roachuse Fisharebest\Webtrees\Family;
256fd01894SGreg Roachuse Fisharebest\Webtrees\Http\ViewResponseTrait;
266fd01894SGreg Roachuse Fisharebest\Webtrees\I18N;
2797def6bcSGreg Roachuse Fisharebest\Webtrees\Individual;
286b9cb339SGreg Roachuse Fisharebest\Webtrees\Registry;
296fd01894SGreg Roachuse Fisharebest\Webtrees\Services\TreeService;
306fd01894SGreg Roachuse Fisharebest\Webtrees\Tree;
31b55cbc6bSGreg Roachuse Fisharebest\Webtrees\Validator;
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
5092a78a2fSGreg Roach    /**
5192a78a2fSGreg Roach     * @param TreeService $tree_service
5292a78a2fSGreg Roach     */
536fd01894SGreg Roach    public function __construct(TreeService $tree_service)
546fd01894SGreg Roach    {
556fd01894SGreg Roach        $this->tree_service = $tree_service;
566fd01894SGreg Roach    }
576fd01894SGreg Roach
586fd01894SGreg Roach    /**
596fd01894SGreg Roach     * @param ServerRequestInterface $request
606fd01894SGreg Roach     *
616fd01894SGreg Roach     * @return ResponseInterface
626fd01894SGreg Roach     */
636fd01894SGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
646fd01894SGreg Roach    {
656fd01894SGreg Roach        $this->layout = 'layouts/administration';
666fd01894SGreg Roach
67b55cbc6bSGreg Roach        $tree                 = Validator::attributes($request)->tree();
686fd01894SGreg Roach        $title                = e($tree->name()) . ' — ' . I18N::translate('Privacy');
6997def6bcSGreg Roach        $all_tags             = $this->tagsForPrivacy();
706fd01894SGreg Roach        $privacy_constants    = $this->privacyConstants();
716fd01894SGreg Roach        $privacy_restrictions = $this->privacyRestrictions($tree);
726fd01894SGreg Roach
736fd01894SGreg Roach        return $this->viewResponse('admin/trees-privacy', [
746fd01894SGreg Roach            'all_tags'             => $all_tags,
756fd01894SGreg Roach            'count_trees'          => $this->tree_service->all()->count(),
766fd01894SGreg Roach            'privacy_constants'    => $privacy_constants,
776fd01894SGreg Roach            'privacy_restrictions' => $privacy_restrictions,
786fd01894SGreg Roach            'title'                => $title,
796fd01894SGreg Roach            'tree'                 => $tree,
806fd01894SGreg Roach        ]);
816fd01894SGreg Roach    }
826fd01894SGreg Roach
836fd01894SGreg Roach    /**
846fd01894SGreg Roach     * Names of our privacy levels
856fd01894SGreg Roach     *
866fd01894SGreg Roach     * @return array<string,string>
876fd01894SGreg Roach     */
886fd01894SGreg Roach    private function privacyConstants(): array
896fd01894SGreg Roach    {
906fd01894SGreg Roach        return [
916fd01894SGreg Roach            'none'         => I18N::translate('Show to visitors'),
926fd01894SGreg Roach            'privacy'      => I18N::translate('Show to members'),
936fd01894SGreg Roach            'confidential' => I18N::translate('Show to managers'),
946fd01894SGreg Roach            'hidden'       => I18N::translate('Hide from everyone'),
956fd01894SGreg Roach        ];
966fd01894SGreg Roach    }
976fd01894SGreg Roach
986fd01894SGreg Roach    /**
996fd01894SGreg Roach     * The current privacy restrictions for a tree.
1006fd01894SGreg Roach     *
1016fd01894SGreg Roach     * @param Tree $tree
1026fd01894SGreg Roach     *
10376d39c55SGreg Roach     * @return array<object>
1046fd01894SGreg Roach     */
1056fd01894SGreg Roach    private function privacyRestrictions(Tree $tree): array
1066fd01894SGreg Roach    {
1076fd01894SGreg Roach        return DB::table('default_resn')
1086fd01894SGreg Roach            ->where('gedcom_id', '=', $tree->id())
1096fd01894SGreg Roach            ->get()
110f70bcff5SGreg Roach            ->map(static function (object $row) use ($tree): object {
1116fd01894SGreg Roach                $row->record = null;
1126fd01894SGreg Roach                $row->label  = '';
1136fd01894SGreg Roach
1146fd01894SGreg Roach                if ($row->xref !== null) {
1156b9cb339SGreg Roach                    $row->record = Registry::gedcomRecordFactory()->make($row->xref, $tree);
1166fd01894SGreg Roach                }
1176fd01894SGreg Roach
1186fd01894SGreg Roach                if ($row->tag_type) {
11997def6bcSGreg Roach                    $row->tag_label = $row->tag_type;
12097def6bcSGreg Roach
12197def6bcSGreg Roach                    foreach (['', Family::RECORD_TYPE . ':', Individual::RECORD_TYPE . ':'] as $prefix) {
12297def6bcSGreg Roach                        $element = Registry::elementFactory()->make($prefix . $row->tag_type);
12397def6bcSGreg Roach
12497def6bcSGreg Roach                        if (!$element instanceof UnknownElement) {
12597def6bcSGreg Roach                            $row->tag_label = $element->label();
12697def6bcSGreg Roach                            break;
12797def6bcSGreg Roach                        }
12897def6bcSGreg Roach                    }
1296fd01894SGreg Roach                } else {
1306fd01894SGreg Roach                    $row->tag_label = '';
1316fd01894SGreg Roach                }
1326fd01894SGreg Roach
1336fd01894SGreg Roach                return $row;
1346fd01894SGreg Roach            })
135*f25fc0f9SGreg Roach            ->sort(static fn (object $x, object $y): int => I18N::comparator()($x->tag_label, $y->tag_label))
1366fd01894SGreg Roach            ->all();
1376fd01894SGreg Roach    }
1386fd01894SGreg Roach
1396fd01894SGreg Roach    /**
14097def6bcSGreg Roach     * Generate a list of tags that can be used in privacy settings.
1416fd01894SGreg Roach     *
14224f2a3afSGreg Roach     * @return array<string>
1436fd01894SGreg Roach     */
14497def6bcSGreg Roach    private function tagsForPrivacy(): array
1456fd01894SGreg Roach    {
14697def6bcSGreg Roach        $tags = [];
1476fd01894SGreg Roach
14897def6bcSGreg Roach        $exclude = ['SEX'];
1496fd01894SGreg Roach
15097def6bcSGreg Roach        foreach ([Family::RECORD_TYPE, Individual::RECORD_TYPE] as $record_type) {
15197def6bcSGreg Roach            foreach (Registry::elementFactory()->make($record_type)->subtags() as $subtag => $occurrence) {
15297def6bcSGreg Roach                if (!in_array($subtag, $exclude, true)) {
15397def6bcSGreg Roach                    $tags[$subtag] = Registry::elementFactory()->make($record_type . ':' . $subtag)->label();
15497def6bcSGreg Roach                }
1556fd01894SGreg Roach            }
1566fd01894SGreg Roach        }
1576fd01894SGreg Roach
15897def6bcSGreg Roach        // SOUR overwrites INDI:SOUR
15997def6bcSGreg Roach        $include = ['REPO', 'SOUR', 'SUBN'];
16097def6bcSGreg Roach
16197def6bcSGreg Roach        foreach ($include as $tag) {
16297def6bcSGreg Roach            $tags[$tag] = Registry::elementFactory()->make($tag) -> label();
16397def6bcSGreg Roach        }
16497def6bcSGreg Roach
16597def6bcSGreg Roach        uasort($tags, I18N::comparator());
1666fd01894SGreg Roach
1676fd01894SGreg Roach        return array_merge(
1686fd01894SGreg Roach            ['' => I18N::translate('All facts and events')],
16997def6bcSGreg Roach            $tags
1706fd01894SGreg Roach        );
1716fd01894SGreg Roach    }
1726fd01894SGreg Roach}
173