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