xref: /webtrees/app/Http/RequestHandlers/TreePrivacyPage.php (revision 37646143628d6d9ab8f62ea7e5fc7d45a028daa9)
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
226fd01894SGreg Roachuse Fisharebest\Webtrees\GedcomTag;
236fd01894SGreg Roachuse Fisharebest\Webtrees\Http\ViewResponseTrait;
246fd01894SGreg Roachuse Fisharebest\Webtrees\I18N;
256b9cb339SGreg Roachuse Fisharebest\Webtrees\Registry;
266fd01894SGreg Roachuse Fisharebest\Webtrees\Services\TreeService;
276fd01894SGreg Roachuse Fisharebest\Webtrees\Tree;
286fd01894SGreg Roachuse Illuminate\Database\Capsule\Manager as DB;
296fd01894SGreg Roachuse Psr\Http\Message\ResponseInterface;
306fd01894SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
316fd01894SGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
326fd01894SGreg Roachuse stdClass;
336fd01894SGreg Roach
346fd01894SGreg Roachuse function array_merge;
356fd01894SGreg Roachuse function array_unique;
366fd01894SGreg Roachuse function assert;
376fd01894SGreg Roachuse function e;
386fd01894SGreg Roachuse function explode;
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
486fd01894SGreg Roach    /** @var TreeService */
496fd01894SGreg Roach    private $tree_service;
506fd01894SGreg Roach
516fd01894SGreg Roach    public function __construct(TreeService $tree_service)
526fd01894SGreg Roach    {
536fd01894SGreg Roach        $this->tree_service = $tree_service;
546fd01894SGreg Roach    }
556fd01894SGreg Roach
566fd01894SGreg Roach    /**
576fd01894SGreg Roach     * @param ServerRequestInterface $request
586fd01894SGreg Roach     *
596fd01894SGreg Roach     * @return ResponseInterface
606fd01894SGreg Roach     */
616fd01894SGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
626fd01894SGreg Roach    {
636fd01894SGreg Roach        $this->layout = 'layouts/administration';
646fd01894SGreg Roach
656fd01894SGreg Roach        $tree = $request->getAttribute('tree');
666fd01894SGreg Roach        assert($tree instanceof Tree);
676fd01894SGreg Roach
686fd01894SGreg Roach        $title                = e($tree->name()) . ' — ' . I18N::translate('Privacy');
696fd01894SGreg Roach        $all_tags             = $this->tagsForPrivacy($tree);
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     *
1036fd01894SGreg Roach     * @return array<string,string>
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()
1106fd01894SGreg Roach            ->map(static function (stdClass $row) use ($tree): stdClass {
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) {
1196fd01894SGreg Roach                    $row->tag_label = GedcomTag::getLabel($row->tag_type);
1206fd01894SGreg Roach                } else {
1216fd01894SGreg Roach                    $row->tag_label = '';
1226fd01894SGreg Roach                }
1236fd01894SGreg Roach
1246fd01894SGreg Roach                return $row;
1256fd01894SGreg Roach            })
1266fd01894SGreg Roach            ->sort(static function (stdClass $x, stdClass $y): int {
127*37646143SGreg Roach                return I18N::comparator()($x->tag_label, $y->tag_label);
1286fd01894SGreg Roach            })
1296fd01894SGreg Roach            ->all();
1306fd01894SGreg Roach    }
1316fd01894SGreg Roach
1326fd01894SGreg Roach    /**
1336fd01894SGreg Roach     * Generate a list of potential problems with the server.
1346fd01894SGreg Roach     *
1356fd01894SGreg Roach     * @param Tree $tree
1366fd01894SGreg Roach     *
13724f2a3afSGreg Roach     * @return array<string>
1386fd01894SGreg Roach     */
1396fd01894SGreg Roach    private function tagsForPrivacy(Tree $tree): array
1406fd01894SGreg Roach    {
1416fd01894SGreg Roach        $tags = array_unique(array_merge(
1426fd01894SGreg Roach            explode(',', $tree->getPreference('INDI_FACTS_ADD')),
1436fd01894SGreg Roach            explode(',', $tree->getPreference('INDI_FACTS_UNIQUE')),
1446fd01894SGreg Roach            explode(',', $tree->getPreference('FAM_FACTS_ADD')),
1456fd01894SGreg Roach            explode(',', $tree->getPreference('FAM_FACTS_UNIQUE')),
1466fd01894SGreg Roach            explode(',', $tree->getPreference('NOTE_FACTS_ADD')),
1476fd01894SGreg Roach            explode(',', $tree->getPreference('NOTE_FACTS_UNIQUE')),
1486fd01894SGreg Roach            explode(',', $tree->getPreference('SOUR_FACTS_ADD')),
1496fd01894SGreg Roach            explode(',', $tree->getPreference('SOUR_FACTS_UNIQUE')),
1506fd01894SGreg Roach            explode(',', $tree->getPreference('REPO_FACTS_ADD')),
1516fd01894SGreg Roach            explode(',', $tree->getPreference('REPO_FACTS_UNIQUE')),
1526fd01894SGreg Roach            [
1536fd01894SGreg Roach                'SOUR',
1546fd01894SGreg Roach                'REPO',
1556fd01894SGreg Roach                'OBJE',
1566fd01894SGreg Roach                '_PRIM',
1576fd01894SGreg Roach                'NOTE',
1586fd01894SGreg Roach                'SUBM',
1596fd01894SGreg Roach                'SUBN',
1606fd01894SGreg Roach                '_UID',
1616fd01894SGreg Roach                'CHAN',
1626fd01894SGreg Roach            ]
1636fd01894SGreg Roach        ));
1646fd01894SGreg Roach
1656fd01894SGreg Roach        $all_tags = [];
1666fd01894SGreg Roach
1676fd01894SGreg Roach        foreach ($tags as $tag) {
1686fd01894SGreg Roach            if ($tag) {
1696fd01894SGreg Roach                $all_tags[$tag] = GedcomTag::getLabel($tag);
1706fd01894SGreg Roach            }
1716fd01894SGreg Roach        }
1726fd01894SGreg Roach
173*37646143SGreg Roach        uasort($all_tags, I18N::comparator());
1746fd01894SGreg Roach
1756fd01894SGreg Roach        return array_merge(
1766fd01894SGreg Roach            ['' => I18N::translate('All facts and events')],
1776fd01894SGreg Roach            $all_tags
1786fd01894SGreg Roach        );
1796fd01894SGreg Roach    }
1806fd01894SGreg Roach}
181