1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15:
16:
17: defined('CON_FRAMEWORK') || die('Illegal call: Missing framework initialization - request aborted.');
18:
19: 20: 21: 22: 23: 24: 25:
26: abstract class Item extends cItemBaseAbstract {
27:
28: 29: 30: 31: 32:
33: public $values;
34:
35: 36: 37: 38: 39: 40:
41: protected $modifiedValues;
42:
43: 44: 45: 46: 47:
48: protected $oldPrimaryKey;
49:
50: 51: 52: 53: 54:
55: protected $_arrInFilters = array(
56: 'htmlspecialchars',
57: 'addslashes'
58: );
59:
60: 61: 62: 63: 64: 65:
66: protected $_arrOutFilters = array(
67: 'stripslashes',
68: 'htmldecode'
69: );
70:
71: 72: 73: 74: 75:
76: protected $_metaObject;
77:
78: 79: 80: 81: 82:
83: protected $_lastSQL;
84:
85: 86: 87: 88: 89: 90: 91:
92: public function __construct($sTable, $sPrimaryKey) {
93: parent::__construct($sTable, $sPrimaryKey, get_parent_class($this));
94: }
95:
96: 97: 98: 99: 100: 101: 102: 103: 104: 105:
106: public function loadBy($sField, $mValue, $bSafe = true) {
107: if ($bSafe) {
108: $mValue = $this->_inFilter($mValue);
109: }
110:
111:
112: $aRecordSet = NULL;
113: if ($sField === $this->primaryKey) {
114: $aRecordSet = $this->_oCache->getItem($mValue);
115: } else {
116: $aRecordSet = $this->_oCache->getItemByProperty($sField, $mValue);
117: }
118:
119: if ($aRecordSet) {
120:
121: $this->loadByRecordSet($aRecordSet);
122: return true;
123: }
124:
125:
126: $sql = "SELECT * FROM `%s` WHERE %s = '%s'";
127: $sql = $this->db->prepare($sql, $this->table, $sField, $mValue);
128:
129:
130: $this->db->query($sql);
131:
132: $this->_lastSQL = $sql;
133:
134: if ($this->db->numRows() > 1) {
135: $msg = "Tried to load a single line with field $sField and value $mValue from " . $this->table . " but found more than one row";
136: throw new cException($msg);
137: }
138:
139:
140: if (!$this->db->nextRecord()) {
141: return false;
142: }
143:
144: $this->loadByRecordSet($this->db->toArray());
145: return true;
146: }
147:
148: 149: 150: 151: 152: 153: 154: 155: 156:
157: public function loadByMany(array $aAttributes, $bSafe = true) {
158: if ($bSafe) {
159: $aAttributes = $this->_inFilter($aAttributes);
160: }
161:
162:
163: $aRecordSet = NULL;
164: if (count($aAttributes) == 1 && isset($aAttributes[$this->primaryKey])) {
165: $aRecordSet = $this->_oCache->getItem($aAttributes[$this->primaryKey]);
166: } else {
167: $aRecordSet = $this->_oCache->getItemByProperties($aAttributes);
168: }
169:
170: if ($aRecordSet) {
171:
172: $this->loadByRecordSet($aRecordSet);
173: return true;
174: }
175:
176:
177: $sql = 'SELECT * FROM `:mytab` WHERE';
178: foreach (array_keys($aAttributes) as $sKey) {
179:
180: if (is_string($sKey)) {
181: $sql .= " $sKey = ':$sKey' AND";
182: } else {
183: $sql .= " $sKey = :$sKey AND";
184: }
185: }
186:
187: $sql = substr($sql, 0, strlen($sql) - 4);
188: $sql = $this->db->prepare($sql, array_merge(array(
189: 'mytab' => $this->table
190: ), $aAttributes));
191:
192:
193: $this->db->query($sql);
194:
195: $this->_lastSQL = $sql;
196:
197: if ($this->db->numRows() > 1) {
198: $msg = 'Tried to load a single line with fields ' . print_r(array_keys($aAttributes), true) . ' and values ' . print_r(array_values($aAttributes), true) . ' from ' . $this->table . ' but found more than one row';
199: throw new cException($msg);
200: }
201:
202:
203: if (!$this->db->nextRecord()) {
204: return false;
205: }
206:
207: $this->loadByRecordSet($this->db->toArray());
208: return true;
209: }
210:
211: 212: 213: 214: 215: 216: 217: 218: 219: 220: 221: 222: 223:
224: protected function _loadByWhereClause($sWhere) {
225:
226: $sql = "SELECT %s AS pk FROM `%s` WHERE " . (string) $sWhere;
227: $sql = $this->db->prepare($sql, $this->primaryKey, $this->table);
228:
229:
230: $this->db->query($sql);
231:
232: $this->_lastSQL = $sql;
233:
234: if ($this->db->numRows() > 1) {
235: $msg = "Tried to load a single line with where clause '" . $sWhere . "' from " . $this->table . " but found more than one row";
236: throw new cException($msg);
237: }
238:
239:
240: if (!$this->db->nextRecord()) {
241: return false;
242: }
243:
244: $id = $this->db->f('pk');
245: return $this->loadByPrimaryKey($id);
246: }
247:
248: 249: 250: 251: 252: 253:
254: public function loadByPrimaryKey($mValue) {
255: $bSuccess = $this->loadBy($this->primaryKey, $mValue);
256:
257: if ($bSuccess == true && method_exists($this, '_onLoad')) {
258: $this->_onLoad();
259: }
260:
261: return $bSuccess;
262: }
263:
264: 265: 266: 267: 268:
269: public function loadByRecordSet(array $aRecordSet) {
270: $this->values = $aRecordSet;
271: $this->oldPrimaryKey = $this->values[$this->primaryKey];
272: $this->virgin = false;
273: $this->_oCache->addItem($this->oldPrimaryKey, $this->values);
274:
275: if (method_exists($this, '_onLoad')) {
276: $this->_onLoad();
277: }
278: }
279:
280: 281: 282: 283: 284:
285: public function isLoaded() {
286: return !$this->virgin;
287: }
288:
289: 290: 291: 292:
293: protected function _onLoad() {
294: }
295:
296: 297: 298: 299: 300: 301: 302:
303: public function getField($sField, $bSafe = true) {
304: if ($this->virgin == true) {
305: $this->lasterror = 'No item loaded';
306: return false;
307: }
308:
309: if (true == $bSafe) {
310: return $this->_outFilter($this->values[$sField]);
311: } else {
312: return $this->values[$sField];
313: }
314: }
315:
316: 317: 318: 319: 320: 321: 322:
323: public function get($sField, $bSafe = true) {
324: return $this->getField($sField, $bSafe);
325: }
326:
327: 328: 329: 330: 331: 332: 333: 334:
335: public function setField($sField, $mValue, $bSafe = true) {
336: if ($this->virgin == true) {
337: $this->lasterror = 'No item loaded';
338: return false;
339: }
340:
341: if ($sField == $this->primaryKey) {
342: $this->oldPrimaryKey = $this->values[$sField];
343: }
344:
345:
346: if (true == $bSafe) {
347: $mValue = $this->_inFilter($mValue);
348: }
349:
350:
351: if ($this->values[$sField] != $mValue || strlen($this->values[$sField]) != strlen($mValue)) {
352: $this->modifiedValues[$sField] = true;
353: }
354:
355:
356: $this->values[$sField] = $mValue;
357:
358: return true;
359: }
360:
361: 362: 363: 364: 365: 366: 367:
368: public function set($sField, $mValue, $bSafe = true) {
369: return $this->setField($sField, $mValue, $bSafe);
370: }
371:
372: 373: 374: 375: 376:
377: public function store() {
378: $this->_executeCallbacks(self::STORE_BEFORE, get_class($this), array(
379: $this
380: ));
381:
382: if ($this->virgin == true) {
383: $this->lasterror = 'No item loaded';
384: $this->_executeCallbacks(self::STORE_FAILURE, get_class($this), array(
385: $this
386: ));
387: return false;
388: }
389:
390: $sql = 'UPDATE `' . $this->table . '` SET ';
391: $first = true;
392:
393: if (!is_array($this->modifiedValues)) {
394: $this->_executeCallbacks(self::STORE_SUCCESS, get_class($this), array(
395: $this
396: ));
397: return true;
398: }
399:
400: foreach ($this->modifiedValues as $key => $bValue) {
401: $value = $this->values[$key];
402: if (is_string($value)) {
403: $value = $this->db->escape($value);
404: }
405:
406: if ($first == true) {
407: $sql .= "`$key` = '" . $value . "'";
408: $first = false;
409: } else {
410: $sql .= ", `$key` = '" . $value . "'";
411: }
412: }
413:
414: $sql .= " WHERE " . $this->primaryKey . " = '" . $this->oldPrimaryKey . "'";
415:
416: $this->db->query($sql);
417:
418: $this->_lastSQL = $sql;
419:
420: if ($this->db->affectedRows() > 0) {
421: $this->_oCache->addItem($this->oldPrimaryKey, $this->values);
422: $this->_executeCallbacks(self::STORE_SUCCESS, get_class($this), array(
423: $this
424: ));
425: return true;
426: }
427:
428: $this->_executeCallbacks(self::STORE_FAILURE, get_class($this), array(
429: $this
430: ));
431: return false;
432: }
433:
434: 435: 436: 437: 438:
439: public function toArray() {
440: if ($this->virgin == true) {
441: $this->lasterror = 'No item loaded';
442: return false;
443: }
444:
445: $aReturn = array();
446: foreach ($this->values as $field => $value) {
447: $aReturn[$field] = $this->getField($field);
448: }
449: return $aReturn;
450: }
451:
452: 453: 454: 455: 456:
457: public function toObject() {
458: $return = $this->toArray();
459: return (false !== $return) ? (object) $return : $return;
460: }
461:
462: 463: 464: 465: 466: 467: 468: 469: 470:
471: public function setProperty($sType, $sName, $mValue, $iClient = 0) {
472:
473: if ($this->virgin == true) {
474: $this->lasterror = 'No item loaded';
475: return false;
476: }
477:
478:
479: $oProperties = $this->_getPropertiesCollectionInstance($iClient);
480: $bResult = $oProperties->setValue($this->primaryKey, $this->get($this->primaryKey), $sType, $sName, $mValue);
481: return $bResult;
482: }
483:
484: 485: 486: 487: 488: 489: 490: 491:
492: public function getProperty($sType, $sName, $iClient = 0) {
493:
494: if ($this->virgin == true) {
495: $this->lasterror = 'No item loaded';
496: return false;
497: }
498:
499:
500: $oProperties = $this->_getPropertiesCollectionInstance($iClient);
501: $mValue = $oProperties->getValue($this->primaryKey, $this->get($this->primaryKey), $sType, $sName);
502: return $mValue;
503: }
504:
505: 506: 507: 508: 509: 510: 511: 512:
513: public function deleteProperty($sType, $sName, $iClient = 0) {
514:
515: if ($this->virgin == true) {
516: $this->lasterror = 'No item loaded';
517: return false;
518: }
519:
520:
521: $oProperties = $this->_getPropertiesCollectionInstance($iClient);
522: $bResult = $oProperties->deleteValue($this->primaryKey, $this->get($this->primaryKey), $sType, $sName);
523: return $bResult;
524: }
525:
526: 527: 528: 529: 530: 531:
532: public function deletePropertyById($idprop) {
533: $oProperties = $this->_getPropertiesCollectionInstance();
534: return $oProperties->delete($idprop);
535: }
536:
537:
538:
539:
540:
541:
542:
543:
544:
545:
546: 547: 548: 549: 550: 551: 552: 553: 554: 555: 556: 557: 558: 559:
560: public function setFilters($aInFilters = array(), $aOutFilters = array()) {
561: $this->_arrInFilters = $aInFilters;
562: $this->_arrOutFilters = $aOutFilters;
563: }
564:
565: 566: 567: 568: 569: 570: 571: 572: 573:
574: public function _inFilter($mData) {
575: foreach ($this->_arrInFilters as $_function) {
576: if (function_exists($_function)) {
577: if (is_array($mData)) {
578: foreach ($mData as $key => $value) {
579: $mData[$key] = $_function($value);
580: }
581: } else {
582: $mData = $_function($mData);
583: }
584: }
585: }
586: return $mData;
587: }
588:
589: 590: 591: 592: 593: 594: 595: 596: 597:
598: protected function _outFilter($mData) {
599: foreach ($this->_arrOutFilters as $_function) {
600: if (function_exists($_function)) {
601: if (is_array($mData)) {
602: foreach ($mData as $key => $value) {
603: $mData[$key] = $_function($value);
604: }
605: } else {
606: $mData = $_function($mData);
607: }
608: }
609: }
610: return $mData;
611: }
612:
613: 614: 615: 616: 617:
618: protected function _setMetaObject($metaObject) {
619: $this->_metaObject = $metaObject;
620: }
621:
622: 623: 624: 625: 626: 627:
628: public function getMetaObject() {
629: global $_metaObjectCache;
630:
631: if (!is_array($_metaObjectCache)) {
632: $_metaObjectCache = array();
633: }
634:
635: $sClassName = $this->_metaObject;
636: $qclassname = strtolower($sClassName);
637:
638: if (array_key_exists($qclassname, $_metaObjectCache)) {
639: if (is_object($_metaObjectCache[$qclassname])) {
640: if (strtolower(get_class($_metaObjectCache[$qclassname])) == $qclassname) {
641: $_metaObjectCache[$qclassname]->setPayloadObject($this);
642: return $_metaObjectCache[$qclassname];
643: }
644: }
645: }
646:
647: if (class_exists($sClassName)) {
648: $_metaObjectCache[$qclassname] = new $sClassName($this);
649: return $_metaObjectCache[$qclassname];
650: }
651: }
652: }
653: