Passed
Pull Request — master (#81)
by Michael
02:58
created

class/SuspensionsHandler.php (1 issue)

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