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