xref: /webtrees/app/Site.php (revision 71239cb694d278d044f33328daaa60c8ed7431e9)
1<?php
2/**
3 * webtrees: online genealogy
4 * Copyright (C) 2018 webtrees development team
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16declare(strict_types=1);
17
18namespace Fisharebest\Webtrees;
19
20/**
21 * Provide an interface to the wt_site_setting table.
22 */
23class Site
24{
25    /**
26     * Everything from the wt_site_setting table.
27     *
28     * @var array
29     */
30    private static $preferences = [];
31
32    /**
33     * Get the site’s configuration settings
34     *
35     * @param string $setting_name
36     * @param string $default
37     *
38     * @return string
39     */
40    public static function getPreference(string $setting_name, string $default = ''): string
41    {
42        // There are lots of settings, and we need to fetch lots of them on every page
43        // so it is quicker to fetch them all in one go.
44        if (empty(self::$preferences)) {
45            self::$preferences = Database::prepare(
46                "SELECT setting_name, setting_value FROM `##site_setting`"
47            )->fetchAssoc();
48        }
49
50        return self::$preferences[$setting_name] ?? $default;
51    }
52
53    /**
54     * Set the site’s configuration settings.
55     *
56     * @param string $setting_name
57     * @param string $setting_value
58     *
59     * @return void
60     */
61    public static function setPreference($setting_name, $setting_value)
62    {
63        if (self::getPreference($setting_name) !== $setting_value) {
64            Database::prepare(
65                "REPLACE INTO `##site_setting` (setting_name, setting_value)" .
66                " VALUES (:setting_name, LEFT(:setting_value, 2000))"
67            )->execute([
68                'setting_name'  => $setting_name,
69                'setting_value' => $setting_value,
70            ]);
71
72            self::$preferences[$setting_name] = $setting_value;
73
74            Log::addConfigurationLog('Site preference "' . $setting_name . '" set to "' . $setting_value . '"', null);
75        }
76    }
77}
78