1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10:
11:
12: namespace Xoops\Core\Kernel\Model;
13:
14: use Xoops\Core\Kernel\CriteriaElement;
15: use Xoops\Core\Kernel\XoopsModelAbstract;
16:
17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27:
28: class Read extends XoopsModelAbstract
29: {
30: 31: 32: 33: 34: 35: 36: 37: 38: 39:
40: public function getAll(CriteriaElement $criteria = null, $fields = null, $asObject = true, $id_as_key = true)
41: {
42: $qb = $this->handler->db2->createXoopsQueryBuilder();
43:
44: if (is_array($fields) && count($fields) > 0) {
45: if (!in_array($this->handler->keyName, $fields)) {
46: $fields[] = $this->handler->keyName;
47: }
48: $first=true;
49: foreach ($fields as $field) {
50: if ($first) {
51: $first=false;
52: $qb->select($field);
53: } else {
54: $qb->addSelect($field);
55: }
56: }
57: } else {
58: $qb->select('*');
59: }
60: $qb->from($this->handler->table, null);
61: if (isset($criteria)) {
62: $qb = $criteria->renderQb($qb);
63: }
64:
65: $ret = array();
66: $result = $qb->execute();
67: if (!$result) {
68: return $ret;
69: }
70: if ($asObject) {
71: while ($myrow = $result->fetch(\PDO::FETCH_ASSOC)) {
72: $object = $this->handler->create(false);
73: $object->assignVars($myrow);
74: if ($id_as_key) {
75: $ret[$myrow[$this->handler->keyName]] = $object;
76: } else {
77: $ret[] = $object;
78: }
79: unset($object);
80: }
81: } else {
82: $object = $this->handler->create(false);
83: while ($myrow = $result->fetch(\PDO::FETCH_ASSOC)) {
84: $object->assignVars($myrow);
85: if ($id_as_key) {
86: $ret[$myrow[$this->handler->keyName]] = $object->getValues();
87: } else {
88: $ret[] = $object->getValues();
89: }
90: }
91: unset($object);
92: }
93: return $ret;
94: }
95:
96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106:
107: public function getObjects(CriteriaElement $criteria = null, $id_as_key = false, $as_object = true)
108: {
109: $objects = $this->getAll($criteria, null, $as_object, $id_as_key);
110: return $objects;
111: }
112:
113: 114: 115: 116: 117: 118: 119: 120: 121:
122: public function getList(CriteriaElement $criteria = null, $limit = 0, $start = 0)
123: {
124:
125: $qb = $this->handler->db2->createXoopsQueryBuilder();
126:
127: $ret = array();
128:
129: $qb->select($this->handler->keyName);
130: if (!empty($this->handler->identifierName)) {
131: $qb->addSelect($this->handler->identifierName);
132: }
133: $qb->from($this->handler->table, null);
134: if ($limit!=0 || $start!=0) {
135: $qb->setFirstResult($start)
136: ->setMaxResults($limit);
137: }
138: $qb->orderBy($this->handler->keyName);
139: if (!empty($criteria)) {
140: $qb = $criteria->renderQb($qb);
141: }
142: $result = $qb->execute();
143: if (!$result) {
144: return $ret;
145: }
146:
147: $myts = \Xoops\Core\Text\Sanitizer::getInstance();
148: while ($myrow = $result->fetch(\PDO::FETCH_ASSOC)) {
149:
150: $ret[$myrow[$this->handler->keyName]] = empty($this->handler->identifierName) ? 1
151: : $myts->htmlSpecialChars($myrow[$this->handler->identifierName]);
152: }
153: return $ret;
154: }
155:
156: 157: 158: 159: 160: 161: 162:
163: public function getIds(CriteriaElement $criteria = null)
164: {
165: $qb = $this->handler->db2->createXoopsQueryBuilder();
166:
167: $ret = array();
168:
169: $qb->select($this->handler->keyName);
170: $qb->from($this->handler->table, null);
171: if (!empty($criteria)) {
172: $qb = $criteria->renderQb($qb);
173: }
174: $result = $qb->execute();
175: if (!$result) {
176: return $ret;
177: }
178:
179: while ($myrow = $result->fetch(\PDO::FETCH_ASSOC)) {
180: $ret[] = $myrow[$this->handler->keyName];
181: }
182: return $ret;
183: }
184:
185: 186: 187: 188: 189: 190: 191:
192: public function getRandomObject(CriteriaElement $criteria = null)
193: {
194: $qb = $this->handler->db2->createXoopsQueryBuilder();
195: $qb ->select('COUNT(*)')
196: ->from($this->handler->table, null);
197: if (null !== $criteria) {
198: $qb = $criteria->renderQb($qb);
199: }
200: $result = $qb->execute();
201: $count = $result->fetchColumn();
202:
203: $offset = mt_rand(0, $count - 1);
204:
205: $qb = $this->handler->db2->createXoopsQueryBuilder();
206: $qb ->select($this->handler->keyName)
207: ->from($this->handler->table, null);
208: if (null !== $criteria) {
209: $qb = $criteria->renderQb($qb);
210: }
211: $qb ->setFirstResult($offset)
212: ->setMaxResults(1);
213:
214: $result = $qb->execute();
215: $randomKey = $result->fetchColumn();
216:
217: return $this->handler->get($randomKey);
218: }
219: }
220: