ILIAS  trunk Revision v12.0_alpha-377-g3641b37b9db
class.ilDclBaseRecordModel.php
Go to the documentation of this file.
1<?php
2
19declare(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;
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(ilDclNotificationType::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();
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();
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,
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): mixed
315 {
316 if ($field_id === null) {
317 return null;
318 }
320 return $this->getStandardField($field_id);
321 } else {
322 return ilDclCache::getRecordFieldCache($this, ilDclCache::getFieldCache((int) $field_id))->getValue();
323 }
324 }
325
329 public function fillRecordFieldExcelExport(ilExcel $worksheet, int &$row, int &$col, $field_id): void
330 {
331 $this->loadRecordFields();
333 if ($field_id === 'owner') {
334 $worksheet->setCell($row, $col, ilObjUser::_lookupLogin($this->getOwner()));
335 $col++;
336 $name_array = ilObjUser::_lookupName($this->getOwner());
337 $worksheet->setCell($row, $col, $name_array['lastname'] . ', ' . $name_array['firstname']);
338 } elseif ($field_id === 'last_update') {
339 $date_time = $this->getLastUpdate()->get(IL_CAL_DATETIME, '', $this->user->getTimeZone());
340 $worksheet->setCell($row, $col, $date_time);
341 } elseif ($field_id === 'create_date') {
342 $date_time = $this->getCreateDate()->get(IL_CAL_DATETIME, '', $this->user->getTimeZone());
343 $worksheet->setCell($row, $col, $date_time);
344 } else {
345 $worksheet->setCell($row, $col, $this->getStandardFieldHTML($field_id));
346 }
347 $col++;
348 } else {
349 $this->recordfields[$field_id]->fillExcelExport($worksheet, $row, $col);
350 }
351 }
352
356 public function getRecordFieldFormulaValue($field_id): string
357 {
358 $this->loadRecordFields();
360 $value = $this->getStandardFieldFormulaValue($field_id);
361 } else {
362 if (is_object($this->recordfields[$field_id])) {
363 $value = $this->recordfields[$field_id]->getFormulaValue();
364 } else {
365 $value = '';
366 }
367 }
368
369 return $value;
370 }
371
375 public function getRecordFieldHTML($field_id, array $options = []): string
376 {
377 $this->loadRecordFields();
379 $html = $this->getStandardFieldHTML($field_id, $options);
380 } else {
381 if (array_key_exists($field_id, $this->recordfields) && is_object($this->recordfields[$field_id])) {
382 $html = $this->recordfields[$field_id]->getRecordRepresentation()->getHTML(true, $options);
383 } else {
384 $html = '';
385 }
386 }
387
388 return $html;
389 }
390
394 public function getRecordFieldSingleHTML($field_id, array $options = []): string
395 {
396 $this->loadRecordFields();
397
399 $html = $this->getStandardFieldHTML($field_id);
400 } else {
401 $field = $this->recordfields[$field_id];
406 $html = $field->getRecordRepresentation()->getSingleHTML($options);
407 }
408
409 return $html;
410 }
411
415 public function fillRecordFieldFormInput($field_id, ilPropertyFormGUI $form): void
416 {
417 $this->loadRecordFields();
419 $this->fillStandardFieldFormInput($field_id, $form);
420 } else {
421 $this->recordfields[$field_id]->getRecordRepresentation()->fillFormInput($form);
422 }
423 }
424
428 protected function setStandardFieldFromForm($field_id, ilPropertyFormGUI $form): void
429 {
430 if ($item = $form->getItemByPostVar("field_" . $field_id)) {
431 $this->setStandardField($field_id, $item->getValue());
432 }
433 }
434
439 protected function setStandardField($field_id, $value)
440 {
441 if ($field_id == "last_edit_by") {
442 $this->setLastEditBy($value);
443 return;
444 }
445 $this->{$field_id} = $value;
446 }
447
451 protected function fillStandardFieldFormInput($field_id, ilPropertyFormGUI $form): void
452 {
453 if ($item = $form->getItemByPostVar('field_' . $field_id)) {
454 $item->setValue($this->getStandardField($field_id));
455 }
456 }
457
461 protected function getStandardField($field_id): string
462 {
463 switch ($field_id) {
464 case "last_edit_by":
465 return ilObjUser::_lookupName($this->getLastEditBy())['login'];
466 case 'owner':
467 return ilObjUser::_lookupName($this->getOwner())['login'];
468 }
469
470 return $this->{$field_id};
471 }
472
476 public function getStandardFieldFormulaValue($field_id): string
477 {
478 return $this->getStandardFieldHTML($field_id);
479 }
480
481 public function getStandardFieldHTML(string $field_id, array $options = []): string
482 {
483 switch ($field_id) {
484 case 'id':
485 return (string) $this->getId();
486 case 'owner':
487 return ilUserUtil::getNamePresentation($this->getOwner());
488 case 'last_edit_by':
489 return ilUserUtil::getNamePresentation($this->getLastEditBy());
490 case 'last_update':
491 return ilDatePresentation::formatDate($this->getLastUpdate());
492 case 'create_date':
493 return ilDatePresentation::formatDate($this->getCreateDate());
494 case 'comments':
495
496 $ref_id = $this->http->wrapper()->query()->retrieve('ref_id', $this->refinery->kindlyTo()->int());
497
499 1,
500 $ref_id,
501 'dcl',
502 $this->table->getCollectionObject()
503 ->getId(),
504 'dcl',
505 $this->getId()
506 );
507 $update_code = "il.UI.counter.getCounterObject($(\".ilc_page_Page\")).incrementStatusCount(1);";
508 $ajax_link = ilNoteGUI::getListCommentsJSCall($ajax_hash, $update_code);
509 $nr_comments = $this->getNrOfComments();
510
511 $comment_glyph = $this->ui_factory->symbol()->glyph()->comment()->withCounter(
512 $this->ui_factory->counter()->status($nr_comments)
513 )->withAdditionalOnLoadCode(function ($id) use ($ajax_link): string {
514 return "document.getElementById('$id').onclick = function (event) { $ajax_link; };";
515 });
516 return $this->renderer->render($comment_glyph);
517 }
518
519 return "";
520 }
521
526 public function getStandardFieldPlainText(string $field_id)
527 {
528 switch ($field_id) {
529 case 'comments':
530 return $this->getNrOfComments();
531 default:
532 return strip_tags($this->getStandardFieldHTML($field_id));
533 }
534 }
535
536 private function loadRecordFields(): void
537 {
538 if ($this->recordfields == null) {
539 $this->loadTable();
540 $recordfields = [];
541 foreach ($this->table->getRecordFields() as $field) {
542 if (($recordfields[$field->getId()] ?? null) === null) {
543 $recordfields[$field->getId()] = ilDclCache::getRecordFieldCache($this, $field);
544 }
545 }
546
547 $this->recordfields = $recordfields;
548 }
549 }
550
551 private function loadTable(): void
552 {
553 if ($this->table === null) {
554 $this->table = ilDclCache::getTableCache($this->getTableId());
555 }
556 }
557
558 public function getRecordField(int $field_id): ilDclBaseRecordFieldModel
559 {
560 $this->loadRecordFields();
561
562 return $this->recordfields[$field_id];
563 }
564
565 public function doDelete(bool $omit_notification = false): void
566 {
567 $this->loadRecordFields();
568 foreach ($this->recordfields as $recordfield) {
569 $recordfield->delete();
570 }
571
572 $query = "DELETE FROM il_dcl_record WHERE id = " . $this->db->quote($this->getId(), "integer");
573 $this->db->manipulate($query);
574
575 $this->table->loadRecords();
576
577 if (!$omit_notification) {
578 $ref_id = $this->http->wrapper()->query()->retrieve('ref_id', $this->refinery->kindlyTo()->int());
579 $objDataCollection = new ilObjDataCollection($ref_id);
580 $objDataCollection->sendRecordNotification(ilDclNotificationType::RECORD_DELETE, $this);
581
582 $this->event->raise(
583 'components/ILIAS/DataCollection',
584 'deleteRecord',
585 [
586 'dcl' => ilDclCache::getTableCache($this->getTableId())->getCollectionObject(),
587 'table_id' => $this->table_id,
588 'record_id' => $this->getId(),
589 'record' => $this,
590 ]
591 );
592 }
593 }
594
595 // TODO: Find better way to copy data (including all references)
596 public function cloneStructure(int $original_id, array $new_fields): void
597 {
598 $original = ilDclCache::getRecordCache($original_id);
599 $this->setCreateDate($original->getCreateDate());
600 $this->setLastEditBy($original->getLastEditBy());
601 $this->setLastUpdate($original->getLastUpdate());
602 $this->setOwner($original->getOwner());
603 $this->doCreate();
604 foreach ($new_fields as $old => $new) {
605 $old_rec_field = $original->getRecordField($old);
606 $new_rec_field = ilDclCache::getRecordFieldCache($this, $new);
607 $new_rec_field->cloneStructure($old_rec_field);
608 $this->recordfields[] = $new_rec_field;
609 }
610
611 // mandatory for all cloning functions
613 }
614
615 public function deleteFile(int $obj_id): void
616 {
617 if (ilObject2::_exists($obj_id, false)) {
618 $file = new ilObjFile($obj_id, false);
619 $file->delete();
620 }
621 }
622
623 public function hasPermissionToEdit(int $ref_id): bool
624 {
625 return $this->getTable()->hasPermissionToEditRecord($ref_id, $this);
626 }
627
628 public function hasPermissionToDelete(int $ref_id): bool
629 {
630 return $this->getTable()->hasPermissionToDeleteRecord($ref_id, $this);
631 }
632
633 public function hasPermissionToView(int $ref_id): bool
634 {
635 return $this->getTable()->hasPermissionToViewRecord($ref_id, $this);
636 }
637
641 public function getRecordFields(): array
642 {
643 $this->loadRecordFields();
644
645 return $this->recordfields;
646 }
647
648 public function getTable(): ilDclTable
649 {
650 $this->loadTable();
651
652 return $this->table;
653 }
654
658 public function getNrOfComments(): int
659 {
660 if ($this->nr_of_comments === null) {
661 $context = $this->notes
662 ->data()
663 ->context(
664 $this->table->getCollectionObject()->getId(),
665 $this->getId(),
666 'dcl'
667 );
668 $this->nr_of_comments = $this->notes
669 ->domain()
670 ->getNrOfCommentsForContext($context);
671 }
672
673 return $this->nr_of_comments;
674 }
675}
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
renderer()
Builds data types.
Definition: Factory.php:36
Class Services.
Definition: Services.php:38
const IL_CAL_DATETIME
Global event handler.
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.
static formatDate(ilDateTime $date, bool $a_skip_day=false, bool $a_include_wd=false, bool $include_seconds=false, ?ilObjUser $user=null,)
@classDescription Date and time handling
setLastUpdate(ilDateTime $a_datetime)
getNrOfComments()
Get nr of comments of this record.
doDelete(bool $omit_notification=false)
fillRecordFieldExcelExport(ilExcel $worksheet, int &$row, int &$col, $field_id)
fillRecordFieldFormInput($field_id, ilPropertyFormGUI $form)
fillStandardFieldFormInput($field_id, ilPropertyFormGUI $form)
getRecordFieldHTML($field_id, array $options=[])
setStandardFieldValueFromExcel(ilExcel $excel, int $row, int $col, ilDclBaseFieldModel $field)
getRecordFieldValueFromExcel(ilExcel $excel, int $row, int $col, ilDclBaseFieldModel $field)
getStandardFieldHTML(string $field_id, array $options=[])
getRecordFieldValue(?string $field_id)
Get Field Value.
setCreateDate(ilDateTime $a_datetime)
ILIAS Refinery Factory $refinery
getStandardFieldPlainText(string $field_id)
setStandardFieldFromForm($field_id, ilPropertyFormGUI $form)
cloneStructure(int $original_id, array $new_fields)
doUpdate(bool $omit_notification=false)
setRecordFieldValueFromForm(int $field_id, ilPropertyFormGUI $form)
Set a field value.
setRecordFieldValue($field_id, $value)
static getRecordCache(?int $record_id)
static getRecordFieldCache(object $record, object $field)
static getTableCache(?int $table_id=null)
static getFieldCache(int $field_id=0)
static setCloneOf(int $old, int $new, string $type)
static _isStandardField($field_id)
static _tableExists(int $table_id)
setCell(int $a_row, int $col, $value, ?string $datatype=null, bool $disable_strip_tags_for_strings=false)
Set cell value.
Base class for ILIAS Exception handling.
static getListCommentsJSCall(string $a_hash, ?string $a_update_code=null)
Get list comments js call.
Class ilObjFile.
User class.
static _lookupName(int $a_user_id)
static _lookupLogin(int $a_user_id)
static _exists(int $id, bool $reference=false, ?string $type=null)
checks if an object exists in object_data
This class represents a property form user interface.
getItemByPostVar(string $a_post_var)
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=null)
Default behaviour is:
Interface ilDBInterface.
$ref_id
Definition: ltiauth.php:66
static http()
Fetches the global http state from ILIAS.
global $DIC
Definition: shib_login.php:26
$context
Definition: webdav.php:31