xref: /webtrees/app/Site.php (revision d0c1a61ef810518d72356545b8b85abf2582ef6a)
1<?php
2/**
3 * webtrees: online genealogy
4 * Copyright (C) 2019 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
20use Illuminate\Database\Capsule\Manager as DB;
21use function mb_substr;
22
23/**
24 * Provide an interface to the wt_site_setting table.
25 */
26class Site
27{
28    /**
29     * Everything from the wt_site_setting table.
30     *
31     * @var array
32     */
33    public static $preferences = [];
34
35    /**
36     * Get the site’s configuration settings
37     *
38     * @param string $setting_name
39     * @param string $default
40     *
41     * @return string
42     */
43    public static function getPreference(string $setting_name, string $default = ''): string
44    {
45        // There are lots of settings, and we need to fetch lots of them on every page
46        // so it is quicker to fetch them all in one go.
47        if (empty(self::$preferences)) {
48            self::$preferences = DB::table('site_setting')
49                ->pluck('setting_value', 'setting_name')
50                ->all();
51        }
52
53        return self::$preferences[$setting_name] ?? $default;
54    }
55
56    /**
57     * Set the site’s configuration settings.
58     *
59     * @param string $setting_name
60     * @param string $setting_value
61     *
62     * @return void
63     */
64    public static function setPreference($setting_name, $setting_value): void
65    {
66        // The database column is only this long.
67        $setting_value = mb_substr($setting_value, 0, 2000);
68
69        if (self::getPreference($setting_name) !== $setting_value) {
70            DB::table('site_setting')->updateOrInsert([
71                'setting_name' => $setting_name,
72            ], [
73                'setting_value' => $setting_value,
74            ]);
75
76            self::$preferences[$setting_name] = $setting_value;
77
78            Log::addConfigurationLog('Site preference "' . $setting_name . '" set to "' . $setting_value . '"', null);
79        }
80    }
81}
82