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