ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
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="\)
 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.

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

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.
Set document properties
+ 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.

References array, createPrimaryKey(), hasPrimary(), initDBBridge(), parsePrimary(), and properties.

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  }
Set document properties
+ Here is the call graph for this function:

◆ create()

ilADTBasedObject::create ( )

Create record (only if valid)

Returns
boolean

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

References createPrimaryKey(), ilADTDBException\getColumn(), hasPrimary(), initActiveRecordInstance(), isValid(), and update().

Referenced by update().

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  }
initActiveRecordInstance()
Init active record helper for current table, primary and properties.
hasPrimary()
Check if currently has primary.
createPrimaryKey()
Create new primary key, e.g.
update()
Update record (only if valid)
+ 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

Referenced by __call(), and create().

+ Here is the caller graph for this function:

◆ delete()

ilADTBasedObject::delete ( )

Delete record.

Returns
boolean

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

References hasPrimary(), and initActiveRecordInstance().

236  {
237  if($this->hasPrimary())
238  {
239  $rec = $this->initActiveRecordInstance();
240  $rec->delete();
241  return true;
242  }
243  return false;
244  }
initActiveRecordInstance()
Init active record helper for current table, primary and properties.
hasPrimary()
Check if currently has primary.
+ 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 291 of file class.ilADTBasedObject.php.

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

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  }
translateDBErrorCodes(array $a_codes)
Translate DB error codes.
getDBErrors()
Get DB errors.
getProperties()
Get all properties.
Create styles array
The data for the language used.
+ Here is the call graph for this function:

◆ getDBErrors()

ilADTBasedObject::getDBErrors ( )

Get DB errors.

Returns
array

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

References $db_errors.

Referenced by getAllTranslatedErrors().

252  {
253  return $this->db_errors;
254  }
+ 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().

52  {
53  return $this->properties;
54  }
+ Here is the caller graph for this function:

◆ hasPrimary()

ilADTBasedObject::hasPrimary ( )
abstractprotected

Check if currently has primary.

Returns
bool

Referenced by __call(), 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 134 of file class.ilADTBasedObject.php.

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

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

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  }
hasPrimary()
Check if currently has primary.
static getInstance()
Get singleton.
initDBBridge(ilADTGroupDBBridge $a_adt_db)
Init (properties) DB bridge.
global $ilDB
Set document properties
+ 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

Referenced by __call(), and initActiveRecordInstance().

+ Here is the caller graph for this function:

◆ initProperties()

ilADTBasedObject::initProperties ( )
abstractprotected

Init properties (aka set ADT definition)

Returns
ilADT

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.

References properties.

Referenced by create(), and update().

62  {
63  return $this->properties->isValid();
64  }
Set document properties
+ 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

Referenced by __call(), and __construct().

+ Here is the caller graph for this function:

◆ read()

ilADTBasedObject::read ( )

Read record.

Returns
boolean

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

References hasPrimary(), and initActiveRecordInstance().

Referenced by __construct().

160  {
161  if($this->hasPrimary())
162  {
163  $rec = $this->initActiveRecordInstance();
164  return $rec->read();
165  }
166  return false;
167  }
initActiveRecordInstance()
Init active record helper for current table, primary and properties.
hasPrimary()
Check if currently has primary.
+ 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 262 of file class.ilADTBasedObject.php.

References $code, $lng, $res, array, and MDB2_ERROR_CONSTRAINT.

Referenced by getAllTranslatedErrors().

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  }
$code
Definition: example_050.php:99
const MDB2_ERROR_CONSTRAINT
Definition: MDB2.php:75
Create styles array
The data for the language used.
global $lng
Definition: privfeed.php:17
+ Here is the caller graph for this function:

◆ update()

ilADTBasedObject::update ( )

Update record (only if valid)

Returns
boolean

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

References create(), ilADTDBException\getColumn(), hasPrimary(), initActiveRecordInstance(), and isValid().

Referenced by create().

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  }
initActiveRecordInstance()
Init active record helper for current table, primary and properties.
hasPrimary()
Check if currently has primary.
create()
Create record (only if valid)
+ 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: