xref: /webtrees/app/Http/RequestHandlers/TreePreferencesPage.php (revision 92a78a2f1d314ef404e1445d7916cbee426ce636)
16fd01894SGreg Roach<?php
26fd01894SGreg Roach
36fd01894SGreg Roach/**
46fd01894SGreg Roach * webtrees: online genealogy
55bfc6897SGreg Roach * Copyright (C) 2022 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\Auth;
233faaf002SGreg Roachuse Fisharebest\Webtrees\Contracts\ElementInterface;
246fd01894SGreg Roachuse Fisharebest\Webtrees\Contracts\UserInterface;
256fd01894SGreg Roachuse Fisharebest\Webtrees\Date;
263faaf002SGreg Roachuse Fisharebest\Webtrees\Elements\UnknownElement;
276fd01894SGreg Roachuse Fisharebest\Webtrees\Http\ViewResponseTrait;
286fd01894SGreg Roachuse Fisharebest\Webtrees\I18N;
296fd01894SGreg Roachuse Fisharebest\Webtrees\Module\ModuleThemeInterface;
306b9cb339SGreg Roachuse Fisharebest\Webtrees\Registry;
316fd01894SGreg Roachuse Fisharebest\Webtrees\Services\ModuleService;
326fd01894SGreg Roachuse Fisharebest\Webtrees\Services\TreeService;
336fd01894SGreg Roachuse Fisharebest\Webtrees\Services\UserService;
346fd01894SGreg Roachuse Fisharebest\Webtrees\SurnameTradition;
35b55cbc6bSGreg Roachuse Fisharebest\Webtrees\Validator;
3637646143SGreg Roachuse Illuminate\Support\Collection;
376fd01894SGreg Roachuse Psr\Http\Message\ResponseInterface;
386fd01894SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
396fd01894SGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
406fd01894SGreg Roach
416fd01894SGreg Roachuse function app;
426fd01894SGreg Roachuse function e;
436fd01894SGreg Roachuse function explode;
443faaf002SGreg Roachuse function in_array;
456fd01894SGreg Roach
466fd01894SGreg Roach/**
476fd01894SGreg Roach * Edit the tree preferences.
486fd01894SGreg Roach */
496fd01894SGreg Roachclass TreePreferencesPage implements RequestHandlerInterface
506fd01894SGreg Roach{
516fd01894SGreg Roach    use ViewResponseTrait;
526fd01894SGreg Roach
53c4943cffSGreg Roach    private ModuleService $module_service;
546fd01894SGreg Roach
55c4943cffSGreg Roach    private TreeService $tree_service;
566fd01894SGreg Roach
57c4943cffSGreg Roach    private UserService $user_service;
586fd01894SGreg Roach
59*92a78a2fSGreg Roach    /**
60*92a78a2fSGreg Roach     * @param ModuleService $module_service
61*92a78a2fSGreg Roach     * @param TreeService   $tree_service
62*92a78a2fSGreg Roach     * @param UserService   $user_service
63*92a78a2fSGreg Roach     */
646fd01894SGreg Roach    public function __construct(
656fd01894SGreg Roach        ModuleService $module_service,
666fd01894SGreg Roach        TreeService $tree_service,
676fd01894SGreg Roach        UserService $user_service
686fd01894SGreg Roach    ) {
696fd01894SGreg Roach        $this->module_service = $module_service;
706fd01894SGreg Roach        $this->tree_service   = $tree_service;
716fd01894SGreg Roach        $this->user_service   = $user_service;
726fd01894SGreg Roach    }
736fd01894SGreg Roach
746fd01894SGreg Roach    /**
756fd01894SGreg Roach     * @param ServerRequestInterface $request
766fd01894SGreg Roach     *
776fd01894SGreg Roach     * @return ResponseInterface
786fd01894SGreg Roach     */
796fd01894SGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
806fd01894SGreg Roach    {
816fd01894SGreg Roach        $this->layout = 'layouts/administration';
826fd01894SGreg Roach
83b55cbc6bSGreg Roach        $tree        = Validator::attributes($request)->tree();
846b9cb339SGreg Roach        $data_folder = Registry::filesystem()->dataName();
856fd01894SGreg Roach
866fd01894SGreg Roach        $french_calendar_start    = new Date('22 SEP 1792');
876fd01894SGreg Roach        $french_calendar_end      = new Date('31 DEC 1805');
886fd01894SGreg Roach        $gregorian_calendar_start = new Date('15 OCT 1582');
896fd01894SGreg Roach
906fd01894SGreg Roach        $surname_list_styles = [
916fd01894SGreg Roach            /* I18N: Layout option for lists of names */
926fd01894SGreg Roach            'style1' => I18N::translate('list'),
936fd01894SGreg Roach            /* I18N: Layout option for lists of names */
946fd01894SGreg Roach            'style2' => I18N::translate('table'),
956fd01894SGreg Roach            /* I18N: Layout option for lists of names */
966fd01894SGreg Roach            'style3' => I18N::translate('tag cloud'),
976fd01894SGreg Roach        ];
986fd01894SGreg Roach
996fd01894SGreg Roach        $page_layouts = [
1006fd01894SGreg Roach            /* I18N: page orientation */
1016fd01894SGreg Roach            0 => I18N::translate('Portrait'),
1026fd01894SGreg Roach            /* I18N: page orientation */
1036fd01894SGreg Roach            1 => I18N::translate('Landscape'),
1046fd01894SGreg Roach        ];
1056fd01894SGreg Roach
1066fd01894SGreg Roach        $formats = [
107fc5ef972SGreg Roach            /* I18N: https://en.wikipedia.org/wiki/Plain_text */
10867721f6fSGreg Roach            ''         => I18N::translate('plain text'),
1096fd01894SGreg Roach            /* I18N: https://en.wikipedia.org/wiki/Markdown */
1106fd01894SGreg Roach            'markdown' => I18N::translate('markdown'),
1116fd01894SGreg Roach        ];
1126fd01894SGreg Roach
1136fd01894SGreg Roach        $source_types = [
1146fd01894SGreg Roach            0 => I18N::translate('none'),
1156fd01894SGreg Roach            1 => I18N::translate('facts'),
1166fd01894SGreg Roach            2 => I18N::translate('records'),
1176fd01894SGreg Roach        ];
1186fd01894SGreg Roach
1196fd01894SGreg Roach        $theme_options = $this->module_service
1206fd01894SGreg Roach            ->findByInterface(ModuleThemeInterface::class)
1216fd01894SGreg Roach            ->map($this->module_service->titleMapper())
1226fd01894SGreg Roach            ->prepend(I18N::translate('<default theme>'), '');
1236fd01894SGreg Roach
1246fd01894SGreg Roach        $privacy_options = [
1256fd01894SGreg Roach            Auth::PRIV_USER => I18N::translate('Show to members'),
1266fd01894SGreg Roach            Auth::PRIV_NONE => I18N::translate('Show to managers'),
1276fd01894SGreg Roach            Auth::PRIV_HIDE => I18N::translate('Hide from everyone'),
1286fd01894SGreg Roach        ];
1296fd01894SGreg Roach
1306fd01894SGreg Roach        // For historical reasons, we have two fields in one
1316fd01894SGreg Roach        $calendar_formats = explode('_and_', $tree->getPreference('CALENDAR_FORMAT') . '_and_');
1326fd01894SGreg Roach
1336fd01894SGreg Roach        // Split into separate fields
1346fd01894SGreg Roach        $relatives_events = explode(',', $tree->getPreference('SHOW_RELATIVES_EVENTS'));
1356fd01894SGreg Roach
1366b9cb339SGreg Roach        $pedigree_individual = Registry::individualFactory()->make($tree->getPreference('PEDIGREE_ROOT_ID'), $tree);
1376fd01894SGreg Roach
1386fd01894SGreg Roach        $members = $this->user_service->all()->filter(static function (UserInterface $user) use ($tree): bool {
1396fd01894SGreg Roach            return Auth::isMember($tree, $user);
1406fd01894SGreg Roach        });
1416fd01894SGreg Roach
1423faaf002SGreg Roach        $ignore_facts = ['CHAN', 'CHIL', 'FAMC', 'FAMS', 'HUSB', 'NOTE', 'OBJE', 'SOUR', 'SUBM', 'WIFE'];
1433faaf002SGreg Roach
1443faaf002SGreg Roach        $all_family_facts = Collection::make(Registry::elementFactory()->make('FAM')->subtags())
1453faaf002SGreg Roach            ->filter(static fn (string $value, string $key): bool => !in_array($key, $ignore_facts, true))
1463faaf002SGreg Roach            ->mapWithKeys(static fn (string $value, string $key): array => [$key => 'FAM:' . $key])
1473faaf002SGreg Roach            ->map(static fn (string $tag): ElementInterface => Registry::elementFactory()->make($tag))
1483faaf002SGreg Roach            ->filter(static fn (ElementInterface $element): bool => !$element instanceof UnknownElement)
1493faaf002SGreg Roach            ->map(static fn (ElementInterface $element): string => $element->label())
15037646143SGreg Roach            ->sort(I18N::comparator());
15137646143SGreg Roach
1523faaf002SGreg Roach        $all_individual_facts = Collection::make(Registry::elementFactory()->make('INDI')->subtags())
1533faaf002SGreg Roach            ->filter(static fn (string $value, string $key): bool => !in_array($key, $ignore_facts, true))
1543faaf002SGreg Roach            ->mapWithKeys(static fn (string $value, string $key): array => [$key => 'INDI:' . $key])
1553faaf002SGreg Roach            ->map(static fn (string $tag): ElementInterface => Registry::elementFactory()->make($tag))
1563faaf002SGreg Roach            ->filter(static fn (ElementInterface $element): bool => !$element instanceof UnknownElement)
1573faaf002SGreg Roach            ->map(static fn (ElementInterface $element): string => $element->label())
15837646143SGreg Roach            ->sort(I18N::comparator());
1596fd01894SGreg Roach
1606fd01894SGreg Roach        $all_surname_traditions = SurnameTradition::allDescriptions();
1616fd01894SGreg Roach
1626fd01894SGreg Roach        $tree_count = $this->tree_service->all()->count();
1636fd01894SGreg Roach
1646fd01894SGreg Roach        $title = I18N::translate('Preferences') . ' — ' . e($tree->title());
1656fd01894SGreg Roach
166b55cbc6bSGreg Roach        $base_url = Validator::attributes($request)->string('base_url');
1676fd01894SGreg Roach
1686fd01894SGreg Roach        return $this->viewResponse('admin/trees-preferences', [
1693faaf002SGreg Roach            'all_family_facts'         => $all_family_facts,
1703faaf002SGreg Roach            'all_individual_facts'     => $all_individual_facts,
1716fd01894SGreg Roach            'all_surname_traditions'   => $all_surname_traditions,
1726fd01894SGreg Roach            'base_url'                 => $base_url,
1736fd01894SGreg Roach            'calendar_formats'         => $calendar_formats,
1746fd01894SGreg Roach            'data_folder'              => $data_folder,
1756fd01894SGreg Roach            'formats'                  => $formats,
1766fd01894SGreg Roach            'french_calendar_end'      => $french_calendar_end,
1776fd01894SGreg Roach            'french_calendar_start'    => $french_calendar_start,
1786fd01894SGreg Roach            'gregorian_calendar_start' => $gregorian_calendar_start,
1796fd01894SGreg Roach            'members'                  => $members,
1806fd01894SGreg Roach            'page_layouts'             => $page_layouts,
1816fd01894SGreg Roach            'pedigree_individual'      => $pedigree_individual,
1826fd01894SGreg Roach            'privacy_options'          => $privacy_options,
1836fd01894SGreg Roach            'relatives_events'         => $relatives_events,
1846fd01894SGreg Roach            'source_types'             => $source_types,
1856fd01894SGreg Roach            'surname_list_styles'      => $surname_list_styles,
1866fd01894SGreg Roach            'theme_options'            => $theme_options,
1876fd01894SGreg Roach            'title'                    => $title,
1886fd01894SGreg Roach            'tree'                     => $tree,
1896fd01894SGreg Roach            'tree_count'               => $tree_count,
1906fd01894SGreg Roach        ]);
1916fd01894SGreg Roach    }
1926fd01894SGreg Roach}
193