xref: /webtrees/app/Http/RequestHandlers/SitePreferencesAction.php (revision a1bd562d15d6654c7f635d5f296b83f5d866e1cb)
1c7aa856bSGreg Roach<?php
2c7aa856bSGreg Roach
3c7aa856bSGreg Roach/**
4c7aa856bSGreg Roach * webtrees: online genealogy
5d11be702SGreg Roach * Copyright (C) 2023 webtrees development team
6c7aa856bSGreg Roach * This program is free software: you can redistribute it and/or modify
7c7aa856bSGreg Roach * it under the terms of the GNU General Public License as published by
8c7aa856bSGreg Roach * the Free Software Foundation, either version 3 of the License, or
9c7aa856bSGreg Roach * (at your option) any later version.
10c7aa856bSGreg Roach * This program is distributed in the hope that it will be useful,
11c7aa856bSGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
12c7aa856bSGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13c7aa856bSGreg Roach * GNU General Public License for more details.
14c7aa856bSGreg 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/>.
16c7aa856bSGreg Roach */
17c7aa856bSGreg Roach
18c7aa856bSGreg Roachdeclare(strict_types=1);
19c7aa856bSGreg Roach
20c7aa856bSGreg Roachnamespace Fisharebest\Webtrees\Http\RequestHandlers;
21c7aa856bSGreg Roach
22c7aa856bSGreg Roachuse Fisharebest\Webtrees\FlashMessages;
23c7aa856bSGreg Roachuse Fisharebest\Webtrees\I18N;
24c7aa856bSGreg Roachuse Fisharebest\Webtrees\Site;
25748dbe15SGreg Roachuse Fisharebest\Webtrees\Validator;
26c7aa856bSGreg Roachuse Psr\Http\Message\ResponseInterface;
27c7aa856bSGreg Roachuse Psr\Http\Message\ServerRequestInterface;
28c7aa856bSGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
29c7aa856bSGreg Roach
30*a1bd562dSGreg Roachuse function e;
31*a1bd562dSGreg Roachuse function is_writable;
32c7aa856bSGreg Roachuse function redirect;
33c7aa856bSGreg Roachuse function route;
34c7aa856bSGreg Roach
35c7aa856bSGreg Roach/**
36c7aa856bSGreg Roach * Edit the site preferences.
37c7aa856bSGreg Roach */
38c7aa856bSGreg Roachclass SitePreferencesAction implements RequestHandlerInterface
39c7aa856bSGreg Roach{
40c7aa856bSGreg Roach    /**
41c7aa856bSGreg Roach     * @param ServerRequestInterface $request
42c7aa856bSGreg Roach     *
43c7aa856bSGreg Roach     * @return ResponseInterface
44c7aa856bSGreg Roach     */
45c7aa856bSGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
46c7aa856bSGreg Roach    {
47748dbe15SGreg Roach        $index_directory     = Validator::parsedBody($request)->string('INDEX_DIRECTORY');
48748dbe15SGreg Roach        $allow_change_gedcom = Validator::parsedBody($request)->boolean('ALLOW_CHANGE_GEDCOM');
49748dbe15SGreg Roach        $language            = Validator::parsedBody($request)->string('LANGUAGE');
50748dbe15SGreg Roach        $theme_dir           = Validator::parsedBody($request)->string('THEME_DIR');
51748dbe15SGreg Roach        $timezone            = Validator::parsedBody($request)->string('TIMEZONE');
52c7aa856bSGreg Roach
53748dbe15SGreg Roach        if (!str_ends_with($index_directory, '/')) {
54748dbe15SGreg Roach            $index_directory .= '/';
55c7aa856bSGreg Roach        }
56c7aa856bSGreg Roach
57748dbe15SGreg Roach        if (is_dir($index_directory)) {
58*a1bd562dSGreg Roach            if (is_writable($index_directory)) {
59748dbe15SGreg Roach                Site::setPreference('INDEX_DIRECTORY', $index_directory);
60c7aa856bSGreg Roach            } else {
61*a1bd562dSGreg Roach                FlashMessages::addMessage(I18N::translate('Cannot write to the folder “%s”.', e($index_directory)), 'danger');
62*a1bd562dSGreg Roach            }
63*a1bd562dSGreg Roach        } else {
64748dbe15SGreg Roach            FlashMessages::addMessage(I18N::translate('The folder “%s” does not exist.', e($index_directory)), 'danger');
65c7aa856bSGreg Roach        }
66c7aa856bSGreg Roach
67748dbe15SGreg Roach        Site::setPreference('ALLOW_CHANGE_GEDCOM', (string) $allow_change_gedcom);
68748dbe15SGreg Roach        Site::setPreference('LANGUAGE', $language);
69748dbe15SGreg Roach        Site::setPreference('THEME_DIR', $theme_dir);
70748dbe15SGreg Roach        Site::setPreference('TIMEZONE', $timezone);
71c7aa856bSGreg Roach
72c7aa856bSGreg Roach        FlashMessages::addMessage(I18N::translate('The website preferences have been updated.'), 'success');
73c7aa856bSGreg Roach        $url = route(ControlPanel::class);
74c7aa856bSGreg Roach
75c7aa856bSGreg Roach        return redirect($url);
76c7aa856bSGreg Roach    }
77c7aa856bSGreg Roach}
78