1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13:
14:
15: defined('CON_FRAMEWORK') || die('Illegal call: Missing framework initialization - request aborted.');
16:
17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27:
28: class cApiSystemPropertyCollection extends ItemCollection {
29:
30: 31: 32: 33: 34:
35: protected static $_entries;
36:
37: 38: 39: 40: 41:
42: protected static $_enableCache;
43:
44: 45: 46: 47: 48: 49: 50:
51: public function __construct() {
52: global $cfg;
53: parent::__construct($cfg['tab']['system_prop'], 'idsystemprop');
54: $this->_setItemClass('cApiSystemProperty');
55:
56: if (!isset(self::$_enableCache)) {
57: if (isset($cfg['properties']) && isset($cfg['properties']['system_prop']) && isset($cfg['properties']['system_prop']['enable_cache'])) {
58: self::$_enableCache = (bool) $cfg['properties']['system_prop']['enable_cache'];
59: } else {
60: self::$_enableCache = false;
61: }
62: }
63:
64: if (self::$_enableCache && !isset(self::$_entries)) {
65: $this->_loadFromCache();
66: }
67: }
68:
69: 70: 71:
72: public static function reset() {
73: self::$_enableCache = null;
74: self::$_entries = null;
75: }
76:
77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89:
90: public function setTypeNameValueById($type, $name, $value, $id) {
91: $item = $this->fetchById($id);
92: if (!$item) {
93: return NULL;
94: }
95:
96: $item->set('type', $type);
97: $item->set('name', $name);
98: $item->set('value', $value);
99: $item->store();
100:
101: if (self::$_enableCache) {
102: $this->_addToCache($item);
103: }
104:
105: return $item;
106: }
107:
108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120:
121: public function setValueByTypeName($type, $name, $value) {
122: $item = $this->fetchByTypeName($type, $name);
123: if ($item) {
124: $item->set('value', $value);
125: $item->store();
126: } else {
127: $item = $this->create($type, $name, $value);
128: }
129:
130: if (self::$_enableCache) {
131: $this->_addToCache($item);
132: }
133:
134: return $item;
135: }
136:
137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147:
148: public function create($type, $name, $value) {
149: $item = $this->createNewItem();
150:
151: $item->set('type', $type);
152: $item->set('name', $name);
153: $item->set('value', $value);
154: $item->store();
155:
156: if (self::$_enableCache) {
157: $this->_addToCache($item);
158: }
159:
160: return $item;
161: }
162:
163: 164: 165: 166: 167: 168: 169: 170: 171:
172: public function fetchAll($orderBy = '') {
173: if (self::$_enableCache) {
174:
175: return $this->_fetchAllFromCache();
176: }
177:
178: $this->select('', '', $this->escape($orderBy));
179: $props = array();
180: while (($property = $this->next()) !== false) {
181: $props[] = clone $property;
182: }
183: return $props;
184: }
185:
186: 187: 188: 189: 190: 191: 192:
193: public function fetchById($id) {
194: if (self::$_enableCache) {
195: return $this->_fetchByIdFromCache($id);
196: }
197:
198: $item = parent::fetchById($id);
199: return ($item && $item->isLoaded()) ? $item : NULL;
200: }
201:
202: 203: 204: 205: 206: 207: 208: 209: 210: 211: 212:
213: public function fetchByTypeName($type, $name) {
214: if (self::$_enableCache) {
215: return $this->_fetchByTypeNameFromCache($type, $name);
216: }
217:
218: $sql = $this->db->prepare("type = '%s' AND name = '%s'", $type, $name);
219: $this->select($sql);
220: if (($property = $this->next()) !== false) {
221: return $property;
222: }
223: return NULL;
224: }
225:
226: 227: 228: 229: 230: 231: 232: 233: 234: 235:
236: public function fetchByType($type) {
237: if (self::$_enableCache) {
238: return $this->_fetchByTypeFromCache($type);
239: }
240:
241: $sql = $this->db->prepare("type = '%s'", $type);
242: $this->select($sql);
243: $props = array();
244: while (($property = $this->next()) !== false) {
245: $props[] = clone $property;
246: }
247: return $props;
248: }
249:
250: 251: 252: 253: 254: 255: 256: 257: 258: 259: 260: 261:
262: public function deleteByTypeName($type, $name) {
263: $sql = $this->db->prepare("type = '%s' AND name = '%s'", $type, $name);
264: $this->select($sql);
265: return $this->_deleteSelected();
266: }
267:
268: 269: 270: 271: 272: 273: 274: 275: 276: 277: 278:
279: public function deleteByType($type) {
280: $sql = $this->db->prepare("type = '%s'", $type);
281: $this->select($sql);
282: return $this->_deleteSelected();
283: }
284:
285: 286: 287: 288: 289: 290: 291: 292: 293:
294: protected function _deleteSelected() {
295: $result = false;
296: while (($system = $this->next()) !== false) {
297: $id = $system->get('idsystemprop');
298: if (self::$_enableCache) {
299: $this->_deleteFromCache($id);
300: }
301: $result = $this->delete($id);
302: }
303: return $result;
304: }
305:
306: 307: 308: 309: 310: 311:
312: protected function _loadFromCache() {
313: self::$_entries = array();
314: $this->select();
315: while (($property = $this->next()) !== false) {
316: $data = $property->toArray();
317: self::$_entries[$data['idsystemprop']] = $data;
318: }
319: }
320:
321: 322: 323: 324: 325:
326: protected function _addToCache($entry) {
327: $data = $entry->toArray();
328: self::$_entries[$data['idsystemprop']] = $data;
329: }
330:
331: 332: 333: 334: 335:
336: protected function _fetchAllFromCache() {
337: $props = array();
338: $obj = new cApiSystemProperty();
339: foreach (self::$_entries as $entry) {
340: $obj->loadByRecordSet($entry);
341: $props[] = clone $obj;
342: }
343: return $props;
344: }
345:
346: 347: 348: 349: 350: 351:
352: protected function _fetchByIdFromCache($id) {
353: $obj = new cApiSystemProperty();
354: foreach (self::$_entries as $_id => $entry) {
355: if ($_id == $id) {
356: $obj->loadByRecordSet($entry);
357: return $obj;
358: }
359: }
360: return NULL;
361: }
362:
363: 364: 365: 366: 367: 368: 369:
370: protected function _fetchByTypeNameFromCache($type, $name) {
371: $obj = new cApiSystemProperty();
372: foreach (self::$_entries as $entry) {
373: if ($entry['type'] == $type && $entry['name'] == $name) {
374: $obj->loadByRecordSet($entry);
375: return $obj;
376: }
377: }
378: return NULL;
379: }
380:
381: 382: 383: 384: 385: 386:
387: protected function _fetchByTypeFromCache($type) {
388: $props = array();
389: $obj = new cApiSystemProperty();
390: foreach (self::$_entries as $entry) {
391: if ($entry['type'] == $type) {
392: $obj->loadByRecordSet($entry);
393: $props[] = clone $obj;
394: }
395: }
396: return $props;
397: }
398:
399: 400: 401: 402: 403:
404: protected function _deleteFromCache($id) {
405: if (isset(self::$_entries[$id])) {
406: unset(self::$_entries[$id]);
407: }
408: }
409:
410: }
411:
412: 413: 414: 415: 416: 417: 418: 419: 420: 421: 422: 423: 424: 425: 426: 427: 428: 429:
430: class cApiSystemProperty extends Item
431: {
432: 433: 434: 435: 436: 437: 438: 439: 440:
441: public function __construct($mId = false) {
442: global $cfg;
443: parent::__construct($cfg['tab']['system_prop'], 'idsystemprop');
444: $this->setFilters(array(), array());
445: if ($mId !== false) {
446: $this->loadByPrimaryKey($mId);
447: }
448: }
449:
450: 451: 452: 453: 454: 455: 456: 457:
458: public function updateValue($value) {
459: $this->set('value', $value);
460: return $this->store();
461: }
462:
463: }
464: