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