xref: /webtrees/app/Site.php (revision cbc1590a8c715aa2d88bd745610b899587bd9563)
1a25f0a04SGreg Roach<?php
2dd04c183SGreg Roachnamespace Fisharebest\Webtrees;
3a25f0a04SGreg Roach
4a25f0a04SGreg Roach/**
5a25f0a04SGreg Roach * webtrees: online genealogy
6a25f0a04SGreg Roach * Copyright (C) 2015 webtrees development team
7a25f0a04SGreg Roach * This program is free software: you can redistribute it and/or modify
8a25f0a04SGreg Roach * it under the terms of the GNU General Public License as published by
9a25f0a04SGreg Roach * the Free Software Foundation, either version 3 of the License, or
10a25f0a04SGreg Roach * (at your option) any later version.
11a25f0a04SGreg Roach * This program is distributed in the hope that it will be useful,
12a25f0a04SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
13a25f0a04SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14a25f0a04SGreg Roach * GNU General Public License for more details.
15a25f0a04SGreg Roach * You should have received a copy of the GNU General Public License
16a25f0a04SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
17a25f0a04SGreg Roach */
18a25f0a04SGreg Roach
19a25f0a04SGreg Roach/**
20a25f0a04SGreg Roach * class Site - Provide an interface to the wt_site_setting table.
21a25f0a04SGreg Roach */
22a25f0a04SGreg Roachclass Site {
23a25f0a04SGreg Roach	/**
24a25f0a04SGreg Roach	 * Everything from the wt_site_setting table.
25a25f0a04SGreg Roach	 *
26a25f0a04SGreg Roach	 * @var array
27a25f0a04SGreg Roach	 */
28c999a340SGreg Roach	private static $settings = null;
29a25f0a04SGreg Roach
30a25f0a04SGreg Roach	/**
31a25f0a04SGreg Roach	 * Get the site’s configuration settings
32a25f0a04SGreg Roach	 *
33a25f0a04SGreg Roach	 * @param string $setting_name
34a25f0a04SGreg Roach	 *
35a25f0a04SGreg Roach	 * @return string|null
36a25f0a04SGreg Roach	 */
37a25f0a04SGreg Roach	public static function getPreference($setting_name) {
38a25f0a04SGreg Roach		// There are lots of settings, and we need to fetch lots of them on every page
39a25f0a04SGreg Roach		// so it is quicker to fetch them all in one go.
40c999a340SGreg Roach		if (self::$settings === null) {
41c999a340SGreg Roach			self::$settings = Database::prepare(
42a25f0a04SGreg Roach				"SELECT SQL_CACHE setting_name, setting_value FROM `##site_setting`"
43a25f0a04SGreg Roach			)->fetchAssoc();
44a25f0a04SGreg Roach		}
45a25f0a04SGreg Roach
46a25f0a04SGreg Roach		// A setting that hasn't yet been set?
47c999a340SGreg Roach		if (!array_key_exists($setting_name, self::$settings)) {
48c999a340SGreg Roach			self::$settings[$setting_name] = null;
49a25f0a04SGreg Roach		}
50a25f0a04SGreg Roach
51c999a340SGreg Roach		return self::$settings[$setting_name];
52a25f0a04SGreg Roach	}
53a25f0a04SGreg Roach
54a25f0a04SGreg Roach	/**
55a25f0a04SGreg Roach	 * Set the site’s configuration settings.
56a25f0a04SGreg Roach	 *
57a25f0a04SGreg Roach	 * @param string          $setting_name
58a25f0a04SGreg Roach	 * @param string|int|bool $setting_value
59a25f0a04SGreg Roach	 */
60a25f0a04SGreg Roach	public static function setPreference($setting_name, $setting_value) {
61a25f0a04SGreg Roach		// Only need to update the database if the setting has actually changed.
62a25f0a04SGreg Roach		if (self::getPreference($setting_name) != $setting_value) {
63a25f0a04SGreg Roach			if ($setting_value === null) {
64a25f0a04SGreg Roach				Database::prepare(
65a25f0a04SGreg Roach					"DELETE FROM `##site_setting` WHERE setting_name = :setting_name"
66a25f0a04SGreg Roach				)->execute(array(
67*cbc1590aSGreg Roach					'setting_name' => $setting_name,
68a25f0a04SGreg Roach				));
69a25f0a04SGreg Roach			} else {
70a25f0a04SGreg Roach				Database::prepare(
71a25f0a04SGreg Roach					"REPLACE INTO `##site_setting` (setting_name, setting_value)" .
72a25f0a04SGreg Roach					" VALUES (:setting_name, LEFT(:setting_value, 255))"
73a25f0a04SGreg Roach				)->execute(array(
74a25f0a04SGreg Roach					'setting_name'  => $setting_name,
75a25f0a04SGreg Roach					'setting_value' => $setting_value,
76a25f0a04SGreg Roach				));
77a25f0a04SGreg Roach			}
78a25f0a04SGreg Roach
79c999a340SGreg Roach			self::$settings[$setting_name] = $setting_value;
80a25f0a04SGreg Roach
81a25f0a04SGreg Roach			Log::addConfigurationLog('Site setting "' . $setting_name . '" set to "' . $setting_value . '"');
82a25f0a04SGreg Roach		}
83a25f0a04SGreg Roach	}
84a25f0a04SGreg Roach}
85