XOOPS  2.6.0
Write.php
Go to the documentation of this file.
1 <?php
2 /*
3  You may not change or alter any portion of this comment or credits
4  of supporting developers from this source code or any supporting source code
5  which is considered copyrighted (c) material of the original comment or credit authors.
6 
7  This program is distributed in the hope that it will be useful,
8  but WITHOUT ANY WARRANTY; without even the implied warranty of
9  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10 */
11 
12 namespace Xoops\Core\Kernel\Model;
13 
18 
32 {
45  public function cleanVars(XoopsObject &$object)
46  {
47  $vars = $object->getVars();
48  $object->cleanVars = array();
49  foreach ($vars as $k => $v) {
50  if (!$v["changed"]) {
51  continue;
52  }
53  $object->cleanVars[$k] = Dtype::cleanVar($object, $k, false);
54  }
55  $object->unsetDirty();
56  $errors = $object->getErrors();
57  return empty($errors) ? true : false;
58  //return $object->cleanVars();
59  }
60 
69  public function insert(XoopsObject &$object, $force = true)
70  {
71  if (!(class_exists($this->handler->className) && $object instanceof $this->handler->className)) {
72  trigger_error(
73  "Object '" . get_class($object) . "' is not an instance of '" . $this->handler->className . "'",
74  E_USER_NOTICE
75  );
76  return false;
77  }
78  if (!$object->isDirty()) {
79  trigger_error(
80  "Data entry is not inserted - the object '" . get_class($object) . "' is not dirty",
81  E_USER_NOTICE
82  );
83  return false;
84  }
85  if (!$this->cleanVars($object)) {
86  trigger_error(
87  "Insert failed in method 'cleanVars' of object '" . get_class($object) . "'" . $object->getHtmlErrors(),
88  E_USER_WARNING
89  );
90  return false;
91  }
92  //$queryFunc = empty($force) ? "query" : "queryF";
93 
94  if ($object->isNew()) {
95  if (empty($object->cleanVars)) {
96  trigger_error(
97  "Data entry is not inserted - no variable is changed in object of '" . get_class($object) . "'",
98  E_USER_NOTICE
99  );
100  return false;
101  }
102  if (!$this->handler->db2->insert($this->handler->table, $object->cleanVars)) {
103  return false;
104  }
105  if (!$object->getVar($this->handler->keyName) && $object_id = $this->handler->db2->lastInsertId()) {
106  $object->assignVar($this->handler->keyName, $object_id);
107  }
108  $object->unsetNew(); // object is no longer New
109  } else {
110  if (!empty($object->cleanVars)) {
111  $result = $this->handler->db2->update(
112  $this->handler->table,
113  $object->cleanVars,
114  array($this->handler->keyName => $object->getVar($this->handler->keyName))
115  );
116  if (!$result && intval($this->handler->db2->errorCode())) {
117  return false;
118  }
119  }
120  }
121  return $object->getVar($this->handler->keyName);
122  }
123 
132  public function delete(XoopsObject &$object, $force = false)
133  {
134  if (!(class_exists($this->handler->className) && $object instanceof $this->handler->className)) {
135  trigger_error(
136  "Object '" . get_class($object) . "' is not an instance of '" . $this->handler->className . "'",
137  E_USER_NOTICE
138  );
139  return false;
140  }
141 
142  $qb = $this->handler->db2->createXoopsQueryBuilder();
143  $eb = $qb->expr();
144 
145  $qb->delete($this->handler->table);
146  if (is_array($this->handler->keyName)) {
147  for ($i = 0; $i < count($this->handler->keyName); ++$i) {
148  if ($i == 0) {
149  $qb->where(
150  $eb->eq(
151  $this->handler->keyName[$i],
152  $qb->createNamedParameter($object->getVar($this->handler->keyName[$i]))
153  )
154  );
155  } else {
156  $qb->andWhere(
157  $eb->eq(
158  $this->handler->keyName[$i],
159  $qb->createNamedParameter($object->getVar($this->handler->keyName[$i]))
160  )
161  );
162  }
163  }
164  } else {
165  $qb->where(
166  $eb->eq(
167  $this->handler->keyName,
168  $qb->createNamedParameter($object->getVar($this->handler->keyName))
169  )
170  );
171  }
172  $result = $qb->execute();
173  return empty($result) ? false : true;
174  }
175 
185  public function deleteAll(CriteriaElement $criteria = null, $force = false, $asObject = false)
186  {
187  if ($asObject) {
188  $objects = $this->handler->getAll($criteria);
189  $num = 0;
190  foreach (array_keys($objects) as $key) {
191  $num += $this->delete($objects[$key], $force) ? 1 : 0;
192  }
193  unset($objects);
194  return $num;
195  }
196  //$queryFunc = empty($force) ? 'query' : 'queryF';
197  $qb = $this->handler->db2->createXoopsQueryBuilder();
198  $qb->delete($this->handler->table);
199  if (isset($criteria)) {
200  $qb = $criteria->renderQb($qb);
201  }
202  return $qb->execute();
203  }
204 
215  public function updateAll($fieldname, $fieldvalue, CriteriaElement $criteria = null, $force = false)
216  {
217  $qb = $this->handler->db2->createXoopsQueryBuilder();
218 
219  //$queryFunc = empty($force) ? 'query' : 'queryF';
220  $qb->update($this->handler->table);
221  if (isset($criteria)) {
222  $qb = $criteria->renderQb($qb);
223  }
224  $qb->set($fieldname, $qb->createNamedParameter($fieldvalue));
225 
226  return $qb->execute();
227  }
228 }
updateAll($fieldname, $fieldvalue, CriteriaElement $criteria=null, $force=false)
Definition: Write.php:215
insert(XoopsObject &$object, $force=true)
Definition: Write.php:69
$i
Definition: dialog.php:68
static cleanVar(XoopsObject $obj, $key, $quote=true)
Definition: Dtype.php:38
$result
Definition: pda.php:33
deleteAll(CriteriaElement $criteria=null, $force=false, $asObject=false)
Definition: Write.php:185
getVar($key, $format= 's')
$criteria
cleanVars(XoopsObject &$object)
Definition: Write.php:45