Passed
Push — master ( 5e1018...b92779 )
by Richard
06:04 queued 12s
created

XoopsImageSetHandler::unlinkThemeset()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 9
nc 3
nop 2
dl 0
loc 14
rs 9.9666
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 Sets
27
 *
28
 * @package         kernel
29
 * @author          Kazumi Ono  <[email protected]>
30
 * @copyright   (c) 2000-2016 XOOPS Project - www.xoops.org
31
 */
32
class XoopsImageSet extends XoopsObject
33
{
34
    /**
35
     * XoopsImageSet constructor.
36
     */
37
    public function __construct()
38
    {
39
        parent::__construct();
40
        $this->initVar('imgset_id', XOBJ_DTYPE_INT, null, false);
41
        $this->initVar('imgset_name', XOBJ_DTYPE_TXTBOX, null, true, 50);
42
        $this->initVar('imgset_refid', XOBJ_DTYPE_INT, 0, false);
43
    }
44
45
    /**
46
     * Returns Class Base Variable imgset_id
47
     * @param  string $format
48
     * @return mixed
49
     */
50
    public function id($format = 'N')
51
    {
52
        return $this->getVar('imgset_id', $format);
53
    }
54
55
    /**
56
     * Returns Class Base Variable imgset_id
57
     * @param  string $format
58
     * @return mixed
59
     */
60
    public function imgset_id($format = '')
61
    {
62
        return $this->getVar('imgset_id', $format);
63
    }
64
65
    /**
66
     * Returns Class Base Variable imgset_name
67
     * @param  string $format
68
     * @return mixed
69
     */
70
    public function imgset_name($format = '')
71
    {
72
        return $this->getVar('imgset_name', $format);
73
    }
74
75
    /**
76
     * Returns Class Base Variable imgset_refid
77
     * @param  string $format
78
     * @return mixed
79
     */
80
    public function imgset_refid($format = '')
81
    {
82
        return $this->getVar('imgset_refid', $format);
83
    }
84
}
85
86
/**
87
 * XOOPS imageset handler class.
88
 * This class is responsible for providing data access mechanisms to the data source
89
 * of XOOPS imageset class objects.
90
 *
91
 *
92
 * @author  Kazumi Ono <[email protected]>
93
 */
94
class XoopsImageSetHandler extends XoopsObjectHandler
95
{
96
    /**
97
     * Create a new {@link XoopsImageSet}
98
     *
99
     * @param  boolean $isNew Flag the object as "new"
100
     * @return XoopsImageSet
101
     **/
102
    public function create($isNew = true)
103
    {
104
        $imgset = new XoopsImageSet();
105
        if ($isNew) {
106
            $imgset->setNew();
107
        }
108
109
        return $imgset;
110
    }
111
112
    /**
113
     * Load a {@link XoopsImageSet} object from the database
114
     *
115
     * @param int $id ID
116
     *
117
     * @internal param bool $getbinary
118
     * @return XoopsImageSet {@link XoopsImageSet}, FALSE on fail
119
     */
120
    public function get($id)
121
    {
122
        $id     = (int)$id;
123
        $imgset = false;
124
        if ($id > 0) {
125
            $sql = 'SELECT * FROM ' . $this->db->prefix('imgset') . ' WHERE imgset_id=' . $id;
126
            if (!$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

126
            if (!$result = $this->db->/** @scrutinizer ignore-call */ query($sql)) {
Loading history...
127
                return $imgset;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $imgset returns the type false which is incompatible with the documented return type XoopsImageSet.
Loading history...
128
            }
129
            $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

129
            /** @scrutinizer ignore-call */ 
130
            $numrows = $this->db->getRowsNum($result);
Loading history...
130
            if ($numrows == 1) {
131
                $imgset = new XoopsImageSet();
132
                $imgset->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

132
                $imgset->assignVars($this->db->/** @scrutinizer ignore-call */ fetchArray($result));
Loading history...
133
            }
134
        }
135
136
        return $imgset;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $imgset could also return false which is incompatible with the documented return type XoopsImageSet. Did you maybe forget to handle an error condition?

If the returned type also contains false, it is an indicator that maybe an error condition leading to the specific return statement remains unhandled.

Loading history...
137
    }
138
139
    /**
140
     * Write a {@link XoopsImageSet} object to the database
141
     *
142
     * @param  XoopsObject|XoopsImageSet $imgset a XoopsImageSet object
143
     *
144
     * @return bool true on success, otherwise false
145
     */
146
    public function insert(XoopsObject $imgset)
147
    {
148
        $className = 'XoopsImageSet';
149
        if (!($imgset instanceof $className)) {
150
            return false;
151
        }
152
153
        if (!$imgset->isDirty()) {
154
            return true;
155
        }
156
        if (!$imgset->cleanVars()) {
157
            return false;
158
        }
159
        foreach ($imgset->cleanVars as $k => $v) {
160
            ${$k} = $v;
161
        }
162
        if ($imgset->isNew()) {
163
            $imgset_id = $this->db->genId('imgset_imgset_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

163
            /** @scrutinizer ignore-call */ 
164
            $imgset_id = $this->db->genId('imgset_imgset_id_seq');
Loading history...
164
            $sql       = sprintf('INSERT INTO %s (imgset_id, imgset_name, imgset_refid) VALUES (%u, %s, %u)', $this->db->prefix('imgset'), $imgset_id, $this->db->quoteString($imgset_name), $imgset_refid);
0 ignored issues
show
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

164
            $sql       = sprintf('INSERT INTO %s (imgset_id, imgset_name, imgset_refid) VALUES (%u, %s, %u)', $this->db->prefix('imgset'), $imgset_id, $this->db->/** @scrutinizer ignore-call */ quoteString($imgset_name), $imgset_refid);
Loading history...
Comprehensibility Best Practice introduced by
The variable $imgset_name does not exist. Did you maybe mean $imgset?
Loading history...
Comprehensibility Best Practice introduced by
The variable $imgset_refid does not exist. Did you maybe mean $imgset?
Loading history...
165
        } else {
166
            $sql = sprintf('UPDATE %s SET imgset_name = %s, imgset_refid = %u WHERE imgset_id = %u', $this->db->prefix('imgset'), $this->db->quoteString($imgset_name), $imgset_refid, $imgset_id);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $imgset_id does not exist. Did you maybe mean $imgset?
Loading history...
167
        }
168
        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...
169
            return false;
170
        }
171
        if (empty($imgset_id)) {
172
            $imgset_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

172
            /** @scrutinizer ignore-call */ 
173
            $imgset_id = $this->db->getInsertId();
Loading history...
173
        }
174
        $imgset->assignVar('imgset_id', $imgset_id);
175
176
        return true;
177
    }
178
179
    /**
180
     * Delete an XoopsImageSet from the database
181
     *
182
     * @param  XoopsObject|XoopsImageSet $imgset a XoopsImageSet object
183
     *
184
     * @return bool true on success, otherwise false
185
     */
186
    public function delete(XoopsObject $imgset)
187
    {
188
        $className = 'XoopsImageSet';
189
        if (!($imgset instanceof $className)) {
190
            return false;
191
        }
192
        $sql = sprintf('DELETE FROM %s WHERE imgset_id = %u', $this->db->prefix('imgset'), $imgset->getVar('imgset_id'));
0 ignored issues
show
Bug introduced by
It seems like $imgset->getVar('imgset_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

192
        $sql = sprintf('DELETE FROM %s WHERE imgset_id = %u', $this->db->prefix('imgset'), /** @scrutinizer ignore-type */ $imgset->getVar('imgset_id'));
Loading history...
193
        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...
194
            return false;
195
        }
196
        $sql = sprintf('DELETE FROM %s WHERE imgset_id = %u', $this->db->prefix('imgset_tplset_link'), $imgset->getVar('imgset_id'));
197
        $this->db->query($sql);
198
199
        return true;
200
    }
201
202
    /**
203
     * Load {@link XoopsImageSet}s from the database
204
     *
205
     * @param CriteriaElement|CriteriaCompo $criteria  {@link CriteriaElement}
206
     * @param boolean         $id_as_key Use the ID as key into the array
207
     * @internal param bool $getbinary
208
     * @return array Array of {@link XoopsImageSet} objects
209
     */
210
    public function getObjects(CriteriaElement $criteria = null, $id_as_key = false)
211
    {
212
        $ret   = array();
213
        $limit = $start = 0;
214
        $sql   = 'SELECT DISTINCT i.* FROM ' . $this->db->prefix('imgset') . ' i LEFT JOIN ' . $this->db->prefix('imgset_tplset_link') . ' l ON l.imgset_id=i.imgset_id';
215
        if (isset($criteria) && is_subclass_of($criteria, 'CriteriaElement')) {
216
            $sql .= ' ' . $criteria->renderWhere();
0 ignored issues
show
Bug introduced by
The method renderWhere() does not exist on CriteriaElement. Did you maybe mean render()? ( Ignorable by Annotation )

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

216
            $sql .= ' ' . $criteria->/** @scrutinizer ignore-call */ renderWhere();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
217
            $limit = $criteria->getLimit();
218
            $start = $criteria->getStart();
219
        }
220
        $result = $this->db->query($sql, $limit, $start);
221
        if (!$result) {
222
            return $ret;
223
        }
224
        while (false !== ($myrow = $this->db->fetchArray($result))) {
225
            $imgset = new XoopsImageSet();
226
            $imgset->assignVars($myrow);
227
            if (!$id_as_key) {
228
                $ret[] =& $imgset;
229
            } else {
230
                $ret[$myrow['imgset_id']] =& $imgset;
231
            }
232
            unset($imgset);
233
        }
234
235
        return $ret;
236
    }
237
238
    /**
239
     * Load {@link XoopsImage ThemeSet}s into a Database
240
     *
241
     * @param  int    $imgset_id
242
     * @param  string $tplset_name
243
     * @return array
244
     */
245
    public function linkThemeset($imgset_id, $tplset_name)
246
    {
247
        $imgset_id   = (int)$imgset_id;
248
        $tplset_name = trim($tplset_name);
249
        if ($imgset_id <= 0 || $tplset_name == '') {
250
            return false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the documented return type array.
Loading history...
251
        }
252
        if (!$this->unlinkThemeset($imgset_id, $tplset_name)) {
253
            return false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the documented return type array.
Loading history...
254
        }
255
        $sql    = sprintf('INSERT INTO %s (imgset_id, tplset_name) VALUES (%u, %s)', $this->db->prefix('imgset_tplset_link'), $imgset_id, $this->db->quoteString($tplset_name));
256
        $result = $this->db->query($sql);
257
        if (!$result) {
258
            return false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the documented return type array.
Loading history...
259
        }
260
261
        return true;
0 ignored issues
show
Bug Best Practice introduced by
The expression return true returns the type true which is incompatible with the documented return type array.
Loading history...
262
    }
263
264
    /**
265
     * Load {@link XoopsImage ThemeSet}s into a Database
266
     *
267
     * @param  int    $imgset_id
268
     * @param  string $tplset_name
269
     * @return array
270
     */
271
    public function unlinkThemeset($imgset_id, $tplset_name)
272
    {
273
        $imgset_id   = (int)$imgset_id;
274
        $tplset_name = trim($tplset_name);
275
        if ($imgset_id <= 0 || $tplset_name == '') {
276
            return false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the documented return type array.
Loading history...
277
        }
278
        $sql    = sprintf('DELETE FROM %s WHERE imgset_id = %u AND tplset_name = %s', $this->db->prefix('imgset_tplset_link'), $imgset_id, $this->db->quoteString($tplset_name));
279
        $result = $this->db->query($sql);
280
        if (!$result) {
281
            return false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the documented return type array.
Loading history...
282
        }
283
284
        return true;
0 ignored issues
show
Bug Best Practice introduced by
The expression return true returns the type true which is incompatible with the documented return type array.
Loading history...
285
    }
286
287
    /**
288
     * Get a list of XoopsImageSet
289
     *
290
     * @param null $refid
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $refid is correct as it would always require null to be passed?
Loading history...
291
     * @param null $tplset
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $tplset is correct as it would always require null to be passed?
Loading history...
292
     * @internal param int $imgcat_id
293
     * @internal param bool $image_display
294
     * @return array Array of {@link XoopsImage} objects
295
     */
296
    public function getList($refid = null, $tplset = null)
297
    {
298
        $criteria = new CriteriaCompo();
299
        if (isset($refid)) {
300
            $criteria->add(new Criteria('imgset_refid', (int)$refid));
301
        }
302
        if (isset($tplset)) {
303
            $criteria->add(new Criteria('tplset_name', $tplset));
304
        }
305
        $imgsets = $this->getObjects($criteria, true);
306
        $ret     = array();
307
        foreach (array_keys($imgsets) as $i) {
308
            $ret[$i] = $imgsets[$i]->getVar('imgset_name');
309
        }
310
311
        return $ret;
312
    }
313
}
314