1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18:
19: defined('XOOPS_ROOT_PATH') || exit('Restricted access');
20:
21: 22: 23: 24: 25: 26: 27:
28: class XoopsModelRead extends XoopsModelAbstract
29: {
30: 31: 32: 33: 34: 35: 36: 37: 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();
54: if ($groupby = $criteria->getGroupby()) {
55: $sql .= $groupby;
56: }
57: if ($sort = $criteria->getSort()) {
58: $sql .= " ORDER BY {$sort} " . $criteria->getOrder();
59: $orderSet = true;
60: }
61: $limit = $criteria->getLimit();
62: $start = $criteria->getStart();
63: }
64: if (empty($orderSet)) {
65:
66: }
67: $result = $this->handler->db->query($sql, $limit, $start);
68: $ret = array();
69: if ($asObject) {
70: while ($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 ($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: 98: 99: 100: 101: 102: 103: 104: 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: 115: 116: 117: 118: 119: 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: }
127:
128: $sql = "SELECT `{$this->handler->keyName}`";
129: if (!empty($this->handler->identifierName)) {
130: $sql .= ", `{$this->handler->identifierName}`";
131: }
132: $sql .= " FROM `{$this->handler->table}`";
133: if (isset($criteria) && is_subclass_of($criteria, 'criteriaelement')) {
134: $sql .= ' ' . $criteria->renderWhere();
135: if ($sort = $criteria->getSort()) {
136: $sql .= ' ORDER BY ' . $sort . ' ' . $criteria->getOrder();
137: }
138: $limit = $criteria->getLimit();
139: $start = $criteria->getStart();
140: }
141: $result = $this->handler->db->query($sql, $limit, $start);
142: if (!$result) {
143: return $ret;
144: }
145:
146: $myts = MyTextSanitizer::getInstance();
147: while ($myrow = $this->handler->db->fetchArray($result)) {
148:
149: $ret[$myrow[$this->handler->keyName]] = empty($this->handler->identifierName) ? 1 : $myts->htmlSpecialChars($myrow[$this->handler->identifierName]);
150: }
151:
152: return $ret;
153: }
154:
155: 156: 157: 158: 159: 160:
161: public function &getIds(CriteriaElement $criteria = null)
162: {
163: $ret = array();
164: $sql = "SELECT `{$this->handler->keyName}` FROM `{$this->handler->table}`";
165: $limit = $start = null;
166: if (isset($criteria) && is_subclass_of($criteria, 'criteriaelement')) {
167: $sql .= ' ' . $criteria->renderWhere();
168: $limit = $criteria->getLimit();
169: $start = $criteria->getStart();
170: }
171: if (!$result = $this->handler->db->query($sql, $limit, $start)) {
172: return $ret;
173: }
174: while ($myrow = $this->handler->db->fetchArray($result)) {
175: $ret[] = $myrow[$this->handler->keyName];
176: }
177:
178: return $ret;
179: }
180:
181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192:
193: public function &getByLimit($limit = 0, $start = 0, CriteriaElement $criteria = null, $fields = null, $asObject = true)
194: {
195: $GLOBALS['xoopsLogger']->addDeprecated(__CLASS__ . '::' . __FUNCTION__ . '() is deprecated, please use getAll instead.');
196: if (isset($criteria) && is_subclass_of($criteria, 'criteriaelement')) {
197: $criteria->setLimit($limit);
198: $criteria->setStart($start);
199: } elseif (!empty($limit)) {
200: $criteria = new CriteriaCompo();
201: $criteria->setLimit($limit);
202: $criteria->setStart($start);
203: }
204: $ret = $this->handler->getAll($criteria, $fields, $asObject);
205:
206: return $ret;
207: }
208:
209: 210: 211: 212: 213: 214: 215: 216:
217: public function convertResultSet($result, $id_as_key = false, $as_object = true)
218: {
219: $GLOBALS['xoopsLogger']->addDeprecated(__CLASS__ . '::' . __FUNCTION__ . '() is deprecated.');
220: $ret = array();
221: while ($myrow = $this->handler->db->fetchArray($result)) {
222: $obj = $this->handler->create(false);
223: $obj->assignVars($myrow);
224: if (!$id_as_key) {
225: if ($as_object) {
226: $ret[] = $obj;
227: } else {
228: $row = array();
229: $vars = $obj->getVars();
230: foreach (array_keys($vars) as $i) {
231: $row[$i] = $obj->getVar($i);
232: }
233: $ret[] = $row;
234: }
235: } else {
236: if ($as_object) {
237: $ret[$myrow[$this->handler->keyName]] =& $obj;
238: } else {
239: $row = array();
240: $vars = $obj->getVars();
241: foreach (array_keys($vars) as $i) {
242: $row[$i] = $obj->getVar($i);
243: }
244: $ret[$myrow[$this->handler->keyName]] = $row;
245: }
246: }
247: unset($obj);
248: }
249:
250: return $ret;
251: }
252: }
253: