1
|
|
|
<?php namespace XoopsModules\Extcal; |
2
|
|
|
/* |
3
|
|
|
* You may not change or alter any portion of this comment or credits |
4
|
|
|
* of supporting developers from this source code or any supporting source code |
5
|
|
|
* which is considered copyrighted (c) material of the original comment or credit authors. |
6
|
|
|
* |
7
|
|
|
* This program is distributed in the hope that it will be useful, |
8
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
9
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @copyright {@link https://xoops.org/ XOOPS Project} |
14
|
|
|
* @license {@link http://www.gnu.org/licenses/gpl-2.0.html GNU GPL 2 or later} |
15
|
|
|
* @package extcal |
16
|
|
|
* @since |
17
|
|
|
* @author XOOPS Development Team, |
18
|
|
|
*/ |
19
|
|
|
|
20
|
|
|
use XoopsModules\Extcal; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Persistable Object Handler class. |
24
|
|
|
* This class is responsible for providing data access mechanisms to the data source |
25
|
|
|
* of derived class objects. |
26
|
|
|
* |
27
|
|
|
* @author Jan Keller Pedersen <[email protected]> - IDG Danmark A/S <www.idg.dk> |
28
|
|
|
* @copyright copyright (c) 2000-2004 XOOPS.org |
29
|
|
|
*/ |
30
|
|
|
class ExtcalPersistableObjectHandler extends \XoopsPersistableObjectHandler //XoopsObjectHandler |
31
|
|
|
{ |
32
|
|
|
/**#@+ |
33
|
|
|
* Information about the class, the handler is managing |
34
|
|
|
* |
35
|
|
|
* @var string |
36
|
|
|
*/ |
37
|
|
|
// public $table; |
38
|
|
|
// public $keyName; |
39
|
|
|
// public $className; |
40
|
|
|
// public $identifierName; |
41
|
|
|
|
42
|
|
|
/**#@-*/ |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Constructor - called from child classes. |
46
|
|
|
* |
47
|
|
|
* @param XoopsDatabase $db {@link XoopsDatabase} object |
|
|
|
|
48
|
|
|
* @param string $tablename Name of database table |
49
|
|
|
* @param string $classname Name of Class, this handler is managing |
50
|
|
|
* @param string $keyname Name of the property, holding the key |
51
|
|
|
* @param bool $idenfierName |
52
|
|
|
*/ |
53
|
|
|
public function __construct(\XoopsDatabase $db, $tablename, $classname, $keyname, $idenfierName = false) |
54
|
|
|
{ |
55
|
|
|
parent::__construct($db); |
56
|
|
|
$this->table = $db->prefix($tablename); |
57
|
|
|
$this->keyName = $keyname; |
58
|
|
|
$this->className = $classname; |
59
|
|
|
if (false !== $idenfierName) { |
60
|
|
|
$this->identifierName = $idenfierName; |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Constructor. |
66
|
|
|
*/ |
67
|
|
|
// public function ExtcalPersistableObjectHandler($db, $tablename, $classname, $keyname, $idenfierName = false) |
|
|
|
|
68
|
|
|
// { |
69
|
|
|
// $this->__construct($db, $tablename, $classname, $keyname, $idenfierName); |
|
|
|
|
70
|
|
|
// } |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* create a new user. |
74
|
|
|
* |
75
|
|
|
* @param bool $isNew Flag the new objects as "new"? |
76
|
|
|
* |
77
|
|
|
* @return \XoopsObject |
78
|
|
|
*/ |
79
|
|
|
public function create($isNew = true) |
80
|
|
|
{ |
81
|
|
|
$obj = new $this->className(); |
82
|
|
|
if (true === $isNew) { |
83
|
|
|
$obj->setNew(); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
return $obj; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* retrieve an object. |
91
|
|
|
* |
92
|
|
|
* @param mixed $id ID of the object - or array of ids for joint keys. Joint keys MUST be given in the same order as in the constructor |
93
|
|
|
* @param null $fields |
94
|
|
|
* @param bool $as_object |
95
|
|
|
* |
96
|
|
|
* @return mixed reference to the object, FALSE if failed |
97
|
|
|
* |
98
|
|
|
* @internal param bool $asObject whether to return an object or an array |
99
|
|
|
*/ |
100
|
|
|
public function get($id = null, $fields = null, $as_object = true) //get($id, $as_object = true) |
|
|
|
|
101
|
|
|
{ |
102
|
|
|
if (is_array($this->keyName)) { |
103
|
|
|
$criteria = new \CriteriaCompo(); |
104
|
|
|
for ($i = 0, $iMax = count($this->keyName); $i < $iMax; ++$i) { |
105
|
|
|
$criteria->add( new \Criteria($this->keyName[$i], (int)$id[$i])); |
106
|
|
|
} |
107
|
|
|
} else { |
108
|
|
|
$criteria = new \Criteria($this->keyName, (int)$id); |
109
|
|
|
} |
110
|
|
|
$criteria->setLimit(1); |
111
|
|
|
$objectArray = $this->getObjects($criteria, false, true); |
112
|
|
|
if (1 != count($objectArray)) { |
113
|
|
|
return $this->create(); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
return $objectArray[0]; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* retrieve objects from the database. |
121
|
|
|
* |
122
|
|
|
* @param \CriteriaElement $criteria {@link CriteriaElement} conditions to be met |
|
|
|
|
123
|
|
|
* @param bool $idAsKey use the ID as key for the array? |
124
|
|
|
* @param bool $asObject return an array of objects? |
125
|
|
|
* |
126
|
|
|
* @return array |
127
|
|
|
*/ |
128
|
|
|
public function &getObjects(\CriteriaElement $criteria = null, $idAsKey = false, $asObject = true) |
129
|
|
|
{ |
130
|
|
|
$ret = []; |
131
|
|
|
$limit = $start = 0; |
132
|
|
|
$sql = 'SELECT * FROM ' . $this->table; |
133
|
|
View Code Duplication |
if (isset($criteria) && is_subclass_of($criteria, 'CriteriaElement')) { |
|
|
|
|
134
|
|
|
$sql .= ' ' . $criteria->renderWhere(); |
135
|
|
|
if ('' != $criteria->getSort()) { |
136
|
|
|
$sql .= ' ORDER BY ' . $criteria->getSort() . ' ' . $criteria->getOrder(); |
137
|
|
|
} |
138
|
|
|
$limit = $criteria->getLimit(); |
139
|
|
|
$start = $criteria->getStart(); |
140
|
|
|
} |
141
|
|
|
$result = $this->db->query($sql, $limit, $start); |
142
|
|
|
if (!$result) { |
143
|
|
|
return $ret; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
$ret = $this->convertResultSet($result, $idAsKey, $asObject); |
147
|
|
|
|
148
|
|
|
return $ret; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Convert a database resultset to a returnable array. |
153
|
|
|
* |
154
|
|
|
* @param XoopsObject $result database resultset |
155
|
|
|
* @param bool $idAsKey - should NOT be used with joint keys |
156
|
|
|
* @param bool $asObject |
157
|
|
|
* |
158
|
|
|
* @return array |
159
|
|
|
*/ |
160
|
|
|
public function convertResultSet($result, $idAsKey = false, $asObject = true) |
161
|
|
|
{ |
162
|
|
|
$ret = []; |
163
|
|
|
while ($myrow = $this->db->fetchArray($result)) { |
164
|
|
|
$obj = $this->create(false); |
165
|
|
|
$obj->assignVars($myrow); |
166
|
|
|
if (!$idAsKey) { |
167
|
|
View Code Duplication |
if ($asObject) { |
|
|
|
|
168
|
|
|
$ret[] = $obj; |
169
|
|
|
} else { |
170
|
|
|
$row = []; |
171
|
|
|
$vars =& $obj->getVars(); |
172
|
|
|
foreach (array_keys($vars) as $i) { |
173
|
|
|
$row[$i] = $obj->getVar($i); |
174
|
|
|
} |
175
|
|
|
$ret[] = $row; |
176
|
|
|
} |
177
|
|
|
} else { |
178
|
|
|
if ($asObject) { |
179
|
|
|
$ret[$myrow[$this->keyName]] = $obj; |
180
|
|
View Code Duplication |
} else { |
|
|
|
|
181
|
|
|
$row = []; |
182
|
|
|
$vars =& $obj->getVars(); |
183
|
|
|
foreach (array_keys($vars) as $i) { |
184
|
|
|
$row[$i] = $obj->getVar($i); |
185
|
|
|
} |
186
|
|
|
$ret[$myrow[$this->keyName]] = $row; |
187
|
|
|
} |
188
|
|
|
} |
189
|
|
|
unset($obj); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
return $ret; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* Retrieve a list of objects as arrays - DON'T USE WITH JOINT KEYS. |
197
|
|
|
* |
198
|
|
|
* @param \CriteriaElement $criteria {@link CriteriaElement} conditions to be met |
|
|
|
|
199
|
|
|
* @param int $limit Max number of objects to fetch |
200
|
|
|
* @param int $start Which record to start at |
201
|
|
|
* |
202
|
|
|
* @return array |
203
|
|
|
*/ |
204
|
|
|
public function getList(\CriteriaElement $criteria = null, $limit = 0, $start = 0) |
205
|
|
|
{ |
206
|
|
|
$ret = []; |
207
|
|
|
if (null === $criteria) { |
208
|
|
|
$criteria = new \CriteriaCompo(); |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
if ('' == $criteria->getSort()) { |
212
|
|
|
$criteria->setSort($this->identifierName); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
$sql = 'SELECT ' . $this->keyName; |
216
|
|
|
if (!empty($this->identifierName)) { |
217
|
|
|
$sql .= ', ' . $this->identifierName; |
218
|
|
|
} |
219
|
|
|
$sql .= ' FROM ' . $this->table; |
220
|
|
View Code Duplication |
if (isset($criteria) && is_subclass_of($criteria, 'CriteriaElement')) { |
|
|
|
|
221
|
|
|
$sql .= ' ' . $criteria->renderWhere(); |
222
|
|
|
if ('' != $criteria->getSort()) { |
223
|
|
|
$sql .= ' ORDER BY ' . $criteria->getSort() . ' ' . $criteria->getOrder(); |
224
|
|
|
} |
225
|
|
|
$limit = $criteria->getLimit(); |
226
|
|
|
$start = $criteria->getStart(); |
227
|
|
|
} |
228
|
|
|
$result = $this->db->query($sql, $limit, $start); |
229
|
|
|
if (!$result) { |
230
|
|
|
return $ret; |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
$myts = \MyTextSanitizer::getInstance(); |
234
|
|
|
while ($myrow = $this->db->fetchArray($result)) { |
235
|
|
|
//identifiers should be textboxes, so sanitize them like that |
236
|
|
|
$ret[$myrow[$this->keyName]] = empty($this->identifierName) ? 1 : $myts->htmlSpecialChars($myrow[$this->identifierName]); |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
return $ret; |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* count objects matching a condition. |
244
|
|
|
* |
245
|
|
|
* @param \CriteriaElement $criteria {@link CriteriaElement} to match |
|
|
|
|
246
|
|
|
* |
247
|
|
|
* @return int|array count of objects |
248
|
|
|
*/ |
249
|
|
View Code Duplication |
public function getCount(\CriteriaElement $criteria = null) |
|
|
|
|
250
|
|
|
{ |
251
|
|
|
$field = ''; |
252
|
|
|
$groupby = false; |
253
|
|
|
if (isset($criteria) && is_subclass_of($criteria, 'CriteriaElement')) { |
|
|
|
|
254
|
|
|
if ('' != $criteria->groupby) { |
255
|
|
|
$groupby = true; |
256
|
|
|
$field = $criteria->groupby . ', '; //Not entirely secure unless you KNOW that no criteria's groupby clause is going to be mis-used |
257
|
|
|
} |
258
|
|
|
} |
259
|
|
|
$sql = 'SELECT ' . $field . 'COUNT(*) FROM ' . $this->table; |
260
|
|
|
if (isset($criteria) && is_subclass_of($criteria, 'CriteriaElement')) { |
|
|
|
|
261
|
|
|
$sql .= ' ' . $criteria->renderWhere(); |
262
|
|
|
if ('' != $criteria->groupby) { |
263
|
|
|
$sql .= $criteria->getGroupby(); |
264
|
|
|
} |
265
|
|
|
} |
266
|
|
|
$result = $this->db->query($sql); |
267
|
|
|
if (!$result) { |
268
|
|
|
return 0; |
269
|
|
|
} |
270
|
|
|
if (false === $groupby) { |
271
|
|
|
list($count) = $this->db->fetchRow($result); |
272
|
|
|
|
273
|
|
|
return $count; |
274
|
|
|
} else { |
275
|
|
|
$ret = []; |
276
|
|
|
while (list($id, $count) = $this->db->fetchRow($result)) { |
277
|
|
|
$ret[$id] = $count; |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
return $ret; |
281
|
|
|
} |
282
|
|
|
} |
283
|
|
|
|
284
|
|
|
/** |
285
|
|
|
* delete an object from the database by id. |
286
|
|
|
* |
287
|
|
|
* @param mixed $id id of the object to delete |
288
|
|
|
* @param bool $force |
289
|
|
|
* |
290
|
|
|
* @return bool FALSE if failed. |
291
|
|
|
*/ |
292
|
|
|
public function deleteById($id, $force = false) //delete(\XoopsObject $object, $force = false) |
|
|
|
|
293
|
|
|
{ |
294
|
|
|
if (is_array($this->keyName)) { |
295
|
|
|
$clause = []; |
296
|
|
|
for ($i = 0, $iMax = count($this->keyName); $i < $iMax; ++$i) { |
297
|
|
|
$clause[] = $this->keyName[$i] . ' = ' . $id[$i]; |
298
|
|
|
} |
299
|
|
|
$whereclause = implode(' AND ', $clause); |
300
|
|
|
} else { |
301
|
|
|
$whereclause = $this->keyName . ' = ' . $id; |
302
|
|
|
} |
303
|
|
|
$sql = 'DELETE FROM ' . $this->table . ' WHERE ' . $whereclause; |
304
|
|
View Code Duplication |
if (false !== $force) { |
|
|
|
|
305
|
|
|
$result = $this->db->queryF($sql); |
306
|
|
|
} else { |
307
|
|
|
$result = $this->db->query($sql); |
308
|
|
|
} |
309
|
|
|
if (!$result) { |
310
|
|
|
return false; |
311
|
|
|
} |
312
|
|
|
|
313
|
|
|
return true; |
314
|
|
|
} |
315
|
|
|
|
316
|
|
|
/** |
317
|
|
|
* insert a new object in the database. |
318
|
|
|
* |
319
|
|
|
* @param XoopsObject $obj reference to the object |
|
|
|
|
320
|
|
|
* @param bool $force whether to force the query execution despite security settings |
321
|
|
|
* @param bool $checkObject check if the object is dirty and clean the attributes |
322
|
|
|
* |
323
|
|
|
* @return bool FALSE if failed, TRUE if already present and unchanged or successful |
324
|
|
|
*/ |
325
|
|
|
public function insert(\XoopsObject $obj, $force = false, $checkObject = true) |
326
|
|
|
{ |
327
|
|
|
if (false !== $checkObject) { |
328
|
|
|
if (!is_object($obj)) { |
329
|
|
|
// var_dump($obj); |
|
|
|
|
330
|
|
|
return false; |
331
|
|
|
} |
332
|
|
|
|
333
|
|
|
if (!($obj instanceof $this->className && class_exists($this->className))) { |
334
|
|
|
$obj->setErrors(get_class($obj) . ' Differs from ' . $this->className); |
335
|
|
|
|
336
|
|
|
return false; |
337
|
|
|
} |
338
|
|
|
} |
339
|
|
|
if (!$obj->cleanVars()) { |
340
|
|
|
return false; |
341
|
|
|
} |
342
|
|
|
|
343
|
|
|
foreach ($obj->cleanVars as $k => $v) { |
344
|
|
|
if (XOBJ_DTYPE_INT == $obj->vars[$k]['data_type']) { |
345
|
|
|
$cleanvars[$k] = (int)$v; |
|
|
|
|
346
|
|
|
} elseif (is_array($v)) { |
347
|
|
|
$cleanvars[$k] = $this->db->quoteString(implode(',', $v)); |
|
|
|
|
348
|
|
|
} else { |
349
|
|
|
$cleanvars[$k] = $this->db->quoteString($v); |
350
|
|
|
} |
351
|
|
|
} |
352
|
|
|
if ($obj->isNew()) { |
353
|
|
|
if (!is_array($this->keyName)) { |
354
|
|
|
if ($cleanvars[$this->keyName] < 1) { |
355
|
|
|
$cleanvars[$this->keyName] = $this->db->genId($this->table . '_' . $this->keyName . '_seq'); |
356
|
|
|
} |
357
|
|
|
} |
358
|
|
|
$sql = 'INSERT INTO ' . $this->table . ' (' . implode(',', array_keys($cleanvars)) . ') VALUES (' . implode(',', array_values($cleanvars)) . ')'; |
359
|
|
|
} else { |
360
|
|
|
$sql = 'UPDATE ' . $this->table . ' SET'; |
361
|
|
|
foreach ($cleanvars as $key => $value) { |
362
|
|
|
if ((!is_array($this->keyName) && $key == $this->keyName) |
363
|
|
|
|| (is_array($this->keyName) |
364
|
|
|
&& in_array($key, $this->keyName))) { |
365
|
|
|
continue; |
366
|
|
|
} |
367
|
|
|
if (isset($notfirst)) { |
368
|
|
|
$sql .= ','; |
369
|
|
|
} |
370
|
|
|
$sql .= ' ' . $key . ' = ' . $value; |
371
|
|
|
$notfirst = true; |
372
|
|
|
} |
373
|
|
|
if (is_array($this->keyName)) { |
374
|
|
|
$whereclause = ''; |
375
|
|
|
for ($i = 0, $iMax = count($this->keyName); $i < $iMax; ++$i) { |
376
|
|
|
if ($i > 0) { |
377
|
|
|
$whereclause .= ' AND '; |
378
|
|
|
} |
379
|
|
|
$whereclause .= $this->keyName[$i] . ' = ' . $obj->getVar($this->keyName[$i]); |
380
|
|
|
} |
381
|
|
|
} else { |
382
|
|
|
$whereclause = $this->keyName . ' = ' . $obj->getVar($this->keyName); |
383
|
|
|
} |
384
|
|
|
$sql .= ' WHERE ' . $whereclause; |
385
|
|
|
} |
386
|
|
View Code Duplication |
if (false !== $force) { |
|
|
|
|
387
|
|
|
$result = $this->db->queryF($sql); |
388
|
|
|
} else { |
389
|
|
|
$result = $this->db->query($sql); |
390
|
|
|
} |
391
|
|
|
if (!$result) { |
392
|
|
|
return false; |
393
|
|
|
} |
394
|
|
|
if (!is_array($this->keyName) && $obj->isNew()) { |
395
|
|
|
$obj->assignVar($this->keyName, $this->db->getInsertId()); |
396
|
|
|
} |
397
|
|
|
|
398
|
|
|
return true; |
399
|
|
|
} |
400
|
|
|
|
401
|
|
|
/** |
402
|
|
|
* Change a value for objects with a certain criteria. |
403
|
|
|
* |
404
|
|
|
* @param string $fieldname Name of the field |
405
|
|
|
* @param string|array $fieldvalue Value to write |
406
|
|
|
* @param CriteriaElement $criteria {@link CriteriaElement} |
|
|
|
|
407
|
|
|
* @param bool $force |
408
|
|
|
* |
409
|
|
|
* @return bool |
410
|
|
|
*/ |
411
|
|
|
public function updateAll($fieldname, $fieldvalue, \CriteriaElement $criteria = null, $force = false) |
412
|
|
|
{ |
413
|
|
|
$setClause = $fieldname . ' = '; |
414
|
|
|
if (is_numeric($fieldvalue)) { |
415
|
|
|
$setClause .= $fieldvalue; |
416
|
|
|
} elseif (is_array($fieldvalue)) { |
417
|
|
|
$setClause .= $this->db->quoteString(implode(',', $fieldvalue)); |
418
|
|
|
} else { |
419
|
|
|
$setClause .= $this->db->quoteString($fieldvalue); |
420
|
|
|
} |
421
|
|
|
$sql = 'UPDATE ' . $this->table . ' SET ' . $setClause; |
422
|
|
|
if (isset($criteria) && is_subclass_of($criteria, 'CriteriaElement')) { |
|
|
|
|
423
|
|
|
$sql .= ' ' . $criteria->renderWhere(); |
424
|
|
|
} |
425
|
|
View Code Duplication |
if (false !== $force) { |
|
|
|
|
426
|
|
|
$result = $this->db->queryF($sql); |
427
|
|
|
} else { |
428
|
|
|
$result = $this->db->query($sql); |
429
|
|
|
} |
430
|
|
|
if (!$result) { |
431
|
|
|
return false; |
432
|
|
|
} |
433
|
|
|
|
434
|
|
|
return true; |
435
|
|
|
} |
436
|
|
|
|
437
|
|
|
/** |
438
|
|
|
* @param $fieldname |
439
|
|
|
* @param $fieldvalue |
440
|
|
|
* @param null $criteria |
441
|
|
|
* @param bool $force |
442
|
|
|
* |
443
|
|
|
* @return bool |
444
|
|
|
*/ |
445
|
|
|
public function updateFieldValue($fieldname, $fieldvalue, $criteria = null, $force = true) |
446
|
|
|
{ |
447
|
|
|
$sql = 'UPDATE ' . $this->table . ' SET ' . $fieldname . ' = ' . $fieldvalue; |
448
|
|
|
if (isset($criteria) && is_subclass_of($criteria, 'CriteriaElement')) { |
|
|
|
|
449
|
|
|
$sql .= ' ' . $criteria->renderWhere(); |
450
|
|
|
} |
451
|
|
View Code Duplication |
if (false !== $force) { |
|
|
|
|
452
|
|
|
$result = $this->db->queryF($sql); |
453
|
|
|
} else { |
454
|
|
|
$result = $this->db->query($sql); |
455
|
|
|
} |
456
|
|
|
if (!$result) { |
457
|
|
|
return false; |
458
|
|
|
} |
459
|
|
|
|
460
|
|
|
return true; |
461
|
|
|
} |
462
|
|
|
|
463
|
|
|
/** |
464
|
|
|
* delete all objects meeting the conditions. |
465
|
|
|
* |
466
|
|
|
* @param CriteriaElement $criteria {@link CriteriaElement} |
|
|
|
|
467
|
|
|
* with conditions to meet |
468
|
|
|
* @param bool $force |
469
|
|
|
* @param bool $asObject |
470
|
|
|
* |
471
|
|
|
* @return bool |
472
|
|
|
*/ |
473
|
|
|
public function deleteAll(\CriteriaElement $criteria = null, $force = true, $asObject = false) |
474
|
|
|
{ |
475
|
|
|
if (isset($criteria) && is_subclass_of($criteria, 'CriteriaElement')) { |
|
|
|
|
476
|
|
|
$sql = 'DELETE FROM ' . $this->table; |
477
|
|
|
$sql .= ' ' . $criteria->renderWhere(); |
478
|
|
|
if (!$this->db->query($sql)) { |
479
|
|
|
return false; |
480
|
|
|
} |
481
|
|
|
$rows = $this->db->getAffectedRows(); |
482
|
|
|
|
483
|
|
|
return $rows > 0 ? $rows : true; |
484
|
|
|
} |
485
|
|
|
|
486
|
|
|
return false; |
487
|
|
|
} |
488
|
|
|
|
489
|
|
|
/** |
490
|
|
|
* @param $data |
491
|
|
|
* |
492
|
|
|
* @return array |
|
|
|
|
493
|
|
|
*/ |
494
|
|
|
public function _toObject($data) |
495
|
|
|
{ |
496
|
|
|
if (is_array($data)) { |
497
|
|
|
$ret = []; |
498
|
|
|
foreach ($data as $v) { |
499
|
|
|
$object = new $this->className(); |
500
|
|
|
$object->assignVars($v); |
501
|
|
|
$ret[] = $object; |
502
|
|
|
} |
503
|
|
|
|
504
|
|
|
return $ret; |
505
|
|
|
} else { |
506
|
|
|
$object = new $this->className(); |
507
|
|
|
$object->assignVars($v); |
|
|
|
|
508
|
|
|
|
509
|
|
|
return $object; |
510
|
|
|
} |
511
|
|
|
} |
512
|
|
|
|
513
|
|
|
/** |
514
|
|
|
* @param $objects |
515
|
|
|
* @param array $externalKeys |
516
|
|
|
* @param string $format |
517
|
|
|
* |
518
|
|
|
* @return array |
519
|
|
|
*/ |
520
|
|
|
public function objectToArray($objects, $externalKeys = [], $format = 's') |
521
|
|
|
{ |
522
|
|
|
static $cache; |
523
|
|
|
if (!is_array($externalKeys)) { |
524
|
|
|
$externalKeys = [$externalKeys]; |
525
|
|
|
} //JJD |
526
|
|
|
|
527
|
|
|
$ret = []; |
528
|
|
|
if (is_array($objects)) { |
529
|
|
|
$i = 0; |
530
|
|
|
foreach ($objects as $object) { |
531
|
|
|
$vars = $object->getVars(); |
532
|
|
|
foreach ($vars as $k => $v) { |
533
|
|
|
$ret[$i][$k] = $object->getVar($k, $format); |
534
|
|
|
} |
535
|
|
|
foreach ($externalKeys as $key) { |
536
|
|
|
// Replace external key by corresponding object |
537
|
|
|
$externalKey = $object->getExternalKey($key); |
538
|
|
|
if (0 != $ret[$i][$key]) { |
539
|
|
|
// Retrieving data if isn't cached |
540
|
|
|
if (!isset($cached[$externalKey['keyName']][$ret[$i][$key]])) { |
541
|
|
View Code Duplication |
if ($externalKey['core']) { |
|
|
|
|
542
|
|
|
$handler = xoops_getHandler($externalKey['className']); |
543
|
|
|
} else { |
544
|
|
|
$handler = Extcal\Helper::getInstance()->getHandler($externalKey['className']); |
545
|
|
|
} |
546
|
|
|
$getMethod = $externalKey['getMethodeName']; |
547
|
|
|
$cached[$externalKey['keyName']][$ret[$i][$key]] = $this->objectToArrayWithoutExternalKey($handler->$getMethod($ret[$i][$key], true), $format); |
|
|
|
|
548
|
|
|
} |
549
|
|
|
$ret[$i][$externalKey['keyName']] = $cached[$externalKey['keyName']][$ret[$i][$key]]; |
|
|
|
|
550
|
|
|
} |
551
|
|
|
unset($ret[$i][$key]); |
552
|
|
|
} |
553
|
|
|
++$i; |
554
|
|
|
} |
555
|
|
|
} else { |
556
|
|
|
$vars = $objects->getVars(); |
557
|
|
|
foreach ($vars as $k => $v) { |
558
|
|
|
$ret[$k] = $objects->getVar($k, $format); |
559
|
|
|
} |
560
|
|
|
foreach ($externalKeys as $key) { |
561
|
|
|
// Replace external key by corresponding object |
562
|
|
|
$externalKey = $objects->getExternalKey($key); |
563
|
|
|
if (0 != $ret[$key]) { |
564
|
|
|
// Retriving data if isn't cached |
565
|
|
|
if (!isset($cached[$externalKey['keyName']][$ret[$key]])) { |
566
|
|
View Code Duplication |
if ($externalKey['core']) { |
|
|
|
|
567
|
|
|
$handler = xoops_getHandler($externalKey['className']); |
568
|
|
|
} else { |
569
|
|
|
$handler = Extcal\Helper::getInstance()->getHandler($externalKey['className']); |
570
|
|
|
} |
571
|
|
|
$getMethod = $externalKey['getMethodeName']; |
572
|
|
|
$cached[$externalKey['keyName']][$ret[$key]] = $this->objectToArrayWithoutExternalKey($handler->$getMethod($ret[$key], true), $format); |
573
|
|
|
} |
574
|
|
|
$ret[$externalKey['keyName']] = $cached[$externalKey['keyName']][$ret[$key]]; |
575
|
|
|
} |
576
|
|
|
unset($ret[$key]); |
577
|
|
|
} |
578
|
|
|
} |
579
|
|
|
|
580
|
|
|
return $ret; |
581
|
|
|
} |
582
|
|
|
|
583
|
|
|
/** |
584
|
|
|
* @param $object |
585
|
|
|
* @param string $format |
586
|
|
|
* |
587
|
|
|
* @return array |
588
|
|
|
*/ |
589
|
|
|
public function objectToArrayWithoutExternalKey($object, $format = 's') |
590
|
|
|
{ |
591
|
|
|
$ret = []; |
592
|
|
|
if (null !== $object) { |
593
|
|
|
$vars = $object->getVars(); |
594
|
|
|
foreach ($vars as $k => $v) { |
595
|
|
|
$ret[$k] = $object->getVar($k, $format); |
596
|
|
|
} |
597
|
|
|
} |
598
|
|
|
|
599
|
|
|
return $ret; |
600
|
|
|
} |
601
|
|
|
|
602
|
|
|
/** |
603
|
|
|
* @param $fieldname |
604
|
|
|
* @param $criteria |
605
|
|
|
* @param string $op |
606
|
|
|
* |
607
|
|
|
* @return bool |
608
|
|
|
*/ |
609
|
|
|
public function updateCounter($fieldname, $criteria, $op = '+') |
610
|
|
|
{ |
611
|
|
|
$sql = 'UPDATE ' . $this->table . ' SET ' . $fieldname . ' = ' . $fieldname . $op . '1'; |
612
|
|
|
$sql .= ' ' . $criteria->renderWhere(); |
613
|
|
|
$result = $this->db->queryF($sql); |
614
|
|
|
if (!$result) { |
615
|
|
|
return false; |
616
|
|
|
} |
617
|
|
|
|
618
|
|
|
return true; |
619
|
|
|
} |
620
|
|
|
|
621
|
|
|
/** |
622
|
|
|
* @param null|CriteriaElement $criteria |
|
|
|
|
623
|
|
|
* @param string $sum |
624
|
|
|
* |
625
|
|
|
* @return array|string |
626
|
|
|
*/ |
627
|
|
View Code Duplication |
public function getSum(\CriteriaElement $criteria = null, $sum = '*') |
|
|
|
|
628
|
|
|
{ |
629
|
|
|
$field = ''; |
630
|
|
|
$groupby = false; |
631
|
|
|
if (isset($criteria) && is_subclass_of($criteria, 'CriteriaElement')) { |
|
|
|
|
632
|
|
|
if ('' != $criteria->groupby) { |
633
|
|
|
$groupby = true; |
634
|
|
|
$field = $criteria->groupby . ', '; //Not entirely secure unless you KNOW that no criteria's groupby clause is going to be mis-used |
635
|
|
|
} |
636
|
|
|
} |
637
|
|
|
$sql = 'SELECT ' . $field . "SUM($sum) FROM " . $this->table; |
638
|
|
|
if (isset($criteria) && is_subclass_of($criteria, 'CriteriaElement')) { |
|
|
|
|
639
|
|
|
$sql .= ' ' . $criteria->renderWhere(); |
640
|
|
|
if ('' != $criteria->groupby) { |
641
|
|
|
$sql .= $criteria->getGroupby(); |
642
|
|
|
} |
643
|
|
|
} |
644
|
|
|
$result = $this->db->query($sql); |
645
|
|
|
if (!$result) { |
646
|
|
|
return 0; |
|
|
|
|
647
|
|
|
} |
648
|
|
|
if (false === $groupby) { |
649
|
|
|
list($sum) = $this->db->fetchRow($result); |
650
|
|
|
|
651
|
|
|
return $sum; |
652
|
|
|
} else { |
653
|
|
|
$ret = []; |
654
|
|
|
while (list($id, $sum) = $this->db->fetchRow($result)) { |
655
|
|
|
$ret[$id] = $sum; |
656
|
|
|
} |
657
|
|
|
|
658
|
|
|
return $ret; |
659
|
|
|
} |
660
|
|
|
} |
661
|
|
|
|
662
|
|
|
/** |
663
|
|
|
* @param null|CriteriaElement $criteria |
|
|
|
|
664
|
|
|
* @param string $max |
665
|
|
|
* |
666
|
|
|
* @return array|string |
667
|
|
|
*/ |
668
|
|
View Code Duplication |
public function getMax(\CriteriaElement $criteria = null, $max = '*') |
|
|
|
|
669
|
|
|
{ |
670
|
|
|
$field = ''; |
671
|
|
|
$groupby = false; |
672
|
|
|
if (isset($criteria) && is_subclass_of($criteria, 'CriteriaElement')) { |
|
|
|
|
673
|
|
|
if ('' != $criteria->groupby) { |
674
|
|
|
$groupby = true; |
675
|
|
|
$field = $criteria->groupby . ', '; //Not entirely secure unless you KNOW that no criteria's groupby clause is going to be mis-used |
676
|
|
|
} |
677
|
|
|
} |
678
|
|
|
$sql = 'SELECT ' . $field . "MAX($max) FROM " . $this->table; |
679
|
|
|
if (isset($criteria) && is_subclass_of($criteria, 'CriteriaElement')) { |
|
|
|
|
680
|
|
|
$sql .= ' ' . $criteria->renderWhere(); |
681
|
|
|
if ('' != $criteria->groupby) { |
682
|
|
|
$sql .= $criteria->getGroupby(); |
683
|
|
|
} |
684
|
|
|
} |
685
|
|
|
$result = $this->db->query($sql); |
686
|
|
|
if (!$result) { |
687
|
|
|
return 0; |
|
|
|
|
688
|
|
|
} |
689
|
|
|
if (false === $groupby) { |
690
|
|
|
list($max) = $this->db->fetchRow($result); |
691
|
|
|
|
692
|
|
|
return $max; |
693
|
|
|
} else { |
694
|
|
|
$ret = []; |
695
|
|
|
while (list($id, $max) = $this->db->fetchRow($result)) { |
696
|
|
|
$ret[$id] = $max; |
697
|
|
|
} |
698
|
|
|
|
699
|
|
|
return $ret; |
700
|
|
|
} |
701
|
|
|
} |
702
|
|
|
|
703
|
|
|
/** |
704
|
|
|
* @param null $criteria |
705
|
|
|
* @param string $avg |
706
|
|
|
* |
707
|
|
|
* @return int |
708
|
|
|
*/ |
709
|
|
|
public function getAvg($criteria = null, $avg = '*') |
710
|
|
|
{ |
711
|
|
|
$field = ''; |
712
|
|
|
|
713
|
|
|
$sql = 'SELECT ' . $field . "AVG($avg) FROM " . $this->table; |
714
|
|
|
if (isset($criteria) && is_subclass_of($criteria, 'CriteriaElement')) { |
|
|
|
|
715
|
|
|
$sql .= ' ' . $criteria->renderWhere(); |
716
|
|
|
} |
717
|
|
|
$result = $this->db->query($sql); |
718
|
|
|
if (!$result) { |
719
|
|
|
return 0; |
720
|
|
|
} |
721
|
|
|
list($sum) = $this->db->fetchRow($result); |
722
|
|
|
|
723
|
|
|
return $sum; |
724
|
|
|
} |
725
|
|
|
|
726
|
|
|
/** |
727
|
|
|
* @return mixed |
728
|
|
|
*/ |
729
|
|
|
public function getInsertId() |
730
|
|
|
{ |
731
|
|
|
return $this->db->getInsertId(); |
732
|
|
|
} |
733
|
|
|
} |
734
|
|
|
|
This check looks for
@param
annotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.