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 = $this->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', $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: $sql = $this->db->prepare("user_id = '%s'", $this->_userId);
156: $this->select($sql);
157: $props = array();
158: while (($property = $this->next()) !== false) {
159: $props[] = clone $property;
160: }
161: return $props;
162: }
163:
164: 165: 166: 167: 168: 169: 170: 171: 172:
173: public function fetchByTypeName($type, $name) {
174: $sql = $this->db->prepare("type = '%s' AND name = '%s'", $type, $name);
175: $this->select($sql);
176: $props = array();
177: while (($property = $this->next()) !== false) {
178: $props[] = clone $property;
179: }
180: return $props;
181: }
182:
183: 184: 185: 186: 187: 188: 189:
190: public function fetchByUserIdTypeName($type, $name) {
191: if (self::$_enableCache) {
192: return $this->_fetchByUserIdTypeNameFromCache($type, $name);
193: }
194:
195: $sql = $this->db->prepare("user_id = '%s' AND type = '%s' AND name = '%s'", $this->_userId, $type, $name);
196: $this->select($sql);
197: if (($property = $this->next()) !== false) {
198: return $property;
199: }
200: return NULL;
201: }
202:
203: 204: 205: 206: 207: 208:
209: public function fetchByUserIdType($type) {
210: if (self::$_enableCache) {
211: return $this->_fetchByUserIdTypeFromCache($type);
212: }
213:
214: $sql = $this->db->prepare("user_id = '%s' AND type = '%s'", $this->_userId, $type);
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 deleteByUserIdTypeName($type, $name) {
231: $sql = $this->db->prepare("user_id = '%s' AND type = '%s' AND name = '%s'", $this->_userId, $type, $name);
232: $this->select($sql);
233: return $this->_deleteSelected();
234: }
235:
236: 237: 238: 239: 240: 241:
242: public function deleteByUserIdType($type) {
243: $sql = $this->db->prepare("user_id = '%s' AND type = '%s'", $this->_userId, $type);
244: $this->select($sql);
245: return $this->_deleteSelected();
246: }
247:
248: 249: 250: 251: 252:
253: public function deleteByUserId() {
254: $sql = $this->db->prepare("user_id = '%s'", $this->_userId);
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('iduserprop');
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: self::$_entries = array();
281: $sql = $this->db->prepare("user_id = '%s'", $this->_userId);
282: $this->select($sql);
283: while (($property = $this->next()) !== false) {
284: $data = $property->toArray();
285: self::$_entries[$data['iduserprop']] = $data;
286: }
287: }
288:
289: 290: 291: 292: 293:
294: protected function _addToCache($entry) {
295: $data = $entry->toArray();
296: self::$_entries[$data['iduserprop']] = $data;
297: }
298:
299: 300: 301: 302: 303:
304: protected function _fetchByUserIdFromCache() {
305: $props = array();
306: $obj = new cApiUserProperty();
307: foreach (self::$_entries as $entry) {
308: $obj->loadByRecordSet($entry);
309: $props[] = clone $obj;
310: }
311: return $props;
312: }
313:
314: 315: 316: 317: 318: 319: 320:
321: public function _fetchByUserIdTypeNameFromCache($type, $name) {
322: $props = array();
323: $obj = new cApiUserProperty();
324: foreach (self::$_entries 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: public function _fetchByUserIdTypeFromCache($type) {
340: $props = array();
341: $obj = new cApiUserProperty();
342: foreach (self::$_entries 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 _deleteFromCache($id) {
357: if (isset(self::$_entries[$id])) {
358: unset(self::$_entries[$id]);
359: }
360: }
361:
362: }
363:
364: 365: 366: 367: 368: 369:
370: class cApiUserProperty extends Item {
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: public function setField($name, $value, $bSafe = true) {
405: switch ($name) {
406: case 'idcatlang':
407: $value = (int) $value;
408: break;
409: }
410:
411: return parent::setField($name, $value, $bSafe);
412: }
413:
414: }
415: