xref: /webtrees/app/Site.php (revision acf76a541957ae53e4ee93040ef9a88782befc23)
1a25f0a04SGreg Roach<?php
2a25f0a04SGreg Roach/**
3a25f0a04SGreg Roach * webtrees: online genealogy
41062a142SGreg Roach * Copyright (C) 2018 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 */
1676692c8bSGreg Roachnamespace Fisharebest\Webtrees;
17a25f0a04SGreg Roach
18a25f0a04SGreg Roach/**
1976692c8bSGreg Roach * Provide an interface to the wt_site_setting table.
20a25f0a04SGreg Roach */
21c1010edaSGreg Roachclass Site
22c1010edaSGreg Roach{
23a25f0a04SGreg Roach    /**
24a25f0a04SGreg Roach     * Everything from the wt_site_setting table.
25a25f0a04SGreg Roach     *
26a25f0a04SGreg Roach     * @var array
27a25f0a04SGreg Roach     */
2815d603e7SGreg Roach    private static $preferences = [];
29a25f0a04SGreg Roach
30a25f0a04SGreg Roach    /**
31a25f0a04SGreg Roach     * Get the site’s configuration settings
32a25f0a04SGreg Roach     *
33a25f0a04SGreg Roach     * @param string $setting_name
3415d603e7SGreg Roach     * @param string $default
35a25f0a04SGreg Roach     *
3615d603e7SGreg Roach     * @return string
37a25f0a04SGreg Roach     */
38*acf76a54SGreg Roach    public static function getPreference(string $setting_name, string $default = ''): string
39c1010edaSGreg Roach    {
40a25f0a04SGreg Roach        // There are lots of settings, and we need to fetch lots of them on every page
41a25f0a04SGreg Roach        // so it is quicker to fetch them all in one go.
4215d603e7SGreg Roach        if (empty(self::$preferences)) {
4315d603e7SGreg Roach            self::$preferences = Database::prepare(
44e5588fb0SGreg Roach                "SELECT setting_name, setting_value FROM `##site_setting`"
45a25f0a04SGreg Roach            )->fetchAssoc();
46a25f0a04SGreg Roach        }
47a25f0a04SGreg Roach
48*acf76a54SGreg Roach        return self::$preferences[$setting_name] ?? $default;
49a25f0a04SGreg Roach    }
50a25f0a04SGreg Roach
51a25f0a04SGreg Roach    /**
52a25f0a04SGreg Roach     * Set the site’s configuration settings.
53a25f0a04SGreg Roach     *
54a25f0a04SGreg Roach     * @param string $setting_name
5515d603e7SGreg Roach     * @param string $setting_value
56c7ff4153SGreg Roach     *
57c7ff4153SGreg Roach     * @return void
58a25f0a04SGreg Roach     */
59c1010edaSGreg Roach    public static function setPreference($setting_name, $setting_value)
60c1010edaSGreg Roach    {
6115d603e7SGreg Roach        if (self::getPreference($setting_name) !== $setting_value) {
62a25f0a04SGreg Roach            Database::prepare(
63a25f0a04SGreg Roach                "REPLACE INTO `##site_setting` (setting_name, setting_value)" .
64b6d24f4fSMichael Schramm                " VALUES (:setting_name, LEFT(:setting_value, 2000))"
6513abd6f3SGreg Roach            )->execute([
66a25f0a04SGreg Roach                'setting_name'  => $setting_name,
67a25f0a04SGreg Roach                'setting_value' => $setting_value,
6813abd6f3SGreg Roach            ]);
69a25f0a04SGreg Roach
7015d603e7SGreg Roach            self::$preferences[$setting_name] = $setting_value;
71a25f0a04SGreg Roach
72847d5489SGreg Roach            Log::addConfigurationLog('Site preference "' . $setting_name . '" set to "' . $setting_value . '"', null);
73a25f0a04SGreg Roach        }
74a25f0a04SGreg Roach    }
75a25f0a04SGreg Roach}
76