xref: /webtrees/app/Site.php (revision fd6c8c786edaa7cf7534ebe606cd8d58eec364cb)
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 */
16namespace Fisharebest\Webtrees;
17
18/**
19 * Provide an interface to the wt_site_setting table.
20 */
21class Site
22{
23    /**
24     * Everything from the wt_site_setting table.
25     *
26     * @var array
27     */
28    private static $preferences = [];
29
30    /**
31     * Get the site’s configuration settings
32     *
33     * @param string $setting_name
34     * @param string $default
35     *
36     * @return string
37     */
38    public static function getPreference(string $setting_name, string $default = ''): string
39    {
40        // There are lots of settings, and we need to fetch lots of them on every page
41        // so it is quicker to fetch them all in one go.
42        if (empty(self::$preferences)) {
43            self::$preferences = Database::prepare(
44                "SELECT setting_name, setting_value FROM `##site_setting`"
45            )->fetchAssoc();
46        }
47
48        return self::$preferences[$setting_name] ?? $default;
49    }
50
51    /**
52     * Set the site’s configuration settings.
53     *
54     * @param string $setting_name
55     * @param string $setting_value
56     *
57     * @return void
58     */
59    public static function setPreference($setting_name, $setting_value)
60    {
61        if (self::getPreference($setting_name) !== $setting_value) {
62            Database::prepare(
63                "REPLACE INTO `##site_setting` (setting_name, setting_value)" .
64                " VALUES (:setting_name, LEFT(:setting_value, 2000))"
65            )->execute([
66                'setting_name'  => $setting_name,
67                'setting_value' => $setting_value,
68            ]);
69
70            self::$preferences[$setting_name] = $setting_value;
71
72            Log::addConfigurationLog('Site preference "' . $setting_name . '" set to "' . $setting_value . '"', null);
73        }
74    }
75}
76