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