XOOPS 2.5.6  Final
 All Classes Namespaces Files Functions Variables Pages
joint.php
Go to the documentation of this file.
1 <?php
20 defined('XOOPS_ROOT_PATH') or die('Restricted access');
21 
39 {
45  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  return null;
50  }
51  if (empty($this->handler->field_object)) {
52  $this->handler->field_object = $this->handler->field_link;
53  }
54  return true;
55  }
56 
67  function &getByLink($criteria = null, $fields = null, $asObject = true, $field_link = null, $field_object = null)
68  {
69  if (!empty($field_link)) {
70  $this->handler->field_link = $field_link;
71  }
72  if (!empty($field_object)) {
73  $this->handler->field_object = $field_object;
74  }
75  if (!$this->validateLinks()) {
76  return null;
77  }
78 
79  if (is_array($fields) && count($fields)) {
80  if (!in_array("o." . $this->handler->keyName, $fields)) {
81  $fields[] = "o." . $this->handler->keyName;
82  }
83  $select = implode(",", $fields);
84  } else {
85  $select = "o.*, l.*";
86  }
87  $limit = null;
88  $start = null;
89  // $field_object = empty($field_object) ? $field_link : $field_object;
90  $sql = " SELECT {$select}"
91  . " FROM {$this->handler->table} AS o"
92  . " LEFT JOIN {$this->handler->table_link} AS l ON o.{$this->handler->field_object} = l.{$this->handler->field_link}";
93  if (isset($criteria) && is_subclass_of($criteria, "criteriaelement")) {
94  $sql .= " " . $criteria->renderWhere();
95  if ($sort = $criteria->getSort()) {
96  $sql .= " ORDER BY {$sort} " . $criteria->getOrder();
97  $orderSet = true;
98  }
99  $limit = $criteria->getLimit();
100  $start = $criteria->getStart();
101  }
102  if (empty($orderSet)) {
103  $sql .= " ORDER BY o.{$this->handler->keyName} DESC";
104  }
105  $result = $this->handler->db->query($sql, $limit, $start);
106  $ret = array();
107  if ($asObject) {
108  while ($myrow = $this->handler->db->fetchArray($result)) {
109  $object =& $this->handler->create(false);
110  $object->assignVars($myrow);
111  $ret[$myrow[$this->handler->keyName]] = $object;
112  unset($object);
113  }
114  } else {
115  $object =& $this->handler->create(false);
116  while ($myrow = $this->handler->db->fetchArray($result)) {
117  $object->assignVars($myrow);
118  $ret[$myrow[$this->handler->keyName]] = $object->getValues(array_keys($myrow));
119  }
120  unset($object);
121  }
122  return $ret;
123  }
124 
131  function getCountByLink($criteria = null)
132  {
133  if (!$this->validateLinks()) {
134  return null;
135  }
136 
137  $sql = " SELECT COUNT(DISTINCT {$this->handler->keyName}) AS count"
138  . " FROM {$this->handler->table} AS o"
139  . " 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  return intval($myrow["count"]);
148  }
149 
156  function getCountsByLink($criteria = null)
157  {
158  if (!$this->validateLinks()) {
159  return null;
160  }
161  $sql = " SELECT l.{$this->handler->keyName_link}, COUNT(*)"
162  . " FROM {$this->handler->table} AS o"
163  . " LEFT JOIN {$this->handler->table_link} AS l ON o.{$this->handler->field_object} = l.{$this->handler->field_link}";
164  if (isset($criteria) && is_subclass_of($criteria, "criteriaelement")) {
165  $sql .= " " . $criteria->renderWhere();
166  }
167  $sql .= " GROUP BY l.{$this->handler->keyName_link}";
168  if (!$result = $this->handler->db->query($sql)) {
169  return false;
170  }
171  $ret = array();
172  while (list ($id, $count) = $this->handler->db->fetchRow($result)) {
173  $ret[$id] = $count;
174  }
175  return $ret;
176  }
177 
185  function updateByLink($data, $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"
195  . " SET " . implode(", ", $set)
196  . " LEFT JOIN {$this->handler->table_link} AS l ON o.{$this->handler->field_object} = l.{$this->handler->field_link}";
197  if (isset($criteria) && is_subclass_of($criteria, "criteriaelement")) {
198  $sql .= " " . $criteria->renderWhere();
199  }
200  return $this->handler->db->query($sql);
201  }
202 
209  function deleteByLink($criteria = null)
210  {
211  if (!$this->validateLinks()) {
212  return null;
213  }
214  $sql = "DELETE FROM {$this->handler->table} AS o "
215  . " LEFT JOIN {$this->handler->table_link} AS l ON o.{$this->handler->field_object} = l.{$this->handler->field_link}";
216  if (isset($criteria) && is_subclass_of($criteria, "criteriaelement")) {
217  $sql .= " " . $criteria->renderWhere();
218  }
219  return $this->handler->db->query($sql);
220  }
221 }
222 
223 ?>