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