xref: /webtrees/app/Site.php (revision 3e983931fdde6db78f1490364106d7d46e77dea7)
1<?php
2/**
3 * webtrees: online genealogy
4 * Copyright (C) 2017 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 $preferences = [];
28
29	/**
30	 * Get the site’s configuration settings
31	 *
32	 * @param string $setting_name
33	 * @param string $default
34	 *
35	 * @return string
36	 */
37	public static function getPreference($setting_name, $default = '') {
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 (empty(self::$preferences)) {
41			self::$preferences = Database::prepare(
42				"SELECT SQL_CACHE setting_name, setting_value FROM `##site_setting`"
43			)->fetchAssoc();
44		}
45
46		if (!array_key_exists($setting_name, self::$preferences)) {
47			self::$preferences[$setting_name] = $default;
48		}
49
50		return self::$preferences[$setting_name];
51	}
52
53	/**
54	 * Set the site’s configuration settings.
55	 *
56	 * @param string $setting_name
57	 * @param string $setting_value
58	 */
59	public static function setPreference($setting_name, $setting_value) {
60		if (self::getPreference($setting_name) !== $setting_value) {
61			Database::prepare(
62				"REPLACE INTO `##site_setting` (setting_name, setting_value)" .
63				" VALUES (:setting_name, LEFT(:setting_value, 2000))"
64			)->execute([
65				'setting_name'  => $setting_name,
66				'setting_value' => $setting_value,
67			]);
68
69			self::$preferences[$setting_name] = $setting_value;
70
71			Log::addConfigurationLog('Site preference "' . $setting_name . '" set to "' . $setting_value . '"');
72		}
73	}
74}
75