ILIAS  release_8 Revision v8.23
class.ilDclBaseRecordFieldModel.php
Go to the documentation of this file.
1 <?php
2 
20 {
21  protected ?int $id = null;
27  protected $value;
28  protected ilObjUser $user;
29  protected ilCtrl $ctrl;
30  protected ilDBInterface $db;
31  protected ilLanguage $lng;
34 
39  public function __construct(ilDclBaseRecordModel $record, ilDclBaseFieldModel $field)
40  {
41  global $DIC;
42 
43  $this->record = $record;
44  $this->field = $field;
45  $this->ctrl = $DIC->ctrl();
46  $this->user = $DIC->user();
47  $this->db = $DIC->database();
48  $this->lng = $DIC->language();
49  $this->http = $DIC->http();
50  $this->refinery = $DIC->refinery();
51  $this->doRead();
52  }
53 
57  protected function doRead(): void
58  {
59  if (!$this->getRecord()->getId()) {
60  return;
61  }
62 
63  $query = "SELECT * FROM il_dcl_record_field WHERE field_id = " . $this->db->quote(
64  $this->getField()->getId(),
65  "integer"
66  ) . " AND record_id = "
67  . $this->db->quote($this->getRecord()->getId(), "integer");
68  $set = $this->db->query($query);
69  $rec = $this->db->fetchAssoc($set);
70  $this->id = $rec['id'] ?? null;
71 
72  $this->loadValue();
73  }
74 
78  public function doCreate(): void
79  {
80  $id = $this->db->nextId("il_dcl_record_field");
81  $query = "INSERT INTO il_dcl_record_field (id, record_id, field_id) VALUES (" . $this->db->quote(
82  $id,
83  "integer"
84  ) . ", "
85  . $this->db->quote(
86  $this->getRecord()->getId(),
87  "integer"
88  ) . ", " . $this->db->quote($this->getField()->getId(), "text") . ")";
89  $this->db->manipulate($query);
90  $this->id = $id;
91  }
92 
96  public function doUpdate(): void
97  {
98  //$this->loadValue(); //Removed Mantis #0011799
99  $datatype = $this->getField()->getDatatype();
100  $storage_location = ($this->getField()->getStorageLocationOverride() !== null) ? $this->getField()->getStorageLocationOverride() : $datatype->getStorageLocation();
101 
102  if ($storage_location != 0) {
103  $query = "DELETE FROM il_dcl_stloc" . $storage_location . "_value WHERE record_field_id = "
104  . $this->db->quote($this->id, "integer");
105  $this->db->manipulate($query);
106 
107  $next_id = $this->db->nextId("il_dcl_stloc" . $storage_location . "_value");
108 
109  $value = $this->serializeData($this->value);
110 
111  if ($this->getId() == 0) {
112  $this->doCreate();
113  }
114 
115  $insert_params = array(
116  "value" => array($datatype->getDbType(), $value),
117  "record_field_id" => array("integer", $this->getId()),
118  "id" => array("integer", $next_id),
119  );
120 
121  $this->db->insert("il_dcl_stloc" . $storage_location . "_value", $insert_params);
122  }
123  }
124 
128  public function delete(): void
129  {
130  $datatype = $this->getField()->getDatatype();
131  $storage_location = ($this->getField()->getStorageLocationOverride() !== null) ? $this->getField()->getStorageLocationOverride() : $datatype->getStorageLocation();
132 
133  if ($storage_location != 0) {
134  $query = "DELETE FROM il_dcl_stloc" . $storage_location . "_value WHERE record_field_id = "
135  . $this->db->quote($this->id, "integer");
136  $this->db->manipulate($query);
137  }
138 
139  $query2 = "DELETE FROM il_dcl_record_field WHERE id = " . $this->db->quote($this->id, "integer");
140  $this->db->manipulate($query2);
141  }
142 
146  public function getValue()
147  {
148  $this->loadValue();
149 
150  return $this->value;
151  }
152 
156  public function getValueForRepresentation()
157  {
158  return $this->getValue();
159  }
160 
166  public function serializeData($value)
167  {
168  if (is_array($value)) {
169  $value = json_encode($value);
170  }
171 
172  return $value;
173  }
174 
180  public function deserializeData($value)
181  {
182  $deserialize = json_decode($value, true);
183  if (is_array($deserialize)) {
184  return $deserialize;
185  }
186 
187  return $value;
188  }
189 
195  public function setValue($value, bool $omit_parsing = false): void
196  {
197  $this->loadValue();
198  if (!$omit_parsing) {
199  $tmp = $this->parseValue($value);
200  $old = $this->value;
201  //if parse value fails keep the old value
202  if ($tmp !== false) {
203  $this->value = $tmp;
204  }
205  } else {
206  $this->value = $value;
207  }
208  }
209 
210  public function setValueFromForm(ilPropertyFormGUI $form): void
211  {
212  $value = $form->getInput("field_" . $this->getField()->getId());
213 
214  $this->setValue($value);
215  }
216 
217  public function getFormulaValue(): string
218  {
219  return (string) $this->getExportValue();
220  }
221 
227  public function parseExportValue($value)
228  {
229  return $value;
230  }
231 
235  public function getValueFromExcel(ilExcel $excel, int $row, int $col)
236  {
237  $value = $excel->getCell($row, $col) ?? '';
238  return $value;
239  }
240 
246  public function parseValue($value)
247  {
248  return $value;
249  }
250 
254  public function getExportValue()
255  {
256  return $this->parseExportValue($this->getValue());
257  }
258 
259  public function fillExcelExport(ilExcel $worksheet, int &$row, int &$col): void
260  {
261  $worksheet->setCell($row, $col, $this->getExportValue());
262  $col++;
263  }
264 
268  public function getPlainText()
269  {
270  return $this->getExportValue();
271  }
272 
277  public function getSortingValue(bool $link = true)
278  {
279  return $this->parseSortingValue($this->getValue(), $link);
280  }
281 
285  public function addHiddenItemsToConfirmation(ilConfirmationGUI $confirmation)
286  {
287  ;
288  if (!is_array($this->getValue())) {
289  $confirmation->addHiddenItem('field_' . $this->field->getId(), (string) $this->getValue());
290  } else {
291  foreach ($this->getValue() as $key => $value) {
292  $confirmation->addHiddenItem('field_' . $this->field->getId() . "[$key]", $value);
293  }
294  }
295  }
296 
302  public function parseSortingValue($value, bool $link = true)
303  {
304  return $value;
305  }
306 
310  protected function loadValue(): void
311  {
312  if ($this->value === null) {
313  $datatype = $this->getField()->getDatatype();
314 
315  $storage_location = ($this->getField()->getStorageLocationOverride() !== null) ? $this->getField()->getStorageLocationOverride() : $datatype->getStorageLocation();
316  if ($storage_location != 0) {
317  $query = "SELECT * FROM il_dcl_stloc" . $storage_location . "_value WHERE record_field_id = "
318  . $this->db->quote($this->id, "integer");
319 
320  $set = $this->db->query($query);
321  $rec = $this->db->fetchAssoc($set);
322  $value = $this->deserializeData($rec['value'] ?? null);
323  $this->value = $value;
324  }
325  }
326  }
327 
331  public function cloneStructure(ilDclBaseRecordFieldModel $old_record_field): void
332  {
333  $this->setValue($old_record_field->getValue());
334  $this->doUpdate();
335  }
336 
340  public function afterClone(): void
341  {
342  }
343 
344  public function getField(): ilDclBaseFieldModel
345  {
346  return $this->field;
347  }
348 
349  public function getId(): ?int
350  {
351  return $this->id;
352  }
353 
354  public function getRecord(): ilDclBaseRecordModel
355  {
356  return $this->record;
357  }
358 
360  {
362  }
363 
364  public function setRecordRepresentation(ilDclBaseRecordRepresentation $record_representation): void
365  {
366  $this->record_representation = $record_representation;
367  }
368 
370  {
372  }
373 
374  public function setFieldRepresentation(ilDclBaseFieldRepresentation $field_representation): void
375  {
376  $this->field_representation = $field_representation;
377  }
378 }
parseValue($value)
Function to parse incoming data from form input value $value.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
ilDclBaseRecordRepresentation $record_representation
fillExcelExport(ilExcel $worksheet, int &$row, int &$col)
setValue($value, bool $omit_parsing=false)
Set value for record field.
__construct(ilDclBaseRecordModel $record, ilDclBaseFieldModel $field)
setCell(int $a_row, int $a_col, $a_value, ?string $a_datatype=null)
Set cell value.
addHiddenItem(string $a_post_var, string $a_value)
doRead()
Read object data from database.
getValueFromExcel(ilExcel $excel, int $row, int $col)
doCreate()
Creates an Id and a database entry.
cloneStructure(ilDclBaseRecordFieldModel $old_record_field)
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-...
deserializeData($value)
Deserialize data before applying to field.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
ilDclBaseFieldRepresentation $field_representation
global $DIC
Definition: feed.php:28
parseExportValue($value)
Function to parse incoming data from form input value $value.
setRecordRepresentation(ilDclBaseRecordRepresentation $record_representation)
static http()
Fetches the global http state from ILIAS.
addHiddenItemsToConfirmation(ilConfirmationGUI $confirmation)
doUpdate()
Update object in database.
string $key
Consumer key/client ID value.
Definition: System.php:193
setFieldRepresentation(ilDclBaseFieldRepresentation $field_representation)
parseSortingValue($value, bool $link=true)
Returns sortable value for the specific field-types.
$query
serializeData($value)
Serialize data before storing to db.
getCell(int $a_row, int $a_col)
Returns the value of a cell.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...