XCL Web Application Platform 2.5.0
The XoopsCube Legacy Project
Loading...
Searching...
No Matches
handler.php
1<?php
11
12if (!defined('XOOPS_ROOT_PATH')) {
13 exit();
14}
15
20{
21 public $mTable = null;
22 public $mPrimary = null;
23 public $mClass = null;
24 public $mDirname = null;
25 public $mDataname = null;
26
30 public $_mDummyObj = null;
31
32 public function __construct(&$db)
33 {
34 parent::__construct($db);
35 if (!is_null($this->mTable)) {
36 $tableArr = explode('_', $this->mTable);
37 } else {
38 $tableArr = [];
39 }
40 $this->mDirname = array_shift($tableArr);
41 $this->mDataname = implode('_', $tableArr);
42 $this->mTable = $this->db->prefix($this->mTable);
43 }
44
45 public function &create($isNew = true)
46 {
47 $obj = null;
48 if (XC_CLASS_EXISTS($this->mClass)) {
49 $obj =new $this->mClass();
50 $obj->mDirname = $this->getDirname();
51 if ($isNew) {
52 $obj->setNew();
53 }
54 }
55 return $obj;
56 }
57
58 public function &get($id)
59 {
60 $ret = null;
61
62 $criteria =new Criteria($this->mPrimary, $id);
63 $objArr =& $this->getObjects($criteria);
64
65 if (1 == count($objArr)) {
66 $ret =& $objArr[0];
67 }
68
69 return $ret;
70 }
71
83 public function &getObjects($criteria = null, $limit = null, $start = null, $id_as_key = false)
84 {
85 $ret = [];
86
87 $sql = 'SELECT * FROM `' . $this->mTable . '`';
88
89 if (null !== $criteria && $criteria instanceof \CriteriaElement) {
90 $where = $this->_makeCriteria4sql($criteria);
91
92 if (trim((string) $where)) {
93 $sql .= ' WHERE ' . $where;
94 }
95
96 $sorts = [];
97 foreach ($criteria->getSorts() as $sort) {
98 $sorts[] = '`' . $sort['sort'] . '` ' . $sort['order'];
99 }
100 if ('' != $criteria->getSort()) {
101 $sql .= ' ORDER BY ' . implode(',', $sorts);
102 }
103
104 if (null === $limit) {
105 $limit = $criteria->getLimit();
106 }
107
108 if (null === $start) {
109 $start = $criteria->getStart();
110 }
111 } else {
112 if (null === $limit) {
113 $limit = 0;
114 }
115
116 if (null === $start) {
117 $start = 0;
118 }
119 }
120
121 $db = $this->db;
122 $result = $db->query($sql, $limit, $start);
123
124 if (!$result) {
125 return $ret;
126 }
127
128 while ($row = $db->fetchArray($result)) {
129 $obj =new $this->mClass();
130 $obj->mDirname = $this->getDirname();
131 $obj->assignVars($row);
132 $obj->unsetNew();
133
134 if ($id_as_key) {
135 $ret[$obj->get($this->mPrimary)] =& $obj;
136 } else {
137 $ret[]=&$obj;
138 }
139
140 unset($obj);
141 }
142
143 return $ret;
144 }
145
155 public function getIdList($criteria = null, $limit = null, $start = null)
156 {
157 $ret = [];
158
159 $sql = 'SELECT `' . $this->mPrimary . '` FROM `' . $this->mTable . '`';
160
161 if (null !== $criteria && $criteria instanceof \CriteriaElement) {
162 $where = $this->_makeCriteria4sql($criteria);
163
164 if (trim($where)) {
165 $sql .= ' WHERE ' . $where;
166 }
167
168 $sorts = [];
169 foreach ($criteria->getSorts() as $sort) {
170 $sorts[] = '`' . $sort['sort'] . '` ' . $sort['order'];
171 }
172 if ('' != $criteria->getSort()) {
173 $sql .= ' ORDER BY ' . implode(',', $sorts);
174 }
175
176 if (null === $limit) {
177 $limit = $criteria->getLimit();
178 }
179
180 if (null === $start) {
181 $start = $criteria->getStart();
182 }
183 } else {
184 if (null === $limit) {
185 $limit = 0;
186 }
187
188 if (null === $start) {
189 $start = 0;
190 }
191 }
192
193 $result = $this->db->query($sql, $limit, $start);
194
195 if (!$result) {
196 return $ret;
197 }
198
199 while ($row = $this->db->fetchArray($result)) {
200 $ret[] = $row[$this->mPrimary];
201 }
202
203 return $ret;
204 }
205
206 public function getCount($criteria = null)
207 {
208 $sql= 'SELECT COUNT(*) c FROM `' . $this->mTable . '`';
209
210 if (null !== $criteria && $criteria instanceof \CriteriaElement) {
211 $where = $this->_makeCriteria4sql($criteria);
212
213 if ($where) {
214 $sql .= ' WHERE ' . $where;
215 }
216 }
217
218 return $this->_getCount($sql);
219 }
220
226 public function _getCount($sql = null)
227 {
228 $result=$this->db->query($sql);
229
230 if (!$result) {
231 return false;
232 }
233
234 $ret = $this->db->fetchArray($result);
235
236 return $ret['c'];
237 }
238
239 public function insert(&$obj, $force = false)
240 {
241 if (!is_a($obj, $this->mClass)) {
242 return false;
243 }
244
245 $new_flag = false;
246
247 if ($obj->isNew()) {
248 $new_flag = true;
249 $sql = $this->_insert($obj);
250 } else {
251 $sql = $this->_update($obj);
252 }
253
254 $result = $force ? $this->db->queryF($sql) : $this->db->query($sql);
255
256 if (!$result) {
257 return false;
258 }
259
260 if ($new_flag) {
261 $obj->setVar($this->mPrimary, $this->db->getInsertId());
262 $this->_callDelegate('Add', $obj);
263 } else {
264 $this->_callDelegate('Update', $obj);
265 }
266
267 return true;
268 }
269
275 public function _insert(&$obj)
276 {
277 $fields = [];
278 $fileds= [];
279 $values= [];
280
281 $arr = $this->_makeVars4sql($obj);
282
283 foreach ($arr as $_name => $_value) {
284 $fields[] = "`{$_name}`";
285 $values[] = $_value;
286 }
287
288 $sql = @sprintf('INSERT INTO `' . $this->mTable . '` ( %s ) VALUES ( %s )', implode(',', $fields), implode(',', $values));
289
290 return $sql;
291 }
292
298 public function _update(&$obj)
299 {
300 $set_lists= [];
301 $where = '';
302
303 $arr = $this->_makeVars4sql($obj);
304
305 foreach ($arr as $_name => $_value) {
306 if ($_name == $this->mPrimary) {
307 $where = "`{$_name}`={$_value}";
308 } else {
309 $set_lists[] = "`{$_name}`={$_value}";
310 }
311 }
312
313 $sql = @sprintf('UPDATE `' . $this->mTable . '` SET %s WHERE %s', implode(',', $set_lists), $where);
314
315 return $sql;
316 }
317
323 public function _makeVars4sql(&$obj)
324 {
325 $ret = [];
326 foreach ($obj->gets() as $key => $value) {
327 if (null === $value) {
328 $ret[$key] = 'NULL';
329 } else {
330 switch ($obj->mVars[$key]['data_type']) {
331 case XOBJ_DTYPE_STRING:
332 case XOBJ_DTYPE_TEXT:
333 $ret[$key] = $this->db->quoteString($value);
334 break;
335
336 default:
337 $ret[$key] = $value;
338 }
339 }
340 }
341
342 return $ret;
343 }
344
345 public function _makeCriteria4sql($criteria)
346 {
347 if (null == $this->_mDummyObj) {
348 $this->_mDummyObj =& $this->create();
349 }
350
351 return $this->_makeCriteriaElement4sql($criteria, $this->_mDummyObj);
352 }
353
359 public function _makeCriteriaElement4sql($criteria, &$obj)
360 {
361 if ($criteria instanceof \CriteriaElement) {
362 if ($criteria->hasChildElements()) {
363 $maxCount = $criteria->getCountChildElements();
364 $queryString = $this->_makeCriteria4sql($criteria->getChildElement(0));
365 for ($i = 1; $i < $maxCount; $i++) {
366 $queryString .= ' ' . $criteria->getCondition($i) . ' ' . $this->_makeCriteria4sql($criteria->getChildElement($i));
367 }
368 return '('.$queryString.')';
369 } else {
370 //
371 // Render
372 //
373 $name = $criteria->getName();
374 $value = $criteria->getValue();
375 if (null != $name && isset($obj->mVars[$name])) {
376 if (null === $value) {
377 $criteria->operator = '=' == $criteria->getOperator() ? 'IS' : 'IS NOT';
378 $value = 'NULL';
379 } elseif (in_array(strtoupper($criteria->operator), ['IN', 'NOT IN'])) {
380 $value = is_array($value) ? $value : explode(',', $value);
381 $typ = $obj->mVars[$name]['data_type'];
382 foreach ($value as $val) {
383 $tmp[] = $this->_escapeValue($val, $typ);
384 }
385 if (isset($tmp)) {
386 $value = '('.implode(',', $tmp).')';
387 } else {
388 $value = '("")';
389 }
390 } else {
391 $value = $this->_escapeValue($value, $obj->mVars[$name]['data_type']);
392 }
393 } else {
394 $value = $this->db->quoteString($value);
395 }
396
397 return null != $name ? $name . ' ' . $criteria->getOperator() . ' ' . $value : null;
398 }
399 }
400 }
401
402 public function _escapeValue($value, $type)
403 {
404 switch ($type) {
405 case XOBJ_DTYPE_BOOL:
406 return $value ? 1 : 0;
407 case XOBJ_DTYPE_INT:
408 return (int)$value;
409 case XOBJ_DTYPE_FLOAT:
410 return (float)$value;
411 default:
412 return $this->db->quoteString($value);
413 }
414 return null;
415 }
416
424 public function delete(&$obj, $force = false)
425 {
426 //
427 // Because Criteria can generate the most appropriate sentence, use
428 // criteria even if this approach is few slow.
429 //
430 $criteria =new Criteria($this->mPrimary, $obj->get($this->mPrimary));
431 $sql = 'DELETE FROM `' . $this->mTable . '` WHERE ' . $this->_makeCriteriaElement4sql($criteria, $obj);
432
433 $result = $force ? $this->db->queryF($sql) : $this->db->query($sql);
434 if (true == $result) {
435 $this->_callDelegate('delete', $obj);
436 }
437
438 return $result;
439 }
440
451 public function deleteAll($criteria, $force = false)
452 {
453 $objs =& $this->getObjects($criteria);
454
455 $flag = true;
456
457 foreach ($objs as $obj) {
458 $flag &= $this->delete($obj, $force);
459 }
460
461 return $flag;
462 }
463
471 public function getDirname()
472 {
473 return $this->mDirname;
474 }
475
483 public function getDataname()
484 {
485 return $this->mDataname;
486 }
487
496 public function _callDelegate(/*** string ***/ $type, /*** XoopsSimpleObject ***/ &$obj)
497 {
498 $arr = explode('_', $this->mTable);
499 if (isset($arr[2])) {
500 $tableName = $arr[2];
501 for ($i=3;$i<count($arr);$i++) {
502 $tableName .= '_'.$arr[$i];
503 }
504 XCube_DelegateUtils::call(sprintf('Module.%s.Event.%s.%s', $this->getDirname(), $type, $tableName), new XCube_Ref($obj));
505 } else {
506 XCube_DelegateUtils::call(sprintf('Module.%s.Event.%s', $this->getDirname(), $type), new XCube_Ref($obj));
507 }
508 }
509}
& getObjects($criteria=null, $limit=null, $start=null, $id_as_key=false)
Definition handler.php:83
_callDelegate( $type, &$obj)
Definition handler.php:496
getIdList($criteria=null, $limit=null, $start=null)
Definition handler.php:155
_makeCriteriaElement4sql($criteria, &$obj)
Definition handler.php:359
deleteAll($criteria, $force=false)
Definition handler.php:451