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