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 <phppp@users.sourceforge.net>
18: */
19: defined('XOOPS_ROOT_PATH') || exit('Restricted access');
20:
21: /**
22: * Object render handler class.
23: *
24: * @author Taiwen Jiang <phppp@users.sourceforge.net>
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 (!empty($fields) && \is_array($fields)) {
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) && \method_exists($criteria, 'renderWhere')) {
53: $sql .= ' ' . $criteria->renderWhere();
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) && \method_exists($criteria, 'renderWhere')) {
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: return $ret;
150: }
151:
152: $myts = \MyTextSanitizer::getInstance();
153: while (false !== ($myrow = $this->handler->db->fetchArray($result))) {
154: // identifiers should be textboxes, so sanitize them like that
155: $ret[$myrow[$this->handler->keyName]] = empty($this->handler->identifierName) ? 1 : $myts->htmlSpecialChars($myrow[$this->handler->identifierName]);
156: }
157:
158: return $ret;
159: }
160:
161: /**
162: * get IDs of objects matching a condition
163: *
164: * @param CriteriaElement|CriteriaCompo $criteria {@link CriteriaElement} to match
165: * @return array of object IDs
166: */
167: public function &getIds(CriteriaElement $criteria = null)
168: {
169: $ret = array();
170: $sql = "SELECT `{$this->handler->keyName}` FROM `{$this->handler->table}`";
171: $limit = $start = null;
172: if (isset($criteria) && \method_exists($criteria, 'renderWhere')) {
173: $sql .= ' ' . $criteria->renderWhere();
174: $limit = $criteria->getLimit();
175: $start = $criteria->getStart();
176: }
177: $result = $this->handler->db->query($sql, $limit, $start);
178: if (!$this->handler->db->isResultSet($result)) {
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(__METHOD__ . '() 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(__METHOD__ . '() 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: