ILIAS  trunk Revision v12.0_alpha-377-g3641b37b9db
class.ilDclBaseRecordFieldModel.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22{
23 protected ?int $id = null;
29 protected $value;
30 protected ilObjUser $user;
31 protected ilCtrl $ctrl;
32 protected ilDBInterface $db;
33 protected ilLanguage $lng;
36
42 {
43 global $DIC;
44
45 $this->record = $record;
46 $this->field = $field;
47 $this->ctrl = $DIC->ctrl();
48 $this->user = $DIC->user();
49 $this->db = $DIC->database();
50 $this->lng = $DIC->language();
51 $this->http = $DIC->http();
52 $this->refinery = $DIC->refinery();
53 $this->doRead();
54 }
55
56 public function setUser(ilObjUser $user): void
57 {
58 $this->user = $user;
59 }
60
64 protected function doRead(): void
65 {
66 if (!$this->getRecord()->getId()) {
67 return;
68 }
69
70 $query = "SELECT * FROM il_dcl_record_field WHERE field_id = " . $this->db->quote(
71 $this->getField()->getId(),
72 "integer"
73 ) . " AND record_id = "
74 . $this->db->quote($this->getRecord()->getId(), "integer");
75 $set = $this->db->query($query);
76 $rec = $this->db->fetchAssoc($set);
77 $this->id = $rec['id'] ?? null;
78
79 $this->loadValue();
80 }
81
85 public function doCreate(): void
86 {
87 $id = $this->db->nextId("il_dcl_record_field");
88 $query = "INSERT INTO il_dcl_record_field (id, record_id, field_id) VALUES (" . $this->db->quote(
89 $id,
90 "integer"
91 ) . ", "
92 . $this->db->quote(
93 $this->getRecord()->getId(),
94 "integer"
95 ) . ", " . $this->db->quote($this->getField()->getId(), "text") . ")";
96 $this->db->manipulate($query);
97 $this->id = $id;
98 }
99
103 public function doUpdate(): void
104 {
105 //$this->loadValue(); //Removed Mantis #0011799
106 $datatype = $this->getField()->getDatatype();
107 $storage_location = ($this->getField()->getStorageLocationOverride() !== null) ? $this->getField()->getStorageLocationOverride() : $datatype->getStorageLocation();
108
109 if ($storage_location != 0) {
110 $query = "DELETE FROM il_dcl_stloc" . $storage_location . "_value WHERE record_field_id = "
111 . $this->db->quote($this->id, ilDBConstants::T_INTEGER);
112 $this->db->manipulate($query);
113
114 $next_id = $this->db->nextId("il_dcl_stloc" . $storage_location . "_value");
115
116 $value = $this->serializeData($this->value);
117
118 if (empty($this->getId())) {
119 $this->doCreate();
120 }
121
122 $insert_params = [
123 'value' => [$this->getDbType($storage_location), $value],
124 'record_field_id' => [ilDBConstants::T_INTEGER, $this->getId()],
125 'id' => [ilDBConstants::T_INTEGER, $next_id],
126 ];
127
128 $this->db->insert("il_dcl_stloc" . $storage_location . "_value", $insert_params);
129 }
130 }
131
132 private function getDBType(int $storage_location): string
133 {
134 switch ($storage_location) {
135 case 1:
137 case 2:
139 case 3:
141 default:
142 throw new InvalidArgumentException('Unsupported storage_location: ' . $storage_location);
143 }
144 }
145
149 public function delete(): void
150 {
151 $datatype = $this->getField()->getDatatype();
152 $storage_location = ($this->getField()->getStorageLocationOverride() !== null) ? $this->getField()->getStorageLocationOverride() : $datatype->getStorageLocation();
153
154 if ($storage_location != 0) {
155 $query = "DELETE FROM il_dcl_stloc" . $storage_location . "_value WHERE record_field_id = "
156 . $this->db->quote($this->id, "integer");
157 $this->db->manipulate($query);
158 }
159
160 $query2 = "DELETE FROM il_dcl_record_field WHERE id = " . $this->db->quote($this->id, "integer");
161 $this->db->manipulate($query2);
162 }
163
167 public function getValue()
168 {
169 $this->loadValue();
170
171 return $this->value;
172 }
173
179 public function serializeData($value)
180 {
181 if (is_array($value)) {
182 $value = json_encode($value);
183 }
184
185 return $value;
186 }
187
193 public function deserializeData($value)
194 {
195 $deserialize = json_decode((string) $value, true);
196 if (is_array($deserialize)) {
197 return $deserialize;
198 }
199
200 return $value;
201 }
202
208 public function setValue($value, bool $omit_parsing = false): void
209 {
210 $this->loadValue();
211 if (!$omit_parsing) {
212 $tmp = $this->parseValue($value);
213 //if parse value fails keep the old value
214 if ($tmp !== false) {
215 $this->value = $tmp;
216 }
217 } else {
218 $this->value = $value;
219 }
220 }
221
222 public function setValueFromForm(ilPropertyFormGUI $form): void
223 {
224 $value = $form->getInput("field_" . $this->getField()->getId());
225
226 $this->setValue($value);
227 }
228
229 public function getFormulaValue(): string
230 {
231 return (string) $this->getExportValue();
232 }
233
239 public function parseExportValue($value)
240 {
241 return $value;
242 }
243
247 public function getValueFromExcel(ilExcel $excel, int $row, int $col)
248 {
249 return (string) $excel->getCell($row, $col);
250 }
251
256 public function parseValue($value)
257 {
258 return $value;
259 }
260
264 public function getExportValue()
265 {
266 return $this->parseExportValue($this->getValue());
267 }
268
269 public function fillExcelExport(ilExcel $worksheet, int &$row, int &$col): void
270 {
271 $worksheet->setCell($row, $col, $this->getExportValue());
272 $col++;
273 }
274
278 public function getPlainText()
279 {
280 return $this->getExportValue();
281 }
282
287 public function getSortingValue(bool $link = true)
288 {
289 return $this->parseSortingValue($this->getValue(), $link);
290 }
291
295 public function addHiddenItemsToConfirmation(ilConfirmationGUI $confirmation)
296 {
297 if (!is_array($this->getValue())) {
298 $confirmation->addHiddenItem('field_' . $this->field->getId(), (string) $this->getValue());
299 } else {
300 foreach ($this->getValue() as $key => $value) {
301 $confirmation->addHiddenItem('field_' . $this->field->getId() . "[$key]", (string) $value);
302 }
303 }
304 }
305
311 public function parseSortingValue($value, bool $link = true)
312 {
313 return $value;
314 }
315
319 protected function loadValue(): void
320 {
321 if ($this->value === null) {
322 $datatype = $this->getField()->getDatatype();
323
324 $storage_location = ($this->getField()->getStorageLocationOverride() !== null) ? $this->getField()->getStorageLocationOverride() : $datatype->getStorageLocation();
325 if ($storage_location != 0) {
326 $query = "SELECT * FROM il_dcl_stloc" . $storage_location . "_value WHERE record_field_id = "
327 . $this->db->quote($this->id, "integer");
328
329 $set = $this->db->query($query);
330 $rec = $this->db->fetchAssoc($set);
331 $value = $this->deserializeData($rec['value'] ?? null);
332 $this->value = $value;
333 }
334 }
335 }
336
340 public function cloneStructure(ilDclBaseRecordFieldModel $old_record_field): void
341 {
342 $this->setValue($old_record_field->getValue(), true);
343 $this->doUpdate();
344 }
345
349 public function afterClone(): void
350 {
351 }
352
353 public function getField(): ilDclBaseFieldModel
354 {
355 return $this->field;
356 }
357
358 public function getId(): ?int
359 {
360 return $this->id;
361 }
362
364 {
365 return $this->record;
366 }
367
369 {
371 }
372
374 {
375 $this->record_representation = $record_representation;
376 }
377
379 {
381 }
382
384 {
385 $this->field_representation = $field_representation;
386 }
387}
Builds data types.
Definition: Factory.php:36
Class Services.
Definition: Services.php:38
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
addHiddenItem(string $a_post_var, string $a_value)
Class ilCtrl provides processing control methods.
addHiddenItemsToConfirmation(ilConfirmationGUI $confirmation)
getValueFromExcel(ilExcel $excel, int $row, int $col)
ilDclBaseRecordRepresentation $record_representation
setValue($value, bool $omit_parsing=false)
Set value for record field.
deserializeData($value)
Deserialize data before applying to field.
fillExcelExport(ilExcel $worksheet, int &$row, int &$col)
parseValue($value)
Function to parse incoming data from form input value $value.
parseExportValue($value)
Function to parse incoming data from form input value $value.
ilDclBaseFieldRepresentation $field_representation
setRecordRepresentation(ilDclBaseRecordRepresentation $record_representation)
doCreate()
Creates an Id and a database entry.
serializeData($value)
Serialize data before storing to db.
doRead()
Read object data from database.
__construct(ilDclBaseRecordModel $record, ilDclBaseFieldModel $field)
parseSortingValue($value, bool $link=true)
Returns sortable value for the specific field-types.
cloneStructure(ilDclBaseRecordFieldModel $old_record_field)
setFieldRepresentation(ilDclBaseFieldRepresentation $field_representation)
setCell(int $a_row, int $col, $value, ?string $datatype=null, bool $disable_strip_tags_for_strings=false)
Set cell value.
getCell(int $a_row, int $a_col)
Returns the value of a cell.
language handling
User class.
This class represents a property form user interface.
getInput(string $a_post_var, bool $ensureValidation=true)
Returns the input of an item, if item provides getInput method and as fallback the value of the HTTP-...
Interface ilDBInterface.
static http()
Fetches the global http state from ILIAS.
global $DIC
Definition: shib_login.php:26