ILIAS  trunk Revision v11.0_alpha-2638-g80c1d007f79
class.ilDclBaseRecordFieldModel.php
Go to the documentation of this file.
1 <?php
2 
19 declare(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 
41  public function __construct(ilDclBaseRecordModel $record, ilDclBaseFieldModel $field)
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 
59  protected function doRead(): void
60  {
61  if (!$this->getRecord()->getId()) {
62  return;
63  }
64 
65  $query = "SELECT * FROM il_dcl_record_field WHERE field_id = " . $this->db->quote(
66  $this->getField()->getId(),
67  "integer"
68  ) . " AND record_id = "
69  . $this->db->quote($this->getRecord()->getId(), "integer");
70  $set = $this->db->query($query);
71  $rec = $this->db->fetchAssoc($set);
72  $this->id = $rec['id'] ?? null;
73 
74  $this->loadValue();
75  }
76 
80  public function doCreate(): void
81  {
82  $id = $this->db->nextId("il_dcl_record_field");
83  $query = "INSERT INTO il_dcl_record_field (id, record_id, field_id) VALUES (" . $this->db->quote(
84  $id,
85  "integer"
86  ) . ", "
87  . $this->db->quote(
88  $this->getRecord()->getId(),
89  "integer"
90  ) . ", " . $this->db->quote($this->getField()->getId(), "text") . ")";
91  $this->db->manipulate($query);
92  $this->id = $id;
93  }
94 
98  public function doUpdate(): void
99  {
100  //$this->loadValue(); //Removed Mantis #0011799
101  $datatype = $this->getField()->getDatatype();
102  $storage_location = ($this->getField()->getStorageLocationOverride() !== null) ? $this->getField()->getStorageLocationOverride() : $datatype->getStorageLocation();
103 
104  if ($storage_location != 0) {
105  $query = "DELETE FROM il_dcl_stloc" . $storage_location . "_value WHERE record_field_id = "
106  . $this->db->quote($this->id, ilDBConstants::T_INTEGER);
107  $this->db->manipulate($query);
108 
109  $next_id = $this->db->nextId("il_dcl_stloc" . $storage_location . "_value");
110 
111  $value = $this->serializeData($this->value);
112 
113  if (empty($this->getId())) {
114  $this->doCreate();
115  }
116 
117  $insert_params = [
118  'value' => [$this->getDbType($storage_location), $value],
119  'record_field_id' => [ilDBConstants::T_INTEGER, $this->getId()],
120  'id' => [ilDBConstants::T_INTEGER, $next_id],
121  ];
122 
123  $this->db->insert("il_dcl_stloc" . $storage_location . "_value", $insert_params);
124  }
125  }
126 
127  private function getDBType(int $storage_location): string
128  {
129  switch ($storage_location) {
130  case 1:
131  return ilDBConstants::T_TEXT;
132  case 2:
134  case 3:
135  return ilDBConstants::T_DATE;
136  default:
137  throw new InvalidArgumentException('Unsupported storage_location: ' . $storage_location);
138  }
139  }
140 
144  public function delete(): void
145  {
146  $datatype = $this->getField()->getDatatype();
147  $storage_location = ($this->getField()->getStorageLocationOverride() !== null) ? $this->getField()->getStorageLocationOverride() : $datatype->getStorageLocation();
148 
149  if ($storage_location != 0) {
150  $query = "DELETE FROM il_dcl_stloc" . $storage_location . "_value WHERE record_field_id = "
151  . $this->db->quote($this->id, "integer");
152  $this->db->manipulate($query);
153  }
154 
155  $query2 = "DELETE FROM il_dcl_record_field WHERE id = " . $this->db->quote($this->id, "integer");
156  $this->db->manipulate($query2);
157  }
158 
162  public function getValue()
163  {
164  $this->loadValue();
165 
166  return $this->value;
167  }
168 
174  public function serializeData($value)
175  {
176  if (is_array($value)) {
177  $value = json_encode($value);
178  }
179 
180  return $value;
181  }
182 
188  public function deserializeData($value)
189  {
190  $deserialize = json_decode((string) $value, true);
191  if (is_array($deserialize)) {
192  return $deserialize;
193  }
194 
195  return $value;
196  }
197 
203  public function setValue($value, bool $omit_parsing = false): void
204  {
205  $this->loadValue();
206  if (!$omit_parsing) {
207  $tmp = $this->parseValue($value);
208  //if parse value fails keep the old value
209  if ($tmp !== false) {
210  $this->value = $tmp;
211  }
212  } else {
213  $this->value = $value;
214  }
215  }
216 
217  public function setValueFromForm(ilPropertyFormGUI $form): void
218  {
219  $value = $form->getInput("field_" . $this->getField()->getId());
220 
221  $this->setValue($value);
222  }
223 
224  public function getFormulaValue(): string
225  {
226  return (string) $this->getExportValue();
227  }
228 
234  public function parseExportValue($value)
235  {
236  return $value;
237  }
238 
242  public function getValueFromExcel(ilExcel $excel, int $row, int $col)
243  {
244  return (string) $excel->getCell($row, $col);
245  }
246 
252  public function parseValue($value)
253  {
254  return $value;
255  }
256 
260  public function getExportValue()
261  {
262  return $this->parseExportValue($this->getValue());
263  }
264 
265  public function fillExcelExport(ilExcel $worksheet, int &$row, int &$col): void
266  {
267  $worksheet->setCell($row, $col, $this->getExportValue());
268  $col++;
269  }
270 
274  public function getPlainText()
275  {
276  return $this->getExportValue();
277  }
278 
283  public function getSortingValue(bool $link = true)
284  {
285  return $this->parseSortingValue($this->getValue(), $link);
286  }
287 
291  public function addHiddenItemsToConfirmation(ilConfirmationGUI $confirmation)
292  {
293  if (!is_array($this->getValue())) {
294  $confirmation->addHiddenItem('field_' . $this->field->getId(), (string) $this->getValue());
295  } else {
296  foreach ($this->getValue() as $key => $value) {
297  $confirmation->addHiddenItem('field_' . $this->field->getId() . "[$key]", (string) $value);
298  }
299  }
300  }
301 
307  public function parseSortingValue($value, bool $link = true)
308  {
309  return $value;
310  }
311 
315  protected function loadValue(): void
316  {
317  if ($this->value === null) {
318  $datatype = $this->getField()->getDatatype();
319 
320  $storage_location = ($this->getField()->getStorageLocationOverride() !== null) ? $this->getField()->getStorageLocationOverride() : $datatype->getStorageLocation();
321  if ($storage_location != 0) {
322  $query = "SELECT * FROM il_dcl_stloc" . $storage_location . "_value WHERE record_field_id = "
323  . $this->db->quote($this->id, "integer");
324 
325  $set = $this->db->query($query);
326  $rec = $this->db->fetchAssoc($set);
327  $value = $this->deserializeData($rec['value'] ?? null);
328  $this->value = $value;
329  }
330  }
331  }
332 
336  public function cloneStructure(ilDclBaseRecordFieldModel $old_record_field): void
337  {
338  $this->setValue($old_record_field->getValue());
339  $this->doUpdate();
340  }
341 
345  public function afterClone(): void
346  {
347  }
348 
349  public function getField(): ilDclBaseFieldModel
350  {
351  return $this->field;
352  }
353 
354  public function getId(): ?int
355  {
356  return $this->id;
357  }
358 
359  public function getRecord(): ilDclBaseRecordModel
360  {
361  return $this->record;
362  }
363 
365  {
367  }
368 
369  public function setRecordRepresentation(ilDclBaseRecordRepresentation $record_representation): void
370  {
371  $this->record_representation = $record_representation;
372  }
373 
375  {
377  }
378 
379  public function setFieldRepresentation(ilDclBaseFieldRepresentation $field_representation): void
380  {
381  $this->field_representation = $field_representation;
382  }
383 }
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)
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
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
parseExportValue($value)
Function to parse incoming data from form input value $value.
setRecordRepresentation(ilDclBaseRecordRepresentation $record_representation)
setCell(int $a_row, int $col, $value, ?string $datatype=null, bool $disable_strip_tags_for_strings=false)
Set cell value.
static http()
Fetches the global http state from ILIAS.
addHiddenItemsToConfirmation(ilConfirmationGUI $confirmation)
doUpdate()
Update object in database.
global $DIC
Definition: shib_login.php:26
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.