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: * The requested setting will be cached at first time, the next time the
73: * cached value will be returned.
74: *
75: * The order is: System => Client => Client (language) => Group => User
76: *
77: * System properties can be overridden by the group, and group properties
78: * can be overridden by the user.
79: *
80: * @param string $type The type of the item
81: * @param string $name The name of the item
82: * @param string $default Optional default value
83: * @return string bool setting value or false
84: */
85: public static function get($type, $name, $default = '') {
86: global $contenido;
87:
88: // if the DB object is not available, just return
89: // the default value in order to avoid PHP notices
90: try {
91: $db = new cDb();
92: } catch (cException $e) {
93: return $default;
94: }
95:
96: $key = self::_makeKey($type, $name);
97:
98: $value = self::_get($key);
99: if (false !== $value) {
100: return $value;
101: }
102:
103: if (self::_isAuthenticated() && isset($contenido)) {
104: $value = self::_getUserInstance()->getUserProperty($type, $name, true);
105: }
106:
107: if (false == $value) {
108: $value = self::_getLanguageInstance()->getProperty($type, $name);
109: }
110:
111: if (false == $value) {
112: $value = self::_getClientLanguageInstance()->getProperty($type, $name);
113: }
114:
115: if (false == $value) {
116: $value = self::_getClientInstance()->getProperty($type, $name);
117: }
118:
119: if ($value == false) {
120: $value = getSystemProperty($type, $name);
121: }
122:
123: if ($value == false) {
124: $value = $default;
125: }
126:
127: self::_set($key, $value);
128:
129: return $value;
130: }
131:
132: /**
133: * Returns effective setting for a type of properties.
134: * Caches also the collected settings, but contrary to get() it returns
135: * never cached entries.
136: *
137: * The order is:
138: * System => Client => Client (language) => Group => User
139: *
140: * System properties can be overridden by the group, and group
141: * properties can be overridden by the user.
142: *
143: * @param string $type The type of the item
144: * @return array Assoziative array like $arr[name] = value
145: */
146: public static function getByType($type) {
147: global $contenido;
148:
149: $settings = getSystemPropertiesByType($type);
150: $settings = array_merge($settings, self::_getClientInstance()->getPropertiesByType($type));
151: $settings = array_merge($settings, self::_getClientLanguageInstance()->getPropertiesByType($type));
152: if (self::_isAuthenticated() && isset($contenido)) {
153: $settings = array_merge($settings, self::_getUserInstance()->getUserPropertiesByType($type, true));
154: }
155:
156: // cache all settings, to return them from cache in case of calling
157: // get()
158: foreach ($settings as $setting => $value) {
159: $key = self::_makeKey($type, $setting);
160: self::_set($key, $value);
161: }
162:
163: return $settings;
164: }
165:
166: /**
167: * Sets a effective setting.
168: *
169: * Note:
170: * The setting will be set only in cache, not in persistency layer.
171: *
172: * @param string $type The type of the item
173: * @param string $name The name of the item
174: * @param string $value The value of the setting
175: */
176: public static function set($type, $name, $value) {
177: $key = self::_makeKey($type, $name);
178: self::_set($key, $value);
179: }
180:
181: /**
182: * Deletes a effective setting.
183: *
184: * Note:
185: * The setting will be deleted only from cache, not from persistency layer.
186: *
187: * @param string $type The type of the item
188: * @param string $name The name of the item
189: */
190: public static function delete($type, $name) {
191: $keySuffix = '_' . $type . '_' . $name;
192: foreach (self::$_settings as $key => $value) {
193: if (strpos($key, $keySuffix) !== false) {
194: unset(self::$_settings[$key]);
195: }
196: }
197: }
198:
199: /**
200: * Resets all properties of the effective settings class.
201: * Usable to start getting settings from scratch.
202: */
203: public static function reset() {
204: self::$_settings = array();
205: unset(self::$_user, self::$_client, self::$_clientLanguage);
206: }
207:
208: /**
209: * Returns the user object instance.
210: *
211: * @return cApiUser
212: */
213: protected static function _getUserInstance() {
214: global $auth;
215:
216: if (!isset(self::$_user)) {
217: self::$_user = new cApiUser($auth->auth['uid']);
218: }
219: return self::$_user;
220: }
221:
222: /**
223: * Returns the client language object instance.
224: *
225: * @return cApiClientLanguage
226: */
227: protected static function _getClientLanguageInstance() {
228: global $client, $lang;
229:
230: if (!isset(self::$_clientLanguage)) {
231: self::$_clientLanguage = new cApiClientLanguage(false, $client, $lang);
232: }
233: return self::$_clientLanguage;
234: }
235:
236: /**
237: * Returns the language object instance.
238: *
239: * @return cApiLanguage
240: */
241: protected static function _getLanguageInstance() {
242: global $lang;
243:
244: if (!isset(self::$_language)) {
245: self::$_language = new cApiLanguage($lang);
246: }
247: return self::$_language;
248: }
249:
250: /**
251: * Returns the client language object instance.
252: *
253: * @return cApiClient
254: */
255: protected static function _getClientInstance() {
256: global $client;
257:
258: if (!isset(self::$_client)) {
259: self::$_client = new cApiClient($client);
260: }
261: return self::$_client;
262: }
263:
264: /**
265: * Setting getter.
266: *
267: * @param string $key The setting key
268: * @return string bool setting value or false
269: */
270: protected static function _get($key) {
271: return (isset(self::$_settings[$key]))? self::$_settings[$key] : false;
272: }
273:
274: /**
275: * Setting setter.
276: *
277: * @param string $key The setting key
278: * @param string $value Value to store
279: * @return string bool setting value or false
280: */
281: protected static function _set($key, $value) {
282: self::$_settings[$key] = $value;
283: }
284:
285: /**
286: * Setting key getter.
287: *
288: * @param string $type The type of the item
289: * @param string $name Name of the item
290: * @return string The setting key
291: */
292: protected static function _makeKey($type, $name) {
293: global $auth;
294:
295: if ($auth instanceof Contenido_Auth) {
296: $key = $auth->auth['uid'] . '_' . $type . '_' . $name;
297: } else {
298: $key = '_' . $type . '_' . $name;
299: }
300: return $key;
301: }
302:
303: /**
304: * Checks global authentication object and if current user is authenticated.
305: *
306: * @return bool
307: */
308: protected static function _isAuthenticated() {
309: global $auth;
310: return ($auth instanceof cAuth && $auth->isAuthenticated() && !$auth->isLoginForm());
311: }
312: }