ILIAS  release_8 Revision v8.24
class.ilADTBasedObject.php
Go to the documentation of this file.
1<?php
2
3declare(strict_types=1);
4/* Copyright (c) 1998-2009 ILIAS open source, Extended GPL, see docs/LICENSE */
5
12abstract class ilADTBasedObject
13{
14 protected ilADT $properties;
15 protected array $db_errors = [];
16
17 protected ilDBInterface $db;
18 protected ilLanguage $lng;
19
24 public function __construct()
25 {
26 global $DIC;
27 $this->db = $DIC->database();
28 $this->lng = $DIC->language();
29 $this->properties = $this->initProperties();
30
31 // :TODO: to keep constructor "open" we COULD use func_get_args()
32 $this->parsePrimary(func_get_args());
33 $this->read();
34 }
35
36
37 //
38 // properties
39 //
40
45 abstract protected function initProperties(): ilADT;
46
50 public function getProperties(): ilADT
51 {
52 return $this->properties;
53 }
54
59 public function isValid(): bool
60 {
61 return $this->properties->isValid();
62 }
63
72 public function __call($a_method, $a_value)
73 {
74 $type = substr($a_method, 0, 3);
75 switch ($type) {
76 case "get":
77 $parsed = strtolower(preg_replace("/([A-Z])/", " $1", substr($a_method, 3)));
78 $parsed = str_replace(" ", "_", trim($parsed));
79 if (!$this->properties->hasElement($parsed)) {
80 throw new Exception("ilADTObject unknown property " . $parsed);
81 }
82 return $this->properties->getElement($parsed);
83
84 default:
85 throw new Exception("ilADTObject unknown type: " . $type);
86 }
87 }
88
89
90 //
91 // CRUD / active record
92 //
93
99 abstract protected function parsePrimary(array $a_args): void;
100
105 abstract protected function hasPrimary(): bool;
106
111 abstract protected function createPrimaryKeyb(): bool;
112
117 abstract protected function initDBBridge(ilADTDBBridge $a_adt_db): void;
118
124 {
125 if (!$this->hasPrimary()) {
126 throw new Exception("ilADTBasedObject no primary");
127 }
128
130 $adt_db = $factory->getDBBridgeForInstance($this->properties);
131 $this->initDBBridge($adt_db);
132
133 // use custom error handling
134 //FIXME
135 //$this->db->exception = "ilADTDBException";
136
138 return $factory->getActiveRecordInstance($adt_db);
139 }
140
145 public function read(): bool
146 {
147 if ($this->hasPrimary()) {
148 $rec = $this->initActiveRecordInstance();
149 return $rec->read();
150 }
151 return false;
152 }
153
158 public function create(): bool
159 {
160 if ($this->hasPrimary()) {
161 return $this->update();
162 }
163
164 if ($this->isValid()) {
165 if ($this->createPrimaryKeyb()) {
166 try {
167 $rec = $this->initActiveRecordInstance();
168 $rec->create();
169 } catch (ilADTDBException $e) {
170 $this->db_errors[$e->getColumn()][] = $e->getCode();
171 return false;
172 }
173 return true;
174 }
175 }
176 return false;
177 }
178
183 public function update(): bool
184 {
185 if (!$this->hasPrimary()) {
186 return $this->create();
187 }
188
189 if ($this->isValid()) {
190 try {
191 $rec = $this->initActiveRecordInstance();
192 $rec->update();
193 } catch (ilADTDBException $e) {
194 $this->db_errors[$e->getColumn()][] = $e->getCode();
195 return false;
196 }
197 return true;
198 }
199 return false;
200 }
201
206 public function delete(): bool
207 {
208 if ($this->hasPrimary()) {
209 $rec = $this->initActiveRecordInstance();
210 $rec->delete();
211 return true;
212 }
213 return false;
214 }
215
220 public function getDBErrors(): array
221 {
222 return $this->db_errors;
223 }
224
230 public function translateDBErrorCodes(array $a_codes): array
231 {
232 $res = array();
233
234 foreach ($a_codes as $code) {
235 switch ($code) {
236 default:
237 $res[] = "Unknown ADT error code " . $code;
238 break;
239 }
240 }
241 return $res;
242 }
243
249 public function getAllTranslatedErrors(string $delimiter = "\n"): string
250 {
251 $tmp = array();
252
253 foreach ($this->getProperties()->getValidationErrorsByElements() as $error_code => $element_id) {
254 $tmp[] = $element_id . " [validation]: " . $this->getProperties()->translateErrorCode($error_code);
255 }
256
257 foreach ($this->getDBErrors() as $element_id => $codes) {
258 $tmp[] = $element_id . " [db]: " . implode($delimiter, $this->translateDBErrorCodes($codes));
259 }
260
261 if (count($tmp)) {
262 return get_class($this) . $delimiter . implode($delimiter, $tmp);
263 }
264 return '';
265 }
266}
ADT Active Record helper class This class expects a valid primary for all actions!
ADT based-object base class Currently "mixed" with ActiveRecord-pattern, could be splitted.
initActiveRecordInstance()
Init active record helper for current table, primary and properties.
__construct()
Constructor Tries to read record from DB, in accordance to current ILIAS behaviour.
getAllTranslatedErrors(string $delimiter="\n")
Get translated error codes (DB, Validation)
initProperties()
Init properties (aka set ADT definition)
__call($a_method, $a_value)
Get property magic method ("get<PropertyName>()") Setters are type-specific and cannot be magic.
translateDBErrorCodes(array $a_codes)
Translate DB error codes.
createPrimaryKeyb()
Create new primary key, e.g.
getDBErrors()
Get DB errors.
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)
initDBBridge(ilADTDBBridge $a_adt_db)
Init (properties) DB bridge.
getProperties()
Get all properties.
ADT DB bridge base class.
ADT base class.
Definition: class.ilADT.php:12
language handling
global $DIC
Definition: feed.php:28
Interface ilDBInterface.
$res
Definition: ltiservices.php:69
$factory
Definition: metadata.php:75
$type