ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
class.ilADTBasedObject.php
Go to the documentation of this file.
1<?php
2/* Copyright (c) 1998-2009 ILIAS open source, Extended GPL, see docs/LICENSE */
3
13abstract class ilADTBasedObject
14{
15 protected $properties = array(); // [array]
16 protected $db_errors = array(); // [array]
17
25 public function __construct()
26 {
27 $this->properties = $this->initProperties();
28
29 // :TODO: to keep constructor "open" we COULD use func_get_args()
30 $this->parsePrimary(func_get_args());
31 $this->read();
32 }
33
34
35 //
36 // properties
37 //
38
44 abstract protected function initProperties();
45
51 public function getProperties()
52 {
53 return $this->properties;
54 }
55
61 public function isValid()
62 {
63 return $this->properties->isValid();
64 }
65
76 public function __call($a_method, $a_value)
77 {
78 $type = substr($a_method, 0, 3);
79 switch ($type) {
80 case "get":
81 $parsed = strtolower(preg_replace("/([A-Z])/", " $1", substr($a_method, 3)));
82 $parsed = str_replace(" ", "_", trim($parsed));
83 if (!$this->properties->hasElement($parsed)) {
84 throw new Exception("ilADTObject unknown property " . $parsed);
85 }
86 return $this->properties->getElement($parsed);
87
88 default:
89 throw new Exception("ilADTObject unknown method " . $parsed);
90 }
91 }
92
93
94 //
95 // CRUD / active record
96 //
97
104 abstract protected function parsePrimary(array $a_args);
105
111 abstract protected function hasPrimary();
112
118 abstract protected function createPrimaryKey();
119
125 abstract protected function initDBBridge(ilADTGroupDBBridge $a_adt_db);
126
132 protected function initActiveRecordInstance()
133 {
134 global $ilDB;
135
136 if (!$this->hasPrimary()) {
137 throw new Exception("ilADTBasedObject no primary");
138 }
139
141 $this->adt_db = $factory->getDBBridgeForInstance($this->properties);
142 $this->initDBBridge($this->adt_db);
143
144 // use custom error handling
145 include_once "Services/ADT/classes/class.ilADTDBException.php";
146 $ilDB->exception = "ilADTDBException";
147
148 return $factory->getActiveRecordInstance($this->adt_db);
149 }
150
156 public function read()
157 {
158 if ($this->hasPrimary()) {
159 $rec = $this->initActiveRecordInstance();
160 return $rec->read();
161 }
162 return false;
163 }
164
170 public function create()
171 {
172 if ($this->hasPrimary()) {
173 return $this->update();
174 }
175
176 if ($this->isValid()) {
177 if ($this->createPrimaryKey()) {
178 try {
179 $rec = $this->initActiveRecordInstance();
180 $rec->create();
181 } catch (ilADTDBException $e) {
182 $this->db_errors[$e->getColumn()][] = $e->getCode();
183 return false;
184 }
185 return true;
186 }
187 }
188 return false;
189 }
190
196 public function update()
197 {
198 if (!$this->hasPrimary()) {
199 return $this->create();
200 }
201
202 if ($this->isValid()) {
203 try {
204 $rec = $this->initActiveRecordInstance();
205 $rec->update();
206 } catch (ilADTDBException $e) {
207 $this->db_errors[$e->getColumn()][] = $e->getCode();
208 return false;
209 }
210 return true;
211 }
212 return false;
213 }
214
220 public function delete()
221 {
222 if ($this->hasPrimary()) {
223 $rec = $this->initActiveRecordInstance();
224 $rec->delete();
225 return true;
226 }
227 return false;
228 }
229
235 public function getDBErrors()
236 {
237 return $this->db_errors;
238 }
239
246 public function translateDBErrorCodes(array $a_codes)
247 {
248 global $lng;
249
250 $res = array();
251
252 foreach ($a_codes as $code) {
253 switch ($code) {
255 $res[] = $lng->txt("adt_error_db_constraint");
256 break;
257
258 default:
259 $res[] = "Unknown ADT error code " . $code;
260 break;
261 }
262 }
263
264 return $res;
265 }
266
273 public function getAllTranslatedErrors($delimiter = "\n")
274 {
275 $tmp = array();
276
277 foreach ($this->getProperties()->getValidationErrorsByElements() as $error_code => $element_id) {
278 $tmp[] = $element_id . " [validation]: " . $this->getProperties()->translateErrorCode($error_code);
279 }
280
281 foreach ($this->getDBErrors() as $element_id => $codes) {
282 $tmp[] = $element_id . " [db]: " . implode($delimiter, $this->translateDBErrorCodes($codes));
283 }
284
285 if (sizeof($tmp)) {
286 return get_class($this) . $delimiter . implode($delimiter, $tmp);
287 }
288 }
289}
const MDB2_ERROR_CONSTRAINT
Definition: MDB2.php:75
$factory
Definition: metadata.php:47
An exception for terminatinating execution or to throw for unit testing.
ADT based-object base class.
initActiveRecordInstance()
Init active record helper for current table, primary and properties.
initProperties()
Init properties (aka set ADT definition)
__call($a_method, $a_value)
Get property magic method ("get<PropertyName>()")
createPrimaryKey()
Create new primary key, e.g.
translateDBErrorCodes(array $a_codes)
Translate DB error codes.
initDBBridge(ilADTGroupDBBridge $a_adt_db)
Init (properties) DB bridge.
getDBErrors()
Get DB errors.
getAllTranslatedErrors($delimiter="\n")
Get translated error codes (DB, Validation)
hasPrimary()
Check if currently has primary.
parsePrimary(array $a_args)
Parse incoming primary key.
update()
Update record (only if valid)
create()
Create record (only if valid)
getProperties()
Get all properties.
static getInstance()
Get singleton.
$code
Definition: example_050.php:99
global $lng
Definition: privfeed.php:17
$type
foreach($_POST as $key=> $value) $res
$delimiter
Definition: showstats.php:16
global $ilDB