Passed
Branch feature/php-docs2 (404c3e)
by Michael
05:35
created

XoopsImageCategoryHandler::get()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 11
nc 4
nop 1
dl 0
loc 17
rs 9.9
c 0
b 0
f 0
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
 * @author              Kazumi Ono <[email protected]>
22
 * @copyright       (c) 2000-2016 XOOPS Project (www.xoops.org)
23
 **/
24
25
/**
26
 * XOOPS Image Category
27
 *
28
 * @package             kernel
29
 *
30
 * @author              Kazumi Ono    <[email protected]>
31
 * @copyright       (c) 2000-2016 XOOPS Project (www.xoops.org)
32
 */
33
class XoopsImageCategory extends XoopsObject
34
{
35
    /**
36
     * @var int
37
     */
38
    public $_imageCount;
39
40
    /**
41
     * Constructor
42
     **/
43
    public function __construct()
44
    {
45
        parent::__construct();
46
        $this->initVar('imgcat_id', XOBJ_DTYPE_INT, null, false);
47
        $this->initVar('imgcat_name', XOBJ_DTYPE_TXTBOX, null, true, 100);
48
        $this->initVar('imgcat_display', XOBJ_DTYPE_INT, 1, false);
49
        $this->initVar('imgcat_weight', XOBJ_DTYPE_INT, 0, false);
50
        $this->initVar('imgcat_maxsize', XOBJ_DTYPE_INT, 0, false);
51
        $this->initVar('imgcat_maxwidth', XOBJ_DTYPE_INT, 0, false);
52
        $this->initVar('imgcat_maxheight', XOBJ_DTYPE_INT, 0, false);
53
        $this->initVar('imgcat_type', XOBJ_DTYPE_OTHER, null, false);
54
        $this->initVar('imgcat_storetype', XOBJ_DTYPE_OTHER, null, false);
55
    }
56
57
    /**
58
     * Returns Class Base Variable imgcat_id
59
     * @param  string $format
60
     * @return mixed
61
     */
62
    public function id($format = 'N')
63
    {
64
        return $this->getVar('imgcat_id', $format);
65
    }
66
67
    /**
68
     * Returns Class Base Variable imgcat_id
69
     * @param  string $format
70
     * @return mixed
71
     */
72
    public function imgcat_id($format = '')
73
    {
74
        return $this->getVar('imgcat_id', $format);
75
    }
76
77
    /**
78
     * Returns Class Base Variable imgcat_name
79
     * @param  string $format
80
     * @return mixed
81
     */
82
    public function imgcat_name($format = '')
83
    {
84
        return $this->getVar('imgcat_name', $format);
85
    }
86
87
    /**
88
     * Returns Class Base Variable imgcat_display
89
     * @param  string $format
90
     * @return mixed
91
     */
92
    public function imgcat_display($format = '')
93
    {
94
        return $this->getVar('imgcat_display', $format);
95
    }
96
97
    /**
98
     * Returns Class Base Variable imgcat_weight
99
     * @param  string $format
100
     * @return mixed
101
     */
102
    public function imgcat_weight($format = '')
103
    {
104
        return $this->getVar('imgcat_weight', $format);
105
    }
106
107
    /**
108
     * Returns Class Base Variable imgcat_maxsize
109
     * @param  string $format
110
     * @return mixed
111
     */
112
    public function imgcat_maxsize($format = '')
113
    {
114
        return $this->getVar('imgcat_maxsize', $format);
115
    }
116
117
    /**
118
     * Returns Class Base Variable imgcat_maxwidth
119
     * @param  string $format
120
     * @return mixed
121
     */
122
    public function imgcat_maxwidth($format = '')
123
    {
124
        return $this->getVar('imgcat_maxwidth', $format);
125
    }
126
127
    /**
128
     * Returns Class Base Variable imgcat_maxheight
129
     * @param  string $format
130
     * @return mixed
131
     */
132
    public function imgcat_maxheight($format = '')
133
    {
134
        return $this->getVar('imgcat_maxheight', $format);
135
    }
136
137
    /**
138
     * Returns Class Base Variable imgcat_type
139
     * @param  string $format
140
     * @return mixed
141
     */
142
    public function imgcat_type($format = '')
143
    {
144
        return $this->getVar('imgcat_type', $format);
145
    }
146
147
    /**
148
     * Returns Class Base Variable imgcat_storetype
149
     * @param  string $format
150
     * @return mixed
151
     */
152
    public function imgcat_storetype($format = '')
153
    {
154
        return $this->getVar('imgcat_storetype', $format);
155
    }
156
157
    /**
158
     * Enter description here...
159
     *
160
     * @param int $value
161
     */
162
    public function setImageCount($value)
163
    {
164
        $this->_imageCount = (int)$value;
165
    }
166
167
    /**
168
     * Enter description here...
169
     *
170
     * @return int
171
     */
172
    public function getImageCount()
173
    {
174
        return $this->_imageCount;
175
    }
176
}
177
178
/**
179
 * XOOPS image category handler class.
180
 * This class is responsible for providing data access mechanisms to the data source
181
 * of XOOPS image category class objects.
182
 *
183
 * @author  Kazumi Ono <[email protected]>
184
 *
185
 * @property XoopsMySQLDatabase $db
186
 */
187
class XoopsImageCategoryHandler extends XoopsObjectHandler
188
{
189
    /**
190
     * Create a new {@link XoopsImageCategory}
191
     *
192
     * @param  bool $isNew Flag the object as "new"
193
     * @return XoopsImageCategory
194
     **/
195
    public function create($isNew = true)
196
    {
197
        $imgcat = new XoopsImageCategory();
198
        if ($isNew) {
199
            $imgcat->setNew();
200
        }
201
202
        return $imgcat;
203
    }
204
205
    /**
206
     * Load a {@link XoopsImageCategory} object from the database
207
     *
208
     * @param int $id ID
209
     *
210
     * @return false|\XoopsImageCategory {@link XoopsImageCategory}, FALSE on fail
211
     * @internal param bool $getbinary
212
     */
213
    public function get($id)
214
    {
215
        $id = (int)$id;
216
        $imgcat = false;
217
        if ($id > 0) {
218
            $sql = 'SELECT * FROM ' . $this->db->prefix('imagecategory') . ' WHERE imgcat_id=' . $id;
219
            if (!$result = $this->db->query($sql)) {
220
                return $imgcat;
221
            }
222
            $numrows = $this->db->getRowsNum($result);
0 ignored issues
show
Bug introduced by
It seems like $result can also be of type true; however, parameter $result of XoopsMySQLDatabase::getRowsNum() does only seem to accept mysqli_result, 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

222
            $numrows = $this->db->getRowsNum(/** @scrutinizer ignore-type */ $result);
Loading history...
223
            if ($numrows == 1) {
224
                $imgcat = new \XoopsImageCategory();
225
                $imgcat->assignVars($this->db->fetchArray($result));
0 ignored issues
show
Bug introduced by
It seems like $this->db->fetchArray($result) can also be of type false; however, parameter $var_arr of XoopsObject::assignVars() does only seem to accept array, 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

225
                $imgcat->assignVars(/** @scrutinizer ignore-type */ $this->db->fetchArray($result));
Loading history...
Bug introduced by
It seems like $result can also be of type true; however, parameter $result of XoopsMySQLDatabase::fetchArray() does only seem to accept mysqli_result, 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

225
                $imgcat->assignVars($this->db->fetchArray(/** @scrutinizer ignore-type */ $result));
Loading history...
226
            }
227
        }
228
229
        return $imgcat;
230
    }
231
232
    /**
233
     * Write a {@link XoopsImageCategory} object to the database
234
     *
235
     * @param  XoopsObject|XoopsImageCategory $imgcat a XoopsImageCategory object
236
     *
237
     * @return bool true on success, otherwise false
238
     **/
239
    public function insert(XoopsObject $imgcat)
240
    {
241
        $className = 'XoopsImageCategory';
242
        if (!($imgcat instanceof $className)) {
243
            return false;
244
        }
245
246
        if (!$imgcat->isDirty()) {
247
            return true;
248
        }
249
        if (!$imgcat->cleanVars()) {
250
            return false;
251
        }
252
        foreach ($imgcat->cleanVars as $k => $v) {
253
            ${$k} = $v;
254
        }
255
        if ($imgcat->isNew()) {
256
            $imgcat_id = $this->db->genId('imgcat_imgcat_id_seq');
257
            $sql       = sprintf('INSERT INTO %s (imgcat_id, imgcat_name, imgcat_display, imgcat_weight, imgcat_maxsize, imgcat_maxwidth, imgcat_maxheight, imgcat_type, imgcat_storetype) VALUES (%u, %s, %u, %u, %u, %u, %u, %s, %s)', $this->db->prefix('imagecategory'), $imgcat_id, $this->db->quoteString($imgcat_name), $imgcat_display, $imgcat_weight, $imgcat_maxsize, $imgcat_maxwidth, $imgcat_maxheight, $this->db->quoteString($imgcat_type), $this->db->quoteString($imgcat_storetype));
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $imgcat_maxsize does not exist. Did you maybe mean $imgcat?
Loading history...
Comprehensibility Best Practice introduced by
The variable $imgcat_type does not exist. Did you maybe mean $imgcat?
Loading history...
Comprehensibility Best Practice introduced by
The variable $imgcat_storetype does not exist. Did you maybe mean $imgcat?
Loading history...
Comprehensibility Best Practice introduced by
The variable $imgcat_display does not exist. Did you maybe mean $imgcat?
Loading history...
Comprehensibility Best Practice introduced by
The variable $imgcat_maxheight does not exist. Did you maybe mean $imgcat?
Loading history...
Comprehensibility Best Practice introduced by
The variable $imgcat_maxwidth does not exist. Did you maybe mean $imgcat?
Loading history...
Comprehensibility Best Practice introduced by
The variable $imgcat_name does not exist. Did you maybe mean $imgcat?
Loading history...
Comprehensibility Best Practice introduced by
The variable $imgcat_weight does not exist. Did you maybe mean $imgcat?
Loading history...
258
        } else {
259
            $sql = sprintf('UPDATE %s SET imgcat_name = %s, imgcat_display = %u, imgcat_weight = %u, imgcat_maxsize = %u, imgcat_maxwidth = %u, imgcat_maxheight = %u, imgcat_type = %s WHERE imgcat_id = %u', $this->db->prefix('imagecategory'), $this->db->quoteString($imgcat_name), $imgcat_display, $imgcat_weight, $imgcat_maxsize, $imgcat_maxwidth, $imgcat_maxheight, $this->db->quoteString($imgcat_type), $imgcat_id);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $imgcat_id seems to be never defined.
Loading history...
260
        }
261
        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...
262
            return false;
263
        }
264
        if (empty($imgcat_id)) {
265
            $imgcat_id = $this->db->getInsertId();
266
        }
267
        $imgcat->assignVar('imgcat_id', $imgcat_id);
268
269
        return true;
270
    }
271
272
    /**
273
     * Delete an image from the database
274
     *
275
     * @param  XoopsObject|XoopsImageCategory $imgcat a XoopsImageCategory object
276
     *
277
     * @return bool true on success, otherwise false
278
     **/
279
    public function delete(XoopsObject $imgcat)
280
    {
281
        $className = 'XoopsImageCategory';
282
        if (!($imgcat instanceof $className)) {
283
            return false;
284
        }
285
286
        $sql = sprintf('DELETE FROM %s WHERE imgcat_id = %u', $this->db->prefix('imagecategory'), $imgcat->getVar('imgcat_id'));
0 ignored issues
show
Bug introduced by
It seems like $imgcat->getVar('imgcat_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

286
        $sql = sprintf('DELETE FROM %s WHERE imgcat_id = %u', $this->db->prefix('imagecategory'), /** @scrutinizer ignore-type */ $imgcat->getVar('imgcat_id'));
Loading history...
287
        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...
288
            return false;
289
        }
290
291
        return true;
292
    }
293
294
    /**
295
     * Enter description here...
296
     *
297
     * @param \CriteriaElement|null $criteria
298
     * @param  bool            $id_as_key if true, use id as array key
299
     * @return array of XoopsImageCategory objects
300
     */
301
    public function getObjects(CriteriaElement $criteria = null, $id_as_key = false)
302
    {
303
        $ret   = array();
304
        $limit = $start = 0;
305
        $sql   = 'SELECT DISTINCT c.* FROM ' . $this->db->prefix('imagecategory') . ' c LEFT JOIN ' . $this->db->prefix('group_permission') . " l ON l.gperm_itemid=c.imgcat_id WHERE (l.gperm_name = 'imgcat_read' OR l.gperm_name = 'imgcat_write')";
306
        if (isset($criteria) && is_subclass_of($criteria, 'CriteriaElement')) {
307
            $where = $criteria->render();
308
            $sql .= ($where != '') ? ' AND ' . $where : '';
309
            $limit = $criteria->getLimit();
310
            $start = $criteria->getStart();
311
        }
312
        $sql .= ' ORDER BY imgcat_weight, imgcat_id ASC';
313
        $result = $this->db->query($sql, $limit, $start);
314
        if (!$result) {
315
            return $ret;
316
        }
317
        while (false !== ($myrow = $this->db->fetchArray($result))) {
0 ignored issues
show
Bug introduced by
It seems like $result can also be of type true; however, parameter $result of XoopsMySQLDatabase::fetchArray() does only seem to accept mysqli_result, 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

317
        while (false !== ($myrow = $this->db->fetchArray(/** @scrutinizer ignore-type */ $result))) {
Loading history...
318
            $imgcat = new XoopsImageCategory();
319
            $imgcat->assignVars($myrow);
320
            if (!$id_as_key) {
321
                $ret[] = $imgcat;
322
            } else {
323
                $ret[$myrow['imgcat_id']] = $imgcat;
324
            }
325
            unset($imgcat);
326
        }
327
328
        return $ret;
329
    }
330
331
    /**
332
     * Count some images
333
     *
334
     * @param \CriteriaElement|null $criteria {@link CriteriaElement}
335
     * @return int
336
     **/
337
    public function getCount(CriteriaElement $criteria = null)
338
    {
339
        $sql = 'SELECT COUNT(*) FROM ' . $this->db->prefix('imagecategory') . ' i LEFT JOIN ' . $this->db->prefix('group_permission') . " l ON l.gperm_itemid=i.imgcat_id WHERE (l.gperm_name = 'imgcat_read' OR l.gperm_name = 'imgcat_write')";
340
        if (isset($criteria) && is_subclass_of($criteria, 'CriteriaElement')) {
341
            $where = $criteria->render();
342
            $sql .= ($where != '') ? ' AND ' . $where : '';
343
        }
344
        if (!$result = $this->db->query($sql)) {
345
            return 0;
346
        }
347
        list($count) = $this->db->fetchRow($result);
0 ignored issues
show
Bug introduced by
It seems like $result can also be of type true; however, parameter $result of XoopsMySQLDatabase::fetchRow() does only seem to accept mysqli_result, 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

347
        list($count) = $this->db->fetchRow(/** @scrutinizer ignore-type */ $result);
Loading history...
348
349
        return $count;
350
    }
351
352
    /**
353
     * Get a list of image categories
354
     *
355
     * @param array    $groups
356
     * @param string   $perm
357
     * @param int|null $display
358
     * @param int|null $storetype
359
     * @return array Array of {@link XoopsImage} objects
360
     * @internal param bool $image_display
361
     * @internal param int $imgcat_id
362
     */
363
    public function getList($groups = array(), $perm = 'imgcat_read', $display = null, $storetype = null)
364
    {
365
        $criteria = new CriteriaCompo();
366
        if (is_array($groups) && !empty($groups)) {
367
            $criteriaTray = new CriteriaCompo();
368
            foreach ($groups as $gid) {
369
                $criteriaTray->add(new Criteria('gperm_groupid', $gid), 'OR');
370
            }
371
            $criteria->add($criteriaTray);
372
            if ($perm === 'imgcat_read' || $perm === 'imgcat_write') {
373
                $criteria->add(new Criteria('gperm_name', $perm));
374
                $criteria->add(new Criteria('gperm_modid', 1));
375
            }
376
        }
377
        if (isset($display)) {
378
            $criteria->add(new Criteria('imgcat_display', (string)$display));
379
        }
380
        if (isset($storetype)) {
381
            $criteria->add(new Criteria('imgcat_storetype', (string)$storetype));
382
        }
383
        $categories = $this->getObjects($criteria, true);
384
        $ret        = array();
385
        foreach (array_keys($categories) as $i) {
386
            $ret[$i] = $categories[$i]->getVar('imgcat_name');
387
        }
388
389
        return $ret;
390
    }
391
}
392