1: <?php
2: 3: 4: 5: 6: 7: 8: 9:
10:
11: if (!defined('XOOPS_ROOT_PATH')) exit();
12:
13: 14: 15:
16: class XoopsObjectGenericHandler extends XoopsObjectHandler
17: {
18: public $mTable = null;
19: public $mPrimary = null;
20: public $mClass = null;
21: public $mDirname = null;
22: public $mDataname = null;
23:
24: 25: 26:
27: var $_mDummyObj = null;
28:
29: function XoopsObjectGenericHandler(&$db)
30: {
31: parent::XoopsObjectHandler($db);
32: $tableArr = explode('_', $this->mTable);
33: $this->mDirname = array_shift($tableArr);
34: $this->mDataname = implode('_', $tableArr);
35: $this->mTable = $this->db->prefix($this->mTable);
36: }
37:
38: function &create($isNew = true)
39: {
40: $obj = null;
41: if (XC_CLASS_EXISTS($this->mClass)) {
42: $obj =new $this->mClass();
43: $obj->mDirname = $this->getDirname();
44: if($isNew)
45: $obj->setNew();
46: }
47: return $obj;
48: }
49:
50: function &get($id)
51: {
52: $ret = null;
53:
54: $criteria =new Criteria($this->mPrimary, $id);
55: $objArr =& $this->getObjects($criteria);
56:
57: if (count($objArr) == 1) {
58: $ret =& $objArr[0];
59: }
60:
61: return $ret;
62: }
63:
64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74:
75: function &getObjects($criteria = null, $limit = null, $start = null, $id_as_key = false)
76: {
77: $ret = array();
78:
79: $sql = 'SELECT * FROM `' . $this->mTable . '`';
80:
81: if($criteria !== null && is_a($criteria, 'CriteriaElement')) {
82: $where = $this->_makeCriteria4sql($criteria);
83:
84: if (trim($where)) {
85: $sql .= ' WHERE ' . $where;
86: }
87:
88: $sorts = array();
89: foreach ($criteria->getSorts() as $sort) {
90: $sorts[] = '`' . $sort['sort'] . '` ' . $sort['order'];
91: }
92: if ($criteria->getSort() != '') {
93: $sql .= ' ORDER BY ' . implode(',', $sorts);
94: }
95:
96: if ($limit === null) {
97: $limit = $criteria->getLimit();
98: }
99:
100: if ($start === null) {
101: $start = $criteria->getStart();
102: }
103: }
104: else {
105: if ($limit === null) {
106: $limit = 0;
107: }
108:
109: if ($start === null) {
110: $start = 0;
111: }
112: }
113:
114: $db = $this->db;
115: $result = $db->query($sql, $limit, $start);
116:
117: if (!$result) {
118: return $ret;
119: }
120:
121: while($row = $db->fetchArray($result)) {
122: $obj =new $this->mClass();
123: $obj->mDirname = $this->getDirname();
124: $obj->assignVars($row);
125: $obj->unsetNew();
126:
127: if ($id_as_key) {
128: $ret[$obj->get($this->mPrimary)] =& $obj;
129: }
130: else {
131: $ret[]=&$obj;
132: }
133:
134: unset($obj);
135: }
136:
137: return $ret;
138: }
139:
140: 141: 142: 143: 144: 145: 146: 147: 148:
149: public function getIdList($criteria = null, $limit = null, $start = null)
150: {
151: $ret = array();
152:
153: $sql = "SELECT `".$this->mPrimary."` FROM `" . $this->mTable . '`';
154:
155: if($criteria !== null && is_a($criteria, 'CriteriaElement')) {
156: $where = $this->_makeCriteria4sql($criteria);
157:
158: if (trim($where)) {
159: $sql .= " WHERE " . $where;
160: }
161:
162: $sorts = array();
163: foreach ($criteria->getSorts() as $sort) {
164: $sorts[] = '`' . $sort['sort'] . '` ' . $sort['order'];
165: }
166: if ($criteria->getSort() != '') {
167: $sql .= " ORDER BY " . implode(',', $sorts);
168: }
169:
170: if ($limit === null) {
171: $limit = $criteria->getLimit();
172: }
173:
174: if ($start === null) {
175: $start = $criteria->getStart();
176: }
177: }
178: else {
179: if ($limit === null) {
180: $limit = 0;
181: }
182:
183: if ($start === null) {
184: $start = 0;
185: }
186: }
187:
188: $result = $this->db->query($sql, $limit, $start);
189:
190: if (!$result) {
191: return $ret;
192: }
193:
194: while($row = $this->db->fetchArray($result)) {
195: $ret[] = $row[$this->mPrimary];
196: }
197:
198: return $ret;
199: }
200:
201: function getCount($criteria = null)
202: {
203: $sql="SELECT COUNT(*) c FROM `" . $this->mTable . '`';
204:
205: if($criteria !== null && is_a($criteria, 'CriteriaElement')) {
206: $where = $this->_makeCriteria4sql($criteria);
207:
208: if ($where) {
209: $sql .= " WHERE " . $where;
210: }
211: }
212:
213: return $this->_getCount($sql);
214: }
215:
216: 217: 218:
219: function _getCount($sql = null)
220: {
221: $result=$this->db->query($sql);
222:
223: if (!$result) {
224: return false;
225: }
226:
227: $ret = $this->db->fetchArray($result);
228:
229: return $ret['c'];
230: }
231:
232: function insert(&$obj, $force = false)
233: {
234: if(!is_a($obj, $this->mClass)) {
235: return false;
236: }
237:
238: $new_flag = false;
239:
240: if ($obj->isNew()) {
241: $new_flag = true;
242: $sql = $this->_insert($obj);
243: }
244: else {
245: $sql = $this->_update($obj);
246: }
247:
248: $result = $force ? $this->db->queryF($sql) : $this->db->query($sql);
249:
250: if (!$result){
251: return false;
252: }
253:
254: if ($new_flag) {
255: $obj->setVar($this->mPrimary, $this->db->getInsertId());
256: $this->_callDelegate('Add', $obj);
257: }
258: else{
259: $this->_callDelegate('Update', $obj);
260: }
261:
262: return true;
263: }
264:
265: 266: 267:
268: function _insert(&$obj) {
269: $fileds=array();
270: $values=array();
271:
272: $arr = $this->_makeVars4sql($obj);
273:
274: foreach($arr as $_name => $_value) {
275: $fields[] = "`${_name}`";
276: $values[] = $_value;
277: }
278:
279: $sql = @sprintf("INSERT INTO `" . $this->mTable . "` ( %s ) VALUES ( %s )", implode(",", $fields), implode(",", $values));
280:
281: return $sql;
282: }
283:
284: 285: 286:
287: function _update(&$obj) {
288: $set_lists=array();
289: $where = "";
290:
291: $arr = $this->_makeVars4sql($obj);
292:
293: foreach ($arr as $_name => $_value) {
294: if ($_name == $this->mPrimary) {
295: $where = "`${_name}`=${_value}";
296: }
297: else {
298: $set_lists[] = "`${_name}`=${_value}";
299: }
300: }
301:
302: $sql = @sprintf("UPDATE `" . $this->mTable . "` SET %s WHERE %s", implode(",",$set_lists), $where);
303:
304: return $sql;
305: }
306:
307: 308: 309: 310: 311:
312: function _makeVars4sql(&$obj)
313: {
314: $ret = array();
315: foreach ($obj->gets() as $key => $value) {
316: if ($value === null) {
317: $ret[$key] = 'NULL';
318: }
319: else {
320: switch ($obj->mVars[$key]['data_type']) {
321: case XOBJ_DTYPE_STRING:
322: case XOBJ_DTYPE_TEXT:
323: $ret[$key] = $this->db->quoteString($value);
324: break;
325:
326: default:
327: $ret[$key] = $value;
328: }
329: }
330: }
331:
332: return $ret;
333: }
334:
335: function _makeCriteria4sql($criteria)
336: {
337: if ($this->_mDummyObj == null) {
338: $this->_mDummyObj =& $this->create();
339: }
340:
341: return $this->_makeCriteriaElement4sql($criteria, $this->_mDummyObj);
342: }
343:
344: 345: 346: 347:
348: function _makeCriteriaElement4sql($criteria, &$obj)
349: {
350: if (is_a($criteria, 'CriteriaElement')) {
351: if ($criteria->hasChildElements()) {
352: $maxCount = $criteria->getCountChildElements();
353: $queryString = $this->_makeCriteria4sql($criteria->getChildElement(0));
354: for ($i = 1; $i < $maxCount; $i++) {
355: $queryString .= ' ' . $criteria->getCondition($i) . ' ' . $this->_makeCriteria4sql($criteria->getChildElement($i));
356: }
357: return '('.$queryString.')';
358: } else {
359:
360:
361:
362: $name = $criteria->getName();
363: $value = $criteria->getValue();
364: if ($name != null && isset($obj->mVars[$name])) {
365: if ($value === null) {
366: $criteria->operator = $criteria->getOperator() == '=' ? 'IS' : 'IS NOT';
367: $value = 'NULL';
368: } elseif (in_array(strtoupper($criteria->operator), array('IN', 'NOT IN'))) {
369: $value = is_array($value) ? $value : explode(',', $value);
370: $typ = $obj->mVars[$name]['data_type'];
371: foreach ( $value as $val ) {
372: $tmp[] = $this->_escapeValue($val, $typ);
373: }
374: if(isset($tmp)){
375: $value = '('.implode(',', $tmp).')';
376: }
377: else{
378: $value = '("")';
379: }
380: } else {
381: $value = $this->_escapeValue($value, $obj->mVars[$name]['data_type']);
382: }
383: } else {
384: $value = $this->db->quoteString($value);
385: }
386:
387: return $name != null?$name . ' ' . $criteria->getOperator() . ' ' . $value : null;
388: }
389: }
390: }
391:
392: function _escapeValue($value, $type)
393: {
394: switch ($type) {
395: case XOBJ_DTYPE_BOOL:
396: return $value ? 1 : 0;
397: case XOBJ_DTYPE_INT:
398: return (int)$value;
399: case XOBJ_DTYPE_FLOAT:
400: return (float)$value;
401: default:
402: return $this->db->quoteString($value);
403: }
404: return null;
405: }
406:
407: 408: 409: 410: 411:
412: function delete(&$obj, $force = false)
413: {
414:
415:
416:
417:
418: $criteria =new Criteria($this->mPrimary, $obj->get($this->mPrimary));
419: $sql = "DELETE FROM `" . $this->mTable . "` WHERE " . $this->_makeCriteriaElement4sql($criteria, $obj);
420:
421: $result = $force ? $this->db->queryF($sql) : $this->db->query($sql);
422: if($result==true) $this->_callDelegate('delete', $obj);
423:
424: return $result;
425: }
426:
427: 428: 429: 430: 431: 432: 433: 434: 435:
436: function deleteAll($criteria, $force = false)
437: {
438: $objs =& $this->getObjects($criteria);
439:
440: $flag = true;
441:
442: foreach ($objs as $obj) {
443: $flag &= $this->delete($obj, $force);
444: }
445:
446: return $flag;
447: }
448:
449: 450: 451: 452: 453: 454: 455:
456: public function getDirname()
457: {
458: return $this->mDirname;
459: }
460:
461: 462: 463: 464: 465: 466: 467:
468: public function getDataname()
469: {
470: return $this->mDataname;
471: }
472:
473: 474: 475: 476: 477: 478: 479: 480:
481: public function _callDelegate( $type, &$obj)
482: {
483: $arr = explode('_', $this->mTable);
484: if(isset($arr[2])){
485: $tableName = $arr[2];
486: for($i=3;$i<count($arr);$i++) $tableName .= '_'.$arr[$i];
487: XCube_DelegateUtils::call(sprintf('Module.%s.Event.%s.%s', $this->getDirname(), $type, $tableName), new XCube_Ref($obj));
488: }
489: else{
490: XCube_DelegateUtils::call(sprintf('Module.%s.Event.%s', $this->getDirname(), $type), new XCube_Ref($obj));
491: }
492: }
493:
494: }
495: ?>
496: