ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
ilADTBasedObject Class Reference

ADT based-object base class. More...

+ Inheritance diagram for ilADTBasedObject:
+ Collaboration diagram for ilADTBasedObject:

Public Member Functions

 __construct ()
 Constructor. More...
 
 getProperties ()
 Get all properties. More...
 
 isValid ()
 Validate. More...
 
 __call ($a_method, $a_value)
 Get property magic method ("get<PropertyName>()") More...
 
 read ()
 Read record. More...
 
 create ()
 Create record (only if valid) More...
 
 update ()
 Update record (only if valid) More...
 
 delete ()
 Delete record. More...
 
 getDBErrors ()
 Get DB errors. More...
 
 translateDBErrorCodes (array $a_codes)
 Translate DB error codes. More...
 
 getAllTranslatedErrors ($delimiter="\n")
 Get translated error codes (DB, Validation) More...
 

Protected Member Functions

 initProperties ()
 Init properties (aka set ADT definition) More...
 
 parsePrimary (array $a_args)
 Parse incoming primary key. More...
 
 hasPrimary ()
 Check if currently has primary. More...
 
 createPrimaryKey ()
 Create new primary key, e.g. More...
 
 initDBBridge (ilADTGroupDBBridge $a_adt_db)
 Init (properties) DB bridge. More...
 
 initActiveRecordInstance ()
 Init active record helper for current table, primary and properties. More...
 

Protected Attributes

 $properties = array()
 
 $db_errors = array()
 

Detailed Description

ADT based-object base class.

Currently "mixed" with ActiveRecord-pattern, could be splitted

Author
Jörg Lützenkirchen luetz.nosp@m.enki.nosp@m.rchen.nosp@m.@lei.nosp@m.fos.c.nosp@m.om
Version
$Id$

Definition at line 13 of file class.ilADTBasedObject.php.

Constructor & Destructor Documentation

◆ __construct()

ilADTBasedObject::__construct ( )

Constructor.

Tries to read record from DB, in accordance to current ILIAS behaviour

Returns
self

Definition at line 25 of file class.ilADTBasedObject.php.

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 }
initProperties()
Init properties (aka set ADT definition)
parsePrimary(array $a_args)
Parse incoming primary key.

References initProperties(), parsePrimary(), and read().

+ Here is the call graph for this function:

Member Function Documentation

◆ __call()

ilADTBasedObject::__call (   $a_method,
  $a_value 
)

Get property magic method ("get<PropertyName>()")

Setters are type-specific and cannot be magic

Exceptions
Exception
Parameters
string$a_method
mixed$a_value
Returns
ilADT

Definition at line 76 of file class.ilADTBasedObject.php.

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 }
$type

References $type.

◆ create()

ilADTBasedObject::create ( )

Create record (only if valid)

Returns
boolean

Definition at line 172 of file class.ilADTBasedObject.php.

173 {
174 if ($this->hasPrimary()) {
175 return $this->update();
176 }
177
178 if ($this->isValid()) {
179 if ($this->createPrimaryKey()) {
180 try {
181 $rec = $this->initActiveRecordInstance();
182 $rec->create();
183 } catch (ilADTDBException $e) {
184 $this->db_errors[$e->getColumn()][] = $e->getCode();
185 return false;
186 }
187 return true;
188 }
189 }
190 return false;
191 }
initActiveRecordInstance()
Init active record helper for current table, primary and properties.
createPrimaryKey()
Create new primary key, e.g.
hasPrimary()
Check if currently has primary.
update()
Update record (only if valid)

References Vendor\Package\$e, createPrimaryKey(), hasPrimary(), initActiveRecordInstance(), isValid(), and update().

Referenced by update().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ createPrimaryKey()

ilADTBasedObject::createPrimaryKey ( )
abstractprotected

Create new primary key, e.g.

sequence

Returns
bool

Reimplemented in ilADTTest.

Referenced by create().

+ Here is the caller graph for this function:

◆ delete()

ilADTBasedObject::delete ( )

Delete record.

Returns
boolean

Definition at line 222 of file class.ilADTBasedObject.php.

223 {
224 if ($this->hasPrimary()) {
225 $rec = $this->initActiveRecordInstance();
226 $rec->delete();
227 return true;
228 }
229 return false;
230 }

References hasPrimary(), and initActiveRecordInstance().

+ Here is the call graph for this function:

◆ getAllTranslatedErrors()

ilADTBasedObject::getAllTranslatedErrors (   $delimiter = "\n")

Get translated error codes (DB, Validation)

Parameters
type$delimiter
Returns
string

Definition at line 277 of file class.ilADTBasedObject.php.

278 {
279 $tmp = array();
280
281 foreach ($this->getProperties()->getValidationErrorsByElements() as $error_code => $element_id) {
282 $tmp[] = $element_id . " [validation]: " . $this->getProperties()->translateErrorCode($error_code);
283 }
284
285 foreach ($this->getDBErrors() as $element_id => $codes) {
286 $tmp[] = $element_id . " [db]: " . implode($delimiter, $this->translateDBErrorCodes($codes));
287 }
288
289 if (sizeof($tmp)) {
290 return get_class($this) . $delimiter . implode($delimiter, $tmp);
291 }
292 }
translateDBErrorCodes(array $a_codes)
Translate DB error codes.
getDBErrors()
Get DB errors.
getProperties()
Get all properties.

References getDBErrors(), getProperties(), and translateDBErrorCodes().

+ Here is the call graph for this function:

◆ getDBErrors()

ilADTBasedObject::getDBErrors ( )

Get DB errors.

Returns
array

Definition at line 237 of file class.ilADTBasedObject.php.

238 {
239 return $this->db_errors;
240 }

References $db_errors.

Referenced by getAllTranslatedErrors().

+ Here is the caller graph for this function:

◆ getProperties()

ilADTBasedObject::getProperties ( )

Get all properties.

Returns
array ilADT

Definition at line 51 of file class.ilADTBasedObject.php.

References $properties.

Referenced by getAllTranslatedErrors().

+ Here is the caller graph for this function:

◆ hasPrimary()

ilADTBasedObject::hasPrimary ( )
abstractprotected

Check if currently has primary.

Returns
bool

Reimplemented in ilADTTest.

Referenced by create(), delete(), initActiveRecordInstance(), read(), and update().

+ Here is the caller graph for this function:

◆ initActiveRecordInstance()

ilADTBasedObject::initActiveRecordInstance ( )
protected

Init active record helper for current table, primary and properties.

Returns
ilADTActiveRecord

Definition at line 132 of file class.ilADTBasedObject.php.

133 {
134 global $DIC;
135
136 $ilDB = $DIC['ilDB'];
137
138 if (!$this->hasPrimary()) {
139 throw new Exception("ilADTBasedObject no primary");
140 }
141
143 $this->adt_db = $factory->getDBBridgeForInstance($this->properties);
144 $this->initDBBridge($this->adt_db);
145
146 // use custom error handling
147 include_once "Services/ADT/classes/class.ilADTDBException.php";
148 $ilDB->exception = "ilADTDBException";
149
150 return $factory->getActiveRecordInstance($this->adt_db);
151 }
initDBBridge(ilADTGroupDBBridge $a_adt_db)
Init (properties) DB bridge.
static getInstance()
Get singleton.
$factory
Definition: metadata.php:58
global $ilDB
$DIC
Definition: xapitoken.php:46

References $DIC, $factory, $ilDB, ilADTFactory\getInstance(), hasPrimary(), and initDBBridge().

Referenced by create(), delete(), read(), and update().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ initDBBridge()

ilADTBasedObject::initDBBridge ( ilADTGroupDBBridge  $a_adt_db)
abstractprotected

Init (properties) DB bridge.

Parameters
ilADTGroupDBBridge$a_adt_db

Reimplemented in ilADTTest.

Referenced by initActiveRecordInstance().

+ Here is the caller graph for this function:

◆ initProperties()

ilADTBasedObject::initProperties ( )
abstractprotected

Init properties (aka set ADT definition)

Returns
ilADT

Reimplemented in ilADTTest.

Referenced by __construct().

+ Here is the caller graph for this function:

◆ isValid()

ilADTBasedObject::isValid ( )

Validate.

Returns
bool

Definition at line 61 of file class.ilADTBasedObject.php.

62 {
63 return $this->properties->isValid();
64 }

Referenced by create(), and update().

+ Here is the caller graph for this function:

◆ parsePrimary()

ilADTBasedObject::parsePrimary ( array  $a_args)
abstractprotected

Parse incoming primary key.

See also
__construct()
Parameters
array$a_args

Reimplemented in ilADTTest.

Referenced by __construct().

+ Here is the caller graph for this function:

◆ read()

ilADTBasedObject::read ( )

Read record.

Returns
boolean

Definition at line 158 of file class.ilADTBasedObject.php.

159 {
160 if ($this->hasPrimary()) {
161 $rec = $this->initActiveRecordInstance();
162 return $rec->read();
163 }
164 return false;
165 }

References hasPrimary(), and initActiveRecordInstance().

Referenced by __construct().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ translateDBErrorCodes()

ilADTBasedObject::translateDBErrorCodes ( array  $a_codes)

Translate DB error codes.

Parameters
array$a_codes
Returns
array

Definition at line 248 of file class.ilADTBasedObject.php.

249 {
250 global $DIC;
251
252 $lng = $DIC['lng'];
253
254 $res = array();
255
256 foreach ($a_codes as $code) {
257 switch ($code) {
258 case MDB2_ERROR_CONSTRAINT:
259 $res[] = $lng->txt("adt_error_db_constraint");
260 break;
261
262 default:
263 $res[] = "Unknown ADT error code " . $code;
264 break;
265 }
266 }
267
268 return $res;
269 }
$lng
foreach($_POST as $key=> $value) $res

References $DIC, $lng, and $res.

Referenced by getAllTranslatedErrors().

+ Here is the caller graph for this function:

◆ update()

ilADTBasedObject::update ( )

Update record (only if valid)

Returns
boolean

Definition at line 198 of file class.ilADTBasedObject.php.

199 {
200 if (!$this->hasPrimary()) {
201 return $this->create();
202 }
203
204 if ($this->isValid()) {
205 try {
206 $rec = $this->initActiveRecordInstance();
207 $rec->update();
208 } catch (ilADTDBException $e) {
209 $this->db_errors[$e->getColumn()][] = $e->getCode();
210 return false;
211 }
212 return true;
213 }
214 return false;
215 }
create()
Create record (only if valid)

References Vendor\Package\$e, create(), hasPrimary(), initActiveRecordInstance(), and isValid().

Referenced by create().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Field Documentation

◆ $db_errors

ilADTBasedObject::$db_errors = array()
protected

Definition at line 16 of file class.ilADTBasedObject.php.

Referenced by getDBErrors().

◆ $properties

ilADTBasedObject::$properties = array()
protected

Definition at line 15 of file class.ilADTBasedObject.php.

Referenced by getProperties().


The documentation for this class was generated from the following file: