1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace XoopsModules\Suico; |
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
|
|
|
* @category Module |
17
|
|
|
* @copyright {@link https://xoops.org/ XOOPS Project} |
18
|
|
|
* @license GNU GPL 2.0 or later (https://www.gnu.org/licenses/gpl-2.0.html) |
19
|
|
|
* @author Bruno Barthez, Marcello Brandão aka Suico, Mamba, LioMJ <https://xoops.org> |
20
|
|
|
*/ |
21
|
|
|
|
22
|
|
|
use CriteriaElement; |
23
|
|
|
use MyTextSanitizer; |
24
|
|
|
use XoopsDatabase; |
25
|
|
|
use XoopsObject; |
26
|
|
|
use XoopsPersistableObjectHandler; |
27
|
|
|
|
28
|
|
|
require_once XOOPS_ROOT_PATH . '/kernel/object.php'; |
29
|
|
|
require_once XOOPS_ROOT_PATH . '/class/module.textsanitizer.php'; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* NotesHandler class. |
33
|
|
|
* This class provides simple mechanism forNotes object |
34
|
|
|
*/ |
35
|
|
|
class NotesHandler extends XoopsPersistableObjectHandler |
36
|
|
|
{ |
37
|
|
|
public Helper $helper; |
38
|
|
|
public $isAdmin; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Constructor |
42
|
|
|
* @param \XoopsDatabase|null $xoopsDatabase |
43
|
|
|
* @param \XoopsModules\Suico\Helper|null $helper |
44
|
|
|
*/ |
45
|
|
|
public function __construct( |
46
|
|
|
?XoopsDatabase $xoopsDatabase = null, |
47
|
|
|
$helper = null |
48
|
|
|
) { |
49
|
|
|
/** @var \XoopsModules\Suico\Helper $this- >helper */ |
50
|
|
|
if (null === $helper) { |
51
|
|
|
$this->helper = Helper::getInstance(); |
52
|
|
|
} else { |
53
|
|
|
$this->helper = $helper; |
54
|
|
|
} |
55
|
|
|
$this->isAdmin = $this->helper->isUserAdmin(); |
56
|
|
|
parent::__construct($xoopsDatabase, 'suico_notes', Notes::class, 'note_id', 'note_id'); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* create a new Groups |
61
|
|
|
* |
62
|
|
|
* @param bool $isNew flag the new objects as "new"? |
63
|
|
|
* @return \XoopsObject Groups |
64
|
|
|
*/ |
65
|
|
|
public function create( |
66
|
|
|
$isNew = true |
67
|
|
|
) { |
68
|
|
|
$obj = parent::create($isNew); |
69
|
|
|
if ($isNew) { |
70
|
|
|
$obj->setNew(); |
71
|
|
|
} else { |
72
|
|
|
$obj->unsetNew(); |
73
|
|
|
} |
74
|
|
|
$obj->helper = $this->helper; |
75
|
|
|
|
76
|
|
|
return $obj; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* retrieve aNotes |
81
|
|
|
* |
82
|
|
|
* @param int|null $id of theNotes |
83
|
|
|
* @param null $fields |
|
|
|
|
84
|
|
|
* @return false|\XoopsModules\Suico\Notes reference to the {@linkNotes} object, FALSE if failed |
85
|
|
|
*/ |
86
|
|
|
public function get2( |
87
|
|
|
$id = null, |
88
|
|
|
$fields = null |
|
|
|
|
89
|
|
|
) { |
90
|
|
|
$sql = 'SELECT * FROM ' . $this->db->prefix('suico_notes') . ' WHERE note_id=' . $id; |
91
|
|
|
if (!$result = $this->db->query($sql)) { |
92
|
|
|
return false; |
93
|
|
|
} |
94
|
|
|
$numrows = $this->db->getRowsNum($result); |
95
|
|
|
if (1 === $numrows) { |
96
|
|
|
$suico_notes = new Notes(); |
97
|
|
|
$suico_notes->assignVars($this->db->fetchArray($result)); |
98
|
|
|
|
99
|
|
|
return $suico_notes; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
return false; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* insert a new Notes in the database |
107
|
|
|
* |
108
|
|
|
* @param \XoopsObject $object reference to the {@linkNotes} |
109
|
|
|
* object |
110
|
|
|
* @param bool $force |
111
|
|
|
* @return bool FALSE if failed, TRUE if already present and unchanged or successful |
112
|
|
|
*/ |
113
|
|
|
public function insert2( |
114
|
|
|
XoopsObject $object, |
115
|
|
|
$force = false |
116
|
|
|
) { |
117
|
|
|
global $xoopsConfig; |
118
|
|
|
if (!$object instanceof Notes) { |
119
|
|
|
return false; |
120
|
|
|
} |
121
|
|
|
if (!$object->isDirty()) { |
122
|
|
|
return true; |
123
|
|
|
} |
124
|
|
|
if (!$object->cleanVars()) { |
125
|
|
|
return false; |
126
|
|
|
} |
127
|
|
|
$private = ''; |
128
|
|
|
$date_created = ''; |
129
|
|
|
$note_to = ''; |
130
|
|
|
$note_from = ''; |
131
|
|
|
$noteId = ''; |
132
|
|
|
foreach ($object->cleanVars as $k => $v) { |
133
|
|
|
${$k} = $v; |
134
|
|
|
} |
135
|
|
|
// $now = 'date_add(now(), interval ' . $xoopsConfig['server_TZ'] . ' hour)'; |
136
|
|
|
if ($object->isNew()) { |
137
|
|
|
// add / modify a Notes |
138
|
|
|
$object = new Notes(); |
139
|
|
|
$format = 'INSERT INTO %s (note_id, note_text, note_from, note_to, date_created, private)'; |
140
|
|
|
$format .= 'VALUES (%u, %s, %u, %u, %u,%u)'; |
141
|
|
|
$sql = \sprintf( |
142
|
|
|
$format, |
143
|
|
|
$this->db->prefix('suico_notes'), |
144
|
|
|
$noteId, |
145
|
|
|
$this->db->quoteString($note_text), |
|
|
|
|
146
|
|
|
$note_from, |
147
|
|
|
$note_to, |
148
|
|
|
$date_created, |
149
|
|
|
$private |
150
|
|
|
); |
151
|
|
|
$force = true; |
152
|
|
|
} else { |
153
|
|
|
$format = 'UPDATE %s SET '; |
154
|
|
|
$format .= 'note_id=%u, note_text=%s, note_from=%u, note_to=%u, date_created=%u, private=%u'; |
155
|
|
|
$format .= ' WHERE note_id = %u'; |
156
|
|
|
$sql = \sprintf( |
157
|
|
|
$format, |
158
|
|
|
$this->db->prefix('suico_notes'), |
159
|
|
|
$noteId, |
160
|
|
|
$this->db->quoteString($note_text), |
161
|
|
|
$note_from, |
162
|
|
|
$note_to, |
163
|
|
|
$date_created, |
164
|
|
|
$private, |
165
|
|
|
$noteId |
166
|
|
|
); |
167
|
|
|
} |
168
|
|
|
if ($force) { |
169
|
|
|
$result = $this->db->queryF($sql); |
170
|
|
|
} else { |
171
|
|
|
$result = $this->db->query($sql); |
172
|
|
|
} |
173
|
|
|
if (!$result) { |
174
|
|
|
return false; |
175
|
|
|
} |
176
|
|
|
if (empty($noteId)) { |
|
|
|
|
177
|
|
|
$noteId = $this->db->getInsertId(); |
178
|
|
|
} |
179
|
|
|
$object->assignVar('note_id', $noteId); |
180
|
|
|
|
181
|
|
|
return true; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* delete aNotes from the database |
186
|
|
|
* |
187
|
|
|
* @param \XoopsObject $object reference to theNotes to delete |
188
|
|
|
* @param bool $force |
189
|
|
|
* @return bool FALSE if failed. |
190
|
|
|
*/ |
191
|
|
|
public function delete( |
192
|
|
|
XoopsObject $object, |
193
|
|
|
$force = false |
194
|
|
|
) { |
195
|
|
|
if (!$object instanceof Notes) { |
196
|
|
|
return false; |
197
|
|
|
} |
198
|
|
|
$sql = \sprintf( |
199
|
|
|
'DELETE FROM %s WHERE note_id = %u', |
200
|
|
|
$this->db->prefix('suico_notes'), |
201
|
|
|
(int)$object->getVar('note_id') |
202
|
|
|
); |
203
|
|
|
if ($force) { |
204
|
|
|
$result = $this->db->queryF($sql); |
205
|
|
|
} else { |
206
|
|
|
$result = $this->db->query($sql); |
207
|
|
|
} |
208
|
|
|
if (!$result) { |
209
|
|
|
return false; |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
return true; |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* retrieve suico_notes from the database |
217
|
|
|
* |
218
|
|
|
* @param \CriteriaElement|\CriteriaCompo|null $criteria {@link CriteriaElement} conditions to be met |
219
|
|
|
* @param bool $id_as_key use the UID as key for the array? |
220
|
|
|
* @param bool $as_object |
221
|
|
|
* @return array array of {@linkNotes} objects |
222
|
|
|
*/ |
223
|
|
|
public function &getObjects( |
224
|
|
|
?CriteriaElement $criteria = null, |
225
|
|
|
$id_as_key = false, |
226
|
|
|
$as_object = true |
227
|
|
|
) { |
228
|
|
|
$ret = []; |
229
|
|
|
$start = 0; |
230
|
|
|
$limit = 0; |
231
|
|
|
$sql = 'SELECT * FROM ' . $this->db->prefix('suico_notes'); |
232
|
|
|
if (isset($criteria) && is_subclass_of($criteria, 'CriteriaElement')) { |
233
|
|
|
$sql .= ' ' . $criteria->renderWhere(); |
|
|
|
|
234
|
|
|
if ('' !== $criteria->getSort()) { |
235
|
|
|
$sql .= ' ORDER BY ' . $criteria->getSort() . ' ' . $criteria->getOrder(); |
236
|
|
|
} |
237
|
|
|
$limit = $criteria->getLimit(); |
238
|
|
|
$start = $criteria->getStart(); |
239
|
|
|
} |
240
|
|
|
$result = $this->db->query($sql, $limit, $start); |
241
|
|
|
if (!$result) { |
242
|
|
|
return $ret; |
243
|
|
|
} |
244
|
|
|
while (false !== ($myrow = $this->db->fetchArray($result))) { |
245
|
|
|
$suico_notes = new Notes(); |
246
|
|
|
$suico_notes->assignVars($myrow); |
247
|
|
|
if ($id_as_key) { |
248
|
|
|
$ret[$myrow['note_id']] = &$suico_notes; |
249
|
|
|
} else { |
250
|
|
|
$ret[] = &$suico_notes; |
251
|
|
|
} |
252
|
|
|
unset($suico_notes); |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
return $ret; |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
/** |
259
|
|
|
* count suico_notes matching a condition |
260
|
|
|
* |
261
|
|
|
* @param \CriteriaElement|\CriteriaCompo|null $criteria {@link CriteriaElement} to match |
262
|
|
|
* @return int count of suico_notes |
263
|
|
|
*/ |
264
|
|
|
public function getCount( |
265
|
|
|
?CriteriaElement $criteria = null |
266
|
|
|
) { |
267
|
|
|
$sql = 'SELECT COUNT(*) FROM ' . $this->db->prefix('suico_notes'); |
268
|
|
|
if (isset($criteria) && is_subclass_of($criteria, 'CriteriaElement')) { |
269
|
|
|
$sql .= ' ' . $criteria->renderWhere(); |
270
|
|
|
} |
271
|
|
|
$result = $this->db->query($sql); |
272
|
|
|
if (!$result) { |
273
|
|
|
return 0; |
274
|
|
|
} |
275
|
|
|
[$count] = $this->db->fetchRow($result); |
276
|
|
|
|
277
|
|
|
return (int)$count; |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
/** |
281
|
|
|
* delete suico_notes matching a set of conditions |
282
|
|
|
* |
283
|
|
|
* @param \CriteriaElement|\CriteriaCompo|null $criteria {@link CriteriaElement} |
284
|
|
|
* @param bool $force |
285
|
|
|
* @param bool $asObject |
286
|
|
|
* @return bool FALSE if deletion failed |
287
|
|
|
*/ |
288
|
|
|
public function deleteAll( |
289
|
|
|
?CriteriaElement $criteria = null, |
290
|
|
|
$force = true, |
291
|
|
|
$asObject = false |
292
|
|
|
) { |
293
|
|
|
$sql = 'DELETE FROM ' . $this->db->prefix('suico_notes'); |
294
|
|
|
if (isset($criteria) && is_subclass_of($criteria, 'CriteriaElement')) { |
295
|
|
|
$sql .= ' ' . $criteria->renderWhere(); |
296
|
|
|
} |
297
|
|
|
if (!$result = $this->db->query($sql)) { |
|
|
|
|
298
|
|
|
return false; |
299
|
|
|
} |
300
|
|
|
|
301
|
|
|
return true; |
302
|
|
|
} |
303
|
|
|
|
304
|
|
|
/** |
305
|
|
|
* @param $countNotes |
306
|
|
|
* @param \CriteriaElement|\CriteriaCompo|null $criteria |
307
|
|
|
* @return array |
308
|
|
|
*/ |
309
|
|
|
public function getNotes( |
310
|
|
|
$countNotes, |
|
|
|
|
311
|
|
|
$criteria |
312
|
|
|
) { |
313
|
|
|
$myts = new MyTextSanitizer(); |
314
|
|
|
$ret = []; |
|
|
|
|
315
|
|
|
$sql = 'SELECT note_id, uid, uname, user_avatar, note_from, note_text, date_created FROM ' . $this->db->prefix( |
316
|
|
|
'suico_notes' |
317
|
|
|
) . ', ' . $this->db->prefix( |
318
|
|
|
'users' |
319
|
|
|
); |
320
|
|
|
if (($criteria instanceof \CriteriaCompo) || ($criteria instanceof \Criteria)) { |
321
|
|
|
$sql .= ' ' . $criteria->renderWhere(); |
322
|
|
|
//attention here this is kind of a hack |
323
|
|
|
$sql .= ' AND uid = note_from'; |
324
|
|
|
if ('' !== $criteria->getSort()) { |
325
|
|
|
$sql .= ' ORDER BY ' . $criteria->getSort() . ' ' . $criteria->getOrder(); |
326
|
|
|
} |
327
|
|
|
$limit = $criteria->getLimit(); |
328
|
|
|
$start = $criteria->getStart(); |
329
|
|
|
$result = $this->db->query($sql, $limit, $start); |
330
|
|
|
$vetor = []; |
331
|
|
|
$i = 0; |
332
|
|
|
while (false !== ($myrow = $this->db->fetchArray($result))) { |
333
|
|
|
$vetor[$i]['uid'] = $myrow['uid']; |
334
|
|
|
$vetor[$i]['uname'] = $myrow['uname']; |
335
|
|
|
$vetor[$i]['user_avatar'] = $myrow['user_avatar']; |
336
|
|
|
$temptext = $myts->xoopsCodeDecode($myrow['note_text'], 1); |
337
|
|
|
$vetor[$i]['text'] = $myts->nl2Br($temptext); |
338
|
|
|
$vetor[$i]['id'] = $myrow['note_id']; |
339
|
|
|
$vetor[$i]['date_created'] = \formatTimestamp($myrow['date_created'], 's'); |
340
|
|
|
$i++; |
341
|
|
|
} |
342
|
|
|
|
343
|
|
|
return $vetor; |
344
|
|
|
} |
345
|
|
|
} |
346
|
|
|
} |
347
|
|
|
|