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