Passed
Push — master ( fcd029...abd998 )
by Richard
05:25 queued 16s
created

XoopsConfigOptionHandler::getCount()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 8
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 11
rs 10
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
 * A Config-Option
22
 *
23
 * @author              Kazumi Ono    <[email protected]>
24
 * @copyright       (c) 2000-2016 XOOPS Project (www.xoops.org)
25
 *
26
 * @package             kernel
27
 */
28
class XoopsConfigOption extends XoopsObject
29
{
30
    /**
31
     * Constructor
32
     */
33
    public function __construct()
34
    {
35
        parent::__construct();
36
        $this->initVar('confop_id', XOBJ_DTYPE_INT, null);
37
        $this->initVar('confop_name', XOBJ_DTYPE_TXTBOX, null, true, 255);
38
        $this->initVar('confop_value', XOBJ_DTYPE_TXTBOX, null, true, 255);
39
        $this->initVar('conf_id', XOBJ_DTYPE_INT, 0);
40
    }
41
42
    /**
43
     * Returns Class Base Variable confop_id
44
     * @param  string $format
45
     * @return mixed
46
     */
47
    public function id($format = 'N')
48
    {
49
        return $this->getVar('confop_id', $format);
50
    }
51
52
    /**
53
     * Returns Class Base Variable confop_id
54
     * @param  string $format
55
     * @return mixed
56
     */
57
    public function confop_id($format = '')
58
    {
59
        return $this->getVar('confop_id', $format);
60
    }
61
62
    /**
63
     * Returns Class Base Variable confop_name
64
     * @param  string $format
65
     * @return mixed
66
     */
67
    public function confop_name($format = '')
68
    {
69
        return $this->getVar('confop_name', $format);
70
    }
71
72
    /**
73
     * Returns Class Base Variable confop_value
74
     * @param  string $format
75
     * @return mixed
76
     */
77
    public function confop_value($format = '')
78
    {
79
        return $this->getVar('confop_value', $format);
80
    }
81
82
    /**
83
     * Returns Class Base Variable conf_id
84
     * @param  string $format
85
     * @return mixed
86
     */
87
    public function conf_id($format = '')
88
    {
89
        return $this->getVar('conf_id', $format);
90
    }
91
}
92
93
/**
94
 * XOOPS configuration option handler class.
95
 * This class is responsible for providing data access mechanisms to the data source
96
 * of XOOPS configuration option class objects.
97
 *
98
 * @copyright       (c) 2000-2016 XOOPS Project (www.xoops.org)
99
 * @author              Kazumi Ono <[email protected]>
100
 *
101
 * @package             kernel
102
 * @subpackage          config
103
 */
104
class XoopsConfigOptionHandler extends XoopsObjectHandler
105
{
106
    /**
107
     * Create a new option
108
     *
109
     * @param bool $isNew Flag the option as "new"?
110
     *
111
     * @return XoopsConfigOption {@link XoopsConfigOption}
112
     */
113
    public function create($isNew = true)
114
    {
115
        $confoption = new XoopsConfigOption();
116
        if ($isNew) {
117
            $confoption->setNew();
118
        }
119
120
        return $confoption;
121
    }
122
123
    /**
124
     * Get an option from the database
125
     *
126
     * @param int $id ID of the option
127
     *
128
     * @return XoopsConfigOption reference to the {@link XoopsConfigOption}, FALSE on fail
129
     */
130
    public function get($id)
131
    {
132
        $confoption = false;
133
        $id         = (int)$id;
134
        if ($id > 0) {
135
            $sql = 'SELECT * FROM ' . $this->db->prefix('configoption') . ' WHERE confop_id=' . $id;
136
            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

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

139
            /** @scrutinizer ignore-call */ 
140
            $numrows = $this->db->getRowsNum($result);
Loading history...
140
            if ($numrows == 1) {
141
                $confoption = new XoopsConfigOption();
142
                $confoption->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

142
                $confoption->assignVars($this->db->/** @scrutinizer ignore-call */ fetchArray($result));
Loading history...
143
            }
144
        }
145
146
        return $confoption;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $confoption could also return false which is incompatible with the documented return type XoopsConfigOption. 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...
147
    }
148
149
    /**
150
     * Insert a new {@link XoopsConfigOption}
151
     *
152
     * @param XoopsObject|XoopsConfigOption $confoption a XoopsConfigOption object
153
     *
154
     * @return bool true on success, otherwise false
155
     */
156
    public function insert(XoopsObject $confoption)
157
    {
158
        $className = 'XoopsConfigOption';
159
        if (!($confoption instanceof $className)) {
160
            return false;
161
        }
162
        if (!$confoption->isDirty()) {
163
            return true;
164
        }
165
        if (!$confoption->cleanVars()) {
166
            return false;
167
        }
168
169
        $confop_id = $confoption->getVar('confop_id');
170
        $confop_name = $confoption->getVar('confop_name');
171
        $confop_value = $confoption->getVar('confop_value');
172
        $conf_id = $confoption->getVar('conf_id');
173
174
        if ($confoption->isNew()) {
175
            $confop_id = $this->db->genId('configoption_confop_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

175
            /** @scrutinizer ignore-call */ 
176
            $confop_id = $this->db->genId('configoption_confop_id_seq');
Loading history...
176
            $sql       = sprintf(
177
                'INSERT INTO %s (confop_id, confop_name, confop_value, conf_id) VALUES (%u, %s, %s, %u)',
178
                $this->db->prefix('configoption'),
179
                $confop_id,
180
                $this->db->quote($confop_name),
0 ignored issues
show
Bug introduced by
The method quote() 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

180
                $this->db->/** @scrutinizer ignore-call */ 
181
                           quote($confop_name),
Loading history...
181
                $this->db->quote($confop_value),
182
                $conf_id
0 ignored issues
show
Bug introduced by
It seems like $conf_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

182
                /** @scrutinizer ignore-type */ $conf_id
Loading history...
183
            );
184
        } else {
185
            $sql = sprintf(
186
                'UPDATE %s SET confop_name = %s, confop_value = %s WHERE confop_id = %u',
187
                $this->db->prefix('configoption'),
188
                $this->db->quote($confop_name),
189
                $this->db->quote($confop_value),
190
                $confop_id
191
            );
192
        }
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
        if (empty($confop_id)) {
197
            $confop_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

197
            /** @scrutinizer ignore-call */ 
198
            $confop_id = $this->db->getInsertId();
Loading history...
198
        }
199
        $confoption->assignVar('confop_id', $confop_id);
200
201
        return $confop_id;
202
    }
203
204
    /**
205
     * Delete a {@link XoopsConfigOption}
206
     *
207
     * @param XoopsObject|XoopsConfigOption $confoption a XoopsConfigOption object
208
     *
209
     * @return bool true on success, otherwise false
210
     */
211
    public function delete(XoopsObject $confoption)
212
    {
213
        $className = 'XoopsConfigOption';
214
        if (!($confoption instanceof $className)) {
215
            return false;
216
        }
217
        $sql = sprintf('DELETE FROM %s WHERE confop_id = %u', $this->db->prefix('configoption'), $confoption->getVar('confop_id'));
0 ignored issues
show
Bug introduced by
It seems like $confoption->getVar('confop_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

217
        $sql = sprintf('DELETE FROM %s WHERE confop_id = %u', $this->db->prefix('configoption'), /** @scrutinizer ignore-type */ $confoption->getVar('confop_id'));
Loading history...
218
        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...
219
            return false;
220
        }
221
222
        return true;
223
    }
224
225
    /**
226
     * Get some {@link XoopsConfigOption}s
227
     *
228
     * @param CriteriaElement|CriteriaCompo $criteria  {@link CriteriaElement}
229
     * @param bool            $id_as_key Use the IDs as array-keys?
230
     *
231
     * @return array Array of {@link XoopsConfigOption}s
232
     */
233
    public function getObjects(CriteriaElement $criteria = null, $id_as_key = false)
234
    {
235
        $ret   = array();
236
        $limit = $start = 0;
237
        $sql   = 'SELECT * FROM ' . $this->db->prefix('configoption');
238
        if (isset($criteria) && is_subclass_of($criteria, 'CriteriaElement')) {
239
            $sql .= ' ' . $criteria->renderWhere() . ' ORDER BY confop_id ' . $criteria->getOrder();
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

239
            $sql .= ' ' . $criteria->/** @scrutinizer ignore-call */ renderWhere() . ' ORDER BY confop_id ' . $criteria->getOrder();

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...
240
            $limit = $criteria->getLimit();
241
            $start = $criteria->getStart();
242
        }
243
        $result = $this->db->query($sql, $limit, $start);
244
        if (!$result) {
245
            return $ret;
246
        }
247
        while (false !== ($myrow = $this->db->fetchArray($result))) {
248
            $confoption = new XoopsConfigOption();
249
            $confoption->assignVars($myrow);
250
            if (!$id_as_key) {
251
                $ret[] =& $confoption;
252
            } else {
253
                $ret[$myrow['confop_id']] = &$confoption;
254
            }
255
            unset($confoption);
256
        }
257
258
        return $ret;
259
    }
260
261
    /**
262
     * get count of matching configoption rows
263
     *
264
     * @param CriteriaElement $criteria
265
     *
266
     * @return int Count of matching XoopsConfigOption
267
     */
268
    public function getCount(CriteriaElement $criteria = null)
269
    {
270
        $sql = 'SELECT COUNT(*) as `count` FROM ' . $this->db->prefix('configoption');
271
        if (isset($criteria) && $criteria instanceof \CriteriaElement) {
272
            $sql .= ' ' . $criteria->renderWhere();
273
        }
274
        $result = $this->db->query($sql);
275
        $row = $this->db->fetchArray($result);
276
        $count = $row['count'];
277
        $this->db->freeRecordSet($result);
0 ignored issues
show
Bug introduced by
The method freeRecordSet() 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

277
        $this->db->/** @scrutinizer ignore-call */ 
278
                   freeRecordSet($result);
Loading history...
278
        return (int)$count;
279
    }
280
}
281