Completed
Pull Request — master (#607)
by Richard
14:27
created

Read   A

Complexity

Total Complexity 29

Size/Duplication

Total Lines 190
Duplicated Lines 0 %

Test Coverage

Coverage 89.01%

Importance

Changes 0
Metric Value
eloc 92
dl 0
loc 190
rs 10
c 0
b 0
f 0
ccs 81
cts 91
cp 0.8901
wmc 29

5 Methods

Rating   Name   Duplication   Size   Complexity  
B getList() 0 32 8
A getRandomObject() 0 26 3
A getObjects() 0 4 1
C getAll() 0 54 13
A getIds() 0 20 4
1
<?php
2
/*
3
 You may not change or alter any portion of this comment or credits
4
 of supporting developers from this source code or any supporting source code
5
 which is considered copyrighted (c) material of the original comment or credit authors.
6
7
 This program is distributed in the hope that it will be useful,
8
 but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10
*/
11
12
namespace Xoops\Core\Kernel\Model;
13
14
use Xoops\Core\Kernel\CriteriaElement;
15
use Xoops\Core\Kernel\XoopsModelAbstract;
16
use Doctrine\DBAL\FetchMode;
17
18
/**
19
 * Object render handler class.
20
 *
21
 * @category  Xoops\Core\Kernel\Model\Read
22
 * @package   Xoops\Core\Kernel
23
 * @author    Taiwen Jiang <[email protected]>
24
 * @copyright 2000-2019 XOOPS Project (https://xoops.org)
25
 * @license   GNU GPL 2 or later (https://www.gnu.org/licenses/gpl-2.0.html)
26
 */
27
class Read extends XoopsModelAbstract
28
{
29
    /**
30
     * get all objects matching a condition
31
     *
32
     * @param CriteriaElement|null $criteria  criteria to match
33
     * @param array                $fields    variables to fetch
34
     * @param bool                 $asObject  flag indicating as object, otherwise as array
35
     * @param bool                 $id_as_key use the ID as key for the array
36
     *
37
     * @return array of objects/array as specified in $asObject
38
     */
39 30
    public function getAll(CriteriaElement $criteria = null, $fields = null, $asObject = true, $id_as_key = true)
40
    {
41 30
        $qb = $this->handler->db2->createXoopsQueryBuilder();
42
43 30
        if (is_array($fields) && count($fields) > 0) {
44 1
            if (!in_array($this->handler->keyName, $fields)) {
45 1
                $fields[] = $this->handler->keyName;
46
            }
47 1
            $first=true;
48 1
            foreach ($fields as $field) {
49 1
                if ($first) {
50 1
                    $first=false;
51 1
                    $qb->select($field);
52
                } else {
53 1
                    $qb->addSelect($field);
54
                }
55
            }
56
        } else {
57 29
            $qb->select('*');
58
        }
59 30
        $qb->from($this->handler->table, null);
60 30
        if (isset($criteria)) {
61 23
            $qb = $criteria->renderQb($qb);
62
        }
63
64 30
        $ret = array();
65 30
        $result = $qb->execute();
66 30
        if (!$result) {
67
            return $ret;
68
        }
69 30
        if ($asObject) {
70 29
            while ($myrow = $result->fetch(FetchMode::ASSOCIATIVE)) {
71 25
                $object = $this->handler->create(false);
72 25
                $object->assignVars($myrow);
73 25
                if ($id_as_key) {
74 16
                    $ret[$myrow[$this->handler->keyName]] = $object;
75
                } else {
76 9
                    $ret[] = $object;
77
                }
78 25
                unset($object);
79
            }
80
        } else {
81 1
            $object = $this->handler->create(false);
82 1
            while ($myrow = $result->fetch(FetchMode::ASSOCIATIVE)) {
83 1
                $object->assignVars($myrow);
84 1
                if ($id_as_key) {
85
                    $ret[$myrow[$this->handler->keyName]] = $object->getValues();
86
                } else {
87 1
                    $ret[] = $object->getValues();
88
                }
89
            }
90 1
            unset($object);
91
        }
92 30
        return $ret;
93
    }
94
95
    /**
96
     * retrieve objects from the database
97
     *
98
     * For performance consideration, getAll() is recommended
99
     *
100
     * @param CriteriaElement|null $criteria  criteria to match
101
     * @param bool                 $id_as_key use the ID as key for the array
102
     * @param bool                 $as_object return an array of objects?
103
     *
104
     * @return array
105
     */
106 27
    public function getObjects(CriteriaElement $criteria = null, $id_as_key = false, $as_object = true)
107
    {
108 27
        $objects = $this->getAll($criteria, null, $as_object, $id_as_key);
109 27
        return $objects;
110
    }
111
112
    /**
113
     * Retrieve a list of objects data
114
     *
115
     * @param CriteriaElement|null $criteria criteria to match
116
     * @param int                  $limit    Max number of objects to fetch
117
     * @param int                  $start    Which record to start at
118
     *
119
     * @return array
120
     */
121 2
    public function getList(CriteriaElement $criteria = null, $limit = 0, $start = 0)
122
    {
123
        //$qb = Xoops::getInstance()->db()->createXoopsQueryBuilder();
124 2
        $qb = $this->handler->db2->createXoopsQueryBuilder();
125
126 2
        $ret = array();
127
128 2
        $qb->select($this->handler->keyName);
129 2
        if (!empty($this->handler->identifierName)) {
130 2
            $qb->addSelect($this->handler->identifierName);
131
        }
132 2
        $qb->from($this->handler->table, null);
133 2
        if ($limit!=0 || $start!=0) {
134
            $qb->setFirstResult($start)
135
                ->setMaxResults($limit);
136
        }
137 2
        $qb->orderBy($this->handler->keyName); // any criteria order will override
138 2
        if (!empty($criteria)) {
139
            $qb = $criteria->renderQb($qb);
140
        }
141 2
        $result = $qb->execute();
142 2
        if (!$result) {
143
            return $ret;
144
        }
145
146 2
        $myts = \Xoops\Core\Text\Sanitizer::getInstance();
147 2
        while ($myrow = $result->fetch(FetchMode::ASSOCIATIVE)) {
148
            // identifiers should be textboxes, so sanitize them like that
149 2
            $ret[$myrow[$this->handler->keyName]] = empty($this->handler->identifierName) ? 1
150 2
                : $myts->htmlSpecialChars($myrow[$this->handler->identifierName]);
151
        }
152 2
        return $ret;
153
    }
154
155
    /**
156
     * get IDs of objects matching a condition
157
     *
158
     * @param CriteriaElement|null $criteria criteria to match
159
     *
160
     * @return array of object IDs
161
     */
162 2
    public function getIds(CriteriaElement $criteria = null)
163
    {
164 2
        $qb = $this->handler->db2->createXoopsQueryBuilder();
165
166 2
        $ret = array();
167
168 2
        $qb->select($this->handler->keyName);
169 2
        $qb->from($this->handler->table, null);
170 2
        if (!empty($criteria)) {
171
            $qb = $criteria->renderQb($qb);
172
        }
173 2
        $result = $qb->execute();
174 2
        if (!$result) {
175
            return $ret;
176
        }
177
178 2
        while ($myrow = $result->fetch(FetchMode::ASSOCIATIVE)) {
179 2
            $ret[] = $myrow[$this->handler->keyName];
180
        }
181 2
        return $ret;
182
    }
183
184
    /**
185
     * getRandomObject - return a randomly selected object
186
     *
187
     * @param CriteriaElement|null $criteria criteria to match
188
     *
189
     * @return \Xoops\Core\Kernel\XoopsObject|null object or null if no matching object found
190
     */
191 1
    public function getRandomObject(CriteriaElement $criteria = null)
192
    {
193 1
        $qb = $this->handler->db2->createXoopsQueryBuilder();
194 1
        $qb ->select('COUNT(*)')
195 1
            ->from($this->handler->table, null);
196 1
        if (null !== $criteria) {
197
            $qb = $criteria->renderQb($qb);
198
        }
199 1
        $result = $qb->execute();
200 1
        $count = $result->fetchColumn();
201
202 1
        $offset = mt_rand(0, $count - 1);
203
204 1
        $qb = $this->handler->db2->createXoopsQueryBuilder();
205 1
        $qb ->select($this->handler->keyName)
206 1
            ->from($this->handler->table, null);
207 1
        if (null !== $criteria) {
208
            $qb = $criteria->renderQb($qb);
209
        }
210 1
        $qb ->setFirstResult($offset)
211 1
            ->setMaxResults(1);
212
213 1
        $result = $qb->execute();
214 1
        $randomKey = $result->fetchColumn();
215
216 1
        return $this->handler->get($randomKey);
217
    }
218
}
219