Completed
Pull Request — master (#4)
by Michael
01:27
created

Myiframe::__construct()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 26
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 22
nc 3
nop 1
dl 0
loc 26
rs 8.8571
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 18 and the first side effect is on line 10.

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
 * 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
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...
19
{
20
    public $db;
21
22
    /**
23
     * myiframe constructor.
24
     * @param null $id
25
     */
26
    public function __construct($id = null)
27
    {
28
        $this->db = XoopsDatabaseFactory::getDatabaseConnection();
29
        $this->initVar('frame_frameid', XOBJ_DTYPE_INT, null, false, 10);
30
        $this->initVar('frame_created', XOBJ_DTYPE_INT, null, false, 10);
31
        $this->initVar('frame_uid', XOBJ_DTYPE_INT, null, false, 10);
32
        $this->initVar('frame_description', XOBJ_DTYPE_TXTBOX, null, false, 255);
33
        $this->initVar('frame_width', XOBJ_DTYPE_TXTBOX, null, false, 15);
34
        $this->initVar('frame_height', XOBJ_DTYPE_TXTBOX, null, false, 15);
35
        $this->initVar('frame_align', XOBJ_DTYPE_INT, null, false, 10);
36
        $this->initVar('frame_frameborder', XOBJ_DTYPE_INT, null, false, 10);
37
        $this->initVar('frame_marginwidth', XOBJ_DTYPE_INT, null, false, 10);
38
        $this->initVar('frame_marginheight', XOBJ_DTYPE_INT, null, false, 10);
39
        $this->initVar('frame_scrolling', XOBJ_DTYPE_INT, null, false, 10);
40
        $this->initVar('frame_hits', XOBJ_DTYPE_INT, null, false, 10);
41
        $this->initVar('frame_url', XOBJ_DTYPE_TXTBOX, null, false, 255);
42
        if (!empty($id)) {
43
            if (is_array($id)) {
44
                $this->assignVars($id);
45
            } else {
46
                $this->load((int)$id);
47
            }
48
        } else {
49
            $this->setNew();
50
        }
51
    }
52
53
    /**
54
     * @param $id
55
     */
56
    public function load($id)
57
    {
58
        $sql   = 'SELECT * FROM ' . $this->db->prefix('myiframe') . ' WHERE frame_frameid=' . (int)$id;
59
        $myrow = $this->db->fetchArray($this->db->query($sql));
60
        $this->assignVars($myrow);
61
        if (!$myrow) {
62
            $this->setNew();
63
        }
64
    }
65
}
66
67
/**
68
 * Class MyiframeMyiframeHandler
69
 */
70
class MyiframeMyiframeHandler extends XoopsObjectHandler
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...
71
{
72
    /**
73
     * @param bool $isNew
74
     * @return myiframe
75
     */
76
    public function create($isNew = true)
77
    {
78
        $object = new Myiframe();
79
        if ($isNew) {
80
            $object->setNew();
81
        }
82
83
        return $object;
84
    }
85
86
    /**
87
     * @param int $id
88
     * @return myiframe|null
89
     */
90
    public function &get($id)
91
    {
92
        $ret = null;
93
        $sql = 'SELECT * FROM ' . $this->db->prefix('myiframe') . '  WHERE frame_frameid=' . (int)$id;
94
        if (!$result = $this->db->query($sql)) {
95
            return $ret;
96
        }
97
        $numrows = $this->db->getRowsNum($result);
98
        if ($numrows == 1) {
99
            $object = new Myiframe();
100
            $object->assignVars($this->db->fetchArray($result));
101
            return $object;
102
        }
103
104
        return $ret;
105
    }
106
107
    /**
108
     * @param XoopsObject $object
109
     * @param bool        $force
110
     * @return bool
111
     */
112
    public function insert(XoopsObject $object, $force = false)
113
    {
114
        if (get_class($object) !== 'myiframe') {
115
            return false;
116
        }
117
        if (!$object->isDirty()) {
118
            return true;
119
        }
120
        if (!$object->cleanVars()) {
121
            foreach ($object->getErrors() as $oneerror) {
122
                trigger_error($oneerror);
123
            }
124
            return false;
125
        }
126
        foreach ($object->cleanVars as $k => $v) {
127
            ${$k} = $v;
128
        }
129
130
        if ($object->isNew()) {
131
            $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)';
132
            $sql    = sprintf($format, $this->db->prefix('myiframe'), $frame_created, $frame_uid, $this->db->quoteString($frame_description), $this->db->quoteString($frame_width), $this->db->quoteString($frame_height), $frame_align, $frame_frameborder, $frame_marginwidth, $frame_marginheight,
0 ignored issues
show
Bug introduced by
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...
Bug introduced by
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...
Bug introduced by
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...
Bug introduced by
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...
Bug introduced by
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...
Bug introduced by
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...
Bug introduced by
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...
Bug introduced by
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...
Bug introduced by
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...
133
                              $frame_scrolling, $frame_hits, $this->db->quoteString($frame_url));
0 ignored issues
show
Bug introduced by
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...
Bug introduced by
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...
Bug introduced by
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...
134
            $force  = true;
135
        } else {
136
            $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';
137
            $sql    = sprintf($format, $this->db->prefix('myiframe'), $this->db->quoteString($frame_description), $this->db->quoteString($frame_width), $this->db->quoteString($frame_height), $frame_align, $frame_frameborder, $frame_marginwidth, $frame_marginheight, $frame_scrolling, $frame_hits,
138
                              $this->db->quoteString($frame_url), $frame_frameid);
0 ignored issues
show
Bug introduced by
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...
139
        }
140 View Code Duplication
        if (false !== $force) {
0 ignored issues
show
Duplication introduced by
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...
141
            $result = $this->db->queryF($sql);
142
        } else {
143
            $result = $this->db->query($sql);
144
        }
145
        if (!$result) {
146
            return false;
147
        }
148
        if (empty($frame_frameid)) {
0 ignored issues
show
Bug introduced by
The variable $frame_frameid 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...
149
            $frame_frameid = $this->db->getInsertId();
150
        }
151
        $object->assignVar('frame_frameid', $frame_frameid);
152
153
        return $frame_frameid;
154
    }
155
156
    /**
157
     * @param XoopsObject $object
158
     * @param bool        $force
159
     * @return bool
160
     */
161
    public function delete(XoopsObject $object, $force = false)
162
    {
163
        if (get_class($object) !== 'myiframe') {
164
            return false;
165
        }
166
        $sql = sprintf('DELETE FROM %s WHERE frame_frameid = %u', $this->db->prefix('myiframe'), $object->getVar('frame_frameid'));
167 View Code Duplication
        if (false !== $force) {
0 ignored issues
show
Duplication introduced by
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...
168
            $result = $this->db->queryF($sql);
169
        } else {
170
            $result = $this->db->query($sql);
171
        }
172
        if (!$result) {
173
            return false;
174
        }
175
176
        return true;
177
    }
178
179
    /**
180
     * @param null $criteria
181
     * @param bool $id_as_key
182
     * @return array
183
     */
184
    public function &getObjects($criteria = null, $id_as_key = false)
185
    {
186
        $ret   = array();
187
        $limit = $start = 0;
188
        $sql   = 'SELECT * FROM ' . $this->db->prefix('myiframe');
189
        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...
190
            $sql .= ' ' . $criteria->renderWhere();
191
            if ($criteria->getSort() !== '') {
192
                $sql .= ' ORDER BY ' . $criteria->getSort() . ' ' . $criteria->getOrder();
193
            }
194
            $limit = $criteria->getLimit();
195
            $start = $criteria->getStart();
196
        }
197
        $result = $this->db->query($sql, $limit, $start);
198
        if (!$result) {
199
            return $ret;
200
        }
201
        while ($myrow = $this->db->fetchArray($result)) {
202
            if (!$id_as_key) {
203
                $ret[] = new Myiframe($myrow);
204
            } else {
205
                $ret[$myrow['frame_frameid']] = new Myiframe($myrow);
206
            }
207
        }
208
209
        return $ret;
210
    }
211
212
    /**
213
     * @param null $criteria
214
     * @return int
215
     */
216
    public function getCount($criteria = null)
217
    {
218
        $sql = 'SELECT COUNT(*) FROM ' . $this->db->prefix('myiframe');
219
        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...
220
            $sql .= ' ' . $criteria->renderWhere();
221
        }
222
        $result = $this->db->query($sql);
223
        if (!$result) {
224
            return 0;
225
        }
226
        list($count) = $this->db->fetchRow($result);
227
228
        return $count;
229
    }
230
231
    /**
232
     * @param null $criteria
233
     * @return bool
234
     */
235
    public function deleteAll($criteria = null)
236
    {
237
        $sql = 'DELETE FROM ' . $this->db->prefix('myiframe');
238
        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...
239
            $sql .= ' ' . $criteria->renderWhere();
240
        }
241
        if (!$result = $this->db->query($sql)) {
242
            return false;
243
        }
244
245
        return true;
246
    }
247
248
    /**
249
     * @param $frame_id
250
     */
251
    public function updatehits($frame_id)
252
    {
253
        $sql = sprintf('UPDATE %s SET frame_hits = frame_hits+1 WHERE frame_frameid=%u', $this->db->prefix('myiframe'), (int)$frame_id);
254
        $this->db->queryF($sql);
255
    }
256
}
257