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 XoopsDatabase; |
24
|
|
|
use XoopsObject; |
25
|
|
|
use XoopsPersistableObjectHandler; |
26
|
|
|
|
27
|
|
|
require_once XOOPS_ROOT_PATH . '/kernel/object.php'; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* suico_friendrequesthandler class. |
31
|
|
|
* This class provides simple mechanism for Friendrequest object |
32
|
|
|
*/ |
33
|
|
|
class FriendrequestHandler extends XoopsPersistableObjectHandler |
34
|
|
|
{ |
35
|
|
|
/** |
36
|
|
|
* @var \XoopsModules\Suico\Helper |
37
|
|
|
*/ |
38
|
|
|
public Helper $helper; |
39
|
|
|
public $isAdmin; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Constructor |
43
|
|
|
* @param \XoopsDatabase|null $xoopsDatabase |
44
|
|
|
* @param Helper|null $helper |
45
|
|
|
*/ |
46
|
|
|
public function __construct( |
47
|
|
|
?XoopsDatabase $xoopsDatabase = null, |
48
|
|
|
$helper = null |
49
|
|
|
) { |
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_friendrequests', Friendrequest::class, 'friendreq_id', 'friendreq_id'); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param bool $isNew |
61
|
|
|
* |
62
|
|
|
* @return \XoopsObject |
63
|
|
|
*/ |
64
|
|
|
public function create($isNew = true) |
65
|
|
|
{ |
66
|
|
|
$obj = parent::create($isNew); |
67
|
|
|
if ($isNew) { |
68
|
|
|
$obj->setNew(); |
69
|
|
|
} else { |
70
|
|
|
$obj->unsetNew(); |
71
|
|
|
} |
72
|
|
|
$obj->helper = $this->helper; |
73
|
|
|
|
74
|
|
|
return $obj; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* retrieve a Friendrequest |
79
|
|
|
* |
80
|
|
|
* @param int|null $id of the Friendrequest |
81
|
|
|
* @param null $fields |
|
|
|
|
82
|
|
|
* @return false|\XoopsModules\Suico\Friendrequest reference to the {@link Friendrequest} object, FALSE if failed |
83
|
|
|
*/ |
84
|
|
|
public function get2( |
85
|
|
|
$id = null, |
86
|
|
|
$fields = null |
|
|
|
|
87
|
|
|
) { |
88
|
|
|
$sql = 'SELECT * FROM ' . $this->db->prefix('suico_friendrequests') . ' WHERE friendreq_id=' . $id; |
89
|
|
|
if (!$result = $this->db->query($sql)) { |
90
|
|
|
return false; |
91
|
|
|
} |
92
|
|
|
$numrows = $this->db->getRowsNum($result); |
93
|
|
|
if (1 === $numrows) { |
94
|
|
|
$suico_friendrequest = new Friendrequest(); |
95
|
|
|
$suico_friendrequest->assignVars($this->db->fetchArray($result)); |
96
|
|
|
|
97
|
|
|
return $suico_friendrequest; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
return false; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* insert a new Friendrequest in the database |
105
|
|
|
* |
106
|
|
|
* @param \XoopsObject $object reference to the {@link Friendrequest} |
107
|
|
|
* object |
108
|
|
|
* @param bool $force |
109
|
|
|
* @return bool FALSE if failed, TRUE if already present and unchanged or successful |
110
|
|
|
*/ |
111
|
|
|
public function insert2( |
112
|
|
|
XoopsObject $object, |
113
|
|
|
$force = false |
114
|
|
|
) { |
115
|
|
|
global $xoopsConfig; |
116
|
|
|
if (!$object instanceof Friendrequest) { |
117
|
|
|
return false; |
118
|
|
|
} |
119
|
|
|
if (!$object->isDirty()) { |
120
|
|
|
return true; |
121
|
|
|
} |
122
|
|
|
if (!$object->cleanVars()) { |
123
|
|
|
return false; |
124
|
|
|
} |
125
|
|
|
foreach ($object->cleanVars as $k => $v) { |
126
|
|
|
${$k} = $v; |
127
|
|
|
} |
128
|
|
|
$now = 'date_add(now(), interval ' . $xoopsConfig['server_TZ'] . ' hour)'; |
|
|
|
|
129
|
|
|
if ($object->isNew()) { |
130
|
|
|
// ajout/modification d'un Friendrequest |
131
|
|
|
$object = new Friendrequest(); |
132
|
|
|
$format = 'INSERT INTO %s (friendreq_id, friendrequester_uid, friendrequestto_uid, date_created)'; |
133
|
|
|
$format .= 'VALUES (%u, %u, %u, %u)'; |
134
|
|
|
$sql = \sprintf( |
135
|
|
|
$format, |
136
|
|
|
$this->db->prefix('suico_friendrequests'), |
137
|
|
|
$friendreq_id, |
|
|
|
|
138
|
|
|
$friendrequester_uid, |
|
|
|
|
139
|
|
|
$friendrequestto_uid, |
|
|
|
|
140
|
|
|
$date_created |
|
|
|
|
141
|
|
|
); |
142
|
|
|
$force = true; |
143
|
|
|
} else { |
144
|
|
|
$format = 'UPDATE %s SET '; |
145
|
|
|
$format .= 'friendreq_id=%u, friendrequester_uid=%u, friendrequestto_uid=%u, date_created=%u'; |
146
|
|
|
$format .= ' WHERE friendreq_id = %u'; |
147
|
|
|
$sql = \sprintf( |
148
|
|
|
$format, |
149
|
|
|
$this->db->prefix('suico_friendrequests'), |
150
|
|
|
$friendreq_id, |
151
|
|
|
$friendrequester_uid, |
152
|
|
|
$friendrequestto_uid, |
153
|
|
|
$date_created, |
154
|
|
|
$friendreq_id |
155
|
|
|
); |
156
|
|
|
} |
157
|
|
|
if ($force) { |
158
|
|
|
$result = $this->db->queryF($sql); |
159
|
|
|
} else { |
160
|
|
|
$result = $this->db->query($sql); |
161
|
|
|
} |
162
|
|
|
if (!$result) { |
163
|
|
|
return false; |
164
|
|
|
} |
165
|
|
|
if (empty($friendreq_id)) { |
|
|
|
|
166
|
|
|
$friendreq_id = $this->db->getInsertId(); |
167
|
|
|
} |
168
|
|
|
$object->assignVar('friendreq_id', $friendreq_id); |
169
|
|
|
|
170
|
|
|
return true; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* delete a Friendrequest from the database |
175
|
|
|
* |
176
|
|
|
* @param \XoopsObject $object reference to the Friendrequest to delete |
177
|
|
|
* @param bool $force |
178
|
|
|
* @return bool FALSE if failed. |
179
|
|
|
*/ |
180
|
|
|
public function delete( |
181
|
|
|
XoopsObject $object, |
182
|
|
|
$force = false |
183
|
|
|
) { |
184
|
|
|
if (!$object instanceof Friendrequest) { |
185
|
|
|
return false; |
186
|
|
|
} |
187
|
|
|
$sql = \sprintf( |
188
|
|
|
'DELETE FROM %s WHERE friendreq_id = %u', |
189
|
|
|
$this->db->prefix('suico_friendrequests'), |
190
|
|
|
(int)$object->getVar('friendreq_id') |
191
|
|
|
); |
192
|
|
|
if ($force) { |
193
|
|
|
$result = $this->db->queryF($sql); |
194
|
|
|
} else { |
195
|
|
|
$result = $this->db->query($sql); |
196
|
|
|
} |
197
|
|
|
if (!$result) { |
198
|
|
|
return false; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
return true; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* retrieve suico_friendrequests from the database |
206
|
|
|
* |
207
|
|
|
* @param \CriteriaElement|\CriteriaCompo|null $criteria {@link \CriteriaElement} conditions to be met |
208
|
|
|
* @param bool $id_as_key use the UID as key for the array? |
209
|
|
|
* @param bool $as_object |
210
|
|
|
* @return array array of {@link Friendrequest} objects |
211
|
|
|
*/ |
212
|
|
|
public function &getObjects( |
213
|
|
|
?CriteriaElement $criteria = null, |
214
|
|
|
$id_as_key = false, |
215
|
|
|
$as_object = true |
216
|
|
|
) { |
217
|
|
|
$ret = []; |
218
|
|
|
$start = 0; |
219
|
|
|
$limit = 0; |
220
|
|
|
$sql = 'SELECT * FROM ' . $this->db->prefix('suico_friendrequests'); |
221
|
|
|
if (isset($criteria) && $criteria instanceof \CriteriaElement) { |
222
|
|
|
$sql .= ' ' . $criteria->renderWhere(); |
|
|
|
|
223
|
|
|
if ('' !== $criteria->getSort()) { |
224
|
|
|
$sql .= ' ORDER BY ' . $criteria->getSort() . ' ' . $criteria->getOrder(); |
225
|
|
|
} |
226
|
|
|
$limit = $criteria->getLimit(); |
227
|
|
|
$start = $criteria->getStart(); |
228
|
|
|
} |
229
|
|
|
$result = $this->db->query($sql, $limit, $start); |
230
|
|
|
if (!$result) { |
231
|
|
|
return $ret; |
232
|
|
|
} |
233
|
|
|
while (false !== ($myrow = $this->db->fetchArray($result))) { |
234
|
|
|
$suico_friendrequest = new Friendrequest(); |
235
|
|
|
$suico_friendrequest->assignVars($myrow); |
236
|
|
|
if ($id_as_key) { |
237
|
|
|
$ret[$myrow['friendreq_id']] = &$suico_friendrequest; |
238
|
|
|
} else { |
239
|
|
|
$ret[] = &$suico_friendrequest; |
240
|
|
|
} |
241
|
|
|
unset($suico_friendrequest); |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
return $ret; |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
/** |
248
|
|
|
* count suico_friendrequests matching a condition |
249
|
|
|
* |
250
|
|
|
* @param \CriteriaElement|\CriteriaCompo|null $criteria {@link \CriteriaElement} to match |
251
|
|
|
* @return int count of suico_friendrequests |
252
|
|
|
*/ |
253
|
|
|
public function getCount( |
254
|
|
|
?CriteriaElement $criteria = null |
255
|
|
|
) { |
256
|
|
|
$sql = 'SELECT COUNT(*) FROM ' . $this->db->prefix('suico_friendrequests'); |
257
|
|
|
if (isset($criteria) && is_subclass_of($criteria, 'CriteriaElement')) { |
258
|
|
|
$sql .= ' ' . $criteria->renderWhere(); |
259
|
|
|
} |
260
|
|
|
$result = $this->db->query($sql); |
261
|
|
|
if (!$result) { |
262
|
|
|
return 0; |
263
|
|
|
} |
264
|
|
|
[$count] = $this->db->fetchRow($result); |
265
|
|
|
|
266
|
|
|
return (int)$count; |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
/** |
270
|
|
|
* delete suico_friendrequests matching a set of conditions |
271
|
|
|
* |
272
|
|
|
* @param \CriteriaElement|\CriteriaCompo|null $criteria {@link \CriteriaElement} |
273
|
|
|
* @param bool $force |
274
|
|
|
* @param bool $asObject |
275
|
|
|
* @return bool FALSE if deletion failed |
276
|
|
|
*/ |
277
|
|
|
public function deleteAll( |
278
|
|
|
?CriteriaElement $criteria = null, |
279
|
|
|
$force = true, |
280
|
|
|
$asObject = false |
281
|
|
|
) { |
282
|
|
|
$sql = 'DELETE FROM ' . $this->db->prefix('suico_friendrequests'); |
283
|
|
|
if (isset($criteria) && is_subclass_of($criteria, 'CriteriaElement')) { |
284
|
|
|
$sql .= ' ' . $criteria->renderWhere(); |
285
|
|
|
} |
286
|
|
|
if (!$result = $this->db->query($sql)) { |
|
|
|
|
287
|
|
|
return false; |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
return true; |
291
|
|
|
} |
292
|
|
|
} |
293
|
|
|
|