Passed
Push — master ( 37a2f2...290aa0 )
by Michael
02:35
created

MessageHandler::getMsgImg()   A

Complexity

Conditions 5
Paths 6

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 9
nc 6
nop 1
dl 0
loc 15
rs 9.6111
c 0
b 0
f 0
1
<?php namespace XoopsModules\Xfguestbook;
2
3
/*
4
 * You may not change or alter any portion of this comment or credits
5
 * of supporting developers from this source code or any supporting source code
6
 * which is considered copyrighted (c) material of the original comment or credit authors.
7
 *
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
13
/**
14
 * @copyright    {@link https://xoops.org/ XOOPS Project}
15
 * @license      {@link http://www.gnu.org/licenses/gpl-2.0.html GNU GPL 2 or later}
16
 * @package
17
 * @since
18
 * @author       XOOPS Development Team
19
 */
20
21
use XoopsModules\Xfguestbook;
22
23
defined('XOOPS_ROOT_PATH') || die('Restricted access');
24
25
26
/**
27
 * Class MessageHandler
28
 */
29
class MessageHandler
30
{
31
    public $db;
32
33
    /**
34
     * MessageHandler constructor.
35
     * @param \XoopsDatabase|null $db
36
     */
37
    public function __construct(\XoopsDatabase $db = null)
38
    {
39
        $this->db = $db;
40
    }
41
42
    /**
43
     * @return Message
44
     */
45
    public function create()
46
    {
47
        return new Message();
48
    }
49
50
    /**
51
     * @param $id
52
     * @return bool|Message
53
     */
54
    public function get($id)
55
    {
56
        $id = (int)$id;
57
        if ($id > 0) {
58
            $sql = 'SELECT * FROM ' . $this->db->prefix('xfguestbook_msg') . ' WHERE msg_id=' . $id;
0 ignored issues
show
Bug introduced by
The method prefix() does not exist on null. ( Ignorable by Annotation )

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

58
            $sql = 'SELECT * FROM ' . $this->db->/** @scrutinizer ignore-call */ prefix('xfguestbook_msg') . ' WHERE msg_id=' . $id;

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

179
        $sql = sprintf('DELETE FROM `%s` WHERE msg_id = %u', $this->db->prefix('xfguestbook_msg'), /** @scrutinizer ignore-type */ $msg->getVar('msg_id'));
Loading history...
180
        if (isset($this->commentstable) && '' !== $this->commentstable) {
181
            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...
182
        }
183
184
        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...
185
            return false;
186
        }
187
188
        return true;
189
    }
190
191
    /**
192
     * @param  null|\CriteriaElement $criteria
193
     * @return array
194
     */
195
    public function &getObjects(\CriteriaElement $criteria = null)
196
    {
197
        $ret   = [];
198
        $limit = $start = 0;
199
        $sql   = 'SELECT * FROM ' . $this->db->prefix('xfguestbook_msg');
200
        if (null !== $criteria && is_subclass_of($criteria, 'CriteriaElement')) {
201
            $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

201
            $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...
202
            $sort  = ('' !== $criteria->getSort()) ? $criteria->getSort() : 'msg_id';
203
            $sql   .= ' ORDER BY ' . $sort . ' ' . $criteria->getOrder();
204
            $limit = $criteria->getLimit();
205
            $start = $criteria->getStart();
206
        }
207
        $result = $this->db->query($sql, $limit, $start);
208
        if (!$result) {
209
            return $ret;
210
        }
211
        while (false !== ($myrow = $this->db->fetchArray($result))) {
212
            $msg = new Message();
213
            $msg->assignVars($myrow);
214
            $ret[] = $msg;
215
            unset($msg);
216
        }
217
218
        return $ret;
219
    }
220
221
    /**
222
     * @param  null|\CriteriaElement $criteria
223
     * @return int
224
     */
225
    public function countMsg(\CriteriaElement $criteria = null)
226
    {
227
        $sql = 'SELECT COUNT(*) FROM ' . $this->db->prefix('xfguestbook_msg');
228
        if (null !== $criteria && is_subclass_of($criteria, 'CriteriaElement')) {
229
            $sql .= ' ' . $criteria->renderWhere();
230
        }
231
        if (!$result = $this->db->query($sql)) {
232
            return 0;
233
        }
234
        list($count) = $this->db->fetchRow($result);
235
236
        return $count;
237
    }
238
239
    /**
240
     * @param  null|\CriteriaElement $criteria
241
     * @return array|bool
242
     */
243
    public function countMsgByCountry(\CriteriaElement $criteria = null)
244
    {
245
        $arr = [];
246
        $sql = 'SELECT country, flagdir FROM ' . $this->db->prefix('xfguestbook_msg');
247
        if (null !== $criteria && is_subclass_of($criteria, 'CriteriaElement')) {
248
            $sql .= ' ' . $criteria->renderWhere();
249
        }
250
        if (!$result = $this->db->query($sql)) {
251
            return false;
252
        }
253
        while (false !== (list($country, $flagdir) = $this->db->fetchRow($result))) {
254
            $arr[] = $flagdir . '/' . $country;
255
        }
256
        $ret = array_count_values($arr);
257
        arsort($ret);
258
259
        return $ret;
260
    }
261
262
    /**
263
     * @param \CriteriaElement|null $criteria
264
     * @return array|bool
265
     */
266
    public function countMsgByGender(\CriteriaElement $criteria = null)
267
    {
268
        $arr = [];
269
        $sql = 'SELECT gender FROM ' . $this->db->prefix('xfguestbook_msg');
270
        if (null !== $criteria && is_subclass_of($criteria, 'CriteriaElement')) {
271
            $sql .= ' ' . $criteria->renderWhere();
272
        }
273
        if (!$result = $this->db->query($sql)) {
274
            return false;
275
        }
276
        while (false !== (list($gender) = $this->db->fetchRow($result))) {
277
            $arr[] = $gender;
278
        }
279
        $ret = array_count_values($arr);
280
281
        return $ret;
282
    }
283
284
    /**
285
     * @param  null|\CriteriaElement $criteria
286
     * @return array|int
287
     */
288
    public function getMsgImg(\CriteriaElement $criteria = null)
289
    {
290
        $arr = [];
291
        $sql = 'SELECT photo FROM ' . $this->db->prefix('xfguestbook_msg') . " WHERE `photo` LIKE 'msg_%'";
292
        if (null !== $criteria && is_subclass_of($criteria, 'CriteriaElement')) {
293
            $sql .= ' ' . $criteria->renderWhere();
294
        }
295
        if (!$result = $this->db->query($sql)) {
296
            return 0;
297
        }
298
        while (false !== (list($photo) = $this->db->fetchRow($result))) {
299
            $arr[] = $photo;
300
        }
301
302
        return $arr;
303
    }
304
}
305