ILIAS  trunk Revision v12.0_alpha-1227-g7ff6d300864
class.ilDclReferenceFieldRepresentation.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22{
23 public const REFERENCE_SEPARATOR = " -> ";
24
25 public function getInputField(ilPropertyFormGUI $form, ?int $record_id = null): ilSelectInputGUI|ilMultiSelectInputGUI
26 {
27 if ($this->getField()->getProperty(ilDclBaseFieldModel::PROP_N_REFERENCE)) {
28 $input = new ilMultiSelectInputGUI($this->getField()->getTitle(), 'field_' . $this->getField()->getId());
29 $input->setWidth(100);
30 $input->setWidthUnit('%');
31 } else {
32 $input = new ilSelectInputGUI($this->getField()->getTitle(), 'field_' . $this->getField()->getId());
33 }
34
35 $this->setupInputField($input, $this->getField());
36
37 $options = $this->getSortedRecords();
38 if (!$this->getField()->getProperty(ilDclBaseFieldModel::PROP_N_REFERENCE)) {
39 $options = ['' => $this->lng->txt('dcl_please_select')] + $options;
40 }
41
42 $input->setOptions($options);
43 if ($input instanceof ilMultiSelectInputGUI) {
44 $input->setHeight(32 * min(5, max(1, count($options))));
45 }
46
47 $ref_id = $this->http->wrapper()->query()->retrieve('ref_id', $this->refinery->kindlyTo()->int());
48
49 $fieldref = (int) $this->getField()->getProperty(ilDclBaseFieldModel::PROP_REFERENCE);
50 $reffield = ilDclCache::getFieldCache($fieldref);
52 $input->addCustomAttribute('data-ref="1"');
53 $input->addCustomAttribute('data-ref-table-id="' . $reffield->getTableId() . '"');
54 $input->addCustomAttribute('data-ref-field-id="' . $reffield->getId() . '"');
55 }
56
57 return $input;
58 }
59
60 public function addFilterInputFieldToTable(ilTable2GUI $table): array|string|null
61 {
62 $input = $table->addFilterItemByMetaType(
63 "filter_" . $this->getField()->getId(),
65 false,
66 $this->getField()->getId()
67 );
68 $options = ['' => $this->lng->txt('dcl_all_entries')]
69 + $this->getSortedRecords()
70 + ['none' => $this->lng->txt('dcl_no_entry')];
71 $input->setOptions($options);
72
73 $this->setupFilterInputField($input);
74
75 return $this->getFilterInputFieldValue($input);
76 }
77
78 protected function getSortedRecords(): array
79 {
80 $options = [];
81 $fieldref = (int) $this->getField()->getProperty(ilDclBaseFieldModel::PROP_REFERENCE);
82 $reffield = ilDclCache::getFieldCache($fieldref);
83 $reftable = ilDclCache::getTableCache($reffield->getTableId());
84 foreach ($reftable->getRecords() as $record) {
85 $record_field = $record->getRecordField($fieldref);
86 switch ($reffield->getDatatypeId()) {
88 if ($record_field->getValue()) {
89 $file_obj = new ilObjFile($record_field->getValue(), false);
90 $options[$record->getId()] = $file_obj->getFileName();
91 }
92 break;
94 $media_obj = new ilObjMediaObject($record_field->getValue());
95 $options[$record->getId()] = $media_obj->getTitle();
96 break;
98 $options[$record->getId()] = strtotime($record->getRecordField($fieldref)->getPlainText());
99 $options2[$record->getId()] = $record->getRecordField($fieldref)->getPlainText();
100 break;
102 $value = $record_field->getValue();
103 if ($record->getRecordField((int) $fieldref)->getField()->hasProperty(ilDclBaseFieldModel::PROP_URL)) {
104 if (!is_array($value)) {
105 $value = ['title' => '', 'link' => $value];
106 }
107 $value = $value['title'] ?: $value['link'];
108 }
109 $options[$record->getId()] = $value;
110 break;
112 $value = $record_field->getValue();
113 $options[$record->getId()] = ilObject::_lookupTitle(ilObject::_lookupObjectId($value)) . ' [' . $value . ']';
114 break;
115 default:
116 $options[$record->getId()] = $record_field->getExportValue();
117 break;
118 }
119 }
120 asort($options, SORT_NATURAL | SORT_FLAG_CASE);
121
122 if ($reffield->getDatatypeId() === ilDclDatatype::INPUTFORMAT_DATE) {
123 foreach ($options as $key => $opt) {
124 if ($key != "" && isset($options2) && is_array($options2)) {
125 $options[$key] = $options2[$key];
126 }
127 }
128 }
129
130 return $options;
131 }
132
136 public function passThroughFilter(ilDclBaseRecordModel $record, $filter): bool
137 {
138 $value = $record->getRecordFieldValue($this->getField()->getId());
139
140 $pass = false;
141 if ($filter && $this->getField()->getProperty(ilDclBaseFieldModel::PROP_N_REFERENCE) && is_array($value) && in_array(
142 $filter,
143 $value
144 )) {
145 $pass = true;
146 }
147 if (!$filter || $filter == $value) {
148 $pass = true;
149 }
150
151 return $pass;
152 }
153
154 protected function buildFieldCreationInput(ilObjDataCollection $dcl, string $mode = 'create'): ilRadioOption
155 {
156 $opt = parent::buildFieldCreationInput($dcl, $mode);
157
158 $options = [];
159 // Get Tables
160 $tables = $dcl->getTables();
161 foreach ($tables as $table) {
162 foreach ($table->getRecordFields() as $field) {
163 //referencing references may lead to endless loops.
165 $options[$field->getId()] = $table->getTitle() . self::REFERENCE_SEPARATOR . $field->getTitle();
166 }
167 }
168 }
169 $prop_table_selection = new ilSelectInputGUI(
170 $this->lng->txt('dcl_reference_title'),
172 );
173 $prop_table_selection->setOptions($options);
174 $prop_table_selection->setInfo($this->lng->txt('dcl_reference_title_desc'));
175
176 $opt->addSubItem($prop_table_selection);
177
178 $prop_ref_link = new ilDclCheckboxInputGUI(
179 $this->lng->txt('dcl_reference_link'),
181 );
182 $prop_ref_link->setInfo($this->lng->txt('dcl_reference_link_info'));
183 $opt->addSubItem($prop_ref_link);
184
185 $prop_multi_select = new ilDclCheckboxInputGUI(
186 $this->lng->txt('dcl_multiple_selection'),
188 );
189 $opt->addSubItem($prop_multi_select);
190
191 return $opt;
192 }
193}
setupInputField(ilFormPropertyGUI $input, ilDclBaseFieldModel $field)
Sets basic settings on field-input.
setupFilterInputField(?ilTableFilterItem $input)
Set basic settings for filter-input-gui.
getRecordFieldValue(?string $field_id)
Get Field Value.
static getTableCache(?int $table_id=null)
static getFieldCache(int $field_id=0)
passThroughFilter(ilDclBaseRecordModel $record, $filter)
getInputField(ilPropertyFormGUI $form, ?int $record_id=null)
Returns field-input.
addFilterInputFieldToTable(ilTable2GUI $table)
Add filter input to TableGUI.
buildFieldCreationInput(ilObjDataCollection $dcl, string $mode='create')
Build the creation-input-field.
This class represents a multi selection list property in a property form.
static hasPermissionToAddRecord(int $ref_id, int $table_id)
Class ilObjFile.
static _lookupObjectId(int $ref_id)
static _lookupTitle(int $obj_id)
This class represents a property form user interface.
This class represents an option in a radio group.
This class represents a selection list property in a property form.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
addFilterItemByMetaType(string $id, int $type=self::FILTER_TEXT, bool $a_optional=false, string $caption="")
Add filter by standard type.
$ref_id
Definition: ltiauth.php:66
static http()
Fetches the global http state from ILIAS.