XoopsPrivmessageHandler::delete()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 12
rs 10
c 0
b 0
f 0
cc 3
nc 3
nop 1
1
<?php
2
/**
3
 * XOOPS Kernel Class
4
 *
5
 * You may not change or alter any portion of this comment or credits
6
 * of supporting developers from this source code or any supporting source code
7
 * which is considered copyrighted (c) material of the original comment or credit authors.
8
 * This program is distributed in the hope that it will be useful,
9
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11
 *
12
 * @copyright       (c) 2000-2025 XOOPS Project (https://xoops.org)
13
 * @license             GNU GPL 2 (https://www.gnu.org/licenses/gpl-2.0.html)
14
 * @package             kernel
15
 * @since               2.0.0
16
 * @author              Kazumi Ono (AKA onokazu) http://www.myweb.ne.jp/, http://jp.xoops.org/
17
 */
18
defined('XOOPS_ROOT_PATH') || exit('Restricted access');
19
20
/**
21
 * Private Messages
22
 *
23
 * @author              Kazumi Ono <[email protected]>
24
 * @copyright       (c) 2000-2025 XOOPS Project (https://xoops.org)
25
 *
26
 * @package             kernel
27
 **/
28
class XoopsPrivmessage extends XoopsObject
29
{
30
    //PHP 8.2 Dynamic properties deprecated
31
    public $msg_id;
32
    public $msg_image;
33
    public $subject;
34
    public $from_userid;
35
    public $to_userid;
36
    public $msg_time;
37
    public $msg_text;
38
    public $read_msg;
39
40
    /**
41
     * constructor
42
     **/
43
    public function __construct()
44
    {
45
        parent::__construct();
46
        $this->initVar('msg_id', XOBJ_DTYPE_INT, null, false);
47
        $this->initVar('msg_image', XOBJ_DTYPE_OTHER, null, false, 100);
48
        $this->initVar('subject', XOBJ_DTYPE_TXTBOX, null, true, 255);
49
        $this->initVar('from_userid', XOBJ_DTYPE_INT, null, true);
50
        $this->initVar('to_userid', XOBJ_DTYPE_INT, null, true);
51
        $this->initVar('msg_time', XOBJ_DTYPE_OTHER, null, false);
52
        $this->initVar('msg_text', XOBJ_DTYPE_TXTAREA, null, true);
53
        $this->initVar('read_msg', XOBJ_DTYPE_INT, 0, false);
54
    }
55
56
    /**
57
     * Returns Class Base Variable msg_id
58
     * @param string $format
59
     * @return mixed
60
     */
61
    public function id($format = 'N')
62
    {
63
        return $this->getVar('msg_id', $format);
64
    }
65
66
    /**
67
     * Returns Class Base Variable msg_id
68
     * @param string $format
69
     * @return mixed
70
     */
71
    public function msg_id($format = '')
72
    {
73
        return $this->getVar('msg_id', $format);
74
    }
75
76
    /**
77
     * Returns Class Base Variable msg_image
78
     * @param string $format
79
     * @return mixed
80
     */
81
    public function msg_image($format = '')
82
    {
83
        return $this->getVar('msg_image', $format);
84
    }
85
86
    /**
87
     * Returns Class Base Variable subject
88
     * @param string $format
89
     * @return mixed
90
     */
91
    public function subject($format = '')
92
    {
93
        return $this->getVar('subject', $format);
94
    }
95
96
    /**
97
     * Returns Class Base Variable not_id
98
     * @param string $format
99
     * @return mixed
100
     */
101
    public function from_userid($format = '')
102
    {
103
        return $this->getVar('from_userid', $format);
104
    }
105
106
    /**
107
     * Returns Class Base Variable to_userid
108
     * @param string $format
109
     * @return mixed
110
     */
111
    public function to_userid($format = '')
112
    {
113
        return $this->getVar('to_userid', $format);
114
    }
115
116
    /**
117
     * Returns Class Base Variable msg_time
118
     * @param string $format
119
     * @return mixed
120
     */
121
    public function msg_time($format = '')
122
    {
123
        return $this->getVar('msg_time', $format);
124
    }
125
126
    /**
127
     * Returns Class Base Variable msg_text
128
     * @param string $format
129
     * @return mixed
130
     */
131
    public function msg_text($format = '')
132
    {
133
        return $this->getVar('msg_text', $format);
134
    }
135
136
    /**
137
     * Returns Class Base Variable read_msg
138
     * @param string $format
139
     * @return mixed
140
     */
141
    public function read_msg($format = '')
142
    {
143
        return $this->getVar('read_msg', $format);
144
    }
145
}
146
147
/**
148
 * XOOPS private message handler class.
149
 *
150
 * This class is responsible for providing data access mechanisms to the data source
151
 * of XOOPS private message class objects.
152
 *
153
 * @package             kernel
154
 *
155
 * @author              Kazumi Ono    <[email protected]>
156
 * @copyright       (c) 2000-2025 XOOPS Project (https://xoops.org)
157
 *
158
 *
159
 * @todo Why is this not a XoopsPersistableObjectHandler?
160
 */
161
class XoopsPrivmessageHandler extends XoopsObjectHandler
162
{
163
    /**
164
     * Create a new {@link XoopsPrivmessage} object
165
     * @param  bool $isNew Flag as "new"?
166
     * @return XoopsPrivmessage
167
     **/
168
    public function create($isNew = true)
169
    {
170
        $pm = new XoopsPrivmessage();
171
        if ($isNew) {
172
            $pm->setNew();
173
        }
174
175
        return $pm;
176
    }
177
178
    /**
179
     * Load a {@link XoopsPrivmessage} object
180
     * @param  int $id ID of the message
181
     * @return XoopsPrivmessage|false
182
     **/
183
    public function get($id)
184
    {
185
        $pm = false;
186
        $id = (int) $id;
187
        if ($id > 0) {
188
            $sql = 'SELECT * FROM ' . $this->db->prefix('priv_msgs') . ' WHERE msg_id=' . $id;
189
            $result = $this->db->query($sql);
0 ignored issues
show
Bug introduced by
The method query() does not exist on XoopsDatabase. Since it exists in all sub-types, consider adding an abstract or default implementation to XoopsDatabase. ( Ignorable by Annotation )

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

189
            /** @scrutinizer ignore-call */ 
190
            $result = $this->db->query($sql);
Loading history...
190
            if (!$this->db->isResultSet($result)) {
191
                return $pm;
192
            }
193
            $numrows = $this->db->getRowsNum($result);
0 ignored issues
show
Bug introduced by
The method getRowsNum() does not exist on XoopsDatabase. Since it exists in all sub-types, consider adding an abstract or default implementation to XoopsDatabase. ( Ignorable by Annotation )

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

193
            /** @scrutinizer ignore-call */ 
194
            $numrows = $this->db->getRowsNum($result);
Loading history...
194
            if ($numrows == 1) {
195
                $pm = new XoopsPrivmessage();
196
                $pm->assignVars($this->db->fetchArray($result));
0 ignored issues
show
Bug introduced by
The method fetchArray() does not exist on XoopsDatabase. Since it exists in all sub-types, consider adding an abstract or default implementation to XoopsDatabase. ( Ignorable by Annotation )

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

196
                $pm->assignVars($this->db->/** @scrutinizer ignore-call */ fetchArray($result));
Loading history...
197
            }
198
        }
199
200
        return $pm;
201
    }
202
203
    /**
204
     * Insert a message in the database
205
     *
206
     * @param  XoopsPrivmessage $pm    {@link XoopsPrivmessage} object
207
     * @param  bool   $force flag to force the query execution skip request method check, which might be required in some situations
208
     * @param  XoopsObject|XoopsPrivmessage $pm a XoopsMembership object
209
     *
210
     * @return bool true on success, otherwise false
211
     **/
212
    public function insert(XoopsObject $pm, $force = false)
213
    {
214
        $className = 'XoopsPrivmessage';
215
        if (!($pm instanceof $className)) {
216
            return false;
217
        }
218
219
        if (!$pm->isDirty()) {
220
            return true;
221
        }
222
        if (!$pm->cleanVars()) {
223
            return false;
224
        }
225
        foreach ($pm->cleanVars as $k => $v) {
226
            ${$k} = $v;
227
        }
228
        if ($pm->isNew()) {
229
            $msg_id = $this->db->genId('priv_msgs_msg_id_seq');
0 ignored issues
show
Bug introduced by
The method genId() does not exist on XoopsDatabase. Since it exists in all sub-types, consider adding an abstract or default implementation to XoopsDatabase. ( Ignorable by Annotation )

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

229
            /** @scrutinizer ignore-call */ 
230
            $msg_id = $this->db->genId('priv_msgs_msg_id_seq');
Loading history...
230
            $sql    = sprintf('INSERT INTO %s (msg_id, msg_image, subject, from_userid, to_userid, msg_time, msg_text, read_msg) VALUES (%u, %s, %s, %u, %u, %u, %s, %u)', $this->db->prefix('priv_msgs'), $msg_id, $this->db->quoteString($msg_image), $this->db->quoteString($subject), $from_userid, $to_userid, time(), $this->db->quoteString($msg_text), 0);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $from_userid seems to be never defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable $to_userid seems to be never defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable $msg_text seems to be never defined.
Loading history...
Bug introduced by
The method quoteString() does not exist on XoopsDatabase. Since it exists in all sub-types, consider adding an abstract or default implementation to XoopsDatabase. ( Ignorable by Annotation )

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

230
            $sql    = sprintf('INSERT INTO %s (msg_id, msg_image, subject, from_userid, to_userid, msg_time, msg_text, read_msg) VALUES (%u, %s, %s, %u, %u, %u, %s, %u)', $this->db->prefix('priv_msgs'), $msg_id, $this->db->/** @scrutinizer ignore-call */ quoteString($msg_image), $this->db->quoteString($subject), $from_userid, $to_userid, time(), $this->db->quoteString($msg_text), 0);
Loading history...
Comprehensibility Best Practice introduced by
The variable $subject seems to be never defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable $msg_image seems to be never defined.
Loading history...
231
        } else {
232
            $sql = sprintf('UPDATE %s SET msg_image = %s, subject = %s, from_userid = %u, to_userid = %u, msg_text = %s, read_msg = %u WHERE msg_id = %u', $this->db->prefix('priv_msgs'), $this->db->quoteString($msg_image), $this->db->quoteString($subject), $from_userid, $to_userid, $this->db->quoteString($msg_text), $read_msg, $msg_id);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $msg_id seems to be never defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable $read_msg seems to be never defined.
Loading history...
233
        }
234
        $queryFunc = empty($force) ? 'query' : 'queryF';
235
        if (!$result = $this->db->{$queryFunc}($sql)) {
0 ignored issues
show
Unused Code introduced by
The assignment to $result is dead and can be removed.
Loading history...
236
            return false;
237
        }
238
        if (empty($msg_id)) {
239
            $msg_id = $this->db->getInsertId();
0 ignored issues
show
Bug introduced by
The method getInsertId() does not exist on XoopsDatabase. Since it exists in all sub-types, consider adding an abstract or default implementation to XoopsDatabase. ( Ignorable by Annotation )

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

239
            /** @scrutinizer ignore-call */ 
240
            $msg_id = $this->db->getInsertId();
Loading history...
240
        }
241
        $pm->assignVar('msg_id', $msg_id);
242
243
        return true;
244
    }
245
246
    /**
247
     * Delete from the database
248
     * @param  XoopsPrivmessage $pm {@link XoopsPrivmessage} object
249
     * @return bool
250
     **/
251
    public function delete(XoopsObject $pm)
252
    {
253
        $className = 'XoopsPrivmessage';
254
        if (!($pm instanceof $className)) {
255
            return false;
256
        }
257
258
        if (!$result = $this->db->query(sprintf('DELETE FROM %s WHERE msg_id = %u', $this->db->prefix('priv_msgs'), $pm->getVar('msg_id')))) {
0 ignored issues
show
Bug introduced by
It seems like $pm->getVar('msg_id') can also be of type array and array; however, parameter $values of sprintf() does only seem to accept double|integer|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

258
        if (!$result = $this->db->query(sprintf('DELETE FROM %s WHERE msg_id = %u', $this->db->prefix('priv_msgs'), /** @scrutinizer ignore-type */ $pm->getVar('msg_id')))) {
Loading history...
Unused Code introduced by
The assignment to $result is dead and can be removed.
Loading history...
259
            return false;
260
        }
261
262
        return true;
263
    }
264
265
    /**
266
     * Load messages from the database
267
     * @param  CriteriaElement|CriteriaCompo $criteria  {@link CriteriaElement} object
268
     * @param  bool   $id_as_key use ID as key into the array?
269
     * @return array  Array of {@link XoopsPrivmessage} objects
270
     **/
271
    public function getObjects(?CriteriaElement $criteria = null, $id_as_key = false)
272
    {
273
        $ret   = [];
274
        $limit = $start = 0;
275
        $sql   = 'SELECT * FROM ' . $this->db->prefix('priv_msgs');
276
        if (isset($criteria) && \method_exists($criteria, 'renderWhere')) {
277
            $sql .= ' ' . $criteria->renderWhere();
278
            $sort = !in_array(
279
                $criteria->getSort(),
280
                [
281
                    'msg_id',
282
                    'msg_time',
283
                    'from_userid',
284
                ],
285
            ) ? 'msg_id' : $criteria->getSort();
286
            $sql .= ' ORDER BY ' . $sort . ' ' . $criteria->getOrder();
287
            $limit = $criteria->getLimit();
288
            $start = $criteria->getStart();
289
        }
290
        $result = $this->db->query($sql, $limit, $start);
291
        if (!$this->db->isResultSet($result)) {
292
            return $ret;
293
        }
294
        /** @var array $myrow */
295
        while (false !== ($myrow = $this->db->fetchArray($result))) {
296
            $pm = new XoopsPrivmessage();
297
            $pm->assignVars($myrow);
298
            if (!$id_as_key) {
299
                $ret[] = & $pm;
300
            } else {
301
                $ret[$myrow['msg_id']] = & $pm;
302
            }
303
            unset($pm);
304
        }
305
306
        return $ret;
307
    }
308
309
    /**
310
     * Count message
311
     * @param  CriteriaElement|CriteriaCompo $criteria = null     {@link CriteriaElement} object
312
     * @return int
313
     **/
314
    public function getCount(?CriteriaElement $criteria = null)
315
    {
316
        $sql = 'SELECT COUNT(*) FROM ' . $this->db->prefix('priv_msgs');
317
        if (isset($criteria) && \method_exists($criteria, 'renderWhere')) {
318
            $sql .= ' ' . $criteria->renderWhere();
319
        }
320
        $result = $this->db->query($sql);
321
        if (!$this->db->isResultSet($result)) {
322
            return 0;
323
        }
324
        [$count] = $this->db->fetchRow($result);
0 ignored issues
show
Bug introduced by
The method fetchRow() does not exist on XoopsDatabase. Since it exists in all sub-types, consider adding an abstract or default implementation to XoopsDatabase. ( Ignorable by Annotation )

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

324
        /** @scrutinizer ignore-call */ 
325
        [$count] = $this->db->fetchRow($result);
Loading history...
325
326
        return (int) $count;
327
    }
328
329
    /**
330
     * Mark a message as read
331
     * @param  XoopsPrivmessage $pm {@link XoopsPrivmessage} object
332
     * @return bool
333
     **/
334
    public function setRead(XoopsPrivmessage $pm)
335
    {
336
        /**
337
         * @TODO: Change to if (!(class_exists($this->className) && $obj instanceof $this->className)) when going fully PHP5
338
         */
339
        if (!is_a($pm, 'xoopsprivmessage')) {
340
            return false;
341
        }
342
343
        $sql = sprintf('UPDATE %s SET read_msg = 1 WHERE msg_id = %u', $this->db->prefix('priv_msgs'), $pm->getVar('msg_id'));
0 ignored issues
show
Bug introduced by
It seems like $pm->getVar('msg_id') can also be of type array and array; however, parameter $values of sprintf() does only seem to accept double|integer|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

343
        $sql = sprintf('UPDATE %s SET read_msg = 1 WHERE msg_id = %u', $this->db->prefix('priv_msgs'), /** @scrutinizer ignore-type */ $pm->getVar('msg_id'));
Loading history...
344
        if (!$this->db->queryF($sql)) {
0 ignored issues
show
Bug introduced by
The method queryF() does not exist on XoopsDatabase. Since it exists in all sub-types, consider adding an abstract or default implementation to XoopsDatabase. ( Ignorable by Annotation )

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

344
        if (!$this->db->/** @scrutinizer ignore-call */ queryF($sql)) {
Loading history...
345
            return false;
346
        }
347
348
        return true;
349
    }
350
}
351