SuspensionsHandler::get2()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 9
nc 3
nop 2
dl 0
loc 17
rs 9.9666
c 0
b 0
f 0
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
 * Suspensionshandler class.
31
 * This class provides simple mechanism for Suspensions object
32
 */
33
class SuspensionsHandler extends XoopsPersistableObjectHandler
34
{
35
    public Helper $helper;
36
    public        $isAdmin;
37
38
    /**
39
     * Constructor
40
     * @param \XoopsDatabase|null             $xoopsDatabase
41
     * @param \XoopsModules\Suico\Helper|null $helper
42
     */
43
    public function __construct(
44
        ?XoopsDatabase $xoopsDatabase = null,
45
        $helper = null
46
    ) {
47
        /** @var \XoopsModules\Suico\Helper $this- >helper */
48
        if (null === $helper) {
49
            $this->helper = Helper::getInstance();
50
        } else {
51
            $this->helper = $helper;
52
        }
53
        $this->isAdmin = $this->helper->isUserAdmin();
54
        parent::__construct($xoopsDatabase, 'suico_suspensions', Suspensions::class, 'uid', 'uid');
55
    }
56
57
    /**
58
     * create a new Groups
59
     *
60
     * @param bool $isNew flag the new objects as "new"?
61
     * @return \XoopsObject Groups
62
     */
63
    public function create(
64
        $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 Suspensions
79
     *
80
     * @param int|null $id of the Suspensions
81
     * @param null     $fields
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $fields is correct as it would always require null to be passed?
Loading history...
82
     * @return false|\XoopsModules\Suico\Suspensions reference to the {@link Suspensions} object, FALSE if failed
83
     */
84
    public function get2(
85
        $id = null,
86
        $fields = null
0 ignored issues
show
Unused Code introduced by
The parameter $fields is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

86
        /** @scrutinizer ignore-unused */ $fields = null

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
87
    ) {
88
        $sql = 'SELECT * FROM ' . $this->db->prefix('suico_suspensions') . ' WHERE uid=' . $id;
89
        if (!$result = $this->db->query($sql)) {
90
            return false;
91
        }
92
        $numrows = $this->db->getRowsNum($result);
93
        if (1 === $numrows) {
94
            $suspensions = new Suspensions();
95
            $suspensions->assignVars($this->db->fetchArray($result));
96
97
            return $suspensions;
98
        }
99
100
        return false;
101
    }
102
103
    /**
104
     * insert a new Suspensions in the database
105
     *
106
     * @param \XoopsObject $object        reference to the {@link Suspensions}
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 Suspensions) {
117
            return false;
118
        }
119
        if (!$object->isDirty()) {
120
            return true;
121
        }
122
        if (!$object->cleanVars()) {
123
            return false;
124
        }
125
        $suspension_time = '';
126
        foreach ($object->cleanVars as $k => $v) {
127
            ${$k} = $v;
128
        }
129
        //        $now = 'date_add(now(), interval ' . $xoopsConfig['server_TZ'] . ' hour)';
130
        $uid = 0;
131
        if ($object->isNew()) {
132
            // ajout/modification d'un Suspensions
133
            $object = new Suspensions();
134
            $format      = 'INSERT INTO %s (uid, old_pass, old_email, old_signature, suspension_time)';
135
            $format      .= 'VALUES (%u, %s, %s, %s, %u)';
136
            $sql         = \sprintf(
137
                $format,
138
                $this->db->prefix('suico_suspensions'),
139
                $uid,
140
                $this->db->quoteString($old_pass),
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $old_pass seems to be never defined.
Loading history...
141
                $this->db->quoteString($old_email),
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $old_email seems to be never defined.
Loading history...
142
                $this->db->quoteString($old_signature),
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $old_signature seems to be never defined.
Loading history...
143
                $suspension_time
144
            );
145
            $force       = true;
146
        } else {
147
            $format = 'UPDATE %s SET ';
148
            $format .= 'uid=%u, old_pass=%s, old_email=%s, old_signature=%s, suspension_time=%u';
149
            $format .= ' WHERE uid = %u';
150
            $sql    = \sprintf(
151
                $format,
152
                $this->db->prefix('suico_suspensions'),
153
                $uid,
154
                $this->db->quoteString($old_pass),
155
                $this->db->quoteString($old_email),
156
                $this->db->quoteString($old_signature),
157
                $suspension_time,
158
                $uid
159
            );
160
        }
161
        if ($force) {
162
            $result = $this->db->queryF($sql);
163
        } else {
164
            $result = $this->db->query($sql);
165
        }
166
        if (!$result) {
167
            return false;
168
        }
169
        if (empty($uid)) {
0 ignored issues
show
introduced by
The condition empty($uid) is always true.
Loading history...
170
            $uid = $this->db->getInsertId();
171
        }
172
        $object->assignVar('uid', $uid);
173
174
        return true;
175
    }
176
177
    /**
178
     * delete a Suspensions from the database
179
     *
180
     * @param \XoopsObject $object reference to the Suspensions to delete
181
     * @param bool         $force
182
     * @return bool FALSE if failed.
183
     */
184
    public function delete(
185
        XoopsObject $object,
186
        $force = false
187
    ) {
188
        if (!$object instanceof Suspensions) {
189
            return false;
190
        }
191
        $sql = \sprintf(
192
            'DELETE FROM %s WHERE uid = %u',
193
            $this->db->prefix('suico_suspensions'),
194
            (int)$object->getVar('uid')
195
        );
196
        if ($force) {
197
            $result = $this->db->queryF($sql);
198
        } else {
199
            $result = $this->db->query($sql);
200
        }
201
        if (!$result) {
202
            return false;
203
        }
204
205
        return true;
206
    }
207
208
    /**
209
     * retrieve suico_suspensionss from the database
210
     *
211
     * @param \CriteriaElement|\CriteriaCompo|null $criteria {@link \CriteriaElement} conditions to be met
212
     * @param bool                                 $id_as_key       use the UID as key for the array?
213
     * @param bool                                 $as_object
214
     * @return array array of {@link Suspensions} objects
215
     */
216
    public function &getObjects(
217
        ?CriteriaElement $criteria = null,
218
        $id_as_key = false,
219
        $as_object = true
220
    ) {
221
        $ret   = [];
222
        $start = 0;
223
        $limit = 0;
224
        $sql   = 'SELECT * FROM ' . $this->db->prefix('suico_suspensions');
225
        if (isset($criteria) && is_subclass_of($criteria, 'CriteriaElement')) {
226
            $sql .= ' ' . $criteria->renderWhere();
0 ignored issues
show
Bug introduced by
The method renderWhere() does not exist on CriteriaElement. Did you maybe mean render()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

226
            $sql .= ' ' . $criteria->/** @scrutinizer ignore-call */ renderWhere();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
227
            if ('' !== $criteria->getSort()) {
228
                $sql .= ' ORDER BY ' . $criteria->getSort() . ' ' . $criteria->getOrder();
229
            }
230
            $limit = $criteria->getLimit();
231
            $start = $criteria->getStart();
232
        }
233
        $result = $this->db->query($sql, $limit, $start);
234
        if (!$result) {
235
            return $ret;
236
        }
237
        while (false !== ($myrow = $this->db->fetchArray($result))) {
238
            $suspensions = new Suspensions();
239
            $suspensions->assignVars($myrow);
240
            if ($id_as_key) {
241
                $ret[$myrow['uid']] = &$suspensions;
242
            } else {
243
                $ret[] = &$suspensions;
244
            }
245
            unset($suspensions);
246
        }
247
248
        return $ret;
249
    }
250
251
    /**
252
     * count suico_suspensionss matching a condition
253
     *
254
     * @param \CriteriaElement|\CriteriaCompo|null $criteria {@link \CriteriaElement} to match
255
     * @return int count of suico_suspensionss
256
     */
257
    public function getCount(
258
        ?CriteriaElement $criteria = null
259
    ) {
260
        $sql = 'SELECT COUNT(*) FROM ' . $this->db->prefix('suico_suspensions');
261
        if (isset($criteria) && is_subclass_of($criteria, 'CriteriaElement')) {
262
            $sql .= ' ' . $criteria->renderWhere();
263
        }
264
        $result = $this->db->query($sql);
265
        if (!$result) {
266
            return 0;
267
        }
268
        [$count] = $this->db->fetchRow($result);
269
270
        return (int)$count;
271
    }
272
273
    /**
274
     * delete suico_suspensionss matching a set of conditions
275
     *
276
     * @param \CriteriaElement|\CriteriaCompo|null $criteria {@link \CriteriaElement}
277
     * @param bool                                 $force
278
     * @param bool                                 $asObject
279
     * @return bool FALSE if deletion failed
280
     */
281
    public function deleteAll(
282
        ?CriteriaElement $criteria = null,
283
        $force = true,
284
        $asObject = false
285
    ) {
286
        $sql = 'DELETE FROM ' . $this->db->prefix('suico_suspensions');
287
        if (isset($criteria) && is_subclass_of($criteria, 'CriteriaElement')) {
288
            $sql .= ' ' . $criteria->renderWhere();
289
        }
290
        if (!$result = $this->db->queryF($sql)) {
0 ignored issues
show
Unused Code introduced by
The assignment to $result is dead and can be removed.
Loading history...
291
            return false;
292
        }
293
294
        return true;
295
    }
296
}
297