Completed
Push — master ( 76b4a1...141e43 )
by Michael
06:17 queued 02:44
created

xfguestbookMsgHandler::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 4
rs 10
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
// $Id: msg.php, v 2.20 2005/08/12 C. Felix alias the Cat
3
//  ------------------------------------------------------------------------ //
4
//                XOOPS - PHP Content Management System                      //
5
//                    Copyright (c) 2000 XOOPS.org                           //
6
//                       <http://www.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 $db
71
     */
72
    public function __construct($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($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 ' . $this->db->prefix('xfguestbook_msg')
127
                      . ' (msg_id, user_id, uname, title, message, note, post_time, email, url, poster_ip, moderate, gender, country, photo, flagdir, other) VALUES (' . $msg_id . ',' . $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...
128
                      . $this->db->quoteString($uname) . ', ' . $this->db->quoteString($title) . ', ' . $this->db->quoteString($message) . ', ' . $this->db->quoteString($note) . ', ' . $post_time
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...
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...
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...
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...
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...
129
                      . ',  ' . $this->db->quoteString($email) . ', ' . $this->db->quoteString($url) . ', ' . $this->db->quoteString($poster_ip) . ', ' . $moderate . ', '
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...
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...
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...
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...
130
                      . $this->db->quoteString($gender) . ', ' . $this->db->quoteString($country) . ', ' . $this->db->quoteString($photo) . ', ' . $this->db->quoteString($flagdir) . ', '
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...
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...
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...
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...
131
                      . $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...
132
        } else {
133
            $sql =
134
                'UPDATE ' . $this->db->prefix('xfguestbook_msg') . ' SET user_id=' . $user_id . ', uname=' . $this->db->quoteString($uname) . ', title=' . $this->db->quoteString($title) . ', message='
135
                . $this->db->quoteString($message) . ', note=' . $this->db->quoteString($note) . ', email=' . $this->db->quoteString($email) . ', url=' . $this->db->quoteString($url) . ', moderate='
136
                . $moderate . ', gender=' . $this->db->quoteString($gender) . ', country=' . $this->db->quoteString($country) . ', photo=' . $this->db->quoteString($photo) . ', flagdir='
137
                . $this->db->quoteString($flagdir) . ', other=' . $this->db->quoteString($other) . ' WHERE msg_id=' . $msg_id;
138
        }
139
        if (!$result = $this->db->queryF($sql)) {
140
            return false;
141
        }
142
        if (empty($msg_id)) {
143
            $msg_id = $this->db->getInsertId();
144
        }
145
        $msg->assignVar('msg_id', $msg_id);
146
147
        return $msg_id;
148
    }
149
150
    /**
151
     * @param $msg
152
     * @return bool
153
     */
154
    public function delete($msg)
155
    {
156
        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...
157
        if ('xfguestbookmsg' !== strtolower(get_class($msg))) {
158
            return false;
159
        }
160
        $sql = sprintf('DELETE FROM %s WHERE msg_id = %u', $this->db->prefix('xfguestbook_msg'), $msg->getVar('msg_id'));
161
        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...
162
            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...
163
        }
164
165
        if (!$result = $this->db->query($sql)) {
166
            return false;
167
        }
168
169
        return true;
170
    }
171
172
    /**
173
     * @param null $criteria
174
     * @return array
175
     */
176
    public function &getObjects($criteria = null)
177
    {
178
        $ret   = array();
179
        $limit = $start = 0;
180
        $sql   = 'SELECT * FROM ' . $this->db->prefix('xfguestbook_msg');
181
        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...
182
            $sql .= ' ' . $criteria->renderWhere();
183
            $sort = ($criteria->getSort() !== '') ? $criteria->getSort() : 'msg_id';
184
            $sql .= ' ORDER BY ' . $sort . ' ' . $criteria->getOrder();
185
            $limit = $criteria->getLimit();
186
            $start = $criteria->getStart();
187
        }
188
        $result = $this->db->query($sql, $limit, $start);
189
        if (!$result) {
190
            return $ret;
191
        }
192
        while ($myrow = $this->db->fetchArray($result)) {
193
            $msg = new xfguestbookMsg();
194
            $msg->assignVars($myrow);
195
            $ret[] = $msg;
196
            unset($msg);
197
        }
198
199
        return $ret;
200
    }
201
202
    /**
203
     * @param null $criteria
204
     * @return int
205
     */
206
    public function countMsg($criteria = null)
207
    {
208
        $sql = 'SELECT COUNT(*) FROM ' . $this->db->prefix('xfguestbook_msg');
209
        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...
210
            $sql .= ' ' . $criteria->renderWhere();
211
        }
212
        if (!$result = $this->db->query($sql)) {
213
            return 0;
214
        }
215
        list($count) = $this->db->fetchRow($result);
216
217
        return $count;
218
    }
219
220
    /**
221
     * @param null $criteria
222
     * @return array|bool
223
     */
224
    public function countMsgByCountry($criteria = null)
225
    {
226
        $arr = array();
227
        $sql = 'SELECT country, flagdir FROM ' . $this->db->prefix('xfguestbook_msg');
228
        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...
229
            $sql .= ' ' . $criteria->renderWhere();
230
        }
231
        if (!$result = $this->db->query($sql)) {
232
            return false;
233
        }
234
        while (list($country, $flagdir) = $this->db->fetchRow($result)) {
235
            $arr[] = $flagdir . '/' . $country;
236
        }
237
        $ret = array_count_values($arr);
238
        arsort($ret);
239
240
        return $ret;
241
    }
242
243
    /**
244
     * @param null $criteria
245
     * @return array|bool
246
     */
247
    public function countMsgByGender($criteria = null)
248
    {
249
        $arr = array();
250
        $sql = 'SELECT gender FROM ' . $this->db->prefix('xfguestbook_msg');
251
        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...
252
            $sql .= ' ' . $criteria->renderWhere();
253
        }
254
        if (!$result = $this->db->query($sql)) {
255
            return false;
256
        }
257
        while (list($gender) = $this->db->fetchRow($result)) {
258
            $arr[] = $gender;
259
        }
260
        $ret = array_count_values($arr);
261
262
        return $ret;
263
    }
264
265
    /**
266
     * @param null $criteria
267
     * @return array|int
268
     */
269
    public function getMsgImg($criteria = null)
270
    {
271
        $arr = array();
272
        $sql = 'SELECT photo FROM ' . $this->db->prefix('xfguestbook_msg') . " WHERE `photo` LIKE 'msg_%'";
273
        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...
274
            $sql .= ' ' . $criteria->renderWhere();
275
        }
276
        if (!$result = $this->db->query($sql)) {
277
            return 0;
278
        }
279
        while (list($photo) = $this->db->fetchRow($result)) {
280
            $arr[] = $photo;
281
        }
282
283
        return $arr;
284
    }
285
}
286