1: <?php
2: /**
3: * This file contains the the effective setting class.
4: *
5: * @package Core
6: * @subpackage Backend
7: * @version SVN Revision $Rev:$
8: *
9: * @author Frederic Schneider
10: * @copyright four for business AG <www.4fb.de>
11: * @license http://www.contenido.org/license/LIZENZ.txt
12: * @link http://www.4fb.de
13: * @link http://www.contenido.org
14: */
15:
16: defined('CON_FRAMEWORK') || die('Illegal call: Missing framework initialization - request aborted.');
17:
18: /**
19: * Effective setting manager class.
20: * Provides a interface to retrieve effective
21: * settings.
22: *
23: * Requested effective settings will be cached at first time. Further requests
24: * will
25: * return cached settings.
26: *
27: * The order to retrieve a effective setting is:
28: * System => Client => Client (language) => Group => User
29: *
30: * - System properties can be overridden by the client
31: * - Client properties can be overridden by client language
32: * - Client language properties can be overridden by group
33: * - Group properties can be overridden by user
34: *
35: * @package Core
36: * @subpackage Backend
37: */
38: class cEffectiveSetting {
39:
40: /**
41: *
42: * @var array
43: */
44: protected static $_settings = array();
45:
46: /**
47: *
48: * @var cApiUser
49: */
50: protected static $_user;
51:
52: /**
53: *
54: * @var cApiClient
55: */
56: protected static $_client;
57:
58: /**
59: *
60: * @var cApiClientLanguage
61: */
62: protected static $_clientLanguage;
63:
64: /**
65: *
66: * @var cApiLanguage
67: */
68: protected static $_language;
69:
70: /**
71: * Returns effective setting for a property.
72: *
73: * The order is: System => Client => Client (language) => Group => User
74: *
75: * System properties can be overridden by the group, and group properties
76: * can be overridden by the user.
77: *
78: * NOTE: If you provide a default value (other than empty string), then it will be returned back
79: * in case of not existing or empty setting.
80: *
81: * @param string $type The type of the item
82: * @param string $name The name of the item
83: * @param string $default Optional default value
84: * @return bool|string Setting value or false
85: */
86: public static function get($type, $name, $default = '') {
87: global $contenido;
88:
89: // If the DB object is not available, just return the default value in order
90: // to avoid PHP notices
91: try {
92: $db = new cDb();
93: } catch (cException $e) {
94: return $default;
95: }
96:
97: $key = self::_makeKey($type, $name);
98:
99: $value = self::_get($key);
100: if (false !== $value) {
101: return $value;
102: }
103:
104: if (self::_isAuthenticated() && isset($contenido)) {
105: $value = self::_getUserInstance()->getUserProperty($type, $name, true);
106: }
107:
108: if (false === $value) {
109: $value = self::_getLanguageInstance()->getProperty($type, $name);
110: }
111:
112: if (false === $value) {
113: $value = self::_getClientLanguageInstance()->getProperty($type, $name);
114: }
115:
116: if (false === $value) {
117: $value = self::_getClientInstance()->getProperty($type, $name);
118: }
119:
120: if (false === $value) {
121: $value = getSystemProperty($type, $name);
122: }
123:
124: if (false === $value || NULL === $value) {
125: $value = $default;
126: } else if ('' === $value && '' !== $default) {
127: // NOTE: A non empty default value overrides an empty value
128: $value = $default;
129: }
130:
131: self::_set($key, $value);
132:
133: return $value;
134: }
135:
136: /**
137: * Returns effective setting for a type of properties.
138: * Caches also the collected settings, but contrary to get() it returns
139: * never cached entries.
140: *
141: * The order is:
142: * System => Client => Client (language) => Group => User
143: *
144: * System properties can be overridden by the group, and group
145: * properties can be overridden by the user.
146: *
147: * @param string $type The type of the item
148: * @return array Assoziative array like $arr[name] = value
149: */
150: public static function getByType($type) {
151: global $contenido;
152:
153: $settings = getSystemPropertiesByType($type);
154: $settings = array_merge($settings, self::_getClientInstance()->getPropertiesByType($type));
155: $settings = array_merge($settings, self::_getClientLanguageInstance()->getPropertiesByType($type));
156: if (self::_isAuthenticated() && cRegistry::isBackendEditMode()) {
157: $settings = array_merge($settings, self::_getUserInstance()->getUserPropertiesByType($type, true));
158: }
159:
160: // cache all settings, to return them from cache in case of calling
161: // get()
162: foreach ($settings as $setting => $value) {
163: $key = self::_makeKey($type, $setting);
164: self::_set($key, $value);
165: }
166:
167: return $settings;
168: }
169:
170: /**
171: * Sets a effective setting.
172: *
173: * Note:
174: * The setting will be set only in cache, not in persistency layer.
175: *
176: * @param string $type The type of the item
177: * @param string $name The name of the item
178: * @param string $value The value of the setting
179: */
180: public static function set($type, $name, $value) {
181: $key = self::_makeKey($type, $name);
182: self::_set($key, $value);
183: }
184:
185: /**
186: * Deletes a effective setting.
187: *
188: * Note:
189: * The setting will be deleted only from cache, not from persistency layer.
190: *
191: * @param string $type The type of the item
192: * @param string $name The name of the item
193: */
194: public static function delete($type, $name) {
195: $keySuffix = '_' . $type . '_' . $name;
196: foreach (self::$_settings as $key => $value) {
197: if (strpos($key, $keySuffix) !== false) {
198: unset(self::$_settings[$key]);
199: }
200: }
201: }
202:
203: /**
204: * Resets all properties of the effective settings class.
205: * Usable to start getting settings from scratch.
206: */
207: public static function reset() {
208: self::$_settings = array();
209: unset(self::$_user, self::$_client, self::$_clientLanguage);
210: }
211:
212: /**
213: * Returns the user object instance.
214: *
215: * @return cApiUser
216: */
217: protected static function _getUserInstance() {
218: global $auth;
219:
220: if (!isset(self::$_user)) {
221: self::$_user = new cApiUser($auth->auth['uid']);
222: }
223: return self::$_user;
224: }
225:
226: /**
227: * Returns the client language object instance.
228: *
229: * @return cApiClientLanguage
230: */
231: protected static function _getClientLanguageInstance() {
232: global $client, $lang;
233:
234: if (!isset(self::$_clientLanguage)) {
235: self::$_clientLanguage = new cApiClientLanguage(false, $client, $lang);
236: }
237: return self::$_clientLanguage;
238: }
239:
240: /**
241: * Returns the language object instance.
242: *
243: * @return cApiLanguage
244: */
245: protected static function _getLanguageInstance() {
246: global $lang;
247:
248: if (!isset(self::$_language)) {
249: self::$_language = new cApiLanguage($lang);
250: }
251: return self::$_language;
252: }
253:
254: /**
255: * Returns the client language object instance.
256: *
257: * @return cApiClient
258: */
259: protected static function _getClientInstance() {
260: global $client;
261:
262: if (!isset(self::$_client)) {
263: self::$_client = new cApiClient($client);
264: }
265: return self::$_client;
266: }
267:
268: /**
269: * Setting getter.
270: *
271: * @param string $key The setting key
272: * @return string bool setting value or false
273: */
274: protected static function _get($key) {
275: return (isset(self::$_settings[$key])) ? self::$_settings[$key] : false;
276: }
277:
278: /**
279: * Setting setter.
280: *
281: * @param string $key The setting key
282: * @param string $value Value to store
283: */
284: protected static function _set($key, $value) {
285: self::$_settings[$key] = $value;
286: }
287:
288: /**
289: * Setting key getter.
290: *
291: * @param string $type The type of the item
292: * @param string $name Name of the item
293: * @return string The setting key
294: */
295: protected static function _makeKey($type, $name) {
296: global $auth;
297:
298: if ($auth instanceof cAuth) {
299: $key = $auth->auth['uid'] . '_' . $type . '_' . $name;
300: } else {
301: $key = '_' . $type . '_' . $name;
302: }
303: return $key;
304: }
305:
306: /**
307: * Checks global authentication object and if current user is authenticated.
308: *
309: * @return bool
310: */
311: protected static function _isAuthenticated() {
312: global $auth;
313: return ($auth instanceof cAuth && $auth->isAuthenticated() && !$auth->isLoginForm());
314: }
315: }