XoopsImageHandler   A
last analyzed

Complexity

Total Complexity 39

Size/Duplication

Total Lines 214
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 100
dl 0
loc 214
rs 9.28
c 0
b 0
f 0
wmc 39

7 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 8 2
C insert() 0 51 15
A delete() 0 16 3
A get() 0 18 4
A getCount() 0 13 4
A getList() 0 13 3
B getObjects() 0 33 8
1
<?php
2
/**
3
 * XOOPS Kernel Class
4
 *
5
 * You may not change or alter any portion of this comment or credits
6
 * of supporting developers from this source code or any supporting source code
7
 * which is considered copyrighted (c) material of the original comment or credit authors.
8
 * This program is distributed in the hope that it will be useful,
9
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11
 *
12
 * @copyright       (c) 2000-2016 XOOPS Project (www.xoops.org)
13
 * @license             GNU GPL 2 (https://www.gnu.org/licenses/gpl-2.0.html)
14
 * @package             kernel
15
 * @since               2.0.0
16
 * @author              Kazumi Ono (AKA onokazu) http://www.myweb.ne.jp/, http://jp.xoops.org/
17
 */
18
defined('XOOPS_ROOT_PATH') || exit('Restricted access');
19
20
/**
21
 * An Image
22
 *
23
 * @package          kernel
24
 * @author           Kazumi Ono     <[email protected]>
25
 * @copyright    (c) 2000-2016 XOOPS Project - www.xoops.org
26
 */
27
class XoopsImage extends XoopsObject
28
{
29
    //PHP 8.2 Dynamic properties deprecated
30
    public $image_id;
31
    public $image_nam;
32
    public $image_nicename;
33
    public $image_mimetype;
34
    public $image_created;
35
    public $image_display;
36
    public $image_weight;
37
    public $image_body;
38
    public $imgcat_id;
39
40
    /**
41
     * Constructor
42
     **/
43
    public function __construct()
44
    {
45
        parent::__construct();
46
        $this->initVar('image_id', XOBJ_DTYPE_INT, null, false);
47
        $this->initVar('image_name', XOBJ_DTYPE_OTHER, null, false, 30);
48
        $this->initVar('image_nicename', XOBJ_DTYPE_TXTBOX, null, true, 100);
49
        $this->initVar('image_mimetype', XOBJ_DTYPE_OTHER, null, false);
50
        $this->initVar('image_created', XOBJ_DTYPE_INT, null, false);
51
        $this->initVar('image_display', XOBJ_DTYPE_INT, 1, false);
52
        $this->initVar('image_weight', XOBJ_DTYPE_INT, 0, false);
53
        $this->initVar('image_body', XOBJ_DTYPE_SOURCE, null, true);
54
        $this->initVar('imgcat_id', XOBJ_DTYPE_INT, 0, false);
55
    }
56
57
    /**
58
     * Returns Class Base Variable image_id
59
     * @param  string $format
60
     * @return mixed
61
     */
62
    public function id($format = 'N')
63
    {
64
        return $this->getVar('image_id', $format);
65
    }
66
67
    /**
68
     * Returns Class Base Variable image_id
69
     * @param  string $format
70
     * @return mixed
71
     */
72
    public function image_id($format = '')
73
    {
74
        return $this->getVar('image_id', $format);
75
    }
76
77
    /**
78
     * Returns Class Base Variable image_name
79
     * @param  string $format
80
     * @return mixed
81
     */
82
    public function image_name($format = '')
83
    {
84
        return $this->getVar('image_name', $format);
85
    }
86
87
    /**
88
     * Returns Class Base Variable image_nicename
89
     * @param  string $format
90
     * @return mixed
91
     */
92
    public function image_nicename($format = '')
93
    {
94
        return $this->getVar('image_nicename', $format);
95
    }
96
97
    /**
98
     * Returns Class Base Variable image_mimetype
99
     * @param  string $format
100
     * @return mixed
101
     */
102
    public function image_mimetype($format = '')
103
    {
104
        return $this->getVar('image_mimetype', $format);
105
    }
106
107
    /**
108
     * Returns Class Base Variable image_created
109
     * @param  string $format
110
     * @return mixed
111
     */
112
    public function image_created($format = '')
113
    {
114
        return $this->getVar('image_created', $format);
115
    }
116
117
    /**
118
     * Returns Class Base Variable image_display
119
     * @param  string $format
120
     * @return mixed
121
     */
122
    public function image_display($format = '')
123
    {
124
        return $this->getVar('image_display', $format);
125
    }
126
127
    /**
128
     * Returns Class Base Variable image_weight
129
     * @param  string $format
130
     * @return mixed
131
     */
132
    public function image_weight($format = '')
133
    {
134
        return $this->getVar('image_weight', $format);
135
    }
136
137
    /**
138
     * Returns Class Base Variable image_body
139
     * @param  string $format
140
     * @return mixed
141
     */
142
    public function image_body($format = '')
143
    {
144
        return $this->getVar('image_body', $format);
145
    }
146
147
    /**
148
     * Returns Class Base Variable imgcat_id
149
     * @param  string $format
150
     * @return mixed
151
     */
152
    public function imgcat_id($format = '')
153
    {
154
        return $this->getVar('imgcat_id', $format);
155
    }
156
}
157
158
/**
159
 * XOOPS image handler class.
160
 *
161
 * This class is responsible for providing data access mechanisms to the data source
162
 * of XOOPS image class objects.
163
 *
164
 * @package          kernel
165
 *
166
 * @author           Kazumi Ono     <[email protected]>
167
 * @copyright    (c) 2000-2016 XOOPS Project - www.xoops.org
168
 */
169
class XoopsImageHandler extends XoopsObjectHandler
170
{
171
    /**
172
     * Create a new {@link XoopsImage}
173
     *
174
     * @param  boolean $isNew Flag the object as "new"
175
     * @return XoopsImage
176
     **/
177
    public function create($isNew = true)
178
    {
179
        $image = new XoopsImage();
180
        if ($isNew) {
181
            $image->setNew();
182
        }
183
184
        return $image;
185
    }
186
187
    /**
188
     * Load a {@link XoopsImage} object from the database
189
     *
190
     * @param  int     $id ID
191
     * @param  boolean $getbinary
192
     * @return XoopsImage|false {@link XoopsImage}, false on fail
193
     **/
194
    public function get($id, $getbinary = true)
0 ignored issues
show
Unused Code introduced by
The parameter $getbinary is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

194
    public function get($id, /** @scrutinizer ignore-unused */ $getbinary = true)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
195
    {
196
        $image = false;
197
        $id    = (int)$id;
198
        if ($id > 0) {
199
            $sql = 'SELECT i.*, b.image_body FROM ' . $this->db->prefix('image') . ' i LEFT JOIN ' . $this->db->prefix('imagebody') . ' b ON b.image_id=i.image_id WHERE i.image_id=' . $id;
200
            $result = $this->db->query($sql);
0 ignored issues
show
Bug introduced by
The method query() does not exist on XoopsDatabase. Since it exists in all sub-types, consider adding an abstract or default implementation to XoopsDatabase. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

200
            /** @scrutinizer ignore-call */ 
201
            $result = $this->db->query($sql);
Loading history...
201
            if (!$this->db->isResultSet($result)) {
202
                return $image;
203
            }
204
            $numrows = $this->db->getRowsNum($result);
0 ignored issues
show
Bug introduced by
The method getRowsNum() does not exist on XoopsDatabase. Since it exists in all sub-types, consider adding an abstract or default implementation to XoopsDatabase. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

204
            /** @scrutinizer ignore-call */ 
205
            $numrows = $this->db->getRowsNum($result);
Loading history...
205
            if ($numrows == 1) {
206
                $image = new XoopsImage();
207
                $image->assignVars($this->db->fetchArray($result));
0 ignored issues
show
Bug introduced by
The method fetchArray() does not exist on XoopsDatabase. Since it exists in all sub-types, consider adding an abstract or default implementation to XoopsDatabase. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

207
                $image->assignVars($this->db->/** @scrutinizer ignore-call */ fetchArray($result));
Loading history...
208
            }
209
        }
210
211
        return $image;
212
    }
213
214
    /**
215
     * Write a {@link XoopsImage} object to the database
216
     *
217
     * @param  XoopsObject|XoopsImage $image a XoopsImage object
218
     *
219
     * @return bool true on success, otherwise false
220
     **/
221
    public function insert(XoopsObject $image)
222
    {
223
        $className = 'XoopsImage';
224
        if (!($image instanceof $className)) {
225
            return false;
226
        }
227
228
        if (!$image->isDirty()) {
229
            return true;
230
        }
231
        if (!$image->cleanVars()) {
232
            return false;
233
        }
234
        foreach ($image->cleanVars as $k => $v) {
235
            ${$k} = $v;
236
        }
237
        if ($image->isNew()) {
238
            $image_id = $this->db->genId('image_image_id_seq');
0 ignored issues
show
Bug introduced by
The method genId() does not exist on XoopsDatabase. Since it exists in all sub-types, consider adding an abstract or default implementation to XoopsDatabase. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

238
            /** @scrutinizer ignore-call */ 
239
            $image_id = $this->db->genId('image_image_id_seq');
Loading history...
239
            $sql      = sprintf('INSERT INTO %s (image_id, image_name, image_nicename, image_mimetype, image_created, image_display, image_weight, imgcat_id) VALUES (%u, %s, %s, %s, %u, %u, %u, %u)', $this->db->prefix('image'), $image_id, $this->db->quoteString($image_name), $this->db->quoteString($image_nicename), $this->db->quoteString($image_mimetype), time(), $image_display, $image_weight, $imgcat_id);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $image_mimetype seems to be never defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable $imgcat_id seems to be never defined.
Loading history...
Bug introduced by
The method quoteString() does not exist on XoopsDatabase. Since it exists in all sub-types, consider adding an abstract or default implementation to XoopsDatabase. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

239
            $sql      = sprintf('INSERT INTO %s (image_id, image_name, image_nicename, image_mimetype, image_created, image_display, image_weight, imgcat_id) VALUES (%u, %s, %s, %s, %u, %u, %u, %u)', $this->db->prefix('image'), $image_id, $this->db->/** @scrutinizer ignore-call */ quoteString($image_name), $this->db->quoteString($image_nicename), $this->db->quoteString($image_mimetype), time(), $image_display, $image_weight, $imgcat_id);
Loading history...
Comprehensibility Best Practice introduced by
The variable $image_display does not exist. Did you maybe mean $image_id?
Loading history...
Comprehensibility Best Practice introduced by
The variable $image_name does not exist. Did you maybe mean $image?
Loading history...
Comprehensibility Best Practice introduced by
The variable $image_nicename seems to be never defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable $image_weight does not exist. Did you maybe mean $image_id?
Loading history...
240
            if (!$result = $this->db->query($sql)) {
0 ignored issues
show
Unused Code introduced by
The assignment to $result is dead and can be removed.
Loading history...
241
                return false;
242
            }
243
            if (empty($image_id)) {
244
                $image_id = $this->db->getInsertId();
0 ignored issues
show
Bug introduced by
The method getInsertId() does not exist on XoopsDatabase. Since it exists in all sub-types, consider adding an abstract or default implementation to XoopsDatabase. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

244
                /** @scrutinizer ignore-call */ 
245
                $image_id = $this->db->getInsertId();
Loading history...
245
            }
246
            if (isset($image_body) && $image_body != '') {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $image_body does not exist. Did you maybe mean $image_id?
Loading history...
247
                $sql = sprintf('INSERT INTO %s (image_id, image_body) VALUES (%u, %s)', $this->db->prefix('imagebody'), $image_id, $this->db->quoteString($image_body));
248
                if (!$result = $this->db->query($sql)) {
249
                    $sql = sprintf('DELETE FROM %s WHERE image_id = %u', $this->db->prefix('image'), $image_id);
250
                    $this->db->query($sql);
251
252
                    return false;
253
                }
254
            }
255
            $image->assignVar('image_id', $image_id);
256
        } else {
257
            $sql = sprintf('UPDATE %s SET image_name = %s, image_nicename = %s, image_display = %u, image_weight = %u, imgcat_id = %u WHERE image_id = %u', $this->db->prefix('image'), $this->db->quoteString($image_name), $this->db->quoteString($image_nicename), $image_display, $image_weight, $imgcat_id, $image_id);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $image_id seems to be never defined.
Loading history...
258
            if (!$result = $this->db->query($sql)) {
259
                return false;
260
            }
261
            if (isset($image_body) && $image_body != '') {
262
                $sql = sprintf('UPDATE %s SET image_body = %s WHERE image_id = %u', $this->db->prefix('imagebody'), $this->db->quoteString($image_body), $image_id);
263
                if (!$result = $this->db->query($sql)) {
264
                    $this->db->query(sprintf('DELETE FROM %s WHERE image_id = %u', $this->db->prefix('image'), $image_id));
265
266
                    return false;
267
                }
268
            }
269
        }
270
271
        return true;
272
    }
273
274
    /**
275
     * Delete an image from the database
276
     *
277
     * @param  XoopsObject|XoopsImage $image a XoopsImage object
278
     *
279
     * @return bool true on success, otherwise false
280
     **/
281
    public function delete(XoopsObject $image)
282
    {
283
        $className = 'XoopsImage';
284
        if (!($image instanceof $className)) {
285
            return false;
286
        }
287
288
        $id  = $image->getVar('image_id');
289
        $sql = sprintf('DELETE FROM %s WHERE image_id = %u', $this->db->prefix('image'), $id);
0 ignored issues
show
Bug introduced by
It seems like $id can also be of type array and array; however, parameter $values of sprintf() does only seem to accept double|integer|string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

289
        $sql = sprintf('DELETE FROM %s WHERE image_id = %u', $this->db->prefix('image'), /** @scrutinizer ignore-type */ $id);
Loading history...
290
        if (!$result = $this->db->query($sql)) {
0 ignored issues
show
Unused Code introduced by
The assignment to $result is dead and can be removed.
Loading history...
291
            return false;
292
        }
293
        $sql = sprintf('DELETE FROM %s WHERE image_id = %u', $this->db->prefix('imagebody'), $id);
294
        $this->db->query($sql);
295
296
        return true;
297
    }
298
299
    /**
300
     * Load {@link XoopsImage}s from the database
301
     *
302
     * @param  CriteriaElement|CriteriaCompo $criteria  {@link CriteriaElement}
303
     * @param  boolean         $id_as_key Use the ID as key into the array
304
     * @param  boolean         $getbinary
305
     * @return array           Array of {@link XoopsImage} objects
306
     **/
307
    public function getObjects(CriteriaElement $criteria = null, $id_as_key = false, $getbinary = false)
308
    {
309
        $ret   = array();
310
        $limit = $start = 0;
311
        if ($getbinary) {
312
            $sql = 'SELECT i.*, b.image_body FROM ' . $this->db->prefix('image') . ' i LEFT JOIN ' . $this->db->prefix('imagebody') . ' b ON b.image_id=i.image_id';
313
        } else {
314
            $sql = 'SELECT * FROM ' . $this->db->prefix('image');
315
        }
316
        if (isset($criteria) && \method_exists($criteria, 'renderWhere')) {
317
            $sql .= ' ' . $criteria->renderWhere();
318
            $sort = $criteria->getSort() == '' ? 'image_weight' : $criteria->getSort();
319
            $sql .= ' ORDER BY ' . $sort . ' ' . $criteria->getOrder();
320
            $limit = $criteria->getLimit();
321
            $start = $criteria->getStart();
322
        }
323
        $result = $this->db->query($sql, $limit, $start);
324
        if (!$this->db->isResultSet($result)) {
325
            return $ret;
326
        }
327
        /** @var array $myrow */
328
        while (false !== ($myrow = $this->db->fetchArray($result))) {
329
            $image = new XoopsImage();
330
            $image->assignVars($myrow);
331
            if (!$id_as_key) {
332
                $ret[] =& $image;
333
            } else {
334
                $ret[$myrow['image_id']] =& $image;
335
            }
336
            unset($image);
337
        }
338
339
        return $ret;
340
    }
341
342
    /**
343
     * Count some images
344
     *
345
     * @param  CriteriaElement|CriteriaCompo $criteria {@link CriteriaElement}
346
     * @return int
347
     **/
348
    public function getCount(CriteriaElement $criteria = null)
349
    {
350
        $sql = 'SELECT COUNT(*) FROM ' . $this->db->prefix('image');
351
        if (isset($criteria) && \method_exists($criteria, 'renderWhere')) {
352
            $sql .= ' ' . $criteria->renderWhere();
353
        }
354
        $result = $this->db->query($sql);
355
        if (!$this->db->isResultSet($result)) {
356
            return 0;
357
        }
358
        list($count) = $this->db->fetchRow($result);
0 ignored issues
show
Bug introduced by
The method fetchRow() does not exist on XoopsDatabase. Since it exists in all sub-types, consider adding an abstract or default implementation to XoopsDatabase. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

358
        /** @scrutinizer ignore-call */ 
359
        list($count) = $this->db->fetchRow($result);
Loading history...
359
360
        return (int)$count;
361
    }
362
363
    /**
364
     * Get a list of images
365
     *
366
     * @param  int  $imgcat_id
367
     * @param  bool $image_display
368
     * @return array Array of {@link XoopsImage} objects
369
     **/
370
    public function getList($imgcat_id, $image_display = null)
371
    {
372
        $criteria = new CriteriaCompo(new Criteria('imgcat_id', (int)$imgcat_id));
373
        if (isset($image_display)) {
374
            $criteria->add(new Criteria('image_display', (int)$image_display));
375
        }
376
        $images = $this->getObjects($criteria, false, true);
377
        $ret    = array();
378
        foreach (array_keys($images) as $i) {
379
            $ret[$images[$i]->getVar('image_name')] = $images[$i]->getVar('image_nicename');
380
        }
381
382
        return $ret;
383
    }
384
}
385