Completed
Pull Request — master (#10)
by Michael
02:01
created

XfguestbookMsg::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 18
nc 1
nop 0
dl 0
loc 20
rs 9.4285
c 0
b 0
f 0
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 33 and the first side effect is on line 28.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
//
3
//  ------------------------------------------------------------------------ //
4
//                XOOPS - PHP Content Management System                      //
5
//                  Copyright (c) 2000-2016 XOOPS.org                        //
6
//                         <http://xoops.org/>                               //
7
// ------------------------------------------------------------------------- //
8
//  This program is free software; you can redistribute it and/or modify     //
9
//  it under the terms of the GNU General Public License as published by     //
10
//  the Free Software Foundation; either version 2 of the License, or        //
11
//  (at your option) any later version.                                      //
12
//                                                                           //
13
//  You may not change or alter any portion of this comment or credits       //
14
//  of supporting developers from this source code or any supporting         //
15
//  source code which is considered copyrighted (c) material of the          //
16
//  original comment or credit authors.                                      //
17
//                                                                           //
18
//  This program is distributed in the hope that it will be useful,          //
19
//  but WITHOUT ANY WARRANTY; without even the implied warranty of           //
20
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            //
21
//  GNU General Public License for more details.                             //
22
//                                                                           //
23
//  You should have received a copy of the GNU General Public License        //
24
//  along with this program; if not, write to the Free Software              //
25
//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA //
26
//  ------------------------------------------------------------------------ //
27
28
defined('XOOPS_ROOT_PATH') || exit('XOOPS root path not defined');
29
30
/**
31
 * Class XfguestbookMsg
32
 */
33
class XfguestbookMsg extends XoopsObject
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
34
{
35
    // constructor
36
    /**
37
     * XfguestbookMsg constructor.
38
     */
39
    public function __construct()
40
    {
41
        parent::__construct();
42
        $this->initVar('msg_id', XOBJ_DTYPE_INT, null, false);
43
        $this->initVar('user_id', XOBJ_DTYPE_INT, null, false);
44
        $this->initVar('uname', XOBJ_DTYPE_TXTBOX, '', false);
45
        $this->initVar('title', XOBJ_DTYPE_TXTBOX, '', true);
46
        $this->initVar('message', XOBJ_DTYPE_TXTAREA, '', false);
47
        $this->initVar('note', XOBJ_DTYPE_TXTAREA, '', false);
48
        $this->initVar('post_time', XOBJ_DTYPE_STIME, null, false);
49
        $this->initVar('email', XOBJ_DTYPE_EMAIL, '', false);
50
        $this->initVar('url', XOBJ_DTYPE_URL, '', false);
51
        $this->initVar('poster_ip', XOBJ_DTYPE_OTHER, '', false);
52
        $this->initVar('moderate', XOBJ_DTYPE_INT, null, false);
53
        $this->initVar('gender', XOBJ_DTYPE_TXTBOX, '', false, 1);
54
        $this->initVar('country', XOBJ_DTYPE_TXTBOX, '', false, 5);
55
        $this->initVar('photo', XOBJ_DTYPE_TXTBOX, '', false); // added v2.20
56
        $this->initVar('flagdir', XOBJ_DTYPE_TXTBOX, '', false); // added v2.30
57
        $this->initVar('other', XOBJ_DTYPE_TXTBOX, '', false); // added v2.30
58
    }
59
}
60
61
/**
62
 * Class XfguestbookMsgHandler
63
 */
64
class XfguestbookMsgHandler
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
65
{
66
    public $db;
67
68
    /**
69
     * XfguestbookMsgHandler constructor.
70
     * @param XoopsDatabase $db
71
     */
72
    public function __construct(XoopsDatabase $db)
73
    {
74
        $this->db = $db;
75
    }
76
77
    /**
78
     * @return XfguestbookMsg
79
     */
80
    public function create()
81
    {
82
        return new XfguestbookMsg();
83
    }
84
85
    /**
86
     * @param $id
87
     * @return bool|XfguestbookMsg
88
     */
89
    public function get($id)
90
    {
91
        $id = (int)$id;
92
        if ($id > 0) {
93
            $sql = 'SELECT * FROM ' . $this->db->prefix('xfguestbook_msg') . ' WHERE msg_id=' . $id;
94
            if (!$result = $this->db->query($sql)) {
95
                return false;
96
            }
97
            $numrows = $this->db->getRowsNum($result);
98
            if (1 == $numrows) {
99
                $msg = new XfguestbookMsg();
100
                $msg->assignVars($this->db->fetchArray($result));
101
102
                return $msg;
103
            }
104
        }
105
106
        return false;
107
    }
108
109
    /**
110
     * @param $msg
111
     * @return bool
112
     */
113
    public function insert(XoopsObject $msg)
114
    {
115
        if ('xfguestbookmsg' !== strtolower(get_class($msg))) {
116
            return false;
117
        }
118
        if (!$msg->cleanVars()) {
119
            return false;
120
        }
121
        foreach ($msg->cleanVars as $k => $v) {
122
            ${$k} = $v;
123
        }
124
        if (empty($msg_id)) {
0 ignored issues
show
Bug introduced by
The variable $msg_id seems only to be defined at a later point. As such the call to empty() seems to always evaluate to true.

This check marks calls to isset(...) or empty(...) that are found before the variable itself is defined. These will always have the same result.

This is likely the result of code being shifted around. Consider removing these calls.

Loading history...
125
            $msg_id = $this->db->genId('xfguestbook_msg_msg_id_seq');
126
            $sql    = 'INSERT INTO '
127
                      . $this->db->prefix('xfguestbook_msg')
128
                      . ' (msg_id, user_id, uname, title, message, note, post_time, email, url, poster_ip, moderate, gender, country, photo, flagdir, other) VALUES ('
129
                      . $msg_id
130
                      . ','
131
                      . $user_id
0 ignored issues
show
Bug introduced by
The variable $user_id does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
132
                      . ', '
133
                      . $this->db->quoteString($uname)
0 ignored issues
show
Bug introduced by
The variable $uname does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
134
                      . ', '
135
                      . $this->db->quoteString($title)
0 ignored issues
show
Bug introduced by
The variable $title does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
136
                      . ', '
137
                      . $this->db->quoteString($message)
0 ignored issues
show
Bug introduced by
The variable $message does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
138
                      . ', '
139
                      . $this->db->quoteString($note)
0 ignored issues
show
Bug introduced by
The variable $note does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
140
                      . ', '
141
                      . $post_time
0 ignored issues
show
Bug introduced by
The variable $post_time does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
142
                      . ',  '
143
                      . $this->db->quoteString($email)
0 ignored issues
show
Bug introduced by
The variable $email does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
144
                      . ', '
145
                      . $this->db->quoteString($url)
0 ignored issues
show
Bug introduced by
The variable $url does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
146
                      . ', '
147
                      . $this->db->quoteString($poster_ip)
0 ignored issues
show
Bug introduced by
The variable $poster_ip does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
148
                      . ', '
149
                      . $moderate
0 ignored issues
show
Bug introduced by
The variable $moderate does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
150
                      . ', '
151
                      . $this->db->quoteString($gender)
0 ignored issues
show
Bug introduced by
The variable $gender does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
152
                      . ', '
153
                      . $this->db->quoteString($country)
0 ignored issues
show
Bug introduced by
The variable $country does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
154
                      . ', '
155
                      . $this->db->quoteString($photo)
0 ignored issues
show
Bug introduced by
The variable $photo does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
156
                      . ', '
157
                      . $this->db->quoteString($flagdir)
0 ignored issues
show
Bug introduced by
The variable $flagdir does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
158
                      . ', '
159
                      . $this->db->quoteString($other)
0 ignored issues
show
Bug introduced by
The variable $other does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
160
                      . ')';
161
        } else {
162
            $sql = 'UPDATE '
163
                   . $this->db->prefix('xfguestbook_msg')
164
                   . ' SET user_id='
165
                   . $user_id
166
                   . ', uname='
167
                   . $this->db->quoteString($uname)
168
                   . ', title='
169
                   . $this->db->quoteString($title)
170
                   . ', message='
171
                   . $this->db->quoteString($message)
172
                   . ', note='
173
                   . $this->db->quoteString($note)
174
                   . ', email='
175
                   . $this->db->quoteString($email)
176
                   . ', url='
177
                   . $this->db->quoteString($url)
178
                   . ', moderate='
179
                   . $moderate
180
                   . ', gender='
181
                   . $this->db->quoteString($gender)
182
                   . ', country='
183
                   . $this->db->quoteString($country)
184
                   . ', photo='
185
                   . $this->db->quoteString($photo)
186
                   . ', flagdir='
187
                   . $this->db->quoteString($flagdir)
188
                   . ', other='
189
                   . $this->db->quoteString($other)
190
                   . ' WHERE msg_id='
191
                   . $msg_id;
192
        }
193
        if (!$result = $this->db->queryF($sql)) {
194
            return false;
195
        }
196
        if (empty($msg_id)) {
197
            $msg_id = $this->db->getInsertId();
198
        }
199
        $msg->assignVar('msg_id', $msg_id);
200
201
        return $msg_id;
202
    }
203
204
    /**
205
     * @param $msg
206
     * @return bool
207
     */
208
    public function delete($msg)
209
    {
210
        global $xoopsModule;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
211
        if ('xfguestbookmsg' !== strtolower(get_class($msg))) {
212
            return false;
213
        }
214
        $sql = sprintf('DELETE FROM %s WHERE msg_id = %u', $this->db->prefix('xfguestbook_msg'), $msg->getVar('msg_id'));
215
        if (isset($this->commentstable) && $this->commentstable !== '') {
0 ignored issues
show
Bug introduced by
The property commentstable does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
216
            xoops_comment_delete($xoopsModule->getVar('mid'), $msg_id);
0 ignored issues
show
Bug introduced by
The variable $msg_id does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
217
        }
218
219
        if (!$result = $this->db->query($sql)) {
220
            return false;
221
        }
222
223
        return true;
224
    }
225
226
    /**
227
     * @param  null $criteria
228
     * @return array
229
     */
230
    public function &getObjects($criteria = null)
231
    {
232
        $ret   = [];
233
        $limit = $start = 0;
234
        $sql   = 'SELECT * FROM ' . $this->db->prefix('xfguestbook_msg');
235
        if (isset($criteria) && is_subclass_of($criteria, 'criteriaelement')) {
0 ignored issues
show
Bug introduced by
Due to PHP Bug #53727, is_subclass_of returns inconsistent results on some PHP versions for interfaces; you could instead use ReflectionClass::implementsInterface.
Loading history...
236
            $sql   .= ' ' . $criteria->renderWhere();
237
            $sort  = ($criteria->getSort() !== '') ? $criteria->getSort() : 'msg_id';
238
            $sql   .= ' ORDER BY ' . $sort . ' ' . $criteria->getOrder();
239
            $limit = $criteria->getLimit();
240
            $start = $criteria->getStart();
241
        }
242
        $result = $this->db->query($sql, $limit, $start);
243
        if (!$result) {
244
            return $ret;
245
        }
246
        while ($myrow = $this->db->fetchArray($result)) {
247
            $msg = new XfguestbookMsg();
248
            $msg->assignVars($myrow);
249
            $ret[] = $msg;
250
            unset($msg);
251
        }
252
253
        return $ret;
254
    }
255
256
    /**
257
     * @param  null $criteria
258
     * @return int
259
     */
260
    public function countMsg($criteria = null)
261
    {
262
        $sql = 'SELECT COUNT(*) FROM ' . $this->db->prefix('xfguestbook_msg');
263
        if (isset($criteria) && is_subclass_of($criteria, 'criteriaelement')) {
0 ignored issues
show
Bug introduced by
Due to PHP Bug #53727, is_subclass_of returns inconsistent results on some PHP versions for interfaces; you could instead use ReflectionClass::implementsInterface.
Loading history...
264
            $sql .= ' ' . $criteria->renderWhere();
265
        }
266
        if (!$result = $this->db->query($sql)) {
267
            return 0;
268
        }
269
        list($count) = $this->db->fetchRow($result);
270
271
        return $count;
272
    }
273
274
    /**
275
     * @param  null $criteria
276
     * @return array|bool
277
     */
278
    public function countMsgByCountry($criteria = null)
279
    {
280
        $arr = [];
281
        $sql = 'SELECT country, flagdir FROM ' . $this->db->prefix('xfguestbook_msg');
282
        if (isset($criteria) && is_subclass_of($criteria, 'criteriaelement')) {
0 ignored issues
show
Bug introduced by
Due to PHP Bug #53727, is_subclass_of returns inconsistent results on some PHP versions for interfaces; you could instead use ReflectionClass::implementsInterface.
Loading history...
283
            $sql .= ' ' . $criteria->renderWhere();
284
        }
285
        if (!$result = $this->db->query($sql)) {
286
            return false;
287
        }
288
        while (list($country, $flagdir) = $this->db->fetchRow($result)) {
289
            $arr[] = $flagdir . '/' . $country;
290
        }
291
        $ret = array_count_values($arr);
292
        arsort($ret);
293
294
        return $ret;
295
    }
296
297
    /**
298
     * @param  null $criteria
299
     * @return array|bool
300
     */
301
    public function countMsgByGender($criteria = null)
302
    {
303
        $arr = [];
304
        $sql = 'SELECT gender FROM ' . $this->db->prefix('xfguestbook_msg');
305
        if (isset($criteria) && is_subclass_of($criteria, 'criteriaelement')) {
0 ignored issues
show
Bug introduced by
Due to PHP Bug #53727, is_subclass_of returns inconsistent results on some PHP versions for interfaces; you could instead use ReflectionClass::implementsInterface.
Loading history...
306
            $sql .= ' ' . $criteria->renderWhere();
307
        }
308
        if (!$result = $this->db->query($sql)) {
309
            return false;
310
        }
311
        while (list($gender) = $this->db->fetchRow($result)) {
312
            $arr[] = $gender;
313
        }
314
        $ret = array_count_values($arr);
315
316
        return $ret;
317
    }
318
319
    /**
320
     * @param  null $criteria
321
     * @return array|int
322
     */
323
    public function getMsgImg($criteria = null)
324
    {
325
        $arr = [];
326
        $sql = 'SELECT photo FROM ' . $this->db->prefix('xfguestbook_msg') . " WHERE `photo` LIKE 'msg_%'";
327
        if (isset($criteria) && is_subclass_of($criteria, 'criteriaelement')) {
0 ignored issues
show
Bug introduced by
Due to PHP Bug #53727, is_subclass_of returns inconsistent results on some PHP versions for interfaces; you could instead use ReflectionClass::implementsInterface.
Loading history...
328
            $sql .= ' ' . $criteria->renderWhere();
329
        }
330
        if (!$result = $this->db->query($sql)) {
331
            return 0;
332
        }
333
        while (list($photo) = $this->db->fetchRow($result)) {
334
            $arr[] = $photo;
335
        }
336
337
        return $arr;
338
    }
339
}
340