ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
class.ilDclBaseRecordFieldModel.php
Go to the documentation of this file.
1 <?php
2 
20 declare(strict_types=1);
21 
23 {
24  protected ?int $id = null;
30  protected $value;
31  protected ilObjUser $user;
32  protected ilCtrl $ctrl;
33  protected ilDBInterface $db;
34  protected ilLanguage $lng;
37 
42  public function __construct(ilDclBaseRecordModel $record, ilDclBaseFieldModel $field)
43  {
44  global $DIC;
45 
46  $this->record = $record;
47  $this->field = $field;
48  $this->ctrl = $DIC->ctrl();
49  $this->user = $DIC->user();
50  $this->db = $DIC->database();
51  $this->lng = $DIC->language();
52  $this->http = $DIC->http();
53  $this->refinery = $DIC->refinery();
54  $this->doRead();
55  }
56 
60  protected function doRead(): void
61  {
62  if (!$this->getRecord()->getId()) {
63  return;
64  }
65 
66  $query = "SELECT * FROM il_dcl_record_field WHERE field_id = " . $this->db->quote(
67  $this->getField()->getId(),
68  "integer"
69  ) . " AND record_id = "
70  . $this->db->quote($this->getRecord()->getId(), "integer");
71  $set = $this->db->query($query);
72  $rec = $this->db->fetchAssoc($set);
73  $this->id = $rec['id'] ?? null;
74 
75  $this->loadValue();
76  }
77 
81  public function doCreate(): void
82  {
83  $id = $this->db->nextId("il_dcl_record_field");
84  $query = "INSERT INTO il_dcl_record_field (id, record_id, field_id) VALUES (" . $this->db->quote(
85  $id,
86  "integer"
87  ) . ", "
88  . $this->db->quote(
89  $this->getRecord()->getId(),
90  "integer"
91  ) . ", " . $this->db->quote($this->getField()->getId(), "text") . ")";
92  $this->db->manipulate($query);
93  $this->id = $id;
94  }
95 
99  public function doUpdate(): void
100  {
101  //$this->loadValue(); //Removed Mantis #0011799
102  $datatype = $this->getField()->getDatatype();
103  $storage_location = ($this->getField()->getStorageLocationOverride() !== null) ? $this->getField()->getStorageLocationOverride() : $datatype->getStorageLocation();
104 
105  if ($storage_location != 0) {
106  $query = "DELETE FROM il_dcl_stloc" . $storage_location . "_value WHERE record_field_id = "
107  . $this->db->quote($this->id, ilDBConstants::T_INTEGER);
108  $this->db->manipulate($query);
109 
110  $next_id = $this->db->nextId("il_dcl_stloc" . $storage_location . "_value");
111 
112  $value = $this->serializeData($this->value);
113 
114  if ($this->getId() == 0) {
115  $this->doCreate();
116  }
117 
118  $insert_params = [
119  'value' => [$this->getDbType($storage_location), $value],
120  'record_field_id' => [ilDBConstants::T_INTEGER, $this->getId()],
121  'id' => [ilDBConstants::T_INTEGER, $next_id],
122  ];
123 
124  $this->db->insert("il_dcl_stloc" . $storage_location . "_value", $insert_params);
125  }
126  }
127 
128  private function getDBType(int $storage_location): string
129  {
130  switch ($storage_location) {
131  case 1:
132  return ilDBConstants::T_TEXT;
133  case 2:
135  case 3:
136  return ilDBConstants::T_DATE;
137  default:
138  throw new InvalidArgumentException('Unsupported storage_location: ' . $storage_location);
139  }
140  }
141 
145  public function delete(): void
146  {
147  $datatype = $this->getField()->getDatatype();
148  $storage_location = ($this->getField()->getStorageLocationOverride() !== null) ? $this->getField()->getStorageLocationOverride() : $datatype->getStorageLocation();
149 
150  if ($storage_location != 0) {
151  $query = "DELETE FROM il_dcl_stloc" . $storage_location . "_value WHERE record_field_id = "
152  . $this->db->quote($this->id, "integer");
153  $this->db->manipulate($query);
154  }
155 
156  $query2 = "DELETE FROM il_dcl_record_field WHERE id = " . $this->db->quote($this->id, "integer");
157  $this->db->manipulate($query2);
158  }
159 
163  public function getValue()
164  {
165  $this->loadValue();
166 
167  return $this->value;
168  }
169 
175  public function serializeData($value)
176  {
177  if (is_array($value)) {
178  $value = json_encode($value);
179  }
180 
181  return $value;
182  }
183 
189  public function deserializeData($value)
190  {
191  $deserialize = json_decode((string) $value, true);
192  if (is_array($deserialize)) {
193  return $deserialize;
194  }
195 
196  return $value;
197  }
198 
204  public function setValue($value, bool $omit_parsing = false): void
205  {
206  $this->loadValue();
207  if (!$omit_parsing) {
208  $tmp = $this->parseValue($value);
209  //if parse value fails keep the old value
210  if ($tmp !== false) {
211  $this->value = $tmp;
212  }
213  } else {
214  $this->value = $value;
215  }
216  }
217 
218  public function setValueFromForm(ilPropertyFormGUI $form): void
219  {
220  $value = $form->getInput("field_" . $this->getField()->getId());
221 
222  $this->setValue($value);
223  }
224 
225  public function getFormulaValue(): string
226  {
227  return (string) $this->getExportValue();
228  }
229 
235  public function parseExportValue($value)
236  {
237  return $value;
238  }
239 
243  public function getValueFromExcel(ilExcel $excel, int $row, int $col)
244  {
245  return (string) $excel->getCell($row, $col);
246  }
247 
253  public function parseValue($value)
254  {
255  return $value;
256  }
257 
261  public function getExportValue()
262  {
263  return $this->parseExportValue($this->getValue());
264  }
265 
266  public function fillExcelExport(ilExcel $worksheet, int &$row, int &$col): void
267  {
268  $worksheet->setCell($row, $col, $this->getExportValue());
269  $col++;
270  }
271 
275  public function getPlainText()
276  {
277  return $this->getExportValue();
278  }
279 
284  public function getSortingValue(bool $link = true)
285  {
286  return $this->parseSortingValue($this->getValue(), $link);
287  }
288 
292  public function addHiddenItemsToConfirmation(ilConfirmationGUI $confirmation)
293  {
294  if (!is_array($this->getValue())) {
295  $confirmation->addHiddenItem('field_' . $this->field->getId(), (string) $this->getValue());
296  } else {
297  foreach ($this->getValue() as $key => $value) {
298  $confirmation->addHiddenItem('field_' . $this->field->getId() . "[$key]", (string) $value);
299  }
300  }
301  }
302 
308  public function parseSortingValue($value, bool $link = true)
309  {
310  return $value;
311  }
312 
316  protected function loadValue(): void
317  {
318  if ($this->value === null) {
319  $datatype = $this->getField()->getDatatype();
320 
321  $storage_location = ($this->getField()->getStorageLocationOverride() !== null) ? $this->getField()->getStorageLocationOverride() : $datatype->getStorageLocation();
322  if ($storage_location != 0) {
323  $query = "SELECT * FROM il_dcl_stloc" . $storage_location . "_value WHERE record_field_id = "
324  . $this->db->quote($this->id, "integer");
325 
326  $set = $this->db->query($query);
327  $rec = $this->db->fetchAssoc($set);
328  $value = $this->deserializeData($rec['value'] ?? null);
329  $this->value = $value;
330  }
331  }
332  }
333 
337  public function cloneStructure(ilDclBaseRecordFieldModel $old_record_field): void
338  {
339  $this->setValue($old_record_field->getValue());
340  $this->doUpdate();
341  }
342 
346  public function afterClone(): void
347  {
348  }
349 
350  public function getField(): ilDclBaseFieldModel
351  {
352  return $this->field;
353  }
354 
355  public function getId(): ?int
356  {
357  return $this->id;
358  }
359 
360  public function getRecord(): ilDclBaseRecordModel
361  {
362  return $this->record;
363  }
364 
366  {
368  }
369 
370  public function setRecordRepresentation(ilDclBaseRecordRepresentation $record_representation): void
371  {
372  $this->record_representation = $record_representation;
373  }
374 
376  {
378  }
379 
380  public function setFieldRepresentation(ilDclBaseFieldRepresentation $field_representation): void
381  {
382  $this->field_representation = $field_representation;
383  }
384 }
parseValue($value)
Function to parse incoming data from form input value $value.
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.
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.
serializeData($value)
Serialize data before storing to db.
getCell(int $a_row, int $a_col)
Returns the value of a cell.