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 bool
67: */
68: protected static $_loaded = false;
69:
70: /**
71: *
72: * @var cApiLanguage
73: */
74: protected static $_language;
75:
76: /**
77: * Loads all client, clientlanguage an system properties into an static array.
78: *
79: * The order is: System => Client => Client (language)
80: *
81: */
82: private static function _loadSettings() {
83: if (self::$_loaded == false) {
84: global $contenido;
85:
86: $typeGroup = array();
87:
88: //get all client settings
89: $client = self::_getClientInstance();
90: $settings = $client->getProperties();
91:
92: if (is_array($settings)) {
93: foreach ($settings as $setting) {
94: $key = self::_makeKey($setting['type'], $setting['name']);
95: self::_set($key, $setting['value']);
96: if (!isset($typeGroup[$setting['type']])) {
97: $typeGroup[$setting['type']] = array();
98: }
99: $typeGroup[$setting['type']][$setting['name']] = $setting['value'];
100: }
101: }
102:
103: //get all clientlang setting
104: $clientlang = self::_getClientLanguageInstance();
105: $settings = $clientlang->getProperties();
106:
107: if (is_array($settings)) {
108: foreach ($settings as $setting) {
109: $key = self::_makeKey($setting['type'], $setting['name']);
110: self::_set($key, $setting['value']);
111: if (!isset($typeGroup[$setting['type']])) {
112: $typeGroup[$setting['type']] = array();
113: }
114: $typeGroup[$setting['type']][$setting['name']] = $setting['value'];
115: }
116: }
117:
118: //get user settings
119: if (self::_isAuthenticated() && isset($contenido)) {
120: $user = self::_getUserInstance();
121: $settings = $user->getUserProperties();
122:
123: if (is_array($settings)) {
124: foreach ($settings as $setting) {
125: $key = self::_makeKey($setting['type'], $setting['name']);
126: self::_set($key, $setting['value']);
127: if (!isset($typeGroup[$setting['type']])) {
128: $typeGroup[$setting['type']] = array();
129: }
130: $typeGroup[$setting['type']][$setting['name']] = $setting['value'];
131: }
132: }
133: }
134:
135: //write cache by type settings
136: foreach ($typeGroup as $key => $group) {
137: $key = self::_makeKey($key, ' ');
138: self::_set($key, $group);
139: }
140: }
141:
142: self::$_loaded = true;
143: }
144:
145: /**
146: * Returns effective setting for a property.
147: *
148: * The order is: System => Client => Client (language) => Group => User
149: *
150: * System properties can be overridden by the group, and group properties
151: * can be overridden by the user.
152: *
153: * NOTE: If you provide a default value (other than empty string), then it will be returned back
154: * in case of not existing or empty setting.
155: *
156: * @param string $type
157: * The type of the item
158: * @param string $name
159: * The name of the item
160: * @param string $default [optional]
161: * default value
162: * @return bool|string
163: * Setting value or false
164: */
165: public static function get($type, $name, $default = '') {
166: self::_loadSettings();
167:
168: $key = self::_makeKey($type, $name);
169:
170: $value = self::_get($key);
171: if (false !== $value) {
172: return $value;
173: }
174:
175: if (false === $value) {
176: $value = getSystemProperty($type, $name);
177: }
178:
179: if (false === $value || NULL === $value) {
180: $value = $default;
181: } else if ('' === $value && '' !== $default) {
182: // NOTE: A non empty default value overrides an empty value
183: $value = $default;
184: }
185:
186: return $value;
187: }
188:
189: /**
190: * Returns effective setting for a type of properties.
191: * Caches also the collected settings, but contrary to get() it returns
192: * never cached entries.
193: *
194: * The order is:
195: * System => Client => Client (language) => Group => User
196: *
197: * System properties can be overridden by the group, and group
198: * properties can be overridden by the user.
199: *
200: * @param string $type
201: * The type of the item
202: * @return array
203: * Assoziative array like $arr[name] = value
204: */
205: public static function getByType($type) {
206: self::_loadSettings();
207:
208: $settings = getSystemPropertiesByType($type);
209:
210: $key = self::_makeKey($type, ' ');
211: if (is_array(self::_get($key))) {
212: $settings = array_merge($settings, self::_get($key));
213: }
214:
215: if (isset($settings) && is_array($settings)) {
216: return $settings;
217: } else {
218: return array();
219: }
220: }
221:
222: /**
223: * Sets a effective setting.
224: *
225: * Note:
226: * The setting will be set only in cache, not in persistency layer.
227: *
228: * @param string $type
229: * The type of the item
230: * @param string $name
231: * The name of the item
232: * @param string $value
233: * The value of the setting
234: */
235: public static function set($type, $name, $value) {
236: $key = self::_makeKey($type, $name);
237: self::_set($key, $value);
238: }
239:
240: /**
241: * Deletes a effective setting.
242: *
243: * Note:
244: * The setting will be deleted only from cache, not from persistency layer.
245: *
246: * @param string $type
247: * The type of the item
248: * @param string $name
249: * The name of the item
250: */
251: public static function delete($type, $name) {
252: $keySuffix = '_' . $type . '_' . $name;
253: foreach (self::$_settings as $key => $value) {
254: if (strpos($key, $keySuffix) !== false) {
255: unset(self::$_settings[$key]);
256: }
257: }
258: }
259:
260: /**
261: * Resets all properties of the effective settings class.
262: * Usable to start getting settings from scratch.
263: */
264: public static function reset() {
265: self::$_settings = array();
266: unset(self::$_user, self::$_client, self::$_clientLanguage);
267: }
268:
269: /**
270: * Returns the user object instance.
271: *
272: * @return cApiUser
273: */
274: protected static function _getUserInstance() {
275: global $auth;
276:
277: if (!isset(self::$_user)) {
278: self::$_user = new cApiUser($auth->auth['uid']);
279: }
280: return self::$_user;
281: }
282:
283: /**
284: * Returns the client language object instance.
285: *
286: * @return cApiClientLanguage
287: */
288: protected static function _getClientLanguageInstance() {
289: global $client, $lang;
290:
291: if (!isset(self::$_clientLanguage)) {
292: self::$_clientLanguage = new cApiClientLanguage(false, $client, $lang);
293: }
294: return self::$_clientLanguage;
295: }
296:
297: /**
298: * Returns the language object instance.
299: *
300: * @return cApiLanguage
301: */
302: protected static function _getLanguageInstance() {
303: global $lang;
304:
305: if (!isset(self::$_language)) {
306: self::$_language = new cApiLanguage($lang);
307: }
308: return self::$_language;
309: }
310:
311: /**
312: * Returns the client language object instance.
313: *
314: * @return cApiClient
315: */
316: protected static function _getClientInstance() {
317: global $client;
318:
319: if (!isset(self::$_client)) {
320: self::$_client = new cApiClient($client);
321: }
322: return self::$_client;
323: }
324:
325: /**
326: * Setting getter.
327: *
328: * @param string $key
329: * The setting key
330: * @return string
331: * bool setting value or false
332: */
333: protected static function _get($key) {
334: return (isset(self::$_settings[$key])) ? self::$_settings[$key] : false;
335: }
336:
337: /**
338: * Setting setter.
339: *
340: * @param string $key
341: * The setting key
342: * @param string $value
343: * Value to store
344: */
345: protected static function _set($key, $value) {
346: self::$_settings[$key] = $value;
347: }
348:
349: /**
350: * Setting key getter.
351: *
352: * @param string $type
353: * The type of the item
354: * @param string $name
355: * Name of the item
356: * @return string
357: * The setting key
358: */
359: protected static function _makeKey($type, $name) {
360: global $auth;
361:
362: if ($auth instanceof cAuth) {
363: $key = $auth->auth['uid'] . '_' . $type . '_' . $name;
364: } else {
365: $key = '_' . $type . '_' . $name;
366: }
367: return $key;
368: }
369:
370: /**
371: * Checks global authentication object and if current user is authenticated.
372: *
373: * @return bool
374: */
375: protected static function _isAuthenticated() {
376: global $auth;
377: return $auth instanceof cAuth && $auth->isAuthenticated() && !$auth->isLoginForm();
378: }
379: }