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: class cApiGroupPropertyCollection extends ItemCollection {
28:
29: 30: 31: 32: 33:
34: protected $_groupId = '';
35:
36: 37: 38: 39: 40:
41: protected static $_entries;
42:
43: 44: 45: 46: 47:
48: protected static $_enableCache;
49:
50: 51: 52: 53: 54:
55: protected static $_maxGroups = 3;
56:
57: 58: 59: 60: 61: 62: 63: 64: 65:
66: public function __construct($groupId) {
67: global $cfg;
68: parent::__construct($cfg['tab']['group_prop'], 'idgroupprop');
69: $this->_setItemClass('cApiGroupProperty');
70:
71:
72: $this->_setJoinPartner('cApiGroupCollection');
73:
74: if (!isset(self::$_enableCache)) {
75: if (isset($cfg['properties']) && isset($cfg['properties']['group_prop']) && isset($cfg['properties']['group_prop']['enable_cache'])) {
76: self::$_enableCache = (bool) $cfg['properties']['group_prop']['enable_cache'];
77:
78: if (isset($cfg['properties']['group_prop']['max_groups'])) {
79: self::$_maxGroups = (int) $cfg['properties']['group_prop']['max_groups'];
80:
81:
82: if (self::$_maxGroups < 1) {
83: self::$_maxGroups = 1;
84: }
85: }
86: } else {
87: self::$_enableCache = false;
88: }
89: }
90:
91: $this->setGroupId($groupId);
92: }
93:
94: 95: 96:
97: public static function reset() {
98: self::$_enableCache = null;
99: self::$_entries = null;
100: self::$_maxGroups = 3;
101: }
102:
103: 104: 105: 106: 107: 108: 109: 110: 111:
112: public function setGroupId($groupId) {
113: if (empty($groupId)) {
114: throw new cInvalidArgumentException("Empty group id");
115: }
116: $this->_groupId = $groupId;
117: if (self::$_enableCache) {
118: $this->_loadFromCache();
119: }
120: }
121:
122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135:
136: public function setValueByTypeName($type, $name, $value, $idcatlang = 0) {
137: $item = $this->fetchByGroupIdTypeName($type, $name);
138: if ($item) {
139: $item->set('value', $value);
140: $item->store();
141: } else {
142: $item = $this->create($type, $name, $value, $idcatlang);
143: }
144:
145: if (self::$_enableCache) {
146: $this->_addToCache($item);
147: }
148:
149: return $item;
150: }
151:
152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163:
164: public function create($type, $name, $value, $idcatlang = 0) {
165: $item = $this->createNewItem();
166:
167: $item->set('group_id', $this->_groupId);
168: $item->set('type', $type);
169: $item->set('name', $name);
170: $item->set('value', $value);
171: $item->set('idcatlang', $idcatlang);
172: $item->store();
173:
174: if (self::$_enableCache) {
175: $this->_addToCache($item);
176: }
177:
178: return $item;
179: }
180:
181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191:
192: public function fetchByGroupIdTypeName($type, $name) {
193: if (self::$_enableCache) {
194: return $this->_fetchByGroupIdTypeNameFromCache($type, $name);
195: }
196:
197: $sql = $this->db->prepare("group_id = '%s' AND type = '%s' AND name = '%s'", $this->_groupId, $type, $name);
198: $this->select($sql);
199: if (($property = $this->next()) !== false) {
200: return $property;
201: }
202: return NULL;
203: }
204:
205: 206: 207: 208: 209: 210: 211: 212: 213: 214:
215: public function fetchByGroupIdType($type) {
216: if (self::$_enableCache) {
217: return $this->_fetchByGroupIdTypeFromCache($type);
218: }
219:
220: $sql = $this->db->prepare("group_id = '%s' AND type = '%s'", $this->_groupId, $type);
221: $this->select($sql);
222: $props = array();
223: while (($property = $this->next()) !== false) {
224: $props[] = clone $property;
225: }
226: return $props;
227: }
228:
229: 230: 231: 232: 233: 234: 235: 236:
237: public function fetchByGroupId() {
238: if (self::$_enableCache) {
239: return $this->_fetchByGroupIdFromCache();
240: }
241:
242: $sql = $this->db->prepare("group_id = '%s'", $this->_groupId);
243: $this->select($sql);
244: $props = array();
245: while (($property = $this->next()) !== false) {
246: $props[] = clone $property;
247: }
248: return $props;
249: }
250:
251: 252: 253: 254: 255: 256: 257: 258: 259: 260: 261: 262:
263: public function deleteByGroupIdTypeName($type, $name) {
264: $sql = $this->db->prepare("group_id = '%s' AND type = '%s' AND name = '%s'", $this->_groupId, $type, $name);
265: $this->select($sql);
266: return $this->_deleteSelected();
267: }
268:
269: 270: 271: 272: 273: 274: 275: 276: 277: 278: 279:
280: public function deleteByGroupIdType($type) {
281: $sql = $this->db->prepare("group_id = '%s' AND type = '%s'", $this->_groupId, $type);
282: $this->select($sql);
283: return $this->_deleteSelected();
284: }
285:
286: 287: 288: 289: 290: 291: 292: 293: 294:
295: public function deleteByGroupId() {
296: $sql = $this->db->prepare("group_id = '%s'", $this->_groupId);
297: $this->select($sql);
298: return $this->_deleteSelected();
299: }
300:
301: 302: 303: 304: 305: 306: 307: 308:
309: protected function _deleteSelected() {
310: $result = false;
311: while (($prop = $this->next()) !== false) {
312: $id = $prop->get('idgroupprop');
313: if (self::$_enableCache) {
314: $this->_deleteFromCache($id);
315: }
316: $result = $this->delete($id);
317: }
318: return $result;
319: }
320:
321: 322: 323: 324: 325: 326:
327: protected function _loadFromCache() {
328: if (!isset(self::$_entries)) {
329: self::$_entries = array();
330: }
331:
332: if (isset(self::$_entries[$this->_groupId])) {
333:
334: return;
335: }
336:
337: self::$_entries[$this->_groupId] = array();
338:
339:
340:
341: if (count(self::$_entries) > self::$_maxGroups) {
342: array_shift(self::$_entries);
343: }
344:
345: $sql = $this->db->prepare("group_id = '%s'", $this->_groupId);
346: $this->select($sql);
347: while (($property = $this->next()) !== false) {
348: $data = $property->toArray();
349: self::$_entries[$this->_groupId][$data['idgroupprop']] = $data;
350: }
351: }
352:
353: 354: 355: 356: 357:
358: protected function _addToCache($item) {
359: $data = $item->toArray();
360: self::$_entries[$this->_groupId][$data['idgroupprop']] = $data;
361: }
362:
363: 364: 365: 366: 367: 368: 369:
370: protected function _fetchByGroupIdTypeNameFromCache($type, $name) {
371: $obj = new cApiGroupProperty();
372: foreach (self::$_entries[$this->_groupId] 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 _fetchByGroupIdTypeFromCache($type) {
388: $props = array();
389: $obj = new cApiGroupProperty();
390: foreach (self::$_entries[$this->_groupId] 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 _fetchByGroupIdFromCache() {
405: $props = array();
406: $obj = new cApiGroupProperty();
407: foreach (self::$_entries[$this->_groupId] as $entry) {
408: $obj->loadByRecordSet($entry);
409: $props[] = clone $obj;
410: }
411: return $props;
412: }
413:
414: 415: 416: 417: 418:
419: protected function _deleteFromCache($id) {
420: if (isset(self::$_entries[$this->_groupId][$id])) {
421: unset(self::$_entries[$this->_groupId][$id]);
422: }
423: }
424:
425: }
426:
427: 428: 429: 430: 431: 432: 433: 434: 435: 436: 437: 438: 439: 440: 441: 442: 443: 444: 445: 446:
447: class cApiGroupProperty extends Item
448: {
449: 450: 451: 452: 453: 454: 455: 456: 457: 458:
459: public function __construct($mId = false) {
460: global $cfg;
461: parent::__construct($cfg['tab']['group_prop'], 'idgroupprop');
462: $this->setFilters(array(), array());
463: if ($mId !== false) {
464: $this->loadByPrimaryKey($mId);
465: }
466: }
467:
468: 469: 470: 471: 472: 473: 474: 475:
476: public function updateValue($value) {
477: $this->set('value', $value);
478: return $this->store();
479: }
480:
481: 482: 483: 484: 485: 486: 487: 488: 489:
490: public function setField($name, $value, $bSafe = true) {
491: switch ($name) {
492: case 'idcatlang':
493: $value = (int) $value;
494: break;
495: }
496:
497: return parent::setField($name, $value, $bSafe);
498: }
499:
500: }
501: