|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace XoopsModules\Rssfit; |
|
6
|
|
|
|
|
7
|
|
|
/* |
|
8
|
|
|
* You may not change or alter any portion of this comment or credits |
|
9
|
|
|
* of supporting developers from this source code or any supporting source code |
|
10
|
|
|
* which is considered copyrighted (c) material of the original comment or credit authors. |
|
11
|
|
|
* |
|
12
|
|
|
* This program is distributed in the hope that it will be useful, |
|
13
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
14
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
|
15
|
|
|
*/ |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @copyright XOOPS Project (https://xoops.org) |
|
19
|
|
|
* @license GNU GPL 2 or later (https://www.gnu.org/licenses/gpl-2.0.html) |
|
20
|
|
|
* @package RSSFit - Extendable XML news feed generator |
|
21
|
|
|
* @author NS Tai (aka tuff) <http://www.brandycoke.com> |
|
22
|
|
|
* @author XOOPS Development Team |
|
23
|
|
|
*/ |
|
24
|
|
|
|
|
25
|
|
|
if (!\defined('RSSFIT_ROOT_PATH')) { |
|
26
|
|
|
exit(); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Class MiscHandler |
|
31
|
|
|
* @package XoopsModules\Rssfit |
|
32
|
|
|
*/ |
|
33
|
|
|
class MiscHandler extends \XoopsPersistableObjectHandler |
|
34
|
|
|
{ |
|
35
|
|
|
/** |
|
36
|
|
|
* @var \XoopsMySQLDatabase |
|
37
|
|
|
*/ |
|
38
|
|
|
public $db; |
|
39
|
|
|
public $dbTable; |
|
40
|
|
|
public $objClass = Misc::class; |
|
41
|
|
|
public $objKey = 'misc_id'; |
|
42
|
|
|
public $helper; |
|
43
|
|
|
|
|
44
|
|
|
public function __construct(?\XoopsMySQLDatabase $db = null, ?Helper $helper = null) |
|
45
|
|
|
{ |
|
46
|
|
|
if (null === $helper) { |
|
47
|
|
|
$helper = Helper::getInstance(); |
|
48
|
|
|
} |
|
49
|
|
|
$this->helper = $helper; |
|
50
|
|
|
|
|
51
|
|
|
if (null === $db) { |
|
52
|
|
|
$db = \XoopsDatabaseFactory::getDatabaseConnection(); |
|
53
|
|
|
} |
|
54
|
|
|
$this->db = $db; |
|
55
|
|
|
$table = $db->prefix($helper->getDirname() . '_misc'); |
|
56
|
|
|
$this->dbTable = $table; |
|
57
|
|
|
|
|
58
|
|
|
parent::__construct($db, $table, Misc::class, 'misc_id', 'misc_title'); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
public function getInstance(?\XoopsMySQLDatabase $db = null): MiscHandler |
|
62
|
|
|
{ |
|
63
|
|
|
static $instance; |
|
64
|
|
|
if (null === $instance) { |
|
65
|
|
|
$instance = new static($db); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
return $instance; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @param bool $isNew |
|
73
|
|
|
* @return \XoopsObject |
|
74
|
|
|
*/ |
|
75
|
|
|
public function create($isNew = true): ?\XoopsObject |
|
76
|
|
|
{ |
|
77
|
|
|
$obj = parent::create($isNew); |
|
78
|
|
|
// if ($isNew) { |
|
79
|
|
|
// $obj->setDefaultPermissions(); |
|
80
|
|
|
// } |
|
81
|
|
|
$obj->helper = $this->helper; |
|
82
|
|
|
|
|
83
|
|
|
return $obj; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
// public function get($id = null, $fields = '*') |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* @param null|int $id |
|
90
|
|
|
* @param null|array $fields |
|
91
|
|
|
*/ |
|
92
|
|
|
public function get($id = null, $fields = null): ?\XoopsObject |
|
93
|
|
|
{ |
|
94
|
|
|
$criteria = new \Criteria($this->objKey, (string)$id); |
|
95
|
|
|
$objs = $this->getObjects2($criteria); |
|
96
|
|
|
if (\is_array($objs) && !empty($objs)) { |
|
97
|
|
|
return 1 !== \count($objs) ? null : $objs[0]; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
return null; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* count objects matching a condition |
|
105
|
|
|
* @param null|\Criteria|\CriteriaCompo $criteria |
|
106
|
|
|
*/ |
|
107
|
|
|
public function getCount($criteria = null): ?int |
|
108
|
|
|
{ |
|
109
|
|
|
$ret = null; |
|
110
|
|
|
$sql = 'SELECT COUNT(*) FROM ' . $this->dbTable; |
|
111
|
|
|
if (($criteria instanceof \CriteriaCompo) || ($criteria instanceof \Criteria)) { |
|
112
|
|
|
$sql .= ' ' . $criteria->renderWhere(); |
|
113
|
|
|
} |
|
114
|
|
|
$result = $this->db->query($sql); |
|
115
|
|
|
if ($result instanceof \mysqli_result) { |
|
116
|
|
|
[$ret] = $this->db->fetchRow($result); |
|
117
|
|
|
} |
|
118
|
|
|
return $ret; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* @param null|\Criteria|\CriteriaCompo $criteria |
|
123
|
|
|
*/ |
|
124
|
|
|
public function getObjects2($criteria = null, string $fields = '*', string $key = ''): ?array |
|
125
|
|
|
{ |
|
126
|
|
|
$ret = null; |
|
127
|
|
|
$start = 0; |
|
128
|
|
|
$limit = $start; |
|
129
|
|
|
// $fields = '*'; |
|
130
|
|
|
$sql = 'SELECT ' . $fields . ' FROM ' . $this->dbTable; |
|
131
|
|
|
if (($criteria instanceof \CriteriaCompo) || ($criteria instanceof \Criteria)) { |
|
132
|
|
|
$sql .= ' ' . $criteria->renderWhere(); |
|
133
|
|
|
if ('' !== $criteria->getSort()) { |
|
134
|
|
|
$sql .= ' ORDER BY ' . $criteria->getSort() . ' ' . $criteria->getOrder(); |
|
135
|
|
|
} |
|
136
|
|
|
$limit = $criteria->getLimit(); |
|
137
|
|
|
$start = $criteria->getStart(); |
|
138
|
|
|
} |
|
139
|
|
|
if (!\preg_match('/ORDER BY/', $sql)) { |
|
140
|
|
|
$sql .= ' ORDER BY ' . $this->objKey . ' ASC'; |
|
141
|
|
|
} |
|
142
|
|
|
$result = $this->db->query($sql, $limit, $start); |
|
143
|
|
|
if ($result instanceof \mysqli_result) { |
|
144
|
|
|
$ret = []; |
|
145
|
|
|
while (false !== ($myrow = $this->db->fetchArray($result))) { |
|
146
|
|
|
$obj = new $this->objClass(); |
|
147
|
|
|
$obj->assignVars($myrow); |
|
148
|
|
|
switch ($key) { |
|
149
|
|
|
default: |
|
150
|
|
|
$ret[] = $obj; |
|
151
|
|
|
break; |
|
152
|
|
|
case 'title': |
|
153
|
|
|
$ret[$myrow['misc_title']] = $obj; |
|
154
|
|
|
break; |
|
155
|
|
|
case 'id': |
|
156
|
|
|
$ret[$myrow[$this->objKey]] = $obj; |
|
157
|
|
|
break; |
|
158
|
|
|
} |
|
159
|
|
|
unset($obj); |
|
160
|
|
|
} |
|
161
|
|
|
} |
|
162
|
|
|
return $ret; |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* @param bool $force flag to force the query execution despite security settings |
|
167
|
|
|
* @return array|bool|int|mixed|null |
|
168
|
|
|
*/ |
|
169
|
|
|
public function insert(\XoopsObject $object, $force = false) |
|
170
|
|
|
{ |
|
171
|
|
|
// $force = false; |
|
172
|
|
|
if (mb_strtolower(\get_class($object)) != mb_strtolower($this->objClass)) { |
|
173
|
|
|
return false; |
|
174
|
|
|
} |
|
175
|
|
|
if (!$object->isDirty()) { |
|
176
|
|
|
return true; |
|
177
|
|
|
} |
|
178
|
|
|
if (!$object->cleanVars()) { |
|
179
|
|
|
return false; |
|
180
|
|
|
} |
|
181
|
|
|
foreach ($object->cleanVars as $k => $v) { |
|
182
|
|
|
if (\XOBJ_DTYPE_INT == $object->vars[$k]['data_type']) { |
|
183
|
|
|
$cleanvars[$k] = (int)$v; |
|
184
|
|
|
} else { |
|
185
|
|
|
$cleanvars[$k] = $this->db->quoteString($v); |
|
186
|
|
|
} |
|
187
|
|
|
} |
|
188
|
|
|
if (\count($object->getErrors()) > 0) { |
|
189
|
|
|
return false; |
|
190
|
|
|
} |
|
191
|
|
|
if (empty($cleanvars[$this->objKey]) || $object->isNew()) { |
|
192
|
|
|
$cleanvars[$this->objKey] = $this->db->genId($this->dbTable . '_' . $this->objKey . '_seq'); |
|
193
|
|
|
$sql = 'INSERT INTO ' . $this->dbTable . ' (' . \implode(',', \array_keys($cleanvars)) . ') VALUES (' . \implode(',', $cleanvars) . ')'; |
|
|
|
|
|
|
194
|
|
|
} else { |
|
195
|
|
|
unset($cleanvars[$this->objKey]); |
|
196
|
|
|
$sql = 'UPDATE ' . $this->dbTable . ' SET'; |
|
197
|
|
|
foreach ($cleanvars as $k => $v) { |
|
198
|
|
|
$sql .= ' ' . $k . '=' . $v . ','; |
|
199
|
|
|
} |
|
200
|
|
|
$sql = mb_substr($sql, 0, -1); |
|
201
|
|
|
$sql .= ' WHERE ' . $this->objKey . ' = ' . $object->getVar($this->objKey); |
|
202
|
|
|
} |
|
203
|
|
|
if ($force) { |
|
204
|
|
|
$result = $this->db->queryF($sql); |
|
205
|
|
|
} else { |
|
206
|
|
|
$result = $this->db->query($sql); |
|
207
|
|
|
} |
|
208
|
|
|
if (!$result) { |
|
209
|
|
|
$object->setErrors('Could not store data in the database.<br>' . $this->db->error() . ' (' . $this->db->errno() . ')<br>' . $sql); |
|
210
|
|
|
|
|
211
|
|
|
return false; |
|
212
|
|
|
} |
|
213
|
|
|
if (false === $object->getVar($this->objKey)) { |
|
214
|
|
|
$object->assignVar($this->objKey, $this->db->getInsertId()); |
|
215
|
|
|
} |
|
216
|
|
|
|
|
217
|
|
|
return $object->getVar($this->objKey); |
|
218
|
|
|
} |
|
219
|
|
|
|
|
220
|
|
|
/** |
|
221
|
|
|
* @param bool $force flag to force the query execution despite security settings |
|
222
|
|
|
* @param null|\Criteria|\CriteriaCompo $criteria |
|
223
|
|
|
*/ |
|
224
|
|
|
public function modifyObjects(?\Criteria $criteria = null, array $fields = [], bool $force = false): ?string |
|
225
|
|
|
{ |
|
226
|
|
|
if (\count($fields) > 0) { |
|
227
|
|
|
$obj = new $this->objClass(); |
|
228
|
|
|
$sql = ''; |
|
229
|
|
|
foreach ($fields as $k => $v) { |
|
230
|
|
|
$sql .= $k . ' = '; |
|
231
|
|
|
$sql .= 3 == $obj->vars[$k]['data_type'] ? (int)$v : $this->db->quoteString($v); |
|
232
|
|
|
$sql .= ', '; |
|
233
|
|
|
} |
|
234
|
|
|
$sql = mb_substr($sql, 0, -2); |
|
235
|
|
|
$sql = 'UPDATE ' . $this->dbTable . ' SET ' . $sql; |
|
236
|
|
|
if (($criteria instanceof \CriteriaCompo) || ($criteria instanceof \Criteria)) { |
|
237
|
|
|
$sql .= ' ' . $criteria->renderWhere(); |
|
238
|
|
|
} |
|
239
|
|
|
if ($force) { |
|
240
|
|
|
$result = $this->db->queryF($sql); |
|
241
|
|
|
} else { |
|
242
|
|
|
$result = $this->db->query($sql); |
|
243
|
|
|
} |
|
244
|
|
|
if (!$result) { |
|
245
|
|
|
return 'Could not store data in the database.<br>' . $this->db->error() . ' (' . $this->db->errno() . ')<br>' . $sql; |
|
246
|
|
|
} |
|
247
|
|
|
} |
|
248
|
|
|
|
|
249
|
|
|
return null; |
|
250
|
|
|
} |
|
251
|
|
|
|
|
252
|
|
|
//TODO this should be deleted? |
|
253
|
|
|
/** |
|
254
|
|
|
* delete an object from the database |
|
255
|
|
|
* |
|
256
|
|
|
* @param \XoopsObject $object reference to the object to delete |
|
257
|
|
|
* @param bool $force |
|
258
|
|
|
* @return bool FALSE if failed. |
|
259
|
|
|
*/ |
|
260
|
|
|
public function delete(\XoopsObject $object, $force = false): bool |
|
261
|
|
|
{ |
|
262
|
|
|
// $force = false; |
|
263
|
|
|
if (mb_strtolower(\get_class($object)) != mb_strtolower($this->objClass)) { |
|
264
|
|
|
return false; |
|
265
|
|
|
} |
|
266
|
|
|
$sql = 'DELETE FROM ' . $this->dbTable . ' WHERE ' . $this->objKey . '=' . $object->getVar($this->objKey); |
|
267
|
|
|
if ($force) { |
|
268
|
|
|
$result = $this->db->queryF($sql); |
|
269
|
|
|
} else { |
|
270
|
|
|
$result = $this->db->query($sql); |
|
271
|
|
|
} |
|
272
|
|
|
if (!$result) { |
|
273
|
|
|
return false; |
|
274
|
|
|
} |
|
275
|
|
|
|
|
276
|
|
|
return true; |
|
277
|
|
|
} |
|
278
|
|
|
} |
|
279
|
|
|
|