1<?php 2 3/** 4 * webtrees: online genealogy 5 * Copyright (C) 2022 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\Auth; 23use Fisharebest\Webtrees\Contracts\ElementInterface; 24use Fisharebest\Webtrees\Contracts\UserInterface; 25use Fisharebest\Webtrees\Date; 26use Fisharebest\Webtrees\Elements\UnknownElement; 27use Fisharebest\Webtrees\Http\ViewResponseTrait; 28use Fisharebest\Webtrees\I18N; 29use Fisharebest\Webtrees\Module\ModuleThemeInterface; 30use Fisharebest\Webtrees\Registry; 31use Fisharebest\Webtrees\Services\ModuleService; 32use Fisharebest\Webtrees\Services\TreeService; 33use Fisharebest\Webtrees\Services\UserService; 34use Fisharebest\Webtrees\SurnameTradition; 35use Fisharebest\Webtrees\Validator; 36use Illuminate\Support\Collection; 37use Psr\Http\Message\ResponseInterface; 38use Psr\Http\Message\ServerRequestInterface; 39use Psr\Http\Server\RequestHandlerInterface; 40 41use function e; 42use function explode; 43use function in_array; 44 45/** 46 * Edit the tree preferences. 47 */ 48class TreePreferencesPage implements RequestHandlerInterface 49{ 50 use ViewResponseTrait; 51 52 private ModuleService $module_service; 53 54 private TreeService $tree_service; 55 56 private UserService $user_service; 57 58 /** 59 * @param ModuleService $module_service 60 * @param TreeService $tree_service 61 * @param UserService $user_service 62 */ 63 public function __construct( 64 ModuleService $module_service, 65 TreeService $tree_service, 66 UserService $user_service 67 ) { 68 $this->module_service = $module_service; 69 $this->tree_service = $tree_service; 70 $this->user_service = $user_service; 71 } 72 73 /** 74 * @param ServerRequestInterface $request 75 * 76 * @return ResponseInterface 77 */ 78 public function handle(ServerRequestInterface $request): ResponseInterface 79 { 80 $this->layout = 'layouts/administration'; 81 82 $tree = Validator::attributes($request)->tree(); 83 $data_folder = Registry::filesystem()->dataName(); 84 85 $french_calendar_start = new Date('22 SEP 1792'); 86 $french_calendar_end = new Date('31 DEC 1805'); 87 $gregorian_calendar_start = new Date('15 OCT 1582'); 88 89 $surname_list_styles = [ 90 /* I18N: Layout option for lists of names */ 91 'style1' => I18N::translate('list'), 92 /* I18N: Layout option for lists of names */ 93 'style2' => I18N::translate('table'), 94 /* I18N: Layout option for lists of names */ 95 'style3' => I18N::translate('tag cloud'), 96 ]; 97 98 $page_layouts = [ 99 /* I18N: page orientation */ 100 0 => I18N::translate('Portrait'), 101 /* I18N: page orientation */ 102 1 => I18N::translate('Landscape'), 103 ]; 104 105 $formats = [ 106 /* I18N: https://en.wikipedia.org/wiki/Plain_text */ 107 '' => I18N::translate('plain text'), 108 /* I18N: https://en.wikipedia.org/wiki/Markdown */ 109 'markdown' => I18N::translate('markdown'), 110 ]; 111 112 $source_types = [ 113 0 => I18N::translate('none'), 114 1 => I18N::translate('facts'), 115 2 => I18N::translate('records'), 116 ]; 117 118 $theme_options = $this->module_service 119 ->findByInterface(ModuleThemeInterface::class) 120 ->map($this->module_service->titleMapper()) 121 ->prepend(I18N::translate('<default theme>'), ''); 122 123 $privacy_options = [ 124 Auth::PRIV_USER => I18N::translate('Show to members'), 125 Auth::PRIV_NONE => I18N::translate('Show to managers'), 126 Auth::PRIV_HIDE => I18N::translate('Hide from everyone'), 127 ]; 128 129 // For historical reasons, we have two fields in one 130 $calendar_formats = explode('_and_', $tree->getPreference('CALENDAR_FORMAT') . '_and_'); 131 132 // Split into separate fields 133 $relatives_events = explode(',', $tree->getPreference('SHOW_RELATIVES_EVENTS')); 134 135 $pedigree_individual = Registry::individualFactory()->make($tree->getPreference('PEDIGREE_ROOT_ID'), $tree); 136 137 $members = $this->user_service->all()->filter(static function (UserInterface $user) use ($tree): bool { 138 return Auth::isMember($tree, $user); 139 }); 140 141 $ignore_facts = ['CHAN', 'CHIL', 'FAMC', 'FAMS', 'HUSB', 'NOTE', 'OBJE', 'SOUR', 'SUBM', 'WIFE', 'NAME', 'SEX']; 142 143 $all_family_facts = Collection::make(Registry::elementFactory()->make('FAM')->subtags()) 144 ->filter(static fn (string $value, string $key): bool => !in_array($key, $ignore_facts, true)) 145 ->mapWithKeys(static fn (string $value, string $key): array => [$key => 'FAM:' . $key]) 146 ->map(static fn (string $tag): ElementInterface => Registry::elementFactory()->make($tag)) 147 ->filter(static fn (ElementInterface $element): bool => !$element instanceof UnknownElement) 148 ->map(static fn (ElementInterface $element): string => $element->label()) 149 ->sort(I18N::comparator()); 150 151 $all_individual_facts = Collection::make(Registry::elementFactory()->make('INDI')->subtags()) 152 ->filter(static fn (string $value, string $key): bool => !in_array($key, $ignore_facts, true)) 153 ->mapWithKeys(static fn (string $value, string $key): array => [$key => 'INDI:' . $key]) 154 ->map(static fn (string $tag): ElementInterface => Registry::elementFactory()->make($tag)) 155 ->filter(static fn (ElementInterface $element): bool => !$element instanceof UnknownElement) 156 ->map(static fn (ElementInterface $element): string => $element->label()) 157 ->sort(I18N::comparator()); 158 159 $all_surname_traditions = SurnameTradition::allDescriptions(); 160 161 $tree_count = $this->tree_service->all()->count(); 162 163 $title = I18N::translate('Preferences') . ' — ' . e($tree->title()); 164 165 $base_url = Validator::attributes($request)->string('base_url'); 166 167 return $this->viewResponse('admin/trees-preferences', [ 168 'all_family_facts' => $all_family_facts, 169 'all_individual_facts' => $all_individual_facts, 170 'all_surname_traditions' => $all_surname_traditions, 171 'base_url' => $base_url, 172 'calendar_formats' => $calendar_formats, 173 'data_folder' => $data_folder, 174 'formats' => $formats, 175 'french_calendar_end' => $french_calendar_end, 176 'french_calendar_start' => $french_calendar_start, 177 'gregorian_calendar_start' => $gregorian_calendar_start, 178 'members' => $members, 179 'page_layouts' => $page_layouts, 180 'pedigree_individual' => $pedigree_individual, 181 'privacy_options' => $privacy_options, 182 'relatives_events' => $relatives_events, 183 'source_types' => $source_types, 184 'surname_list_styles' => $surname_list_styles, 185 'theme_options' => $theme_options, 186 'title' => $title, 187 'tree' => $tree, 188 'tree_count' => $tree_count, 189 ]); 190 } 191} 192