Passed
Pull Request — master (#1270)
by Michael
05:36
created

XoopsModelRead::getList()   B

Complexity

Conditions 11
Paths 120

Size

Total Lines 39
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 11
eloc 23
nc 120
nop 3
dl 0
loc 39
rs 7.15
c 1
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Object render handler 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
 * @subpackage          model
16
 * @since               2.3.0
17
 * @author              Taiwen Jiang <[email protected]>
18
 */
19
defined('XOOPS_ROOT_PATH') || exit('Restricted access');
20
21
/**
22
 * Object render handler class.
23
 *
24
 * @author Taiwen Jiang <[email protected]>
25
 *
26
 * {@link XoopsModelAbstract}
27
 */
28
class XoopsModelRead extends XoopsModelAbstract
29
{
30
    /**
31
     * get all objects matching a condition
32
     *
33
     * @param  CriteriaElement|CriteriaCompo $criteria  {@link CriteriaElement} to match
34
     * @param  array           $fields    variables to fetch
35
     * @param  bool            $asObject  flag indicating as object, otherwise as array
36
     * @param  bool            $id_as_key use the ID as key for the array
37
     * @return array           of objects/array {@link XoopsObject}
38
     */
39
    public function &getAll(CriteriaElement $criteria = null, $fields = null, $asObject = true, $id_as_key = true)
40
    {
41
        if (is_array($fields) && count($fields) > 0) {
42
            if (!in_array($this->handler->keyName, $fields)) {
43
                $fields[] = $this->handler->keyName;
44
            }
45
            $select = '`' . implode('`, `', $fields) . '`';
46
        } else {
47
            $select = '*';
48
        }
49
        $limit = null;
50
        $start = null;
51
        $sql   = "SELECT {$select} FROM `{$this->handler->table}`";
52
        if (isset($criteria) && is_subclass_of($criteria, 'CriteriaElement')) {
53
            $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

53
            $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...
54
            if ($groupby = $criteria->getGroupby()) {
55
                $sql .= $groupby;
56
            }
57
            if ($sort = $criteria->getSort()) {
58
                $sql .= " ORDER BY {$sort} " . $criteria->getOrder();
59
            }
60
            $limit = $criteria->getLimit();
61
            $start = $criteria->getStart();
62
        }
63
        $result = $this->handler->db->query($sql, $limit, $start);
64
        if (!$this->handler->db->isResultSet($result)) {
65
            \trigger_error("Query Failed! SQL: $sql- Error: " . $this->handler->db->error(), E_USER_ERROR);
66
        }
67
        $ret    = array();
68
        if (false !== $result) {
69
            if ($asObject) {
70
                while (false !== ($myrow = $this->handler->db->fetchArray($result))) {
71
                    $object = $this->handler->create(false);
72
                    $object->assignVars($myrow);
73
                    if ($id_as_key) {
74
                        $ret[$myrow[$this->handler->keyName]] = $object;
75
                    } else {
76
                        $ret[] = $object;
77
                    }
78
                    unset($object);
79
                }
80
            } else {
81
                $object = $this->handler->create(false);
82
                while (false !== ($myrow = $this->handler->db->fetchArray($result))) {
83
                    $object->assignVars($myrow);
84
                    if ($id_as_key) {
85
                        $ret[$myrow[$this->handler->keyName]] = $object->getValues(array_keys($myrow));
86
                    } else {
87
                        $ret[] = $object->getValues(array_keys($myrow));
88
                    }
89
                }
90
                unset($object);
91
            }
92
        }
93
        return $ret;
94
    }
95
96
    /**
97
     * retrieve objects from the database
98
     *
99
     * For performance consideration, getAll() is recommended
100
     *
101
     * @param  CriteriaElement $criteria  {@link CriteriaElement} conditions to be met
102
     * @param  bool            $id_as_key use the ID as key for the array
103
     * @param  bool            $as_object return an array of objects?
104
     * @return array
105
     */
106
    public function &getObjects(CriteriaElement $criteria = null, $id_as_key = false, $as_object = true)
107
    {
108
        $objects =& $this->getAll($criteria, null, $as_object, $id_as_key);
109
110
        return $objects;
111
    }
112
113
    /**
114
     * Retrieve a list of objects data
115
     *
116
     * @param  CriteriaElement $criteria {@link CriteriaElement} conditions to be met
117
     * @param  int             $limit    Max number of objects to fetch
118
     * @param  int             $start    Which record to start at
119
     * @return array
120
     */
121
    public function getList(CriteriaElement $criteria = null, $limit = 0, $start = 0)
122
    {
123
        $ret = array();
124
        if ($criteria == null) {
125
            $criteria = new CriteriaCompo();
126
            $criteria->setLimit($limit);
127
            $criteria->setStart($start);
128
        }
129
130
        $sql = "SELECT `{$this->handler->keyName}`";
131
        if (!empty($this->handler->identifierName)) {
132
            $sql .= ", `{$this->handler->identifierName}`";
133
        }
134
        $sql .= " FROM `{$this->handler->table}`";
135
        if (isset($criteria) && is_subclass_of($criteria, 'CriteriaElement')) {
136
            $sql .= ' ' . $criteria->renderWhere();
137
            if ($sort = $criteria->getSort()) {
138
                $sql .= ' ORDER BY ' . $sort . ' ' . $criteria->getOrder();
139
            }
140
            if ((0 == $limit) && (0 == $start)) {
141
                $limit = $criteria->getLimit();
142
                $start = $criteria->getStart();
143
            }
144
        }
145
        $result = $this->handler->db->query($sql, $limit, $start);
146
//        if (!$result) {
147
//            return $ret;
148
//        }
149
        if (!$this->handler->db->isResultSet($result)) {
150
            \trigger_error("Query Failed! SQL: $sql- Error: " . $this->handler->db->error(), E_USER_ERROR);
151
        }
152
153
        $myts = MyTextSanitizer::getInstance();
154
        while (false !== ($myrow = $this->handler->db->fetchArray($result))) {
155
            // identifiers should be textboxes, so sanitize them like that
156
            $ret[$myrow[$this->handler->keyName]] = empty($this->handler->identifierName) ? 1 : $myts->htmlSpecialChars($myrow[$this->handler->identifierName]);
157
        }
158
159
        return $ret;
160
    }
161
162
    /**
163
     * get IDs of objects matching a condition
164
     *
165
     * @param  CriteriaElement|CriteriaCompo $criteria {@link CriteriaElement} to match
166
     * @return array           of object IDs
167
     */
168
    public function &getIds(CriteriaElement $criteria = null)
169
    {
170
        $ret   = array();
171
        $sql   = "SELECT `{$this->handler->keyName}` FROM `{$this->handler->table}`";
172
        $limit = $start = null;
173
        if (isset($criteria) && is_subclass_of($criteria, 'CriteriaElement')) {
174
            $sql .= ' ' . $criteria->renderWhere();
175
            $limit = $criteria->getLimit();
176
            $start = $criteria->getStart();
177
        }
178
        $result = $this->handler->db->query($sql, $limit, $start);
179
        if (!$this->handler->db->isResultSet($result)) {
180
            // return $ret;
181
            \trigger_error("Query Failed! SQL: $sql- Error: " . $this->handler->db->error(), E_USER_ERROR);
182
        }
183
184
        while (false !== ($myrow = $this->handler->db->fetchArray($result))) {
185
            $ret[] = $myrow[$this->handler->keyName];
186
        }
187
188
        return $ret;
189
    }
190
191
    /**
192
     * get a limited list of objects matching a condition
193
     *
194
     * {@link CriteriaCompo}
195
     *
196
     * @param  int             $limit    Max number of objects to fetch
197
     * @param  int             $start    Which record to start at
198
     * @param  CriteriaElement $criteria {@link CriteriaElement} to match
199
     * @param  array           $fields   variables to fetch
200
     * @param  bool            $asObject flag indicating as object, otherwise as array
201
     * @return array           of objects    {@link XoopsObject}
202
     */
203
    public function &getByLimit($limit = 0, $start = 0, CriteriaElement $criteria = null, $fields = null, $asObject = true)
204
    {
205
        $GLOBALS['xoopsLogger']->addDeprecated(__CLASS__ . '::' . __FUNCTION__ . '() is deprecated, please use getAll instead.');
206
        if (isset($criteria) && is_subclass_of($criteria, 'CriteriaElement')) {
207
            $criteria->setLimit($limit);
208
            $criteria->setStart($start);
209
        } elseif (!empty($limit)) {
210
            $criteria = new CriteriaCompo();
211
            $criteria->setLimit($limit);
212
            $criteria->setStart($start);
213
        }
214
        $ret = $this->handler->getAll($criteria, $fields, $asObject);
215
216
        return $ret;
217
    }
218
219
    /**
220
     * Convert a database resultset to a returnable array
221
     *
222
     * @param  object $result    database resultset
223
     * @param  bool   $id_as_key - should NOT be used with joint keys
224
     * @param  bool   $as_object
225
     * @return array
226
     */
227
    public function convertResultSet($result, $id_as_key = false, $as_object = true)
228
    {
229
        $GLOBALS['xoopsLogger']->addDeprecated(__CLASS__ . '::' . __FUNCTION__ . '() is deprecated.');
230
        $ret = array();
231
        while (false !== ($myrow = $this->handler->db->fetchArray($result))) {
232
            $obj = $this->handler->create(false);
233
            $obj->assignVars($myrow);
234
            if (!$id_as_key) {
235
                if ($as_object) {
236
                    $ret[] = $obj;
237
                } else {
238
                    $row  = array();
239
                    $vars = $obj->getVars();
240
                    foreach (array_keys($vars) as $i) {
241
                        $row[$i] = $obj->getVar($i);
242
                    }
243
                    $ret[] = $row;
244
                }
245
            } else {
246
                if ($as_object) {
247
                    $ret[$myrow[$this->handler->keyName]] =& $obj;
248
                } else {
249
                    $row  = array();
250
                    $vars = $obj->getVars();
251
                    foreach (array_keys($vars) as $i) {
252
                        $row[$i] = $obj->getVar($i);
253
                    }
254
                    $ret[$myrow[$this->handler->keyName]] = $row;
255
                }
256
            }
257
            unset($obj);
258
        }
259
260
        return $ret;
261
    }
262
}
263