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: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41:
42: class Joint extends XoopsModelAbstract
43: {
44: 45: 46: 47: 48:
49: private function validateLinks()
50: {
51: if (empty($this->handler->table_link) || empty($this->handler->field_link)) {
52: trigger_error("The linked table is not set yet.", E_USER_WARNING);
53: return false;
54: }
55: if (empty($this->handler->field_object)) {
56: $this->handler->field_object = $this->handler->field_link;
57: }
58: return true;
59: }
60:
61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73:
74: public function getByLink(
75: CriteriaElement $criteria = null,
76: $fields = null,
77: $asObject = true,
78: $field_link = null,
79: $field_object = null
80: ) {
81: if (!empty($field_link)) {
82: $this->handler->field_link = $field_link;
83: }
84: if (!empty($field_object)) {
85: $this->handler->field_object = $field_object;
86: }
87: if (!$this->validateLinks()) {
88: return false;
89: }
90:
91: $qb = $this->handler->db2->createXoopsQueryBuilder();
92: if (is_array($fields) && count($fields)) {
93: if (!in_array("o." . $this->handler->keyName, $fields)) {
94: $fields[] = "o." . $this->handler->keyName;
95: }
96: $first = true;
97: foreach ($fields as $field) {
98: if ($first) {
99: $first = false;
100: $qb->select($field);
101: } else {
102: $qb->addSelect($field);
103: }
104: }
105: } else {
106: $qb ->select('o.*')
107: ->addSelect('l.*');
108: }
109: $qb ->from($this->handler->table, 'o')
110: ->leftJoin(
111: 'o',
112: $this->handler->table_link,
113: 'l',
114: "o.{$this->handler->field_object} = l.{$this->handler->field_link}"
115: );
116: if (isset($criteria) && ($criteria instanceof CriteriaElement)) {
117: $qb = $criteria->renderQb($qb);
118: }
119: $result = $qb->execute();
120: $ret = array();
121: if ($asObject) {
122: while ($myrow = $result->fetch(\PDO::FETCH_ASSOC)) {
123: $object = $this->handler->create(false);
124: $object->assignVars($myrow);
125: $ret[$myrow[$this->handler->keyName]] = $object;
126: unset($object);
127: }
128: } else {
129: $object = $this->handler->create(false);
130: while ($myrow = $result->fetch(\PDO::FETCH_ASSOC)) {
131: $object->assignVars($myrow);
132: $ret[$myrow[$this->handler->keyName]] = $object->getValues();
133: }
134: unset($object);
135: }
136: return $ret;
137: }
138:
139: 140: 141: 142: 143: 144: 145:
146: public function getCountByLink(CriteriaElement $criteria = null)
147: {
148: if (!$this->validateLinks()) {
149: return false;
150: }
151:
152: $qb = $this->handler->db2->createXoopsQueryBuilder();
153:
154: $qb ->select("COUNT(DISTINCT o.{$this->handler->keyName})")
155: ->from($this->handler->table, 'o')
156: ->leftJoin(
157: 'o',
158: $this->handler->table_link,
159: 'l',
160: "o.{$this->handler->field_object} = l.{$this->handler->field_link}"
161: );
162:
163: if (isset($criteria) && ($criteria instanceof CriteriaElement)) {
164: $criteria->renderQb($qb);
165: }
166:
167: $result = $qb->execute();
168: return $result->fetchColumn(0);
169: }
170:
171: 172: 173: 174: 175: 176: 177:
178: public function getCountsByLink(CriteriaElement $criteria = null)
179: {
180: if (!$this->validateLinks()) {
181: return false;
182: }
183:
184: $qb = $this->handler->db2->createXoopsQueryBuilder();
185:
186: $qb ->select("l.{$this->handler->keyName_link}")
187: ->addSelect('COUNT(*)')
188: ->from($this->handler->table, 'o')
189: ->leftJoin(
190: 'o',
191: $this->handler->table_link,
192: 'l',
193: "o.{$this->handler->field_object} = l.{$this->handler->field_link}"
194: );
195:
196: if (isset($criteria) && ($criteria instanceof CriteriaElement)) {
197: $criteria->renderQb($qb);
198: }
199:
200: $qb ->groupBy("l.{$this->handler->keyName_link}");
201:
202: $result = $qb->execute();
203:
204: $ret = array();
205: while (list($id, $count) = $result->fetch(\PDO::FETCH_NUM)) {
206: $ret[$id] = $count;
207: }
208: return $ret;
209: }
210:
211: 212: 213: 214: 215: 216: 217: 218: 219: 220: 221:
222: public function updateByLink(array $data, CriteriaElement $criteria = null)
223: {
224: if (!$this->validateLinks()) {
225: return false;
226: }
227: if (empty($data) || empty($criteria)) {
228: return false;
229: }
230:
231: $set = array();
232: foreach ($data as $key => $val) {
233: $set[] = "o.{$key}=" . $this->handler->db2->quote($val);
234: }
235: $sql = " UPDATE {$this->handler->table} AS o" . " SET " . implode(", ", $set)
236: . " LEFT JOIN {$this->handler->table_link} AS l "
237: . "ON o.{$this->handler->field_object} = l.{$this->handler->field_link}";
238: if (isset($criteria) && ($criteria instanceof CriteriaElement)) {
239: $sql .= " " . $criteria->renderWhere();
240: }
241:
242: return $this->handler->db2->executeUpdate($sql);
243: }
244:
245: 246: 247: 248: 249: 250: 251: 252: 253: 254:
255: public function deleteByLink(CriteriaElement $criteria = null)
256: {
257: if (!$this->validateLinks()) {
258: return false;
259: }
260: if (empty($criteria)) {
261: return false;
262: }
263:
264: $sql = "DELETE FROM {$this->handler->table} AS o "
265: . "LEFT JOIN {$this->handler->table_link} AS l "
266: . "ON o.{$this->handler->field_object} = l.{$this->handler->field_link}";
267: if (isset($criteria) && ($criteria instanceof CriteriaElement)) {
268: $sql .= " " . $criteria->renderWhere();
269: }
270:
271: return $this->handler->db2->executeUpdate($sql);
272: }
273: }
274: