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