1<?php 2 3/** 4 * webtrees: online genealogy 5 * Copyright (C) 2020 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 <http://www.gnu.org/licenses/>. 16 */ 17 18declare(strict_types=1); 19 20namespace Fisharebest\Webtrees\Http\RequestHandlers; 21 22use Fisharebest\Webtrees\Factory; 23use Fisharebest\Webtrees\GedcomTag; 24use Fisharebest\Webtrees\Http\ViewResponseTrait; 25use Fisharebest\Webtrees\I18N; 26use Fisharebest\Webtrees\Services\TreeService; 27use Fisharebest\Webtrees\Tree; 28use Illuminate\Database\Capsule\Manager as DB; 29use Psr\Http\Message\ResponseInterface; 30use Psr\Http\Message\ServerRequestInterface; 31use Psr\Http\Server\RequestHandlerInterface; 32use stdClass; 33 34use function array_merge; 35use function array_unique; 36use function assert; 37use function e; 38use function explode; 39use function uasort; 40 41/** 42 * Edit the tree privacy. 43 */ 44class TreePrivacyPage implements RequestHandlerInterface 45{ 46 use ViewResponseTrait; 47 48 /** @var TreeService */ 49 private $tree_service; 50 51 public function __construct(TreeService $tree_service) 52 { 53 $this->tree_service = $tree_service; 54 } 55 56 /** 57 * @param ServerRequestInterface $request 58 * 59 * @return ResponseInterface 60 */ 61 public function handle(ServerRequestInterface $request): ResponseInterface 62 { 63 $this->layout = 'layouts/administration'; 64 65 $tree = $request->getAttribute('tree'); 66 assert($tree instanceof Tree); 67 68 $title = e($tree->name()) . ' — ' . I18N::translate('Privacy'); 69 $all_tags = $this->tagsForPrivacy($tree); 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<string,string> 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 (stdClass $row) use ($tree): stdClass { 111 $row->record = null; 112 $row->label = ''; 113 114 if ($row->xref !== null) { 115 $row->record = Factory::gedcomRecord()->make($row->xref, $tree); 116 } 117 118 if ($row->tag_type) { 119 $row->tag_label = GedcomTag::getLabel($row->tag_type); 120 } else { 121 $row->tag_label = ''; 122 } 123 124 return $row; 125 }) 126 ->sort(static function (stdClass $x, stdClass $y): int { 127 return I18N::strcasecmp($x->tag_label, $y->tag_label); 128 }) 129 ->all(); 130 } 131 132 /** 133 * Generate a list of potential problems with the server. 134 * 135 * @param Tree $tree 136 * 137 * @return string[] 138 */ 139 private function tagsForPrivacy(Tree $tree): array 140 { 141 $tags = array_unique(array_merge( 142 explode(',', $tree->getPreference('INDI_FACTS_ADD')), 143 explode(',', $tree->getPreference('INDI_FACTS_UNIQUE')), 144 explode(',', $tree->getPreference('FAM_FACTS_ADD')), 145 explode(',', $tree->getPreference('FAM_FACTS_UNIQUE')), 146 explode(',', $tree->getPreference('NOTE_FACTS_ADD')), 147 explode(',', $tree->getPreference('NOTE_FACTS_UNIQUE')), 148 explode(',', $tree->getPreference('SOUR_FACTS_ADD')), 149 explode(',', $tree->getPreference('SOUR_FACTS_UNIQUE')), 150 explode(',', $tree->getPreference('REPO_FACTS_ADD')), 151 explode(',', $tree->getPreference('REPO_FACTS_UNIQUE')), 152 [ 153 'SOUR', 154 'REPO', 155 'OBJE', 156 '_PRIM', 157 'NOTE', 158 'SUBM', 159 'SUBN', 160 '_UID', 161 'CHAN', 162 ] 163 )); 164 165 $all_tags = []; 166 167 foreach ($tags as $tag) { 168 if ($tag) { 169 $all_tags[$tag] = GedcomTag::getLabel($tag); 170 } 171 } 172 173 uasort($all_tags, '\Fisharebest\Webtrees\I18N::strcasecmp'); 174 175 return array_merge( 176 ['' => I18N::translate('All facts and events')], 177 $all_tags 178 ); 179 } 180} 181