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