1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace XoopsModules\Xhelp; |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* You may not change or alter any portion of this comment or credits |
7
|
|
|
* of supporting developers from this source code or any supporting source code |
8
|
|
|
* which is considered copyrighted (c) material of the original comment or credit authors. |
9
|
|
|
* |
10
|
|
|
* This program is distributed in the hope that it will be useful, |
11
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
12
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @copyright {@link https://xoops.org/ XOOPS Project} |
17
|
|
|
* @license {@link https://www.gnu.org/licenses/gpl-2.0.html GNU GPL 2 or later} |
18
|
|
|
* @author Nazar Aziz <[email protected]> |
19
|
|
|
* @author XOOPS Development Team |
20
|
|
|
*/ |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* BaseObjectHandler class |
24
|
|
|
*/ |
25
|
|
|
class BaseObjectHandler extends \XoopsPersistableObjectHandler |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* Autoincrementing DB fieldname |
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
public $idfield = 'id'; |
32
|
|
|
public $helper; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Initialization of the handler |
36
|
|
|
* |
37
|
|
|
* @param \XoopsMySQLDatabase|null $db reference to a xoopsDB object |
38
|
|
|
*/ |
39
|
|
|
public function init(\XoopsMySQLDatabase $db = null): void |
40
|
|
|
{ |
41
|
|
|
$this->db = $db; |
42
|
|
|
$this->helper = Helper::getInstance(); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* create a new object |
47
|
|
|
* @return \XoopsModules\Xhelp\BaseObject|\XoopsObject |
48
|
|
|
*/ |
49
|
|
|
// public function create($isNew = true) |
50
|
|
|
// { |
51
|
|
|
//// $obj = new $this->classname(); |
52
|
|
|
// $obj = parent::create($isNew); |
53
|
|
|
//// $obj->helper = Helper::getInstance(); |
54
|
|
|
// |
55
|
|
|
// return $obj; |
56
|
|
|
// } |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* retrieve an object from the database, based on. use in child classes |
60
|
|
|
* @param int $id ID |
61
|
|
|
* @return mixed object if id exists, false if not |
62
|
|
|
*/ |
63
|
|
|
// public function get($id = null, $fields = null) //$id) |
64
|
|
|
// { |
65
|
|
|
// $ret = false; |
66
|
|
|
// $id = (int)$id; |
67
|
|
|
// if ($id > 0) { |
68
|
|
|
// $sql = $this->selectQuery(new \Criteria($this->idfield, (string)$id)); |
69
|
|
|
// if (!$result = $this->db->query($sql)) { |
70
|
|
|
// return $ret; |
71
|
|
|
// } |
72
|
|
|
// $numrows = $this->db->getRowsNum($result); |
73
|
|
|
// if (0 == $numrows) { |
74
|
|
|
//// $obj = new $this->classname(); |
75
|
|
|
// $obj = $this->create(true); |
76
|
|
|
// $obj->setVar('notif_id', $id); |
77
|
|
|
// return $obj; |
78
|
|
|
// } |
79
|
|
|
// if (1 == $numrows) { |
80
|
|
|
// $objData = $this->db->fetchArray($result); |
81
|
|
|
//// $obj = new $this->classname($objData); |
82
|
|
|
// $obj = $this->create($objData); |
83
|
|
|
// |
84
|
|
|
// return $obj; |
85
|
|
|
// } |
86
|
|
|
// } |
87
|
|
|
// |
88
|
|
|
// return $ret; |
89
|
|
|
// } |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* retrieve objects from the database |
93
|
|
|
* |
94
|
|
|
* @param \CriteriaElement|\CriteriaCompo|null $criteria {@link \CriteriaElement} conditions to be met |
95
|
|
|
* @param bool $id_as_key Should the department ID be used as array key |
96
|
|
|
* @return array array of objects |
97
|
|
|
*/ |
98
|
|
|
// public function &getObjects($criteria = null, bool $id_as_key = false) |
99
|
|
|
// public function &getObjects(\CriteriaElement $criteria = null, $id_as_key = false, $as_object = true): array |
100
|
|
|
// { |
101
|
|
|
// $ret = []; |
102
|
|
|
// $limit = $start = 0; |
103
|
|
|
// $sql = $this->selectQuery($criteria); |
104
|
|
|
// $id = $this->idfield; |
105
|
|
|
// |
106
|
|
|
// if (null !== $criteria) { |
107
|
|
|
// $limit = $criteria->getLimit(); |
108
|
|
|
// $start = $criteria->getStart(); |
109
|
|
|
// } |
110
|
|
|
// |
111
|
|
|
// $result = $this->db->query($sql, $limit, $start); |
112
|
|
|
// // If no records from db, return empty array |
113
|
|
|
// if (!$result || $this->db->getRowsNum($result) < 0) { |
114
|
|
|
// return $ret; |
115
|
|
|
// } |
116
|
|
|
// |
117
|
|
|
// // Add each returned record to the result array |
118
|
|
|
// while (false !== ($myrow = $this->db->fetchArray($result))) { |
119
|
|
|
// $obj = new $this->classname($myrow); |
120
|
|
|
//// $obj = $this->create($myrow); |
121
|
|
|
// if ($id_as_key) { |
122
|
|
|
// $ret[$obj->getVar($id)] = $obj; |
123
|
|
|
// } else { |
124
|
|
|
// $ret[] = $obj; |
125
|
|
|
// } |
126
|
|
|
// unset($obj); |
127
|
|
|
// } |
128
|
|
|
// |
129
|
|
|
// return $ret; |
130
|
|
|
// } |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @param \XoopsObject $object |
134
|
|
|
* @param bool $force |
135
|
|
|
* @return bool |
136
|
|
|
*/ |
137
|
|
|
// public function insert(\XoopsObject $object, $force = true): bool |
138
|
|
|
// { |
139
|
|
|
// // Make sure object is of correct type |
140
|
|
|
// if (0 != \strcasecmp($this->classname, \get_class($object))) { |
141
|
|
|
// $object->setErrors('Object is not a ' . $this->classname); |
142
|
|
|
// |
143
|
|
|
// return false; |
144
|
|
|
// } |
145
|
|
|
// |
146
|
|
|
// // Make sure object needs to be stored in DB |
147
|
|
|
// if (!$object->isDirty()) { |
148
|
|
|
// $object->setErrors('Object does not need to be saved'); |
149
|
|
|
// |
150
|
|
|
// return true; |
151
|
|
|
// } |
152
|
|
|
// |
153
|
|
|
// // Make sure object fields are filled with valid values |
154
|
|
|
// if (!$object->cleanVars()) { |
155
|
|
|
// $object->setErrors('Object cannot be sanitized for storage'); |
156
|
|
|
// |
157
|
|
|
// return false; |
158
|
|
|
// } |
159
|
|
|
// |
160
|
|
|
// // Create query for DB update |
161
|
|
|
// if ($object->isNew()) { |
162
|
|
|
// // Determine next auto-gen ID for table |
163
|
|
|
// $id = $this->db->genId($this->db->prefix($this->dbtable) . '_uid_seq'); |
164
|
|
|
// $sql = $this->insertQuery($object); |
165
|
|
|
// } else { |
166
|
|
|
// $sql = $this->updateQuery($object); |
167
|
|
|
// } |
168
|
|
|
// |
169
|
|
|
// // Update DB |
170
|
|
|
// if ($force) { |
171
|
|
|
// $result = $this->db->queryF($sql); |
172
|
|
|
// } else { |
173
|
|
|
// $result = $this->db->query($sql); |
174
|
|
|
// } |
175
|
|
|
// |
176
|
|
|
// if (!$result) { |
177
|
|
|
// return false; |
178
|
|
|
// } |
179
|
|
|
// |
180
|
|
|
// //Make sure auto-gen ID is stored correctly in object |
181
|
|
|
// if ($object->isNew()) { |
182
|
|
|
// $object->assignVar($this->idfield, $this->db->getInsertId()); |
183
|
|
|
// } |
184
|
|
|
// |
185
|
|
|
// return true; |
186
|
|
|
// } |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* Create a "select" SQL query |
190
|
|
|
* @param \CriteriaElement|null $criteria {@link \CriteriaElement} to match |
191
|
|
|
* @return string SQL query |
192
|
|
|
*/ |
193
|
|
|
public function selectQuery(\CriteriaElement $criteria = null): string |
194
|
|
|
{ |
195
|
|
|
$sql = \sprintf('SELECT * FROM `%s`', $this->db->prefix($this->dbtable)); |
196
|
|
|
if (($criteria instanceof \CriteriaCompo) || ($criteria instanceof \Criteria)) { |
197
|
|
|
$sql .= ' ' . $criteria->renderWhere(); |
198
|
|
|
if ('' != $criteria->getSort()) { |
199
|
|
|
$sql .= ' ORDER BY ' . $criteria->getSort() . ' |
200
|
|
|
' . $criteria->getOrder(); |
201
|
|
|
} |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
return $sql; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* count objects matching a criteria |
209
|
|
|
* |
210
|
|
|
* @param \CriteriaElement|\CriteriaCompo|null $criteria {@link \CriteriaElement} to match |
211
|
|
|
* @return int count of objects |
212
|
|
|
*/ |
213
|
|
|
// public function getCount($criteria = null): int |
214
|
|
|
// { |
215
|
|
|
// $sql = 'SELECT COUNT(*) FROM ' . $this->db->prefix($this->dbtable); |
216
|
|
|
// if (($criteria instanceof \CriteriaCompo) || ($criteria instanceof \Criteria)) { |
217
|
|
|
// $sql .= ' ' . $criteria->renderWhere(); |
218
|
|
|
// } |
219
|
|
|
// if (!$result = $this->db->query($sql)) { |
220
|
|
|
// return 0; |
221
|
|
|
// } |
222
|
|
|
// [$count] = $this->db->fetchRow($result); |
223
|
|
|
// |
224
|
|
|
// return (int)$count; |
225
|
|
|
// } |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* delete object based on id |
229
|
|
|
* |
230
|
|
|
* @param \XoopsObject $object {@link XoopsObject} reference to the object to delete |
231
|
|
|
* @param bool $force |
232
|
|
|
* @return bool FALSE if failed. |
233
|
|
|
*/ |
234
|
|
|
// public function delete(\XoopsObject $object, $force = false): bool |
235
|
|
|
// { |
236
|
|
|
// if (0 != \strcasecmp($this->classname, \get_class($object))) { |
237
|
|
|
// return false; |
238
|
|
|
// } |
239
|
|
|
// |
240
|
|
|
// $sql = $this->deleteQuery($object); |
241
|
|
|
// |
242
|
|
|
// if ($force) { |
243
|
|
|
// $result = $this->db->queryF($sql); |
244
|
|
|
// } else { |
245
|
|
|
// $result = $this->db->query($sql); |
246
|
|
|
// } |
247
|
|
|
// if (!$result) { |
248
|
|
|
// return false; |
249
|
|
|
// } |
250
|
|
|
// |
251
|
|
|
// return true; |
252
|
|
|
// } |
253
|
|
|
|
254
|
|
|
/** |
255
|
|
|
* delete department matching a set of conditions |
256
|
|
|
* |
257
|
|
|
* @param \CriteriaElement|\CriteriaCompo|null $criteria {@link \CriteriaElement} |
258
|
|
|
* @return bool FALSE if deletion failed |
259
|
|
|
*/ |
260
|
|
|
// public function deleteAll(\CriteriaElement $criteria = null, $force = true, $asObject = false): bool |
261
|
|
|
// { |
262
|
|
|
// $sql = 'DELETE FROM ' . $this->db->prefix($this->dbtable); |
263
|
|
|
// if (($criteria instanceof \CriteriaCompo) || ($criteria instanceof \Criteria)) { |
264
|
|
|
// $sql .= ' ' . $criteria->renderWhere(); |
265
|
|
|
// } |
266
|
|
|
// if (!$result = $this->db->query($sql)) { |
267
|
|
|
// return false; |
268
|
|
|
// } |
269
|
|
|
// |
270
|
|
|
// return true; |
271
|
|
|
// } |
272
|
|
|
|
273
|
|
|
/** |
274
|
|
|
* Assign a value to 1 field for tickets matching a set of conditions |
275
|
|
|
* |
276
|
|
|
* @param string $fieldname |
277
|
|
|
* @param string|int $fieldvalue |
278
|
|
|
* @param \Criteria|null $criteria {@link \CriteriaElement} |
279
|
|
|
* @return bool FALSE if update failed |
280
|
|
|
*/ |
281
|
|
|
// public function updateAll(string $fieldname, $fieldvalue, \CriteriaElement $criteria = null): bool |
282
|
|
|
// public function updateAll($fieldname, $fieldvalue, \CriteriaElement $criteria = null, $force = false): bool |
283
|
|
|
// { |
284
|
|
|
// $set_clause = \is_numeric($fieldvalue) ? $fieldname . ' = ' . $fieldvalue : $fieldname . ' = ' . $this->db->quoteString($fieldvalue); |
285
|
|
|
// $sql = 'UPDATE ' . $this->db->prefix($this->dbtable) . ' SET ' . $set_clause; |
286
|
|
|
// if (($criteria instanceof \CriteriaCompo) || ($criteria instanceof \Criteria)) { |
287
|
|
|
// $sql .= ' ' . $criteria->renderWhere(); |
288
|
|
|
// } |
289
|
|
|
// if (!$result = $this->db->query($sql)) { |
290
|
|
|
// return false; |
291
|
|
|
// } |
292
|
|
|
// |
293
|
|
|
// return true; |
294
|
|
|
// } |
295
|
|
|
|
296
|
|
|
/** |
297
|
|
|
* @param \XoopsObject $object |
298
|
|
|
* @return string |
299
|
|
|
*/ |
300
|
|
|
public function insertQuery(\XoopsObject $object): string |
|
|
|
|
301
|
|
|
{ |
302
|
|
|
return ''; |
303
|
|
|
} |
304
|
|
|
|
305
|
|
|
/** |
306
|
|
|
* @param \XoopsObject $object |
307
|
|
|
* @return string |
308
|
|
|
*/ |
309
|
|
|
public function updateQuery(\XoopsObject $object): string |
|
|
|
|
310
|
|
|
{ |
311
|
|
|
return ''; |
312
|
|
|
} |
313
|
|
|
|
314
|
|
|
/** |
315
|
|
|
* @param \XoopsObject $object |
316
|
|
|
* @return string |
317
|
|
|
*/ |
318
|
|
|
public function deleteQuery(\XoopsObject $object): string |
|
|
|
|
319
|
|
|
{ |
320
|
|
|
return ''; |
321
|
|
|
} |
322
|
|
|
|
323
|
|
|
/** |
324
|
|
|
* Singleton - prevent multiple instances of this class |
325
|
|
|
* |
326
|
|
|
* @param \XoopsDatabase|null $db |
327
|
|
|
* @return \XoopsModules\Xhelp\BaseObjectHandler |
328
|
|
|
*/ |
329
|
|
|
public function getInstance(\XoopsDatabase $db = null): BaseObjectHandler |
330
|
|
|
{ |
331
|
|
|
static $instance; |
332
|
|
|
if (null === $instance) { |
333
|
|
|
$classname = $this->classname . 'Handler'; |
334
|
|
|
$instance = new $classname($db); |
335
|
|
|
} |
336
|
|
|
|
337
|
|
|
return $instance; |
338
|
|
|
} |
339
|
|
|
} |
340
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.