xref: /webtrees/app/Site.php (revision c1010eda29c0909ed4d5d463f32d32bfefdd4dfe)
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($setting_name, $default = '')
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        if (!array_key_exists($setting_name, self::$preferences)) {
49            self::$preferences[$setting_name] = $default;
50        }
51
52        return self::$preferences[$setting_name];
53    }
54
55    /**
56     * Set the site’s configuration settings.
57     *
58     * @param string $setting_name
59     * @param string $setting_value
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