Completed
Pull Request — master (#6)
by Michael
01:29
created

class/myiframe.php (14 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/**
3
 * ****************************************************************************
4
 * MYIFRAME - MODULE FOR XOOPS
5
 * Copyright (c) Hervé Thouzard of Instant Zero (http://www.instant-zero.com)
6
 * ****************************************************************************
7
 */
8
9
if (!defined('XOOPS_ROOT_PATH')) {
10
    die('XOOPS root path not defined');
11
}
12
13
include_once XOOPS_ROOT_PATH . '/kernel/object.php';
14
15
/**
16
 * Class Myiframe
17
 */
18
class Myiframe extends XoopsObject
19
{
20
//    /** @var \XoopsMySQLDatabase $db */
21
    public $db;
22
23
    /**
24
     * myiframe constructor.
25
     * @param null $id
26
     */
27
    public function __construct($id = null)
28
    {
29
        $this->db = XoopsDatabaseFactory::getDatabaseConnection();
30
        $this->initVar('frame_frameid', XOBJ_DTYPE_INT, null, false, 10);
31
        $this->initVar('frame_created', XOBJ_DTYPE_INT, null, false, 10);
32
        $this->initVar('frame_uid', XOBJ_DTYPE_INT, null, false, 10);
33
        $this->initVar('frame_description', XOBJ_DTYPE_TXTBOX, null, false, 255);
34
        $this->initVar('frame_width', XOBJ_DTYPE_TXTBOX, null, false, 15);
35
        $this->initVar('frame_height', XOBJ_DTYPE_TXTBOX, null, false, 15);
36
        $this->initVar('frame_align', XOBJ_DTYPE_INT, null, false, 10);
37
        $this->initVar('frame_frameborder', XOBJ_DTYPE_INT, null, false, 10);
38
        $this->initVar('frame_marginwidth', XOBJ_DTYPE_INT, null, false, 10);
39
        $this->initVar('frame_marginheight', XOBJ_DTYPE_INT, null, false, 10);
40
        $this->initVar('frame_scrolling', XOBJ_DTYPE_INT, null, false, 10);
41
        $this->initVar('frame_hits', XOBJ_DTYPE_INT, null, false, 10);
42
        $this->initVar('frame_url', XOBJ_DTYPE_TXTBOX, null, false, 255);
43
        if (!empty($id)) {
44
            if (is_array($id)) {
45
                $this->assignVars($id);
46
            } else {
47
                $this->load((int)$id);
48
            }
49
        } else {
50
            $this->setNew();
51
        }
52
    }
53
54
    /**
55
     * @param $id
56
     */
57
    public function load($id)
58
    {
59
        $sql   = 'SELECT * FROM ' . $this->db->prefix('myiframe') . ' WHERE frame_frameid=' . (int)$id;
60
        $myrow = $this->db->fetchArray($this->db->query($sql));
61
        $this->assignVars($myrow);
62
        if (!$myrow) {
63
            $this->setNew();
64
        }
65
    }
66
}
67
68
/**
69
 * Class MyiframeMyiframeHandler
70
 */
71
class MyiframeMyiframeHandler extends XoopsObjectHandler
72
{
73
    /**
74
     * @param bool $isNew
75
     * @return myiframe
76
     */
77
    public function create($isNew = true)
78
    {
79
        $object = new Myiframe();
80
        if ($isNew) {
81
            $object->setNew();
82
        }
83
84
        return $object;
85
    }
86
87
    /**
88
     * @param int $id
89
     * @return myiframe|null
90
     */
91
    public function get($id)
92
    {
93
        $ret = null;
94
        $sql = 'SELECT * FROM ' . $this->db->prefix('myiframe') . '  WHERE frame_frameid=' . (int)$id;
95
        if (!$result = $this->db->query($sql)) {
96
            return $ret;
97
        }
98
        $numrows = $this->db->getRowsNum($result);
99
        if ($numrows == 1) {
100
            $object = new Myiframe();
101
            $object->assignVars($this->db->fetchArray($result));
102
            return $object;
103
        }
104
105
        return $ret;
106
    }
107
108
    /**
109
     * @param XoopsObject $object
110
     * @param bool        $force
111
     * @return bool
112
     */
113
    public function insert(XoopsObject $object, $force = false)
114
    {
115
        if (get_class($object) !== 'Myiframe') {
116
            return false;
117
        }
118
        if (!$object->isDirty()) {
119
            return true;
120
        }
121
        if (!$object->cleanVars()) {
122
            foreach ($object->getErrors() as $oneerror) {
123
                trigger_error($oneerror);
124
            }
125
            return false;
126
        }
127
        foreach ($object->cleanVars as $k => $v) {
128
            ${$k} = $v;
129
        }
130
131
        if ($object->isNew()) {
132
            $format = 'INSERT INTO "%s" (frame_created, frame_uid, frame_description, frame_width, frame_height, frame_align, frame_frameborder, frame_marginwidth, frame_marginheight, frame_scrolling, frame_hits, frame_url) VALUES (%u, %u, %s, %s, %s, %d, %d, %d, %d, %d, %u, %s)';
133
            $sql    = sprintf(
134
                $format,
135
                $this->db->prefix('myiframe'),
136
                $frame_created,
0 ignored issues
show
The variable $frame_created 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...
137
                $frame_uid,
0 ignored issues
show
The variable $frame_uid 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
                $this->db->quoteString($frame_description),
0 ignored issues
show
The variable $frame_description 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...
139
                $this->db->quoteString($frame_width),
0 ignored issues
show
The variable $frame_width 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
                $this->db->quoteString($frame_height),
0 ignored issues
show
The variable $frame_height 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...
141
                $frame_align,
0 ignored issues
show
The variable $frame_align 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
                $frame_frameborder,
0 ignored issues
show
The variable $frame_frameborder 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...
143
                $frame_marginwidth,
0 ignored issues
show
The variable $frame_marginwidth 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
                $frame_marginheight,
0 ignored issues
show
The variable $frame_marginheight 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...
145
                              $frame_scrolling,
0 ignored issues
show
The variable $frame_scrolling 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
                $frame_hits,
0 ignored issues
show
The variable $frame_hits 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...
147
                $this->db->quoteString($frame_url)
0 ignored issues
show
The variable $frame_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...
148
            );
149
            $force  = true;
150
        } else {
151
            $format = 'UPDATE "%s" SET frame_description="%s", frame_width="%s", frame_height="%s", frame_align="%d", frame_frameborder="%d", frame_marginwidth="%d", frame_marginheight="%d", frame_scrolling="%d", frame_hits="%u", frame_url="%s" WHERE frame_frameid="%u"';
152
            $sql    = sprintf(
153
                $format,
154
                $this->db->prefix('myiframe'),
155
                $this->db->quoteString($frame_description),
156
                $this->db->quoteString($frame_width),
157
                $this->db->quoteString($frame_height),
158
                $frame_align,
159
                $frame_frameborder,
160
                $frame_marginwidth,
161
                $frame_marginheight,
162
                $frame_scrolling,
163
                $frame_hits,
164
                              $this->db->quoteString($frame_url),
165
                $frame_frameid
0 ignored issues
show
The variable $frame_frameid seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
166
            );
167
        }
168 View Code Duplication
        if (false !== $force) {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
169
            $result = $this->db->queryF($sql);
170
        } else {
171
            $result = $this->db->query($sql);
172
        }
173
        if (!$result) {
174
            return false;
175
        }
176
        if (empty($frame_frameid)) {
177
            $frame_frameid = $this->db->getInsertId();
178
        }
179
        $object->assignVar('frame_frameid', $frame_frameid);
180
181
        return $frame_frameid;
182
    }
183
184
    /**
185
     * @param XoopsObject $object
186
     * @param bool        $force
187
     * @return bool
188
     */
189
    public function delete(XoopsObject $object, $force = false)
190
    {
191
        if (get_class($object) !== 'myiframe') {
192
            return false;
193
        }
194
        $sql = sprintf('DELETE FROM "%s" WHERE frame_frameid = "%u"', $this->db->prefix('myiframe'), $object->getVar('frame_frameid'));
195 View Code Duplication
        if (false !== $force) {
196
            $result = $this->db->queryF($sql);
197
        } else {
198
            $result = $this->db->query($sql);
199
        }
200
        if (!$result) {
201
            return false;
202
        }
203
204
        return true;
205
    }
206
207
    /**
208
     * @param null|\Criteria  $criteria
209
     * @param bool $id_as_key
210
     * @return array
211
     */
212
    public function &getObjects(Criteria $criteria = null, $id_as_key = false)
213
    {
214
        $ret   = [];
215
        $limit = $start = 0;
216
        $sql   = 'SELECT * FROM ' . $this->db->prefix('myiframe');
217
        if (isset($criteria) && is_subclass_of($criteria, 'criteriaelement')) {
218
            $sql .= ' ' . $criteria->renderWhere();
219
            if ($criteria->getSort() !== '') {
220
                $sql .= ' ORDER BY ' . $criteria->getSort() . ' ' . $criteria->getOrder();
221
            }
222
            $limit = $criteria->getLimit();
223
            $start = $criteria->getStart();
224
        }
225
        $result = $this->db->query($sql, $limit, $start);
226
        if (!$result) {
227
            return $ret;
228
        }
229
        while (false !== ($myrow = $this->db->fetchArray($result))) {
230
            if (!$id_as_key) {
231
                $ret[] = new Myiframe($myrow);
232
            } else {
233
                $ret[$myrow['frame_frameid']] = new Myiframe($myrow);
234
            }
235
        }
236
237
        return $ret;
238
    }
239
240
    /**
241
     * @param null|CriteriaCompo $criteria
242
     * @return int
243
     */
244
    public function getCount(CriteriaCompo $criteria = null)
245
    {
246
        $sql = 'SELECT COUNT(*) FROM ' . $this->db->prefix('myiframe');
247
        if (isset($criteria) && is_subclass_of($criteria, 'criteriaelement')) {
248
            $sql .= ' ' . $criteria->renderWhere();
249
        }
250
        $result = $this->db->query($sql);
251
        if (!$result) {
252
            return 0;
253
        }
254
        list($count) = $this->db->fetchRow($result);
255
256
        return $count;
257
    }
258
259
    /**
260
     * @param null|CriteriaCompo $criteria
261
     * @return bool
262
     */
263
    public function deleteAll(CriteriaCompo $criteria = null)
264
    {
265
        $sql = 'DELETE FROM ' . $this->db->prefix('myiframe');
266
        if (isset($criteria) && is_subclass_of($criteria, 'criteriaelement')) {
267
            $sql .= ' ' . $criteria->renderWhere();
268
        }
269
        if (!$result = $this->db->query($sql)) {
270
            return false;
271
        }
272
273
        return true;
274
    }
275
276
    /**
277
     * @param $frame_id
278
     */
279
    public function updatehits($frame_id)
280
    {
281
        $sql = sprintf('UPDATE "%s" SET frame_hits = frame_hits+1 WHERE frame_frameid="%u"', $this->db->prefix('myiframe'), (int)$frame_id);
282
        $this->db->queryF($sql);
283
    }
284
}
285