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