XoopsImageHandler   A
last analyzed

Complexity

Total Complexity 29

Size/Duplication

Total Lines 184
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 184
rs 10
c 0
b 0
f 0
wmc 29

6 Methods

Rating   Name   Duplication   Size   Complexity  
C insert() 0 53 9
A getCount() 0 12 4
A create() 0 8 2
A getList() 0 13 3
A delete() 0 14 3
C getObjects() 0 32 8
1
<?php
2
/*
3
 * You may not change or alter any portion of this comment or credits
4
 * of supporting developers from this source code or any supporting source code
5
 * which is considered copyrighted (c) material of the original comment or credit authors.
6
 *
7
 * This program is distributed in the hope that it will be useful,
8
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10
 */
11
12
/**
13
 * @copyright    XOOPS Project https://xoops.org/
14
 * @license      GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
15
 * @package
16
 * @since
17
 * @author       XOOPS Development Team, Kazumi Ono (AKA onokazu)
18
 */
19
20
defined('XOOPS_ROOT_PATH') || exit('XOOPS Root Path not defined');
21
22
/**
23
 * An Image
24
 *
25
 * @package       kernel
26
 * @author        Kazumi Ono  <[email protected]>
27
 * @copyright (c) 2000-2003 The Xoops Project - www.xoops.org
28
 */
29
class XoopsImage extends XoopsObject
0 ignored issues
show
Bug introduced by
The type XoopsObject was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
30
{
31
    /**
32
     * Constructor
33
     **/
34
    public function __construct()
35
    {
36
        parent::__construct();
37
        $this->initVar('image_id', XOBJ_DTYPE_INT, null, false);
0 ignored issues
show
Bug introduced by
The constant XOBJ_DTYPE_INT was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
38
        $this->initVar('image_name', XOBJ_DTYPE_OTHER, null, false, 30);
0 ignored issues
show
Bug introduced by
The constant XOBJ_DTYPE_OTHER was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
39
        $this->initVar('image_nicename', XOBJ_DTYPE_TXTBOX, null, true, 100);
0 ignored issues
show
Bug introduced by
The constant XOBJ_DTYPE_TXTBOX was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
40
        $this->initVar('image_mimetype', XOBJ_DTYPE_OTHER, null, false);
41
        $this->initVar('image_created', XOBJ_DTYPE_INT, null, false);
42
        $this->initVar('image_display', XOBJ_DTYPE_INT, 1, false);
43
        $this->initVar('image_weight', XOBJ_DTYPE_INT, 0, false);
44
        $this->initVar('image_body', XOBJ_DTYPE_SOURCE, null, true);
0 ignored issues
show
Bug introduced by
The constant XOBJ_DTYPE_SOURCE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
45
        $this->initVar('imgcat_id', XOBJ_DTYPE_INT, 0, false);
46
    }
47
}
48
49
/**
50
 * XOOPS image handler class.
51
 *
52
 * This class is responsible for providing data access mechanisms to the data source
53
 * of XOOPS image class objects.
54
 *
55
 * @package       kernel
56
 *
57
 * @author        Kazumi Ono  <[email protected]>
58
 * @copyright (c) 2000-2003 The Xoops Project - www.xoops.org
59
 */
60
class XoopsImageHandler extends XoopsObjectHandler
0 ignored issues
show
Bug introduced by
The type XoopsObjectHandler was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
61
{
62
    /**
63
     * Create a new {@link XoopsImage}
64
     *
65
     * @param   boolean $isNew Flag the object as "new"
66
     * @return  XoopsImage
67
     **/
68
    public function &create($isNew = true)
69
    {
70
        $image = new XoopsImage();
71
        if ($isNew) {
72
            $image->setNew();
73
        }
74
75
        return $image;
76
    }
77
78
    /**
79
     * Write a {@link XoopsImage} object to the database
80
     *
81
     * @param XoopsObject $image
82
     * @param string             $itemid
83
     * @return bool
84
     */
85
    public function insert(XoopsObject $image, $itemid = '0')
86
    {
87
        global $image_name;
88
        if ($itemid == '0') {
89
            return false;
90
        }
91
        //if (strtolower(get_class($image)) != 'xoopsimage') {
0 ignored issues
show
Unused Code Comprehensibility introduced by
62% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
92
        //    return false;
93
        //}
94
        if (!$image->isDirty()) {
95
            return true;
96
        }
97
        if (!$image->cleanVars()) {
98
            return false;
99
        }
100
        foreach ($image->cleanVars as $k => $v) {
101
            ${$k} = $v;
102
        }
103
        if ($image->isNew()) {
104
            $image_id = $this->db->genId('image_image_id_seq');
105
            $sql      = sprintf('INSERT INTO %s WHERE itemid=' . $itemid . ' (logourl) VALUES (%s)', $this->db->prefix($module->getVar('dirname', 'n') . '_items'), $this->db->quoteString($image_name));
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $module seems to be never defined.
Loading history...
106
            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...
107
                return false;
108
            }
109
            if (empty($image_id)) {
110
                $image_id = $this->db->getInsertId();
0 ignored issues
show
Unused Code introduced by
The assignment to $image_id is dead and can be removed.
Loading history...
111
            }
112
            /*             if (isset($image_body) && $image_body != '') {
0 ignored issues
show
Unused Code Comprehensibility introduced by
62% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
113
            $sql = sprintf("INSERT INTO %s (image_id, image_body) VALUES (%u, %s)", $this->db->prefix('imagebody'), $image_id, $this->db->quoteString($image_body));
114
            if (!$result = $this->db->query($sql)) {
115
            $sql = sprintf("DELETE FROM %s WHERE image_id = %u", $this->db->prefix('image'), $image_id);
116
            $this->db->query($sql);
117
118
            return false;
119
            }
120
            } */
121
            /* $image->assignVar('image_id', $image_id); */
0 ignored issues
show
Unused Code Comprehensibility introduced by
73% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
122
        } else {
123
            $sql = sprintf('UPDATE %s SET image_name = %s WHERE itemid = %u', $this->db->prefix($module->getVar('dirname', 'n') . '_items'), $this->db->quoteString($image_name));
124
            if (!$result = $this->db->query($sql)) {
125
                return false;
126
            }
127
            /*             if (isset($image_body) && $image_body != '') {
0 ignored issues
show
Unused Code Comprehensibility introduced by
63% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
128
            $sql = sprintf("UPDATE %s SET image_body = %s WHERE image_id = %u", $this->db->prefix('imagebody'), $this->db->quoteString($image_body), $image_id);
129
            if (!$result = $this->db->query($sql)) {
130
            $this->db->query(sprintf("DELETE FROM %s WHERE image_id = %u", $this->db->prefix('image'), $image_id));
131
132
            return false;
133
            }
134
            } */
135
        }
136
137
        return true;
138
    }
139
140
    /**
141
     * Delete an image from the database
142
     *
143
     * @param XoopsObject $image
144
     * @return bool
145
     */
146
    public function delete(XoopsObject $image)
147
    {
148
        if (strtolower(get_class($image)) !== 'xoopsimage') {
149
            return false;
150
        }
151
        $id  = $image->getVar('image_id');
152
        $sql = sprintf('DELETE FROM %s WHERE image_id = %u', $this->db->prefix('image'), $id);
153
        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...
154
            return false;
155
        }
156
        $sql = sprintf('DELETE FROM %s WHERE image_id = %u', $this->db->prefix('imagebody'), $id);
157
        $this->db->query($sql);
158
159
        return true;
160
    }
161
162
    /**
163
     * Load {@link XoopsImage}s from the database
164
     *
165
     * @param   $criteria  {@link CriteriaElement}
166
     * @param   boolean $id_as_key Use the ID as key into the array
167
     * @param   boolean $getbinary
168
     * @return  array   Array of {@link XoopsImage} objects
169
     **/
170
    public function &getObjects(CriteriaElement $criteria = null, $id_as_key = false, $getbinary = false)
0 ignored issues
show
Bug introduced by
The type CriteriaElement was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
171
    {
172
        $ret   = array();
173
        $limit = $start = 0;
174
        if ($getbinary) {
175
            $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';
176
        } else {
177
            $sql = 'SELECT * FROM ' . $this->db->prefix('image');
178
        }
179
        if (isset($criteria) && is_subclass_of($criteria, 'criteriaelement')) {
180
            $sql   .= ' ' . $criteria->renderWhere();
181
            $sort  = !in_array($criteria->getSort(), array('image_id', 'image_created', 'image_mimetype', 'image_display', 'image_weight')) ? 'image_weight' : $criteria->getSort();
182
            $sql   .= ' ORDER BY ' . $sort . ' ' . $criteria->getOrder();
183
            $limit = $criteria->getLimit();
184
            $start = $criteria->getStart();
185
        }
186
        $result = $this->db->query($sql, $limit, $start);
187
        if (!$result) {
188
            return $ret;
189
        }
190
        while ($myrow = $this->db->fetchArray($result)) {
191
            $image = new XoopsImage();
192
            $image->assignVars($myrow);
193
            if (!$id_as_key) {
194
                $ret[] =& $image;
195
            } else {
196
                $ret[$myrow['image_id']] =& $image;
197
            }
198
            unset($image);
199
        }
200
201
        return $ret;
202
    }
203
204
    /**
205
     * Count some images
206
     *
207
     * @param   $criteria {@link CriteriaElement}
208
     * @return  int
209
     **/
210
    public function getCount(CriteriaElement $criteria = null)
211
    {
212
        $sql = 'SELECT COUNT(*) FROM ' . $this->db->prefix('image');
213
        if (isset($criteria) && is_subclass_of($criteria, 'criteriaelement')) {
214
            $sql .= ' ' . $criteria->renderWhere();
215
        }
216
        if (!$result = $this->db->query($sql)) {
217
            return 0;
218
        }
219
        list($count) = $this->db->fetchRow($result);
220
221
        return $count;
222
    }
223
224
    /**
225
     * Get a list of images
226
     *
227
     * @param   int  $imgcat_id
228
     * @param   bool $image_display
229
     * @return  array   Array of {@link XoopsImage} objects
230
     **/
231
    public function &getList($imgcat_id, $image_display = null)
232
    {
233
        $criteria = new CriteriaCompo(new Criteria('imgcat_id', (int)$imgcat_id));
0 ignored issues
show
Bug introduced by
The type CriteriaCompo was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
Bug introduced by
The type Criteria was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
234
        if (isset($image_display)) {
235
            $criteria->add(new Criteria('image_display', (int)$image_display));
236
        }
237
        $images = $this->getObjects($criteria, false, true);
238
        $ret    = array();
239
        foreach (array_keys($images) as $i) {
240
            $ret[$images[$i]->getVar('image_name')] = $images[$i]->getVar('image_nicename');
241
        }
242
243
        return $ret;
244
    }
245
}
246