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:
29: 30: 31: 32: 33: 34: 35: 36: 37:
38: class XoopsModelJoint extends XoopsModelAbstract
39: {
40: 41: 42: 43: 44:
45: public function validateLinks()
46: {
47: if (empty($this->handler->table_link) || empty($this->handler->field_link)) {
48: trigger_error('The linked table is not set yet.', E_USER_WARNING);
49:
50: return null;
51: }
52: if (empty($this->handler->field_object)) {
53: $this->handler->field_object = $this->handler->field_link;
54: }
55:
56: return true;
57: }
58:
59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69:
70: public function &getByLink(CriteriaElement $criteria = null, $fields = null, $asObject = true, $field_link = null, $field_object = null)
71: {
72: if (!empty($field_link)) {
73: $this->handler->field_link = $field_link;
74: }
75: if (!empty($field_object)) {
76: $this->handler->field_object = $field_object;
77: }
78: if (!$this->validateLinks()) {
79: return null;
80: }
81:
82: if (is_array($fields) && count($fields)) {
83: if (!in_array('o.' . $this->handler->keyName, $fields)) {
84: $fields[] = 'o.' . $this->handler->keyName;
85: }
86: $select = implode(',', $fields);
87: } else {
88: $select = 'o.*, l.*';
89: }
90: $limit = null;
91: $start = null;
92:
93: $sql = " SELECT {$select}" . " FROM {$this->handler->table} AS o" . " LEFT JOIN {$this->handler->table_link} AS l ON o.{$this->handler->field_object} = l.{$this->handler->field_link}";
94: if (isset($criteria) && is_subclass_of($criteria, 'criteriaelement')) {
95: $sql .= ' ' . $criteria->renderWhere();
96: if ($sort = $criteria->getSort()) {
97: $sql .= " ORDER BY {$sort} " . $criteria->getOrder();
98: $orderSet = true;
99: }
100: $limit = $criteria->getLimit();
101: $start = $criteria->getStart();
102: }
103: if (empty($orderSet)) {
104: $sql .= " ORDER BY o.{$this->handler->keyName} DESC";
105: }
106: $result = $this->handler->db->query($sql, $limit, $start);
107: $ret = array();
108: if ($asObject) {
109: while ($myrow = $this->handler->db->fetchArray($result)) {
110: $object = $this->handler->create(false);
111: $object->assignVars($myrow);
112: $ret[$myrow[$this->handler->keyName]] = $object;
113: unset($object);
114: }
115: } else {
116: $object = $this->handler->create(false);
117: while ($myrow = $this->handler->db->fetchArray($result)) {
118: $object->assignVars($myrow);
119: $ret[$myrow[$this->handler->keyName]] = $object->getValues(array_keys($myrow));
120: }
121: unset($object);
122: }
123:
124: return $ret;
125: }
126:
127: 128: 129: 130: 131: 132:
133: public function getCountByLink(CriteriaElement $criteria = null)
134: {
135: if (!$this->validateLinks()) {
136: return null;
137: }
138:
139: $sql = " SELECT COUNT(DISTINCT o.{$this->handler->keyName}) AS count" . " FROM {$this->handler->table} AS o" . " LEFT JOIN {$this->handler->table_link} AS l ON o.{$this->handler->field_object} = l.{$this->handler->field_link}";
140: if (isset($criteria) && is_subclass_of($criteria, 'criteriaelement')) {
141: $sql .= ' ' . $criteria->renderWhere();
142: }
143: if (!$result = $this->handler->db->query($sql)) {
144: return false;
145: }
146: $myrow = $this->handler->db->fetchArray($result);
147:
148: return (int)$myrow['count'];
149: }
150:
151: 152: 153: 154: 155: 156:
157: public function getCountsByLink(CriteriaElement $criteria = null)
158: {
159: if (!$this->validateLinks()) {
160: return null;
161: }
162: $sql = " SELECT l.{$this->handler->field_link}, COUNT(*)" . " FROM {$this->handler->table} AS o" . " LEFT JOIN {$this->handler->table_link} AS l ON o.{$this->handler->field_object} = l.{$this->handler->field_link}";
163: if (isset($criteria) && is_subclass_of($criteria, 'criteriaelement')) {
164: $sql .= ' ' . $criteria->renderWhere();
165: }
166: $sql .= " GROUP BY l.{$this->handler->field_link}";
167: if (!$result = $this->handler->db->query($sql)) {
168: return false;
169: }
170: $ret = array();
171: while (list($id, $count) = $this->handler->db->fetchRow($result)) {
172: $ret[$id] = $count;
173: }
174:
175: return $ret;
176: }
177:
178: 179: 180: 181: 182: 183: 184:
185: public function updateByLink($data, CriteriaElement $criteria = null)
186: {
187: if (!$this->validateLinks()) {
188: return null;
189: }
190: $set = array();
191: foreach ($data as $key => $val) {
192: $set[] = "o.{$key}=" . $this->handler->db->quoteString($val);
193: }
194: $sql = " UPDATE {$this->handler->table} AS o" . ' SET ' . implode(', ', $set) . " LEFT JOIN {$this->handler->table_link} AS l ON o.{$this->handler->field_object} = l.{$this->handler->field_link}";
195: if (isset($criteria) && is_subclass_of($criteria, 'criteriaelement')) {
196: $sql .= ' ' . $criteria->renderWhere();
197: }
198:
199: return $this->handler->db->query($sql);
200: }
201:
202: 203: 204: 205: 206: 207:
208: public function deleteByLink(CriteriaElement $criteria = null)
209: {
210: if (!$this->validateLinks()) {
211: return null;
212: }
213: $sql = "DELETE FROM {$this->handler->table} AS o " . " LEFT JOIN {$this->handler->table_link} AS l ON o.{$this->handler->field_object} = l.{$this->handler->field_link}";
214: if (isset($criteria) && is_subclass_of($criteria, 'criteriaelement')) {
215: $sql .= ' ' . $criteria->renderWhere();
216: }
217:
218: return $this->handler->db->query($sql);
219: }
220: }
221: