Passed
Pull Request — master (#1301)
by Michael
05:46
created

XoopsModelRead::getList()   B

Complexity

Conditions 11
Paths 80

Size

Total Lines 37
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 11
eloc 23
nc 80
nop 3
dl 0
loc 37
rs 7.3166
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 (!$this->handler->db->isResultSet($result)) {
147
        //    \trigger_error("Query Failed! SQL: $sql- Error: " . $this->handler->db->error(), E_USER_ERROR);
148
            return $ret;        
149
            }
150
151
        $myts = \MyTextSanitizer::getInstance();
152
        while (false !== ($myrow = $this->handler->db->fetchArray($result))) {
153
            // identifiers should be textboxes, so sanitize them like that
154
            $ret[$myrow[$this->handler->keyName]] = empty($this->handler->identifierName) ? 1 : $myts->htmlSpecialChars($myrow[$this->handler->identifierName]);
155
        }
156
157
        return $ret;
158
    }
159
160
    /**
161
     * get IDs of objects matching a condition
162
     *
163
     * @param  CriteriaElement|CriteriaCompo $criteria {@link CriteriaElement} to match
164
     * @return array           of object IDs
165
     */
166
    public function &getIds(CriteriaElement $criteria = null)
167
    {
168
        $ret   = array();
169
        $sql   = "SELECT `{$this->handler->keyName}` FROM `{$this->handler->table}`";
170
        $limit = $start = null;
171
        if (isset($criteria) && is_subclass_of($criteria, 'CriteriaElement')) {
172
            $sql .= ' ' . $criteria->renderWhere();
173
            $limit = $criteria->getLimit();
174
            $start = $criteria->getStart();
175
        }
176
        $result = $this->handler->db->query($sql, $limit, $start);
177
        if (!$this->handler->db->isResultSet($result)) {
178
        //    \trigger_error("Query Failed! SQL: $sql- Error: " . $this->handler->db->error(), E_USER_ERROR);
179
            return $ret;    
180
         }
181
182
        while (false !== ($myrow = $this->handler->db->fetchArray($result))) {
183
            $ret[] = $myrow[$this->handler->keyName];
184
        }
185
186
        return $ret;
187
    }
188
189
    /**
190
     * get a limited list of objects matching a condition
191
     *
192
     * {@link CriteriaCompo}
193
     *
194
     * @param  int             $limit    Max number of objects to fetch
195
     * @param  int             $start    Which record to start at
196
     * @param  CriteriaElement $criteria {@link CriteriaElement} to match
197
     * @param  array           $fields   variables to fetch
198
     * @param  bool            $asObject flag indicating as object, otherwise as array
199
     * @return array           of objects    {@link XoopsObject}
200
     */
201
    public function &getByLimit($limit = 0, $start = 0, CriteriaElement $criteria = null, $fields = null, $asObject = true)
202
    {
203
        $GLOBALS['xoopsLogger']->addDeprecated(__CLASS__ . '::' . __FUNCTION__ . '() is deprecated, please use getAll instead.');
204
        if (isset($criteria) && is_subclass_of($criteria, 'CriteriaElement')) {
205
            $criteria->setLimit($limit);
206
            $criteria->setStart($start);
207
        } elseif (!empty($limit)) {
208
            $criteria = new CriteriaCompo();
209
            $criteria->setLimit($limit);
210
            $criteria->setStart($start);
211
        }
212
        $ret = $this->handler->getAll($criteria, $fields, $asObject);
213
214
        return $ret;
215
    }
216
217
    /**
218
     * Convert a database resultset to a returnable array
219
     *
220
     * @param  object $result    database resultset
221
     * @param  bool   $id_as_key - should NOT be used with joint keys
222
     * @param  bool   $as_object
223
     * @return array
224
     */
225
    public function convertResultSet($result, $id_as_key = false, $as_object = true)
226
    {
227
        $GLOBALS['xoopsLogger']->addDeprecated(__CLASS__ . '::' . __FUNCTION__ . '() is deprecated.');
228
        $ret = array();
229
        while (false !== ($myrow = $this->handler->db->fetchArray($result))) {
230
            $obj = $this->handler->create(false);
231
            $obj->assignVars($myrow);
232
            if (!$id_as_key) {
233
                if ($as_object) {
234
                    $ret[] = $obj;
235
                } else {
236
                    $row  = array();
237
                    $vars = $obj->getVars();
238
                    foreach (array_keys($vars) as $i) {
239
                        $row[$i] = $obj->getVar($i);
240
                    }
241
                    $ret[] = $row;
242
                }
243
            } else {
244
                if ($as_object) {
245
                    $ret[$myrow[$this->handler->keyName]] =& $obj;
246
                } else {
247
                    $row  = array();
248
                    $vars = $obj->getVars();
249
                    foreach (array_keys($vars) as $i) {
250
                        $row[$i] = $obj->getVar($i);
251
                    }
252
                    $ret[$myrow[$this->handler->keyName]] = $row;
253
                }
254
            }
255
            unset($obj);
256
        }
257
258
        return $ret;
259
    }
260
}
261