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

XoopsModelRead::convertResultSet()   B

Complexity

Conditions 7
Paths 5

Size

Total Lines 34
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

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