ILIAS  release_5-1 Revision 5.0.0-5477-g43f3e3fab5f
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 {
81 case "get":
82 $parsed = strtolower(preg_replace("/([A-Z])/", " $1", substr($a_method, 3)));
83 $parsed = str_replace(" ", "_", trim($parsed));
84 if(!$this->properties->hasElement($parsed))
85 {
86 throw new Exception("ilADTObject unknown property ".$parsed);
87 }
88 return $this->properties->getElement($parsed);
89
90 default:
91 throw new Exception("ilADTObject unknown method ".$parsed);
92 }
93 }
94
95
96 //
97 // CRUD / active record
98 //
99
106 abstract protected function parsePrimary(array $a_args);
107
113 abstract protected function hasPrimary();
114
120 abstract protected function createPrimaryKey();
121
127 abstract protected function initDBBridge(ilADTGroupDBBridge $a_adt_db);
128
134 protected function initActiveRecordInstance()
135 {
136 global $ilDB;
137
138 if(!$this->hasPrimary())
139 {
140 throw new Exception("ilADTBasedObject no primary");
141 }
142
143 $factory = ilADTFactory::getInstance();
144 $this->adt_db = $factory->getDBBridgeForInstance($this->properties);
145 $this->initDBBridge($this->adt_db);
146
147 // use custom error handling
148 include_once "Services/ADT/classes/class.ilADTDBException.php";
149 $ilDB->exception = "ilADTDBException";
150
151 return $factory->getActiveRecordInstance($this->adt_db);
152 }
153
159 public function read()
160 {
161 if($this->hasPrimary())
162 {
163 $rec = $this->initActiveRecordInstance();
164 return $rec->read();
165 }
166 return false;
167 }
168
174 public function create()
175 {
176 if($this->hasPrimary())
177 {
178 return $this->update();
179 }
180
181 if($this->isValid())
182 {
183 if($this->createPrimaryKey())
184 {
185 try
186 {
187 $rec = $this->initActiveRecordInstance();
188 $rec->create();
189 }
190 catch(ilADTDBException $e)
191 {
192 $this->db_errors[$e->getColumn()][] = $e->getCode();
193 return false;
194 }
195 return true;
196 }
197 }
198 return false;
199 }
200
206 public function update()
207 {
208 if(!$this->hasPrimary())
209 {
210 return $this->create();
211 }
212
213 if($this->isValid())
214 {
215 try
216 {
217 $rec = $this->initActiveRecordInstance();
218 $rec->update();
219 }
220 catch(ilADTDBException $e)
221 {
222 $this->db_errors[$e->getColumn()][] = $e->getCode();
223 return false;
224 }
225 return true;
226 }
227 return false;
228 }
229
235 public function delete()
236 {
237 if($this->hasPrimary())
238 {
239 $rec = $this->initActiveRecordInstance();
240 $rec->delete();
241 return true;
242 }
243 return false;
244 }
245
251 public function getDBErrors()
252 {
253 return $this->db_errors;
254 }
255
262 public function translateDBErrorCodes(array $a_codes)
263 {
264 global $lng;
265
266 $res = array();
267
268 foreach($a_codes as $code)
269 {
270 switch($code)
271 {
273 $res[] = $lng->txt("adt_error_db_constraint");
274 break;
275
276 default:
277 $res[] = "Unknown ADT error code ".$code;
278 break;
279 }
280 }
281
282 return $res;
283 }
284
291 public function getAllTranslatedErrors($delimiter = "\n")
292 {
293 $tmp = array();
294
295 foreach($this->getProperties()->getValidationErrorsByElements() as $error_code => $element_id)
296 {
297 $tmp[] = $element_id." [validation]: ".$this->getProperties()->translateErrorCode($error_code);
298 }
299
300 foreach($this->getDBErrors() as $element_id => $codes)
301 {
302 $tmp[] = $element_id." [db]: ".implode($delimiter, $this->translateDBErrorCodes($codes));
303 }
304
305 if(sizeof($tmp))
306 {
307 return get_class($this).$delimiter.implode($delimiter, $tmp);
308 }
309 }
310}
311
312?>
const MDB2_ERROR_CONSTRAINT
Definition: MDB2.php:70
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:40
global $ilDB