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

class/FriendrequestHandler.php (5 issues)

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
// Friendrequest.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
// ------------------Friendrequest user handler class -------------------
40
// -------------------------------------------------------------------------
41
42
/**
43
 * yogurt_friendrequesthandler class.
44
 * This class provides simple mecanisme for Friendrequest object
45
 */
46
class FriendrequestHandler 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_friendrequests', Friendrequest::class, 'friendreq_id', 'friendreq_id');
69
    }
70
71
    /**
72
     * @param bool $isNew
73
     *
74
     * @return \XoopsObject
75
     */
76
    public function create($isNew = true)
77
    {
78
        $obj = parent::create($isNew);
79
        if ($isNew) {
80
            $obj->setNew();
81
        } else {
82
            $obj->unsetNew();
83
        }
84
        $obj->helper = $this->helper;
85
86
        return $obj;
87
    }
88
89
    /**
90
     * retrieve a Friendrequest
91
     *
92
     * @param int  $id of the Friendrequest
93
     * @param null $fields
94
     * @return mixed reference to the {@link Friendrequest} object, FALSE if failed
95
     */
96
    public function get2(
97
        $id = null,
98
        $fields = null
99
    ) {
100
        $sql = 'SELECT * FROM ' . $this->db->prefix('yogurt_friendrequests') . ' WHERE friendreq_id=' . $id;
101
        if (!$result = $this->db->query($sql)) {
102
            return false;
103
        }
104
        $numrows = $this->db->getRowsNum($result);
105
        if (1 === $numrows) {
106
            $yogurt_friendrequest = new Friendrequest();
107
            $yogurt_friendrequest->assignVars($this->db->fetchArray($result));
108
109
            return $yogurt_friendrequest;
110
        }
111
112
        return false;
113
    }
114
115
    /**
116
     * insert a new Friendrequest in the database
117
     *
118
     * @param \XoopsObject $xoopsObject           reference to the {@link Friendrequest}
119
     *                                            object
120
     * @param bool         $force
121
     * @return bool FALSE if failed, TRUE if already present and unchanged or successful
122
     */
123
    public function insert2(
124
        XoopsObject $xoopsObject,
125
        $force = false
126
    ) {
127
        global $xoopsConfig;
128
        if (!$xoopsObject instanceof Friendrequest) {
129
            return false;
130
        }
131
        if (!$xoopsObject->isDirty()) {
132
            return true;
133
        }
134
        if (!$xoopsObject->cleanVars()) {
135
            return false;
136
        }
137
        foreach ($xoopsObject->cleanVars as $k => $v) {
138
            ${$k} = $v;
139
        }
140
        $now = 'date_add(now(), interval ' . $xoopsConfig['server_TZ'] . ' hour)';
141
        if ($xoopsObject->isNew()) {
142
            // ajout/modification d'un Friendrequest
143
            $xoopsObject = new Friendrequest();
144
            $format      = 'INSERT INTO %s (friendreq_id, friendrequester_uid, friendrequestto_uid, date_created)';
145
            $format      .= 'VALUES (%u, %u, %u, %u)';
146
            $sql         = \sprintf(
147
                $format,
148
                $this->db->prefix('yogurt_friendrequests'),
149
                $friendreq_id,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $friendreq_id seems to be never defined.
Loading history...
150
                $friendrequester_uid,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $friendrequester_uid does not exist. Did you maybe mean $friendreq_id?
Loading history...
151
                $friendrequestto_uid,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $friendrequestto_uid does not exist. Did you maybe mean $friendreq_id?
Loading history...
152
                $date_created
153
            );
154
            $force       = true;
155
        } else {
156
            $format = 'UPDATE %s SET ';
157
            $format .= 'friendreq_id=%u, friendrequester_uid=%u, friendrequestto_uid=%u, date_created=%u';
158
            $format .= ' WHERE friendreq_id = %u';
159
            $sql    = \sprintf(
160
                $format,
161
                $this->db->prefix('yogurt_friendrequests'),
162
                $friendreq_id,
163
                $friendrequester_uid,
164
                $friendrequestto_uid,
165
                $date_created,
166
                $friendreq_id
167
            );
168
        }
169
        if ($force) {
170
            $result = $this->db->queryF($sql);
171
        } else {
172
            $result = $this->db->query($sql);
173
        }
174
        if (!$result) {
175
            return false;
176
        }
177
        if (empty($friendreq_id)) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $friendreq_id seems to never exist and therefore empty should always be true.
Loading history...
178
            $friendreq_id = $this->db->getInsertId();
179
        }
180
        $xoopsObject->assignVar('friendreq_id', $friendreq_id);
181
182
        return true;
183
    }
184
185
    /**
186
     * delete a Friendrequest from the database
187
     *
188
     * @param \XoopsObject $xoopsObject reference to the Friendrequest to delete
189
     * @param bool         $force
190
     * @return bool FALSE if failed.
191
     */
192
    public function delete(
193
        XoopsObject $xoopsObject,
194
        $force = false
195
    ) {
196
        if (!$xoopsObject instanceof Friendrequest) {
197
            return false;
198
        }
199
        $sql = \sprintf(
200
            'DELETE FROM %s WHERE friendreq_id = %u',
201
            $this->db->prefix('yogurt_friendrequests'),
202
            $xoopsObject->getVar('friendreq_id')
0 ignored issues
show
It seems like $xoopsObject->getVar('friendreq_id') can also be of type array and array; however, parameter $args of sprintf() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

202
            /** @scrutinizer ignore-type */ $xoopsObject->getVar('friendreq_id')
Loading history...
203
        );
204
        if ($force) {
205
            $result = $this->db->queryF($sql);
206
        } else {
207
            $result = $this->db->query($sql);
208
        }
209
        if (!$result) {
210
            return false;
211
        }
212
213
        return true;
214
    }
215
216
    /**
217
     * retrieve yogurt_friendrequests from the database
218
     *
219
     * @param \CriteriaElement|\CriteriaCompo|null $criteriaElement {@link \CriteriaElement} conditions to be met
220
     * @param bool                                 $id_as_key       use the UID as key for the array?
221
     * @param bool                                 $as_object
222
     * @return array array of {@link Friendrequest} objects
223
     */
224
    public function &getObjects(
225
        ?CriteriaElement $criteriaElement = null,
226
        $id_as_key = false,
227
        $as_object = true
228
    ) {
229
        $ret   = [];
230
        $limit = $start = 0;
231
        $sql   = 'SELECT * FROM ' . $this->db->prefix('yogurt_friendrequests');
232
        if (isset($criteriaElement) && $criteriaElement instanceof CriteriaElement) {
233
            $sql .= ' ' . $criteriaElement->renderWhere();
234
            if ('' !== $criteriaElement->getSort()) {
235
                $sql .= ' ORDER BY ' . $criteriaElement->getSort() . ' ' . $criteriaElement->getOrder();
236
            }
237
            $limit = $criteriaElement->getLimit();
238
            $start = $criteriaElement->getStart();
239
        }
240
        $result = $this->db->query($sql, $limit, $start);
241
        if (!$result) {
242
            return $ret;
243
        }
244
        while (false !== ($myrow = $this->db->fetchArray($result))) {
245
            $yogurt_friendrequest = new Friendrequest();
246
            $yogurt_friendrequest->assignVars($myrow);
247
            if (!$id_as_key) {
248
                $ret[] = &$yogurt_friendrequest;
249
            } else {
250
                $ret[$myrow['friendreq_id']] = &$yogurt_friendrequest;
251
            }
252
            unset($yogurt_friendrequest);
253
        }
254
255
        return $ret;
256
    }
257
258
    /**
259
     * count yogurt_friendrequests matching a condition
260
     *
261
     * @param \CriteriaElement|\CriteriaCompo|null $criteriaElement {@link \CriteriaElement} to match
262
     * @return int count of yogurt_friendrequests
263
     */
264
    public function getCount(
265
        ?CriteriaElement $criteriaElement = null
266
    ) {
267
        $sql = 'SELECT COUNT(*) FROM ' . $this->db->prefix('yogurt_friendrequests');
268
        if (isset($criteriaElement) && $criteriaElement instanceof CriteriaElement) {
269
            $sql .= ' ' . $criteriaElement->renderWhere();
270
        }
271
        $result = $this->db->query($sql);
272
        if (!$result) {
273
            return 0;
274
        }
275
        [$count] = $this->db->fetchRow($result);
276
277
        return (int)$count;
278
    }
279
280
    /**
281
     * delete yogurt_friendrequests matching a set of conditions
282
     *
283
     * @param \CriteriaElement|\CriteriaCompo|null $criteriaElement {@link \CriteriaElement}
284
     * @param bool                                 $force
285
     * @param bool                                 $asObject
286
     * @return bool FALSE if deletion failed
287
     */
288
    public function deleteAll(
289
        ?CriteriaElement $criteriaElement = null,
290
        $force = true,
291
        $asObject = false
292
    ) {
293
        $sql = 'DELETE FROM ' . $this->db->prefix('yogurt_friendrequests');
294
        if (isset($criteriaElement) && $criteriaElement instanceof CriteriaElement) {
295
            $sql .= ' ' . $criteriaElement->renderWhere();
296
        }
297
        if (!$result = $this->db->query($sql)) {
298
            return false;
299
        }
300
301
        return true;
302
    }
303
}
304