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:     public function __construct($groupId) {
 63:         global $cfg;
 64:         parent::__construct($cfg['tab']['group_prop'], 'idgroupprop');
 65:         $this->_setItemClass('cApiGroupProperty');
 66: 
 67:         
 68:         $this->_setJoinPartner('cApiGroupCollection');
 69: 
 70:         if (!isset(self::$_enableCache)) {
 71:             if (isset($cfg['properties']) && isset($cfg['properties']['group_prop']) && isset($cfg['properties']['group_prop']['enable_cache'])) {
 72:                 self::$_enableCache = (bool) $cfg['properties']['group_prop']['enable_cache'];
 73: 
 74:                 if (isset($cfg['properties']['group_prop']['max_groups'])) {
 75:                     self::$_maxGroups = (int) $cfg['properties']['group_prop']['max_groups'];
 76:                     
 77:                     
 78:                     if (self::$_maxGroups < 1) {
 79:                         self::$_maxGroups = 1;
 80:                     }
 81:                 }
 82:             } else {
 83:                 self::$_enableCache = false;
 84:             }
 85:         }
 86: 
 87:         $this->setGroupId($groupId);
 88:     }
 89: 
 90:      91:  92: 
 93:     public static function reset() {
 94:         unset(self::$_enableCache, self::$_entries, self::$_maxGroups);
 95:     }
 96: 
 97:      98:  99: 100: 101: 102: 
103:     public function setGroupId($groupId) {
104:         if (empty($groupId)) {
105:             throw new cInvalidArgumentException("Empty group id");
106:         }
107:         $this->_groupId = $groupId;
108:         if (self::$_enableCache) {
109:             $this->_loadFromCache();
110:         }
111:     }
112: 
113:     114: 115: 116: 117: 118: 119: 120: 121: 
122:     public function setValueByTypeName($type, $name, $value, $idcatlang = 0) {
123:         $item = $this->fetchByGroupIdTypeName($type, $name);
124:         if ($item) {
125:             $item->set('value', $value);
126:             $item->store();
127:         } else {
128:             $item = $this->create($type, $name, $value, $idcatlang);
129:         }
130: 
131:         if (self::$_enableCache) {
132:             $this->_addToCache($item);
133:         }
134: 
135:         return $item;
136:     }
137: 
138:     139: 140: 141: 142: 143: 144: 145: 146: 
147:     public function create($type, $name, $value, $idcatlang = 0) {
148:         $item = $this->createNewItem();
149: 
150:         $item->set('group_id', $this->_groupId);
151:         $item->set('type', $type);
152:         $item->set('name', $name);
153:         $item->set('value', $value);
154:         $item->set('idcatlang', $idcatlang);
155:         $item->store();
156: 
157:         if (self::$_enableCache) {
158:             $this->_addToCache($item);
159:         }
160: 
161:         return $item;
162:     }
163: 
164:     165: 166: 167: 168: 169: 170: 
171:     public function fetchByGroupIdTypeName($type, $name) {
172:         if (self::$_enableCache) {
173:             return $this->_fetchByGroupIdTypeNameFromCache($type, $name);
174:         }
175: 
176:         $sql = $this->db->prepare("group_id = '%s' AND type = '%s' AND name = '%s'", $this->_groupId, $type, $name);
177:         $this->select($sql);
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:         $sql = $this->db->prepare("group_id = '%s' AND type = '%s'", $this->_groupId, $type);
196:         $this->select($sql);
197:         $props = array();
198:         while (($property = $this->next()) !== false) {
199:             $props[] = clone $property;
200:         }
201:         return $props;
202:     }
203: 
204:     205: 206: 207: 208: 
209:     public function fetchByGroupId() {
210:         if (self::$_enableCache) {
211:             return $this->_fetchByGroupIdFromCache();
212:         }
213: 
214:         $sql = $this->db->prepare("group_id = '%s'", $this->_groupId);
215:         $this->select($sql);
216:         $props = array();
217:         while (($property = $this->next()) !== false) {
218:             $props[] = clone $property;
219:         }
220:         return $props;
221:     }
222: 
223:     224: 225: 226: 227: 228: 229: 
230:     public function deleteByGroupIdTypeName($type, $name) {
231:         $sql = $this->db->prepare("group_id = '%s' AND type = '%s' AND name = '%s'", $this->_groupId, $type, $name);
232:         $this->select($sql);
233:         return $this->_deleteSelected();
234:     }
235: 
236:     237: 238: 239: 240: 241: 
242:     public function deleteByGroupIdType($type) {
243:         $sql = $this->db->prepare("group_id = '%s' AND type = '%s'", $this->_groupId, $type);
244:         $this->select($sql);
245:         return $this->_deleteSelected();
246:     }
247: 
248:     249: 250: 251: 252: 
253:     public function deleteByGroupId() {
254:         $sql = $this->db->prepare("group_id = '%s'", $this->_groupId);
255:         $this->select($sql);
256:         return $this->_deleteSelected();
257:     }
258: 
259:     260: 261: 262: 263: 
264:     protected function _deleteSelected() {
265:         $result = false;
266:         while (($prop = $this->next()) !== false) {
267:             $id = $prop->get('idgroupprop');
268:             if (self::$_enableCache) {
269:                 $this->_deleteFromCache($id);
270:             }
271:             $result = $this->delete($id);
272:         }
273:         return $result;
274:     }
275: 
276:     277: 278: 
279:     protected function _loadFromCache() {
280:         if (!isset(self::$_entries)) {
281:             self::$_entries = array();
282:         }
283: 
284:         if (isset(self::$_entries[$this->_groupId])) {
285:             
286:             return;
287:         }
288: 
289:         self::$_entries[$this->_groupId] = array();
290: 
291:         
292:         
293:         if (count(self::$_entries) > self::$_maxGroups) {
294:             array_shift(self::$_entries);
295:         }
296: 
297:         $sql = $this->db->prepare("group_id = '%s'", $this->_groupId);
298:         $this->select($sql);
299:         while (($property = $this->next()) !== false) {
300:             $data = $property->toArray();
301:             self::$_entries[$this->_groupId][$data['idgroupprop']] = $data;
302:         }
303:     }
304: 
305:     306: 307: 308: 309: 
310:     protected function _addToCache($item) {
311:         $data = $item->toArray();
312:         self::$_entries[$this->_groupId][$data['idgroupprop']] = $data;
313:     }
314: 
315:     316: 317: 318: 319: 320: 321: 
322:     protected function _fetchByGroupIdTypeNameFromCache($type, $name) {
323:         $obj = new cApiGroupProperty();
324:         foreach (self::$_entries[$this->_groupId] as $entry) {
325:             if ($entry['type'] == $type && $entry['name'] == $name) {
326:                 $obj->loadByRecordSet($entry);
327:                 return $obj;
328:             }
329:         }
330:         return NULL;
331:     }
332: 
333:     334: 335: 336: 337: 338: 
339:     protected function _fetchByGroupIdTypeFromCache($type) {
340:         $props = array();
341:         $obj = new cApiGroupProperty();
342:         foreach (self::$_entries[$this->_groupId] as $entry) {
343:             if ($entry['type'] == $type) {
344:                 $obj->loadByRecordSet($entry);
345:                 $props[] = clone $obj;
346:             }
347:         }
348:         return $props;
349:     }
350: 
351:     352: 353: 354: 355: 
356:     protected function _fetchByGroupIdFromCache() {
357:         $props = array();
358:         $obj = new cApiGroupProperty();
359:         foreach (self::$_entries[$this->_groupId] as $entry) {
360:             $obj->loadByRecordSet($entry);
361:             $props[] = clone $obj;
362:         }
363:         return $props;
364:     }
365: 
366:     367: 368: 369: 370: 
371:     protected function _deleteFromCache($id) {
372:         if (isset(self::$_entries[$this->_groupId][$id])) {
373:             unset(self::$_entries[$this->_groupId][$id]);
374:         }
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: class cApiGroupProperty extends Item {
400: 
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: 429: 430: 431: 432: 433: 434: 435: 
436:     public function setField($name, $value, $bSafe = true) {
437:         switch ($name) {
438:              case 'idcatlang':
439:                 $value = (int) $value;
440:                 break;
441:         }
442: 
443:         return parent::setField($name, $value, $bSafe);
444:     }
445: 
446: }
447: