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', $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->_groupId);
152: $item->set('type', $type);
153: $item->set('name', $name);
154: $item->set('value', $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: $sql = $this->db->prepare("group_id = '%s' AND type = '%s' AND name = '%s'", $this->_groupId, $type, $name);
178: $this->select($sql);
179: if (($property = $this->next()) !== false) {
180: return $property;
181: }
182: return NULL;
183: }
184:
185: 186: 187: 188: 189: 190:
191: public function fetchByGroupIdType($type) {
192: if (self::$_enableCache) {
193: return $this->_fetchByGroupIdTypeFromCache($type);
194: }
195:
196: $sql = $this->db->prepare("group_id = '%s' AND type = '%s'", $this->_groupId, $type);
197: $this->select($sql);
198: $props = array();
199: while (($property = $this->next()) !== false) {
200: $props[] = clone $property;
201: }
202: return $props;
203: }
204:
205: 206: 207: 208: 209:
210: public function fetchByGroupId() {
211: if (self::$_enableCache) {
212: return $this->_fetchByGroupIdFromCache();
213: }
214:
215: $sql = $this->db->prepare("group_id = '%s'", $this->_groupId);
216: $this->select($sql);
217: $props = array();
218: while (($property = $this->next()) !== false) {
219: $props[] = clone $property;
220: }
221: return $props;
222: }
223:
224: 225: 226: 227: 228: 229: 230:
231: public function deleteByGroupIdTypeName($type, $name) {
232: $sql = $this->db->prepare("group_id = '%s' AND type = '%s' AND name = '%s'", $this->_groupId, $type, $name);
233: $this->select($sql);
234: return $this->_deleteSelected();
235: }
236:
237: 238: 239: 240: 241: 242:
243: public function deleteByGroupIdType($type) {
244: $sql = $this->db->prepare("group_id = '%s' AND type = '%s'", $this->_groupId, $type);
245: $this->select($sql);
246: return $this->_deleteSelected();
247: }
248:
249: 250: 251: 252: 253:
254: public function deleteByGroupId() {
255: $sql = $this->db->prepare("group_id = '%s'", $this->_groupId);
256: $this->select($sql);
257: return $this->_deleteSelected();
258: }
259:
260: 261: 262: 263: 264:
265: protected function _deleteSelected() {
266: $result = false;
267: while (($prop = $this->next()) !== false) {
268: $id = $prop->get('idgroupprop');
269: if (self::$_enableCache) {
270: $this->_deleteFromCache($id);
271: }
272: $result = $this->delete($id);
273: }
274: return $result;
275: }
276:
277: 278: 279:
280: protected function _loadFromCache() {
281: if (!isset(self::$_entries)) {
282: self::$_entries = array();
283: }
284:
285: if (isset(self::$_entries[$this->_groupId])) {
286:
287: return;
288: }
289:
290: self::$_entries[$this->_groupId] = array();
291:
292:
293:
294: if (count(self::$_entries) > self::$_maxGroups) {
295: array_shift(self::$_entries);
296: }
297:
298: $sql = $this->db->prepare("group_id = '%s'", $this->_groupId);
299: $this->select($sql);
300: while (($property = $this->next()) !== false) {
301: $data = $property->toArray();
302: self::$_entries[$this->_groupId][$data['idgroupprop']] = $data;
303: }
304: }
305:
306: 307: 308: 309: 310:
311: protected function _addToCache($item) {
312: $data = $item->toArray();
313: self::$_entries[$this->_groupId][$data['idgroupprop']] = $data;
314: }
315:
316: 317: 318: 319: 320: 321: 322:
323: protected function _fetchByGroupIdTypeNameFromCache($type, $name) {
324: $obj = new cApiGroupProperty();
325: foreach (self::$_entries[$this->_groupId] as $entry) {
326: if ($entry['type'] == $type && $entry['name'] == $name) {
327: $obj->loadByRecordSet($entry);
328: return $obj;
329: }
330: }
331: return NULL;
332: }
333:
334: 335: 336: 337: 338: 339:
340: protected function _fetchByGroupIdTypeFromCache($type) {
341: $props = array();
342: $obj = new cApiGroupProperty();
343: foreach (self::$_entries[$this->_groupId] as $entry) {
344: if ($entry['type'] == $type) {
345: $obj->loadByRecordSet($entry);
346: $props[] = clone $obj;
347: }
348: }
349: return $props;
350: }
351:
352: 353: 354: 355: 356:
357: protected function _fetchByGroupIdFromCache() {
358: $props = array();
359: $obj = new cApiGroupProperty();
360: foreach (self::$_entries[$this->_groupId] as $entry) {
361: $obj->loadByRecordSet($entry);
362: $props[] = clone $obj;
363: }
364: return $props;
365: }
366:
367: 368: 369: 370: 371:
372: protected function _deleteFromCache($id) {
373: if (isset(self::$_entries[$this->_groupId][$id])) {
374: unset(self::$_entries[$this->_groupId][$id]);
375: }
376: }
377:
378: }
379:
380: 381: 382: 383: 384: 385: 386: 387: 388: 389: 390: 391: 392: 393: 394: 395: 396: 397: 398: 399:
400: class cApiGroupProperty extends Item {
401:
402: 403: 404: 405: 406:
407: public function __construct($mId = false) {
408: global $cfg;
409: parent::__construct($cfg['tab']['group_prop'], 'idgroupprop');
410: $this->setFilters(array(), array());
411: if ($mId !== false) {
412: $this->loadByPrimaryKey($mId);
413: }
414: }
415:
416: 417: 418: 419: 420: 421:
422: public function updateValue($value) {
423: $this->set('value', $value);
424: return $this->store();
425: }
426:
427: }
428: