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