1<?php 2 3/** 4 * webtrees: online genealogy 5 * 'Copyright (C) 2023 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 /** 51 * @param TreeService $tree_service 52 */ 53 public function __construct(TreeService $tree_service) 54 { 55 $this->tree_service = $tree_service; 56 } 57 58 /** 59 * @param ServerRequestInterface $request 60 * 61 * @return ResponseInterface 62 */ 63 public function handle(ServerRequestInterface $request): ResponseInterface 64 { 65 $this->layout = 'layouts/administration'; 66 67 $tree = Validator::attributes($request)->tree(); 68 $title = e($tree->name()) . ' — ' . I18N::translate('Privacy'); 69 $all_tags = $this->tagsForPrivacy(); 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<object> 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 (object $row) use ($tree): object { 111 $row->record = null; 112 $row->label = ''; 113 114 if ($row->xref !== null) { 115 $row->record = Registry::gedcomRecordFactory()->make($row->xref, $tree); 116 } 117 118 if ($row->tag_type) { 119 $row->tag_label = $row->tag_type; 120 121 foreach (['', Family::RECORD_TYPE . ':', Individual::RECORD_TYPE . ':'] as $prefix) { 122 $element = Registry::elementFactory()->make($prefix . $row->tag_type); 123 124 if (!$element instanceof UnknownElement) { 125 $row->tag_label = $element->label(); 126 break; 127 } 128 } 129 } else { 130 $row->tag_label = ''; 131 } 132 133 return $row; 134 }) 135 ->sort(static function (object $x, object $y): int { 136 return I18N::comparator()($x->tag_label, $y->tag_label); 137 }) 138 ->all(); 139 } 140 141 /** 142 * Generate a list of tags that can be used in privacy settings. 143 * 144 * @return array<string> 145 */ 146 private function tagsForPrivacy(): array 147 { 148 $tags = []; 149 150 $exclude = ['SEX']; 151 152 foreach ([Family::RECORD_TYPE, Individual::RECORD_TYPE] as $record_type) { 153 foreach (Registry::elementFactory()->make($record_type)->subtags() as $subtag => $occurrence) { 154 if (!in_array($subtag, $exclude, true)) { 155 $tags[$subtag] = Registry::elementFactory()->make($record_type . ':' . $subtag)->label(); 156 } 157 } 158 } 159 160 // SOUR overwrites INDI:SOUR 161 $include = ['REPO', 'SOUR', 'SUBN']; 162 163 foreach ($include as $tag) { 164 $tags[$tag] = Registry::elementFactory()->make($tag) -> label(); 165 } 166 167 uasort($tags, I18N::comparator()); 168 169 return array_merge( 170 ['' => I18N::translate('All facts and events')], 171 $tags 172 ); 173 } 174} 175