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, |
|
|
|
|
137
|
|
|
$frame_uid, |
|
|
|
|
138
|
|
|
$this->db->quoteString($frame_description), |
|
|
|
|
139
|
|
|
$this->db->quoteString($frame_width), |
|
|
|
|
140
|
|
|
$this->db->quoteString($frame_height), |
|
|
|
|
141
|
|
|
$frame_align, |
|
|
|
|
142
|
|
|
$frame_frameborder, |
|
|
|
|
143
|
|
|
$frame_marginwidth, |
|
|
|
|
144
|
|
|
$frame_marginheight, |
|
|
|
|
145
|
|
|
$frame_scrolling, |
|
|
|
|
146
|
|
|
$frame_hits, |
|
|
|
|
147
|
|
|
$this->db->quoteString($frame_url) |
|
|
|
|
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 |
|
|
|
|
166
|
|
|
); |
167
|
|
|
} |
168
|
|
View Code Duplication |
if (false !== $force) { |
|
|
|
|
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
|
|
|
|
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.