ILIAS  trunk Revision v11.0_alpha-2638-g80c1d007f79
class.ilDclBaseRecordModel.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
24 {
25  protected \ILIAS\UI\Factory $ui_factory;
26  protected \ILIAS\UI\Renderer $renderer;
27  protected Service $notes;
28 
32  protected ?array $recordfields = null;
33  protected int $id = 0;
34  protected int $table_id;
35  protected ?ilDclTable $table = null;
36  protected ?int $last_edit_by = null;
37  protected int $owner = 0;
40  protected ?int $nr_of_comments = null;
43  protected ilDBInterface $db;
45  private ilObjUser $user;
46 
47  public function __construct(?int $a_id = 0)
48  {
49  global $DIC;
50  $this->db = $DIC->database();
51  $this->event = $DIC->event();
52  $this->user = $DIC->user();
53  $this->ui_factory = $DIC->ui()->factory();
54  $this->renderer = $DIC->ui()->renderer();
55 
56  if ($a_id && $a_id != 0) {
57  $this->id = $a_id;
58  $this->doRead();
59  }
60 
61  $this->notes = $DIC->notes();
62  $this->http = $DIC->http();
63  $this->refinery = $DIC->refinery();
64  }
65 
66  public function doUpdate(bool $omit_notification = false): void
67  {
68  $values = [
69  "table_id" => [
70  "integer",
71  $this->getTableId(),
72  ],
73  "last_update" => [
74  "date",
75  $this->getLastUpdate()->get(IL_CAL_DATETIME),
76  ],
77  "owner" => [
78  "integer",
79  $this->getOwner(),
80  ],
81  "last_edit_by" => [
82  "integer",
83  $this->getLastEditBy(),
84  ],
85  ];
86  $this->db->update(
87  "il_dcl_record",
88  $values,
89  [
90  "id" => [
91  "integer",
92  $this->id,
93  ],
94  ]
95  );
96 
97  foreach ($this->getRecordFields() as $recordfield) {
98  $recordfield->doUpdate();
99  }
100 
101  //TODO: add event raise
102  if (!$omit_notification) {
103  $ref_id = $this->http->wrapper()->query()->retrieve('ref_id', $this->refinery->kindlyTo()->int());
104  $objDataCollection = new ilObjDataCollection($ref_id);
105  $objDataCollection->sendRecordNotification(ilDataCollectionMailNotification::TYPE_RECORD_UPDATE, $this);
106  }
107  }
108 
109  public function doRead(): void
110  {
111  //build query
112  $query = "Select * From il_dcl_record WHERE id = " . $this->db->quote($this->getId(), "integer") . " ORDER BY id";
113 
114  $set = $this->db->query($query);
115  $rec = $this->db->fetchAssoc($set);
116 
117  if (!$rec) {
118  $this->id = 0;
119  return;
120  }
121 
122  $this->setTableId((int) $rec["table_id"]);
123  $this->loadTable();
124  if (null !== $rec["create_date"]) {
125  $this->setCreateDate(new ilDateTime($rec["create_date"], IL_CAL_DATETIME));
126  }
127  if (null !== $rec["last_update"]) {
128  $this->setLastUpdate(new ilDateTime($rec["last_update"], IL_CAL_DATETIME));
129  } else {
130  $this->setLastUpdate(new ilDateTime($rec["create_date"], IL_CAL_DATETIME));
131  }
132  $this->setOwner((int) $rec["owner"]);
133  if (null !== $rec["last_edit_by"]) {
134  $this->setLastEditBy((int) $rec["last_edit_by"]);
135  }
136  }
137 
141  public function doCreate(): void
142  {
143  if (!ilDclTable::_tableExists($this->getTableId())) {
144  throw new ilException("The field does not have a related table!");
145  }
146 
147  $id = $this->db->nextId("il_dcl_record");
148  $this->setId($id);
149  $query
150  = "INSERT INTO il_dcl_record (
151  id,
152  table_id,
153  create_date,
154  Last_update,
155  owner,
156  last_edit_by
157  ) VALUES (" . $this->db->quote($this->getId(), "integer") . "," . $this->db->quote(
158  $this->getTableId(),
159  "integer"
160  ) . ","
161  . $this->db->quote($this->getCreateDate()->get(IL_CAL_DATETIME), "timestamp") . "," . $this->db->quote(
162  $this->getLastUpdate()->get(IL_CAL_DATETIME),
163  "timestamp"
164  ) . ","
165  . $this->db->quote($this->getOwner(), "integer") . "," . $this->db->quote($this->getLastEditBy(), "integer") . "
166  )";
167  $this->db->manipulate($query);
168 
169  $this->loadRecordFields();
170  foreach ($this->getRecordFields() as $recordField) {
171  $recordField->doCreate();
172  }
173 
174  $this->getTable()->loadRecords();
175  }
176 
177  public function deleteField(int $field_id): void
178  {
179  $this->loadRecordFields();
180  $this->recordfields[$field_id]->delete();
181  if (count($this->recordfields) == 1) {
182  $this->doDelete();
183  }
184  }
185 
186  public function setId(int $a_id): void
187  {
188  $this->id = $a_id;
189  }
190 
191  public function getId(): ?int
192  {
193  return $this->id;
194  }
195 
196  public function setTableId(int $a_id): void
197  {
198  $this->table_id = $a_id;
199  }
200 
201  public function getTableId(): int
202  {
203  return $this->table_id;
204  }
205 
206  public function setCreateDate(ilDateTime $a_datetime): void
207  {
208  $this->create_date = $a_datetime;
209  }
210 
211  public function getCreateDate(): ilDateTime
212  {
213  return $this->create_date;
214  }
215 
216  public function setLastUpdate(ilDateTime $a_datetime): void
217  {
218  $this->last_update = $a_datetime;
219  }
220 
221  public function getLastUpdate(): ilDateTime
222  {
223  return $this->last_update;
224  }
225 
226  public function setOwner(int $a_id): void
227  {
228  $this->owner = $a_id;
229  }
230 
231  public function getOwner(): int
232  {
233  return $this->owner;
234  }
235 
236  public function getLastEditBy(): ?int
237  {
238  return $this->last_edit_by;
239  }
240 
241  public function setLastEditBy(?int $last_edit_by): void
242  {
243  $this->last_edit_by = $last_edit_by;
244  }
245 
251  public function setRecordFieldValue($field_id, $value): void
252  {
253  $this->loadRecordFields();
254  if (ilDclStandardField::_isStandardField($field_id)) {
255  $this->setStandardField($field_id, $value);
256  } else {
257  $this->loadTable();
258  $this->recordfields[$field_id]->setValue($value);
259  }
260  }
261 
266  public function setRecordFieldValueFromForm(int $field_id, ilPropertyFormGUI $form): void
267  {
268  $this->loadRecordFields();
269  if (ilDclStandardField::_isStandardField($field_id)) {
270  $this->setStandardFieldFromForm($field_id, $form);
271  } else {
272  $this->loadTable();
273  $this->recordfields[$field_id]->setValueFromForm($form);
274  }
275  }
276 
280  public function getRecordFieldValueFromExcel(ilExcel $excel, int $row, int $col, ilDclBaseFieldModel $field)
281  {
282  $this->loadRecordFields();
283 
284  return $this->recordfields[$field->getId()]->getValueFromExcel($excel, $row, $col);
285  }
286 
288  ilExcel $excel,
289  int $row,
290  int $col,
291  ilDclBaseFieldModel $field
292  ): void {
293  $value = $field->getValueFromExcel($excel, $row, $col);
294  if ($value) {
295  $this->{$field->getId()} = $value;
296  }
297  }
298 
299  public function getRecordFieldValues(): array
300  {
301  $this->loadRecordFields();
302  $return = [];
303  foreach ($this->recordfields as $id => $record_field) {
304  $return[$id] = $record_field->getValue();
305  }
306 
307  return $return;
308  }
309 
314  public function getRecordFieldValue(?string $field_id)
315  {
316  if ($field_id === null) {
317  return null;
318  }
319  $this->loadRecordFields();
320  if (ilDclStandardField::_isStandardField($field_id)) {
321  return $this->getStandardField($field_id);
322  } else {
323  return $this->recordfields[$field_id]->getValue();
324  }
325  }
326 
330  public function fillRecordFieldExcelExport(ilExcel $worksheet, int &$row, int &$col, $field_id): void
331  {
332  $this->loadRecordFields();
333  if (ilDclStandardField::_isStandardField($field_id)) {
334  if ($field_id === 'owner') {
335  $worksheet->setCell($row, $col, ilObjUser::_lookupLogin($this->getOwner()));
336  $col++;
337  $name_array = ilObjUser::_lookupName($this->getOwner());
338  $worksheet->setCell($row, $col, $name_array['lastname'] . ', ' . $name_array['firstname']);
339  } elseif ($field_id === 'last_update') {
340  $date_time = $this->getLastUpdate()->get(IL_CAL_DATETIME, '', $this->user->getTimeZone());
341  $worksheet->setCell($row, $col, $date_time);
342  } elseif ($field_id === 'create_date') {
343  $date_time = $this->getCreateDate()->get(IL_CAL_DATETIME, '', $this->user->getTimeZone());
344  $worksheet->setCell($row, $col, $date_time);
345  } else {
346  $worksheet->setCell($row, $col, $this->getStandardFieldHTML($field_id));
347  }
348  $col++;
349  } else {
350  $this->recordfields[$field_id]->fillExcelExport($worksheet, $row, $col);
351  }
352  }
353 
357  public function getRecordFieldFormulaValue($field_id): string
358  {
359  $this->loadRecordFields();
360  if (ilDclStandardField::_isStandardField($field_id)) {
361  $value = $this->getStandardFieldFormulaValue($field_id);
362  } else {
363  if (is_object($this->recordfields[$field_id])) {
364  $value = $this->recordfields[$field_id]->getFormulaValue();
365  } else {
366  $value = '';
367  }
368  }
369 
370  return $value;
371  }
372 
376  public function getRecordFieldHTML($field_id, array $options = []): string
377  {
378  $this->loadRecordFields();
379  if (ilDclStandardField::_isStandardField($field_id)) {
380  $html = $this->getStandardFieldHTML($field_id, $options);
381  } else {
382  if (array_key_exists($field_id, $this->recordfields) && is_object($this->recordfields[$field_id])) {
383  $html = $this->recordfields[$field_id]->getRecordRepresentation()->getHTML(true, $options);
384  } else {
385  $html = '';
386  }
387  }
388 
389  return $html;
390  }
391 
395  public function getRecordFieldSingleHTML($field_id, array $options = []): string
396  {
397  $this->loadRecordFields();
398 
399  if (ilDclStandardField::_isStandardField($field_id)) {
400  $html = $this->getStandardFieldHTML($field_id);
401  } else {
402  $field = $this->recordfields[$field_id];
407  $html = $field->getRecordRepresentation()->getSingleHTML($options, false);
408  }
409 
410  return $html;
411  }
412 
416  public function fillRecordFieldFormInput($field_id, ilPropertyFormGUI $form): void
417  {
418  $this->loadRecordFields();
419  if (ilDclStandardField::_isStandardField($field_id)) {
420  $this->fillStandardFieldFormInput($field_id, $form);
421  } else {
422  $this->recordfields[$field_id]->getRecordRepresentation()->fillFormInput($form);
423  }
424  }
425 
429  protected function setStandardFieldFromForm($field_id, ilPropertyFormGUI $form): void
430  {
431  if ($item = $form->getItemByPostVar("field_" . $field_id)) {
432  $this->setStandardField($field_id, $item->getValue());
433  }
434  }
435 
440  protected function setStandardField($field_id, $value)
441  {
442  if ($field_id == "last_edit_by") {
443  $this->setLastEditBy($value);
444  return;
445  }
446  $this->{$field_id} = $value;
447  }
448 
452  protected function fillStandardFieldFormInput($field_id, ilPropertyFormGUI $form): void
453  {
454  if ($item = $form->getItemByPostVar('field_' . $field_id)) {
455  $item->setValue($this->getStandardField($field_id));
456  }
457  }
458 
462  protected function getStandardField($field_id): string
463  {
464  switch ($field_id) {
465  case "last_edit_by":
466  return ilObjUser::_lookupName($this->getLastEditBy())['login'];
467  case 'owner':
468  return ilObjUser::_lookupName($this->getOwner())['login'];
469  }
470 
471  return $this->{$field_id};
472  }
473 
477  public function getStandardFieldFormulaValue($field_id): string
478  {
479  return $this->getStandardFieldHTML($field_id);
480  }
481 
482  public function getStandardFieldHTML(string $field_id, array $options = []): string
483  {
484  switch ($field_id) {
485  case 'id':
486  return (string) $this->getId();
487  case 'owner':
488  return ilUserUtil::getNamePresentation($this->getOwner());
489  case 'last_edit_by':
491  case 'last_update':
493  case 'create_date':
495  case 'comments':
496 
497  $ref_id = $this->http->wrapper()->query()->retrieve('ref_id', $this->refinery->kindlyTo()->int());
498 
500  1,
501  $ref_id,
502  'dcl',
503  $this->table->getCollectionObject()
504  ->getId(),
505  'dcl',
506  $this->getId()
507  );
508  $update_code = "il.UI.counter.getCounterObject($(\".ilc_page_Page\")).incrementStatusCount(1);";
509  $ajax_link = ilNoteGUI::getListCommentsJSCall($ajax_hash, $update_code);
510  $nr_comments = $this->getNrOfComments();
511 
512  $comment_glyph = $this->ui_factory->symbol()->glyph()->comment()->withCounter(
513  $this->ui_factory->counter()->status($nr_comments)
514  )->withAdditionalOnLoadCode(function ($id) use ($ajax_link): string {
515  return "document.getElementById('$id').onclick = function (event) { $ajax_link; };";
516  });
517  return $this->renderer->render($comment_glyph);
518  }
519 
520  return "";
521  }
522 
527  public function getStandardFieldPlainText(string $field_id)
528  {
529  switch ($field_id) {
530  case 'comments':
531  return $this->getNrOfComments();
532  default:
533  return strip_tags($this->getStandardFieldHTML($field_id));
534  }
535  }
536 
537  private function loadRecordFields(): void
538  {
539  if ($this->recordfields == null) {
540  $this->loadTable();
541  $recordfields = [];
542  foreach ($this->table->getRecordFields() as $field) {
543  if (($recordfields[$field->getId()] ?? null) === null) {
544  $recordfields[$field->getId()] = ilDclCache::getRecordFieldCache($this, $field);
545  }
546  }
547 
548  $this->recordfields = $recordfields;
549  }
550  }
551 
552  private function loadTable(): void
553  {
554  if ($this->table === null) {
555  $this->table = ilDclCache::getTableCache($this->getTableId());
556  }
557  }
558 
559  public function getRecordField(int $field_id): ilDclBaseRecordFieldModel
560  {
561  $this->loadRecordFields();
562 
563  return $this->recordfields[$field_id];
564  }
565 
566  public function doDelete(bool $omit_notification = false): void
567  {
568  $this->loadRecordFields();
569  foreach ($this->recordfields as $recordfield) {
570  if ($recordfield->getField()->getDatatypeId() == ilDclDatatype::INPUTFORMAT_MOB) {
571  $this->deleteMob((int) $recordfield->getValue());
572  }
573 
574  $recordfield->delete();
575  }
576 
577  $query = "DELETE FROM il_dcl_record WHERE id = " . $this->db->quote($this->getId(), "integer");
578  $this->db->manipulate($query);
579 
580  $this->table->loadRecords();
581 
582  if (!$omit_notification) {
583  $ref_id = $this->http->wrapper()->query()->retrieve('ref_id', $this->refinery->kindlyTo()->int());
584  $objDataCollection = new ilObjDataCollection($ref_id);
585  $objDataCollection->sendRecordNotification(ilDataCollectionMailNotification::TYPE_RECORD_DELETE, $this);
586 
587  $this->event->raise(
588  'components/ILIAS/DataCollection',
589  'deleteRecord',
590  [
591  'dcl' => ilDclCache::getTableCache($this->getTableId())->getCollectionObject(),
592  'table_id' => $this->table_id,
593  'record_id' => $this->getId(),
594  'record' => $this,
595  ]
596  );
597  }
598  }
599 
600  // TODO: Find better way to copy data (including all references)
601  public function cloneStructure(int $original_id, array $new_fields): void
602  {
603  $original = ilDclCache::getRecordCache($original_id);
604  $this->setCreateDate($original->getCreateDate());
605  $this->setLastEditBy($original->getLastEditBy());
606  $this->setLastUpdate($original->getLastUpdate());
607  $this->setOwner($original->getOwner());
608  $this->doCreate();
609  foreach ($new_fields as $old => $new) {
610  $old_rec_field = $original->getRecordField($old);
611  $new_rec_field = ilDclCache::getRecordFieldCache($this, $new);
612  $new_rec_field->cloneStructure($old_rec_field);
613  $this->recordfields[] = $new_rec_field;
614  }
615 
616  // mandatory for all cloning functions
617  ilDclCache::setCloneOf($original_id, $this->getId(), ilDclCache::TYPE_RECORD);
618  }
619 
620  public function deleteFile(int $obj_id): void
621  {
622  if (ilObject2::_exists($obj_id, false)) {
623  $file = new ilObjFile($obj_id, false);
624  $file->delete();
625  }
626  }
627 
628  public function deleteMob(int $obj_id): void
629  {
630  if (ilObject2::_lookupObjId($obj_id)) {
631  $mob = new ilObjMediaObject($obj_id);
632  $mob->delete();
633  }
634  }
635 
636  public function hasPermissionToEdit(int $ref_id): bool
637  {
638  return $this->getTable()->hasPermissionToEditRecord($ref_id, $this);
639  }
640 
641  public function hasPermissionToDelete(int $ref_id): bool
642  {
643  return $this->getTable()->hasPermissionToDeleteRecord($ref_id, $this);
644  }
645 
646  public function hasPermissionToView(int $ref_id): bool
647  {
648  return $this->getTable()->hasPermissionToViewRecord($ref_id, $this);
649  }
650 
654  public function getRecordFields(): array
655  {
656  $this->loadRecordFields();
657 
658  return $this->recordfields;
659  }
660 
661  public function getTable(): ilDclTable
662  {
663  $this->loadTable();
664 
665  return $this->table;
666  }
667 
671  public function getNrOfComments(): int
672  {
673  if ($this->nr_of_comments === null) {
674  $context = $this->notes
675  ->data()
676  ->context(
677  $this->table->getCollectionObject()->getId(),
678  $this->getId(),
679  'dcl'
680  );
681  $this->nr_of_comments = $this->notes
682  ->domain()
683  ->getNrOfCommentsForContext($context);
684  }
685 
686  return $this->nr_of_comments;
687  }
688 }
getRecordFieldHTML($field_id, array $options=[])
Global event handler.
$context
Definition: webdav.php:31
const IL_CAL_DATETIME
getItemByPostVar(string $a_post_var)
setStandardFieldFromForm($field_id, ilPropertyFormGUI $form)
doUpdate(bool $omit_notification=false)
getRecordFieldValue(?string $field_id)
Get Field Value.
renderer()
setStandardFieldValueFromExcel(ilExcel $excel, int $row, int $col, ilDclBaseFieldModel $field)
static _lookupName(int $a_user_id)
lookup user name
getStandardFieldHTML(string $field_id, array $options=[])
setRecordFieldValue($field_id, $value)
static _lookupObjId(int $ref_id)
setRecordFieldValueFromForm(int $field_id, ilPropertyFormGUI $form)
Set a field value.
static buildAjaxHash(int $node_type, ?int $node_id, string $obj_type, int $obj_id, ?string $sub_type=null, ?int $sub_id=null, int $news_id=0)
Build ajax hash.
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
static _exists(int $id, bool $reference=false, ?string $type=null)
checks if an object exists in object_data
setCell(int $a_row, int $col, $value, ?string $datatype=null, bool $disable_strip_tags_for_strings=false)
Set cell value.
$ref_id
Definition: ltiauth.php:65
static http()
Fetches the global http state from ILIAS.
static getNamePresentation( $a_user_id, bool $a_user_image=false, bool $a_profile_link=false, string $a_profile_back_link='', bool $a_force_first_lastname=false, bool $a_omit_login=false, bool $a_sortable=true, bool $a_return_data_array=false, $a_ctrl_path='ilpublicuserprofilegui')
Default behaviour is:
ILIAS Refinery Factory $refinery
getNrOfComments()
Get nr of comments of this record.
getStandardFieldPlainText(string $field_id)
Class ilObjFile.
global $DIC
Definition: shib_login.php:26
static getTableCache(?int $table_id=null)
static setCloneOf(int $old, int $new, string $type)
getRecordFieldValueFromExcel(ilExcel $excel, int $row, int $col, ilDclBaseFieldModel $field)
static _isStandardField($field_id)
static getListCommentsJSCall(string $a_hash, ?string $a_update_code=null)
Get list comments js call.
fillRecordFieldExcelExport(ilExcel $worksheet, int &$row, int &$col, $field_id)
static getRecordCache(?int $record_id)
setCreateDate(ilDateTime $a_datetime)
static formatDate(ilDateTime $date, bool $a_skip_day=false, bool $a_include_wd=false, bool $include_seconds=false, ?ilObjUser $user=null,)
setLastUpdate(ilDateTime $a_datetime)
static getRecordFieldCache(object $record, object $field)
cloneStructure(int $original_id, array $new_fields)
doDelete(bool $omit_notification=false)
fillRecordFieldFormInput($field_id, ilPropertyFormGUI $form)
static _tableExists(int $table_id)
fillStandardFieldFormInput($field_id, ilPropertyFormGUI $form)
static _lookupLogin(int $a_user_id)