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