Passed
Push — master ( 95e0c4...fc8ab0 )
by
unknown
44s queued 15s
created

class/ConfigsHandler.php (1 issue)

Labels
Severity
1
<?php
2
3
declare(strict_types=1);
4
5
namespace XoopsModules\Suico;
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         suico
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
 * ConfigsHandler class.
34
 * This class provides simple mechanism for Configs object
35
 */
36
class ConfigsHandler extends XoopsPersistableObjectHandler
37
{
38
    public $helper;
39
    public $isAdmin;
40
41
    /**
42
     * Constructor
43
     * @param \XoopsDatabase|null             $xoopsDatabase
44
     * @param \XoopsModules\Suico\Helper|null $helper
45
     */
46
    public function __construct(
47
        ?XoopsDatabase $xoopsDatabase = null,
48
        $helper = null
49
    ) {
50
        /** @var \XoopsModules\Suico\Helper $this ->helper */
51
        if (null === $helper) {
52
            $this->helper = Helper::getInstance();
0 ignored issues
show
The property helper does not seem to exist on XoopsModules\Suico\Helper.
Loading history...
53
        } else {
54
            $this->helper = $helper;
55
        }
56
        $isAdmin = $this->helper->isUserAdmin();
57
        parent::__construct($xoopsDatabase, 'suico_configs', Configs::class, 'config_id', 'config_id');
58
    }
59
60
    /**
61
     * @param bool $isNew
62
     *
63
     * @return \XoopsObject
64
     */
65
    public function create($isNew = true)
66
    {
67
        $obj = parent::create($isNew);
68
        if ($isNew) {
69
            $obj->setNew();
70
        } else {
71
            $obj->unsetNew();
72
        }
73
        $obj->helper = $this->helper;
74
        return $obj;
75
    }
76
77
    /**
78
     * retrieve a Configs
79
     *
80
     * @param int  $id of the Configs
81
     * @param null $fields
82
     * @return mixed reference to the {@link Configs} object, FALSE if failed
83
     */
84
    public function get2(
85
        $id = null,
86
        $fields = null
87
    ) {
88
        $sql = 'SELECT * FROM ' . $this->db->prefix('suico_configs') . ' WHERE config_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_configs = new Configs();
95
            $suico_configs->assignVars($this->db->fetchArray($result));
96
            return $suico_configs;
97
        }
98
        return false;
99
    }
100
101
    /**
102
     * insert a new Configs in the database
103
     *
104
     * @param \XoopsObject $xoopsObject    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 $xoopsObject,
111
        $force = false
112
    ) {
113
        global $xoopsConfig;
114
        if (!$xoopsObject instanceof Configs) {
115
            return false;
116
        }
117
        if (!$xoopsObject->isDirty()) {
118
            return true;
119
        }
120
        if (!$xoopsObject->cleanVars()) {
121
            return false;
122
        }
123
        foreach ($xoopsObject->cleanVars as $k => $v) {
124
            ${$k} = $v;
125
        }
126
        $now = 'date_add(now(), interval ' . $xoopsConfig['server_TZ'] . ' hour)';
127
        if ($xoopsObject->isNew()) {
128
            // addition / modification of a Configs
129
            //            $config_id = null;
130
            $xoopsObject = 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,
137
                $config_uid,
138
                $pictures,
139
                $audio,
140
                $videos,
141
                $groups,
142
                $notes,
143
                $friends,
144
                $profile_contact,
145
                $profile_general,
146
                $profile_stats,
147
                $suspension,
148
                $this->db->quoteString($backup_password),
149
                $this->db->quoteString($backup_email),
150
                $this->db->quoteString($end_suspension)
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)) {
187
            $config_id = $this->db->getInsertId();
188
        }
189
        $xoopsObject->assignVar('config_id', $config_id);
190
        return true;
191
    }
192
193
    /**
194
     * delete a Configs from the database
195
     *
196
     * @param \XoopsObject $xoopsObject reference to the Configs 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 Configs) {
205
            return false;
206
        }
207
        $sql = \sprintf(
208
            'DELETE FROM %s WHERE config_id = %u',
209
            $this->db->prefix('suico_configs'),
210
            $xoopsObject->getVar('config_id')
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
        return true;
221
    }
222
223
    /**
224
     * retrieve suico_configs from the database
225
     *
226
     * @param \CriteriaElement|\CriteriaCompo|null $criteriaElement {@link \CriteriaElement} conditions to be met
227
     * @param bool                                 $id_as_key       use the UID as key for the array?
228
     * @param bool                                 $as_object
229
     * @return array array of {@link Configs} objects
230
     */
231
    public function &getObjects(
232
        ?CriteriaElement $criteriaElement = null,
233
        $id_as_key = false,
234
        $as_object = true
235
    ) {
236
        $ret   = [];
237
        $limit = $start = 0;
238
        $sql   = 'SELECT * FROM ' . $this->db->prefix('suico_configs');
239
        if (isset($criteriaElement) && $criteriaElement instanceof CriteriaElement) {
240
            $sql .= ' ' . $criteriaElement->renderWhere();
241
            if ('' !== $criteriaElement->getSort()) {
242
                $sql .= ' ORDER BY ' . $criteriaElement->getSort() . ' ' . $criteriaElement->getOrder();
243
            }
244
            $limit = $criteriaElement->getLimit();
245
            $start = $criteriaElement->getStart();
246
        }
247
        $result = $this->db->query($sql, $limit, $start);
248
        if (!$result) {
249
            return $ret;
250
        }
251
        while (false !== ($myrow = $this->db->fetchArray($result))) {
252
            $suico_configs = new Configs();
253
            $suico_configs->assignVars($myrow);
254
            if (!$id_as_key) {
255
                $ret[] = &$suico_configs;
256
            } else {
257
                $ret[$myrow['config_id']] = &$suico_configs;
258
            }
259
            unset($suico_configs);
260
        }
261
        return $ret;
262
    }
263
264
    /**
265
     * count suico_configs matching a condition
266
     *
267
     * @param \CriteriaElement|\CriteriaCompo|null $criteriaElement {@link \CriteriaElement} to match
268
     * @return int count of suico_configs
269
     */
270
    public function getCount(
271
        ?CriteriaElement $criteriaElement = null
272
    ) {
273
        $sql = 'SELECT COUNT(*) FROM ' . $this->db->prefix('suico_configs');
274
        if (isset($criteriaElement) && $criteriaElement instanceof CriteriaElement) {
275
            $sql .= ' ' . $criteriaElement->renderWhere();
276
        }
277
        $result = $this->db->query($sql);
278
        if (!$result) {
279
            return 0;
280
        }
281
        [$count] = $this->db->fetchRow($result);
282
        return (int)$count;
283
    }
284
285
    /**
286
     * delete suico_configs matching a set of conditions
287
     *
288
     * @param \CriteriaElement|\CriteriaCompo|null $criteriaElement {@link \CriteriaElement}
289
     * @param bool                                 $force
290
     * @param bool                                 $asObject
291
     * @return bool FALSE if deletion failed
292
     */
293
    public function deleteAll(
294
        ?CriteriaElement $criteriaElement = null,
295
        $force = true,
296
        $asObject = false
297
    ) {
298
        $sql = 'DELETE FROM ' . $this->db->prefix('suico_configs');
299
        if (isset($criteriaElement) && $criteriaElement instanceof CriteriaElement) {
300
            $sql .= ' ' . $criteriaElement->renderWhere();
301
        }
302
        if (!$result = $this->db->query($sql)) {
303
            return false;
304
        }
305
        return true;
306
    }
307
}
308