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\Dtype;
16: use Xoops\Core\Kernel\XoopsObject;
17: use Xoops\Core\Kernel\XoopsModelAbstract;
18:
19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30:
31: class Write extends XoopsModelAbstract
32: {
33: 34: 35: 36: 37: 38: 39: 40: 41:
42: public function cleanVars(XoopsObject $object)
43: {
44: $vars = $object->getVars();
45: $object->cleanVars = array();
46: foreach ($vars as $k => $v) {
47: if (!$v["changed"]) {
48: continue;
49: }
50: $object->cleanVars[$k] = Dtype::cleanVar($object, $k);
51: }
52: $object->unsetDirty();
53: $errors = $object->getErrors();
54: return empty($errors) ? true : false;
55: }
56:
57: 58: 59: 60: 61: 62: 63: 64:
65: public function insert(XoopsObject $object, $force = true)
66: {
67: if (!(class_exists($this->handler->className) && $object instanceof $this->handler->className)) {
68: trigger_error(
69: "Object '" . get_class($object) . "' is not an instance of '" . $this->handler->className . "'",
70: E_USER_NOTICE
71: );
72: return false;
73: }
74: if (!$object->isDirty()) {
75: trigger_error(
76: "Data entry is not inserted - the object '" . get_class($object) . "' is not dirty",
77: E_USER_NOTICE
78: );
79: return false;
80: }
81: if (!$this->cleanVars($object)) {
82: trigger_error(
83: "Insert failed in method 'cleanVars' of object '" . get_class($object) . "'" . $object->getHtmlErrors(),
84: E_USER_WARNING
85: );
86: return false;
87: }
88:
89: if ($object->isNew()) {
90: if (empty($object->cleanVars)) {
91: trigger_error(
92: "Data entry is not inserted - no variable is changed in object of '" . get_class($object) . "'",
93: E_USER_NOTICE
94: );
95: return false;
96: }
97: if ($force) {
98: $this->handler->db2->setForce($force);
99: }
100: if (!$this->handler->db2->insert($this->handler->table, $object->cleanVars)) {
101: return false;
102: }
103: if (!$object->getVar($this->handler->keyName) && $object_id = $this->handler->db2->lastInsertId()) {
104: $object->assignVar($this->handler->keyName, $object_id);
105: }
106: $object->unsetNew();
107: } else {
108: if (!empty($object->cleanVars)) {
109: $result = $this->handler->db2->update(
110: $this->handler->table,
111: $object->cleanVars,
112: array($this->handler->keyName => $object->getVar($this->handler->keyName))
113: );
114: if (!$result && (int)($this->handler->db2->errorCode())) {
115: return false;
116: }
117: }
118: }
119: return $object->getVar($this->handler->keyName);
120: }
121:
122: 123: 124: 125: 126: 127: 128: 129:
130: public function delete(XoopsObject $object, $force = false)
131: {
132: if (!(class_exists($this->handler->className) && $object instanceof $this->handler->className)) {
133: trigger_error(
134: "Object '" . get_class($object) . "' is not an instance of '" . $this->handler->className . "'",
135: E_USER_NOTICE
136: );
137: return false;
138: }
139:
140: $qb = $this->handler->db2->createXoopsQueryBuilder();
141: $eb = $qb->expr();
142:
143: $qb->delete($this->handler->table);
144: if (is_array($this->handler->keyName)) {
145: for ($i = 0; $i < count($this->handler->keyName); ++$i) {
146: if ($i == 0) {
147: $qb->where(
148: $eb->eq(
149: $this->handler->keyName[$i],
150: $qb->createNamedParameter($object->getVar($this->handler->keyName[$i]))
151: )
152: );
153: } else {
154: $qb->andWhere(
155: $eb->eq(
156: $this->handler->keyName[$i],
157: $qb->createNamedParameter($object->getVar($this->handler->keyName[$i]))
158: )
159: );
160: }
161: }
162: } else {
163: $qb->where(
164: $eb->eq(
165: $this->handler->keyName,
166: $qb->createNamedParameter($object->getVar($this->handler->keyName))
167: )
168: );
169: }
170: if ($force) {
171: $this->handler->db2->setForce($force);
172: }
173: $result = $qb->execute();
174: return empty($result) ? false : true;
175: }
176:
177: 178: 179: 180: 181: 182: 183: 184: 185:
186: public function deleteAll(CriteriaElement $criteria = null, $force = false, $asObject = false)
187: {
188: if ($asObject) {
189: $objects = $this->handler->getAll($criteria);
190: $num = 0;
191: foreach (array_keys($objects) as $key) {
192: $num += $this->delete($objects[$key], $force) ? 1 : 0;
193: }
194: unset($objects);
195: return $num;
196: }
197:
198: $qb = $this->handler->db2->createXoopsQueryBuilder();
199: $qb->delete($this->handler->table);
200: if (isset($criteria)) {
201: $qb = $criteria->renderQb($qb);
202: }
203: if ($force) {
204: $this->handler->db2->setForce($force);
205: }
206: return $qb->execute();
207: }
208:
209: 210: 211: 212: 213: 214: 215: 216: 217: 218:
219: public function updateAll($fieldName, $fieldValue, CriteriaElement $criteria = null, $force = false)
220: {
221: $qb = $this->handler->db2->createXoopsQueryBuilder();
222:
223:
224: $qb->update($this->handler->table);
225: if (isset($criteria)) {
226: $qb = $criteria->renderQb($qb);
227: }
228: $qb->set($fieldName, $qb->createNamedParameter($fieldValue));
229: if ($force) {
230: $this->handler->db2->setForce($force);
231: }
232: return $qb->execute();
233: }
234: }
235: