1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace XoopsModules\Publisher; |
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
|
|
|
* Publisher class |
17
|
|
|
* |
18
|
|
|
* @copyright XOOPS Project (https://xoops.org) |
19
|
|
|
* @license https://www.fsf.org/copyleft/gpl.html GNU public license |
20
|
|
|
* @since 1.0 |
21
|
|
|
* @author trabis <[email protected]> |
22
|
|
|
* @author The SmartFactory <www.smartfactory.ca> |
23
|
|
|
*/ |
24
|
|
|
require_once \dirname(__DIR__) . '/include/common.php'; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* BaseObjectHandler class |
28
|
|
|
* |
29
|
|
|
* @copyright XOOPS Project (https://xoops.org) |
30
|
|
|
* @license https://www.fsf.org/copyleft/gpl.html GNU public license |
31
|
|
|
* @since 1.0 |
32
|
|
|
* @author trabis <[email protected]> |
33
|
|
|
* @author Nazar Aziz <[email protected]> |
34
|
|
|
*/ |
35
|
|
|
class BaseObjectHandler extends \XoopsPersistableObjectHandler |
36
|
|
|
{ |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Autoincrementing DB fieldname |
40
|
|
|
* |
41
|
|
|
* @var string |
42
|
|
|
*/ |
43
|
|
|
protected $idfield = 'id'; |
44
|
|
|
public $helper = null; |
45
|
|
|
public $publisherIsAdmin = null; |
46
|
|
|
|
47
|
|
|
public function init(?\XoopsDatabase $db = null): void |
48
|
|
|
{ |
49
|
|
|
$this->db = $db; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* DB Table Name |
54
|
|
|
* |
55
|
|
|
* @var string |
56
|
|
|
*/ |
57
|
|
|
protected $dbtable = 'publisher_mimetypes'; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* create a new object |
61
|
|
|
* |
62
|
|
|
* @param bool $isNew |
63
|
|
|
* @return \XoopsObject |
64
|
|
|
*/ |
65
|
|
|
public function create($isNew = true) |
66
|
|
|
{ |
67
|
|
|
return new $this->className(); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* retrieve an object from the database, based on. use in child classes |
72
|
|
|
* |
73
|
|
|
* @param int|null $id ID |
74
|
|
|
* |
75
|
|
|
* @param array|null $fields |
76
|
|
|
* @return mixed object if id exists, false if not |
77
|
|
|
*/ |
78
|
|
|
public function get($id = null, $fields = null) |
79
|
|
|
{ |
80
|
|
|
$id = (int)$id; |
81
|
|
|
if ($id > 0) { |
82
|
|
|
$sql = $this->selectQuery(new \Criteria($this->idfield, $id)); |
83
|
|
|
if (!$result = $this->db->query($sql)) { |
84
|
|
|
return false; |
85
|
|
|
} |
86
|
|
|
$numrows = $this->db->getRowsNum($result); |
87
|
|
|
if (1 == $numrows) { |
88
|
|
|
$obj = new $this->className($this->db->fetchArray($result)); |
89
|
|
|
|
90
|
|
|
return $obj; |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
return false; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* retrieve objects from the database |
99
|
|
|
* |
100
|
|
|
* @param \Criteria|\CriteriaElement|null $criteria conditions to be met |
101
|
|
|
* @param bool $idAsKey Should the department ID be used as array key |
102
|
|
|
* |
103
|
|
|
* @param bool $asObject |
104
|
|
|
* @return array array of objects |
105
|
|
|
*/ |
106
|
|
|
public function &getObjects($criteria = null, $idAsKey = false, $asObject = true) //&getObjects($criteria = null, $idAsKey = false) |
107
|
|
|
{ |
108
|
|
|
$ret = []; |
109
|
|
|
$limit = $start = 0; |
110
|
|
|
$sql = $this->selectQuery($criteria); |
111
|
|
|
$id = $this->idfield; |
112
|
|
|
if (null !== $criteria) { |
113
|
|
|
$limit = $criteria->getLimit(); |
114
|
|
|
$start = $criteria->getStart(); |
115
|
|
|
} |
116
|
|
|
$result = $this->db->query($sql, $limit, $start); |
117
|
|
|
// if no records from db, return empty array |
118
|
|
|
if (!$result) { |
119
|
|
|
return $ret; |
120
|
|
|
} |
121
|
|
|
// Add each returned record to the result array |
122
|
|
|
while (false !== ($myrow = $this->db->fetchArray($result))) { |
123
|
|
|
$obj = new $this->className($myrow); |
124
|
|
|
if ($idAsKey) { |
125
|
|
|
$ret[$obj->getVar($id)] = $obj; |
126
|
|
|
} else { |
127
|
|
|
$ret[] = $obj; |
128
|
|
|
} |
129
|
|
|
unset($obj); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
return $ret; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @param bool $force |
137
|
|
|
* |
138
|
|
|
* @return bool |
139
|
|
|
*/ |
140
|
|
|
public function insert(\XoopsObject $obj, $force = false)// insert($obj, $force = false) |
141
|
|
|
{ |
142
|
|
|
// Make sure object is of correct type |
143
|
|
|
if (0 != \strcasecmp($this->className, \get_class($obj))) { |
144
|
|
|
return false; |
145
|
|
|
} |
146
|
|
|
// Make sure object needs to be stored in DB |
147
|
|
|
if (!$obj->isDirty()) { |
148
|
|
|
return true; |
149
|
|
|
} |
150
|
|
|
// Make sure object fields are filled with valid values |
151
|
|
|
if (!$obj->cleanVars()) { |
152
|
|
|
return false; |
153
|
|
|
} |
154
|
|
|
// Create query for DB update |
155
|
|
|
if ($obj->isNew()) { |
156
|
|
|
// Determine next auto-gen ID for table |
157
|
|
|
$this->db->genId($this->db->prefix($this->dbtable) . '_uid_seq'); |
158
|
|
|
$sql = $this->insertQuery($obj); |
159
|
|
|
} else { |
160
|
|
|
$sql = $this->updateQuery($obj); |
161
|
|
|
} |
162
|
|
|
// Update DB |
163
|
|
|
if ($force) { |
164
|
|
|
$result = $this->db->queryF($sql); |
165
|
|
|
} else { |
166
|
|
|
$result = $this->db->query($sql); |
167
|
|
|
} |
168
|
|
|
if (!$result) { |
169
|
|
|
$obj->setErrors('The query returned an error. ' . $this->db->error()); |
170
|
|
|
|
171
|
|
|
return false; |
172
|
|
|
} |
173
|
|
|
//Make sure auto-gen ID is stored correctly in object |
174
|
|
|
if ($obj->isNew()) { |
175
|
|
|
$obj->assignVar($this->idfield, $this->db->getInsertId()); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
return true; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Create a "select" SQL query |
183
|
|
|
* |
184
|
|
|
* @param \Criteria|null $criteria {@link \Criteria} to match |
185
|
|
|
* |
186
|
|
|
* @return string SQL query |
187
|
|
|
*/ |
188
|
|
|
private function selectQuery(?\Criteria $criteria = null) |
189
|
|
|
{ |
190
|
|
|
$sql = \sprintf('SELECT * FROM `%s`', $this->db->prefix($this->dbtable)); |
191
|
|
|
if (null !== $criteria && $criteria instanceof \Criteria) { |
192
|
|
|
$sql .= ' ' . $criteria->renderWhere(); |
193
|
|
|
if ('' != $criteria->getSort()) { |
194
|
|
|
$sql .= ' ORDER BY ' . $criteria->getSort() . ' |
195
|
|
|
' . $criteria->getOrder(); |
196
|
|
|
} |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
return $sql; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* count objects matching a criteria |
204
|
|
|
* |
205
|
|
|
* @param \CriteriaElement|null $criteria {@link CriteriaElement} to match |
206
|
|
|
* |
207
|
|
|
* @return int count of objects |
208
|
|
|
*/ |
209
|
|
|
public function getCount(?\CriteriaElement $criteria = null) //getCount($criteria = null) |
210
|
|
|
{ |
211
|
|
|
$sql = 'SELECT COUNT(*) FROM ' . $this->db->prefix($this->dbtable); |
212
|
|
|
if (null !== $criteria && ($criteria instanceof \Criteria || $criteria instanceof \CriteriaCompo)) { |
213
|
|
|
$sql .= ' ' . $criteria->renderWhere(); |
214
|
|
|
} |
215
|
|
|
if (!$result = $this->db->query($sql)) { |
216
|
|
|
return 0; |
217
|
|
|
} |
218
|
|
|
[$count] = $this->db->fetchRow($result); |
219
|
|
|
|
220
|
|
|
return $count; |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* delete object based on id |
225
|
|
|
* |
226
|
|
|
* @param \XoopsObject $obj {@link XoopsObject} |
227
|
|
|
* to delete |
228
|
|
|
* @param bool $force override XOOPS delete protection |
229
|
|
|
* |
230
|
|
|
* @return bool deletion successful? |
231
|
|
|
*/ |
232
|
|
|
public function delete(\XoopsObject $obj, $force = false) //delete($obj, $force = false) |
233
|
|
|
{ |
234
|
|
|
if (0 != \strcasecmp($this->className, \get_class($obj))) { |
235
|
|
|
return false; |
236
|
|
|
} |
237
|
|
|
$sql = $this->deleteQuery($obj); |
238
|
|
|
if ($force) { |
239
|
|
|
$result = $this->db->queryF($sql); |
240
|
|
|
} else { |
241
|
|
|
$result = $this->db->query($sql); |
242
|
|
|
} |
243
|
|
|
if (!$result) { |
244
|
|
|
return false; |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
return true; |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
/** |
251
|
|
|
* delete department matching a set of conditions |
252
|
|
|
* |
253
|
|
|
* @param \CriteriaElement|null $criteria {@link CriteriaElement} |
254
|
|
|
* |
255
|
|
|
* @param bool $force |
256
|
|
|
* @param bool $asObject |
257
|
|
|
* @return bool FALSE if deletion failed |
258
|
|
|
*/ |
259
|
|
|
public function deleteAll(?\CriteriaElement $criteria = null, $force = true, $asObject = false) //deleteAll($criteria = null) |
260
|
|
|
{ |
261
|
|
|
$sql = 'DELETE FROM ' . $this->db->prefix($this->dbtable); |
262
|
|
|
if (null !== $criteria && ($criteria instanceof \Criteria || $criteria instanceof \CriteriaCompo)) { |
263
|
|
|
$sql .= ' ' . $criteria->renderWhere(); |
264
|
|
|
} |
265
|
|
|
if (!$result = $this->db->query($sql)) { |
|
|
|
|
266
|
|
|
return false; |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
return true; |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
/** |
273
|
|
|
* Assign a value to 1 field for tickets matching a set of conditions |
274
|
|
|
* |
275
|
|
|
* @param string $fieldname |
276
|
|
|
* @param string $fieldvalue |
277
|
|
|
* @param \Criteria|\CriteriaCompo|null $criteria |
278
|
|
|
* |
279
|
|
|
* @param bool $force |
280
|
|
|
* @return bool FALSE if update failed |
281
|
|
|
*/ |
282
|
|
|
public function updateAll($fieldname, $fieldvalue, $criteria = null, $force = false) //updateAll($fieldname, $fieldvalue, $criteria = null) |
283
|
|
|
{ |
284
|
|
|
$setClause = \is_numeric($fieldvalue) ? $fieldname . ' = ' . $fieldvalue : $fieldname . ' = ' . $this->db->quoteString($fieldvalue); |
285
|
|
|
$sql = 'UPDATE ' . $this->db->prefix($this->dbtable) . ' SET ' . $setClause; |
286
|
|
|
if (null !== $criteria && ($criteria instanceof \Criteria || $criteria instanceof \CriteriaCompo)) { |
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 $obj |
298
|
|
|
* |
299
|
|
|
* @return bool |
300
|
|
|
*/ |
301
|
|
|
protected function insertQuery($obj) |
302
|
|
|
{ |
303
|
|
|
return false; |
304
|
|
|
} |
305
|
|
|
|
306
|
|
|
/** |
307
|
|
|
* @param \XoopsObject $obj |
308
|
|
|
* |
309
|
|
|
* @return bool|string |
310
|
|
|
*/ |
311
|
|
|
protected function updateQuery($obj) |
312
|
|
|
{ |
313
|
|
|
return false; |
314
|
|
|
} |
315
|
|
|
|
316
|
|
|
/** |
317
|
|
|
* @param \XoopsObject $obj |
318
|
|
|
* |
319
|
|
|
* @return bool |
320
|
|
|
*/ |
321
|
|
|
protected function deleteQuery($obj) |
322
|
|
|
{ |
323
|
|
|
return false; |
324
|
|
|
} |
325
|
|
|
|
326
|
|
|
/** |
327
|
|
|
* Singleton - prevent multiple instances of this class |
328
|
|
|
* |
329
|
|
|
* |
330
|
|
|
* @return \XoopsObject {@link pagesCategoryHandler} |
331
|
|
|
*/ |
332
|
|
|
public function getInstance(?\XoopsDatabase $db = null) |
333
|
|
|
{ |
334
|
|
|
static $instance; |
335
|
|
|
if (null === $instance) { |
336
|
|
|
$className = $this->className . 'Handler'; |
337
|
|
|
$instance = new $className($db); |
338
|
|
|
} |
339
|
|
|
|
340
|
|
|
return $instance; |
341
|
|
|
} |
342
|
|
|
} |
343
|
|
|
|