MessageHandler::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace XoopsModules\Xfguestbook;
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
 * @copyright    {@link https://xoops.org/ XOOPS Project}
17
 * @license      {@link https://www.gnu.org/licenses/gpl-2.0.html GNU GPL 2 or later}
18
 * @package
19
 * @since
20
 * @author       XOOPS Development Team
21
 */
22
23
/**
24
 * Class MessageHandler
25
 */
26
class MessageHandler extends \XoopsPersistableObjectHandler
27
{
28
    /** @var \XoopsMySQLDatabase $db */
29
    public $db;
30
31
    /**
32
     * MessageHandler constructor.
33
     * @param \XoopsDatabase|null $db
34
     */
35
    public function __construct(\XoopsDatabase $db = null)
36
    {
37
        $this->db = $db;
38
    }
39
40
    /**
41
     * @param bool $isNew
42
     * @return Message
43
     */
44
    public function create($isNew = true)
45
    {
46
        return new Message();
47
    }
48
49
    /**
50
     * @param int|null $id
51
     * @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...
52
     * @return bool|string
53
     */
54
    public function get($id = null, $fields = null)
55
    {
56
        $id = (int)$id;
57
        if ($id > 0) {
58
            $sql = 'SELECT * FROM ' . $this->db->prefix('xfguestbook_msg') . ' WHERE msg_id=' . $id;
59
            if (!$result = $this->db->query($sql)) {
60
                return false;
61
            }
62
            $numrows = $this->db->getRowsNum($result);
0 ignored issues
show
Bug introduced by
It seems like $result can also be of type true; however, parameter $result of XoopsMySQLDatabase::getRowsNum() does only seem to accept mysqli_result, 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

62
            $numrows = $this->db->getRowsNum(/** @scrutinizer ignore-type */ $result);
Loading history...
63
            if (1 == $numrows) {
64
                $msg = new Message();
65
                $msg->assignVars($this->db->fetchArray($result));
0 ignored issues
show
Bug introduced by
It seems like $result can also be of type true; however, parameter $result of XoopsMySQLDatabase::fetchArray() does only seem to accept mysqli_result, 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

65
                $msg->assignVars($this->db->fetchArray(/** @scrutinizer ignore-type */ $result));
Loading history...
66
67
                return $msg;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $msg returns the type XoopsModules\Xfguestbook\Message which is incompatible with the documented return type boolean|string.
Loading history...
68
            }
69
        }
70
71
        return false;
72
    }
73
74
    /**
75
     * @param \XoopsObject $msg
76
     * @param bool         $force
77
     * @return bool
78
     */
79
    public function insert(\XoopsObject $msg, $force = true)
80
    {
81
        if (Message::class !== \get_class($msg)) {
82
            return false;
83
        }
84
        if (!$msg->cleanVars()) {
85
            return false;
86
        }
87
        foreach ($msg->cleanVars as $k => $v) {
88
            ${$k} = $v;
89
        }
90
        if (empty($msg_id)) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $msg_id seems to never exist and therefore empty should always be true.
Loading history...
91
            $msg_id = $this->db->genId('xfguestbook_msg_msg_id_seq');
92
            $sql    = 'INSERT INTO '
93
                      . $this->db->prefix('xfguestbook_msg')
94
                      . ' (msg_id, user_id, uname, title, message, note, post_time, email, url, poster_ip, moderate, gender, country, photo, flagdir, other) VALUES ('
95
                      . $msg_id
96
                      . ','
97
                      . $user_id
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $user_id seems to be never defined.
Loading history...
98
                      . ', '
99
                      . $this->db->quoteString($uname)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $uname seems to be never defined.
Loading history...
100
                      . ', '
101
                      . $this->db->quoteString($title)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $title seems to be never defined.
Loading history...
102
                      . ', '
103
                      . $this->db->quoteString($message)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $message seems to be never defined.
Loading history...
104
                      . ', '
105
                      . $this->db->quoteString($note)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $note seems to be never defined.
Loading history...
106
                      . ', '
107
                      . $post_time
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $post_time seems to be never defined.
Loading history...
108
                      . ',  '
109
                      . $this->db->quoteString($email)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $email seems to be never defined.
Loading history...
110
                      . ', '
111
                      . $this->db->quoteString($url)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $url seems to be never defined.
Loading history...
112
                      . ', '
113
                      . $this->db->quoteString($poster_ip)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $poster_ip seems to be never defined.
Loading history...
114
                      . ', '
115
                      . $moderate
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $moderate seems to be never defined.
Loading history...
116
                      . ', '
117
                      . $this->db->quoteString($gender)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $gender seems to be never defined.
Loading history...
118
                      . ', '
119
                      . $this->db->quoteString($country)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $country seems to be never defined.
Loading history...
120
                      . ', '
121
                      . $this->db->quoteString($photo)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $photo seems to be never defined.
Loading history...
122
                      . ', '
123
                      . $this->db->quoteString($flagdir)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $flagdir seems to be never defined.
Loading history...
124
                      . ', '
125
                      . $this->db->quoteString($other)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $other seems to be never defined.
Loading history...
126
                      . ')';
127
        } else {
128
            $sql = 'UPDATE '
129
                   . $this->db->prefix('xfguestbook_msg')
130
                   . ' SET user_id='
131
                   . $user_id
132
                   . ', uname='
133
                   . $this->db->quoteString($uname)
134
                   . ', title='
135
                   . $this->db->quoteString($title)
136
                   . ', message='
137
                   . $this->db->quoteString($message)
138
                   . ', note='
139
                   . $this->db->quoteString($note)
140
                   . ', email='
141
                   . $this->db->quoteString($email)
142
                   . ', url='
143
                   . $this->db->quoteString($url)
144
                   . ', moderate='
145
                   . $moderate
146
                   . ', gender='
147
                   . $this->db->quoteString($gender)
148
                   . ', country='
149
                   . $this->db->quoteString($country)
150
                   . ', photo='
151
                   . $this->db->quoteString($photo)
152
                   . ', flagdir='
153
                   . $this->db->quoteString($flagdir)
154
                   . ', other='
155
                   . $this->db->quoteString($other)
156
                   . ' WHERE msg_id='
157
                   . $msg_id;
158
        }
159
        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...
160
            return false;
161
        }
162
        if (empty($msg_id)) {
163
            $msg_id = $this->db->getInsertId();
164
        }
165
        $msg->assignVar('msg_id', $msg_id);
166
167
        return $msg_id;
168
    }
169
170
    /**
171
     * @param \XoopsObject $msg
172
     * @param bool         $force
173
     * @return bool
174
     */
175
    public function delete(\XoopsObject $msg, $force = false)
176
    {
177
        global $xoopsModule;
178
        if (Message::class !== \get_class($msg)) {
179
            return false;
180
        }
181
        $sql = \sprintf('DELETE FROM `%s` WHERE msg_id = %u', $this->db->prefix('xfguestbook_msg'), $msg->getVar('msg_id'));
0 ignored issues
show
Bug introduced by
It seems like $msg->getVar('msg_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

181
        $sql = \sprintf('DELETE FROM `%s` WHERE msg_id = %u', $this->db->prefix('xfguestbook_msg'), /** @scrutinizer ignore-type */ $msg->getVar('msg_id'));
Loading history...
182
        if (isset($this->commentstable) && '' !== $this->commentstable) {
183
            \xoops_comment_delete($xoopsModule->getVar('mid'), $msg_id);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $msg_id seems to be never defined.
Loading history...
184
        }
185
186
        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...
187
            return false;
188
        }
189
190
        return true;
191
    }
192
193
    /**
194
     * @param null|\CriteriaElement|\CriteriaCompo $criteria
195
     * @param bool                                 $id_as_key
196
     * @param bool                                 $as_object
197
     * @return array
198
     */
199
    public function &getObjects(\CriteriaElement $criteria = null, $id_as_key = false, $as_object = true)//getObjects(\CriteriaElement $criteria = null)
200
    {
201
        $ret   = [];
202
        $limit = $start = 0;
203
        $sql   = 'SELECT * FROM ' . $this->db->prefix('xfguestbook_msg');
204
        if (null !== $criteria && $criteria instanceof \CriteriaElement) {
205
            $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

205
            $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...
206
            $sort  = ('' !== $criteria->getSort()) ? $criteria->getSort() : 'msg_id';
207
            $sql   .= ' ORDER BY ' . $sort . ' ' . $criteria->getOrder();
208
            $limit = $criteria->getLimit();
209
            $start = $criteria->getStart();
210
        }
211
        $result = $this->db->query($sql, $limit, $start);
212
        if (!$result) {
213
            return $ret;
214
        }
215
        while (false !== ($myrow = $this->db->fetchArray($result))) {
0 ignored issues
show
Bug introduced by
It seems like $result can also be of type true; however, parameter $result of XoopsMySQLDatabase::fetchArray() does only seem to accept mysqli_result, 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

215
        while (false !== ($myrow = $this->db->fetchArray(/** @scrutinizer ignore-type */ $result))) {
Loading history...
216
            $msg = new Message();
217
            $msg->assignVars($myrow);
218
            $ret[] = $msg;
219
            unset($msg);
220
        }
221
222
        return $ret;
223
    }
224
225
    /**
226
     * @param null|\CriteriaElement|\CriteriaCompo $criteria
227
     * @return int
228
     */
229
    public function countMsg(\CriteriaElement $criteria = null)
230
    {
231
        $sql = 'SELECT COUNT(*) FROM ' . $this->db->prefix('xfguestbook_msg');
232
        if (null !== $criteria && $criteria instanceof \CriteriaElement) {
233
            $sql .= ' ' . $criteria->renderWhere();
234
        }
235
        if (!$result = $this->db->query($sql)) {
236
            return 0;
237
        }
238
        [$count] = $this->db->fetchRow($result);
0 ignored issues
show
Bug introduced by
It seems like $result can also be of type true; however, parameter $result of XoopsMySQLDatabase::fetchRow() does only seem to accept mysqli_result, 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

238
        [$count] = $this->db->fetchRow(/** @scrutinizer ignore-type */ $result);
Loading history...
239
240
        return $count;
241
    }
242
243
    /**
244
     * @param null|\CriteriaElement|\CriteriaCompo $criteria
245
     * @return array|bool
246
     */
247
    public function countMsgByCountry(\CriteriaElement $criteria = null)
248
    {
249
        $arr = [];
250
        $sql = 'SELECT country, flagdir FROM ' . $this->db->prefix('xfguestbook_msg');
251
        if (null !== $criteria && $criteria instanceof \CriteriaElement) {
252
            $sql .= ' ' . $criteria->renderWhere();
253
        }
254
        if (!$result = $this->db->query($sql)) {
255
            return false;
256
        }
257
        while (list($country, $flagdir) = $this->db->fetchRow($result)) {
0 ignored issues
show
Bug introduced by
It seems like $result can also be of type true; however, parameter $result of XoopsMySQLDatabase::fetchRow() does only seem to accept mysqli_result, 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

257
        while (list($country, $flagdir) = $this->db->fetchRow(/** @scrutinizer ignore-type */ $result)) {
Loading history...
258
            $arr[] = $flagdir . '/' . $country;
259
        }
260
        $ret = \array_count_values($arr);
261
        \arsort($ret);
262
263
        return $ret;
264
    }
265
266
    /**
267
     * @param \CriteriaElement|\CriteriaCompo|null $criteria
268
     * @return array|bool
269
     */
270
    public function countMsgByGender(\CriteriaElement $criteria = null)
271
    {
272
        $arr = [];
273
        $sql = 'SELECT gender FROM ' . $this->db->prefix('xfguestbook_msg');
274
        if (null !== $criteria && $criteria instanceof \CriteriaElement) {
275
            $sql .= ' ' . $criteria->renderWhere();
276
        }
277
        if (!$result = $this->db->query($sql)) {
278
            return false;
279
        }
280
        while (list($gender) = $this->db->fetchRow($result)) {
0 ignored issues
show
Bug introduced by
It seems like $result can also be of type true; however, parameter $result of XoopsMySQLDatabase::fetchRow() does only seem to accept mysqli_result, 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

280
        while (list($gender) = $this->db->fetchRow(/** @scrutinizer ignore-type */ $result)) {
Loading history...
281
            $arr[] = $gender;
282
        }
283
        $ret = \array_count_values($arr);
284
285
        return $ret;
286
    }
287
288
    /**
289
     * @param null|\CriteriaElement|\CriteriaCompo $criteria
290
     * @return array|int
291
     */
292
    public function getMsgImg(\CriteriaElement $criteria = null)
293
    {
294
        $arr = [];
295
        $sql = 'SELECT photo FROM ' . $this->db->prefix('xfguestbook_msg') . " WHERE `photo` LIKE 'msg_%'";
296
        if (null !== $criteria && $criteria instanceof \CriteriaElement) {
297
            $sql .= ' ' . $criteria->renderWhere();
298
        }
299
        if (!$result = $this->db->query($sql)) {
300
            return 0;
301
        }
302
        while (list($photo) = $this->db->fetchRow($result)) {
0 ignored issues
show
Bug introduced by
It seems like $result can also be of type true; however, parameter $result of XoopsMySQLDatabase::fetchRow() does only seem to accept mysqli_result, 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

302
        while (list($photo) = $this->db->fetchRow(/** @scrutinizer ignore-type */ $result)) {
Loading history...
303
            $arr[] = $photo;
304
        }
305
306
        return $arr;
307
    }
308
}
309