ConfigsHandler::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
 * ConfigsHandler class.
31
 * This class provides simple mechanism for Configs object
32
 */
33
class ConfigsHandler 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_configs', Configs::class, 'config_id', 'config_id');
55
    }
56
57
    /**
58
     * @param bool $isNew
59
     *
60
     * @return \XoopsObject
61
     */
62
    public function create($isNew = true)
63
    {
64
        $obj = parent::create($isNew);
65
        if ($isNew) {
66
            $obj->setNew();
67
        } else {
68
            $obj->unsetNew();
69
        }
70
        $obj->helper = $this->helper;
71
72
        return $obj;
73
    }
74
75
    /**
76
     * retrieve a Configs
77
     *
78
     * @param int|null $id of the Configs
79
     * @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...
80
     * @return false|\XoopsModules\Suico\Configs reference to the {@link Configs} object, FALSE if failed
81
     */
82
    public function get2(
83
        $id = null,
84
        $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

84
        /** @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...
85
    ) {
86
        $sql = 'SELECT * FROM ' . $this->db->prefix('suico_configs') . ' WHERE config_id=' . $id;
87
        if (!$result = $this->db->query($sql)) {
88
            return false;
89
        }
90
        $numrows = $this->db->getRowsNum($result);
91
        if (1 === $numrows) {
92
            $suico_configs = new Configs();
93
            $suico_configs->assignVars($this->db->fetchArray($result));
94
95
            return $suico_configs;
96
        }
97
98
        return false;
99
    }
100
101
    /**
102
     * insert a new Configs in the database
103
     *
104
     * @param \XoopsObject $object    reference to the {@link Configs}
105
     *                                     object
106
     * @param bool         $force
107
     * @return bool FALSE if failed, TRUE if already present and unchanged or successful
108
     */
109
    public function insert2(
110
        XoopsObject $object,
111
        $force = false
112
    ) {
113
        global $xoopsConfig;
114
        if (!$object instanceof Configs) {
115
            return false;
116
        }
117
        if (!$object->isDirty()) {
118
            return true;
119
        }
120
        if (!$object->cleanVars()) {
121
            return false;
122
        }
123
        foreach ($object->cleanVars as $k => $v) {
124
            ${$k} = $v;
125
        }
126
        $now = 'date_add(now(), interval ' . $xoopsConfig['server_TZ'] . ' hour)';
0 ignored issues
show
Unused Code introduced by
The assignment to $now is dead and can be removed.
Loading history...
127
        if ($object->isNew()) {
128
            // addition / modification of a Configs
129
            //            $config_id = null;
130
            $object = new Configs();
131
            $format      = 'INSERT INTO %s (config_id, config_uid, pictures, audio, videos, groups, notes, friends, profile_contact, profile_general, profile_stats, suspension, backup_password, backup_email, end_suspension)';
132
            $format      .= 'VALUES (%u, %u, %u, %u, %u, %u, %u, %u, %u, %u, %u, %u, %s, %s, %s)';
133
            $sql         = \sprintf(
134
                $format,
135
                $this->db->prefix('suico_configs'),
136
                $config_id,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $config_id seems to be never defined.
Loading history...
137
                $config_uid,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $config_uid does not exist. Did you maybe mean $config_id?
Loading history...
138
                $pictures,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $pictures seems to be never defined.
Loading history...
139
                $audio,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $audio seems to be never defined.
Loading history...
140
                $videos,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $videos seems to be never defined.
Loading history...
141
                $groups,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $groups seems to be never defined.
Loading history...
142
                $notes,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $notes seems to be never defined.
Loading history...
143
                $friends,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $friends seems to be never defined.
Loading history...
144
                $profile_contact,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $profile_contact seems to be never defined.
Loading history...
145
                $profile_general,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $profile_general seems to be never defined.
Loading history...
146
                $profile_stats,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $profile_stats seems to be never defined.
Loading history...
147
                $suspension,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $suspension seems to be never defined.
Loading history...
148
                $this->db->quoteString($backup_password),
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $backup_password seems to be never defined.
Loading history...
149
                $this->db->quoteString($backup_email),
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $backup_email seems to be never defined.
Loading history...
150
                $this->db->quoteString($end_suspension)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $end_suspension seems to be never defined.
Loading history...
151
            );
152
            $force       = true;
153
        } else {
154
            $format = 'UPDATE %s SET ';
155
            $format .= 'config_id=%u, config_uid=%u, pictures=%u, audio=%u, videos=%u, groups=%u, notes=%u, friends=%u, profile_contact=%u, profile_general=%u, profile_stats=%u, suspension=%u, backup_password=%s, backup_email=%s, end_suspension=%s';
156
            $format .= ' WHERE config_id = %u';
157
            $sql    = \sprintf(
158
                $format,
159
                $this->db->prefix('suico_configs'),
160
                $config_id,
161
                $config_uid,
162
                $pictures,
163
                $audio,
164
                $videos,
165
                $groups,
166
                $notes,
167
                $friends,
168
                $profile_contact,
169
                $profile_general,
170
                $profile_stats,
171
                $suspension,
172
                $this->db->quoteString($backup_password),
173
                $this->db->quoteString($backup_email),
174
                $this->db->quoteString($end_suspension),
175
                $config_id
176
            );
177
        }
178
        if ($force) {
179
            $result = $this->db->queryF($sql);
180
        } else {
181
            $result = $this->db->query($sql);
182
        }
183
        if (!$result) {
184
            return false;
185
        }
186
        if (empty($config_id)) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $config_id seems to never exist and therefore empty should always be true.
Loading history...
187
            $config_id = $this->db->getInsertId();
188
        }
189
        $object->assignVar('config_id', $config_id);
190
191
        return true;
192
    }
193
194
    /**
195
     * delete a Configs from the database
196
     *
197
     * @param \XoopsObject $object reference to the Configs to delete
198
     * @param bool         $force
199
     * @return bool FALSE if failed.
200
     */
201
    public function delete(
202
        XoopsObject $object,
203
        $force = false
204
    ) {
205
        if (!$object instanceof Configs) {
206
            return false;
207
        }
208
        $sql = \sprintf(
209
            'DELETE FROM %s WHERE config_id = %u',
210
            $this->db->prefix('suico_configs'),
211
            (int)$object->getVar('config_id')
212
        );
213
        if ($force) {
214
            $result = $this->db->queryF($sql);
215
        } else {
216
            $result = $this->db->query($sql);
217
        }
218
        if (!$result) {
219
            return false;
220
        }
221
222
        return true;
223
    }
224
225
    /**
226
     * retrieve suico_configs from the database
227
     *
228
     * @param \CriteriaElement|\CriteriaCompo|null $criteria {@link \CriteriaElement} conditions to be met
229
     * @param bool                                 $id_as_key       use the UID as key for the array?
230
     * @param bool                                 $as_object
231
     * @return array array of {@link Configs} objects
232
     */
233
    public function &getObjects(
234
        ?CriteriaElement $criteria = null,
235
        $id_as_key = false,
236
        $as_object = true
237
    ) {
238
        $ret   = [];
239
        $start = 0;
240
        $limit = 0;
241
        $sql   = 'SELECT * FROM ' . $this->db->prefix('suico_configs');
242
        if (isset($criteria) && is_subclass_of($criteria, 'CriteriaElement')) {
243
            $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

243
            $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...
244
            if ('' !== $criteria->getSort()) {
245
                $sql .= ' ORDER BY ' . $criteria->getSort() . ' ' . $criteria->getOrder();
246
            }
247
            $limit = $criteria->getLimit();
248
            $start = $criteria->getStart();
249
        }
250
        $result = $this->db->query($sql, $limit, $start);
251
        if (!$result) {
252
            return $ret;
253
        }
254
        while (false !== ($myrow = $this->db->fetchArray($result))) {
255
            $suico_configs = new Configs();
256
            $suico_configs->assignVars($myrow);
257
            if ($id_as_key) {
258
                $ret[$myrow['config_id']] = &$suico_configs;
259
            } else {
260
                $ret[] = &$suico_configs;
261
            }
262
            unset($suico_configs);
263
        }
264
265
        return $ret;
266
    }
267
268
    /**
269
     * count suico_configs matching a condition
270
     *
271
     * @param \CriteriaElement|\CriteriaCompo|null $criteria {@link \CriteriaElement} to match
272
     * @return int count of suico_configs
273
     */
274
    public function getCount(
275
        ?CriteriaElement $criteria = null
276
    ) {
277
        $sql = 'SELECT COUNT(*) FROM ' . $this->db->prefix('suico_configs');
278
        if (isset($criteria) && is_subclass_of($criteria, 'CriteriaElement')) {
279
            $sql .= ' ' . $criteria->renderWhere();
280
        }
281
        $result = $this->db->query($sql);
282
        if (!$result) {
283
            return 0;
284
        }
285
        [$count] = $this->db->fetchRow($result);
286
287
        return (int)$count;
288
    }
289
290
    /**
291
     * delete suico_configs matching a set of conditions
292
     *
293
     * @param \CriteriaElement|\CriteriaCompo|null $criteria {@link \CriteriaElement}
294
     * @param bool                                 $force
295
     * @param bool                                 $asObject
296
     * @return bool FALSE if deletion failed
297
     */
298
    public function deleteAll(
299
        ?CriteriaElement $criteria = null,
300
        $force = true,
301
        $asObject = false
302
    ) {
303
        $sql = 'DELETE FROM ' . $this->db->prefix('suico_configs');
304
        if (isset($criteria) && is_subclass_of($criteria, 'CriteriaElement')) {
305
            $sql .= ' ' . $criteria->renderWhere();
306
        }
307
        if (!$result = $this->db->query($sql)) {
0 ignored issues
show
Unused Code introduced by
The assignment to $result is dead and can be removed.
Loading history...
308
            return false;
309
        }
310
311
        return true;
312
    }
313
}
314