ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
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
30 public function getInputField(ilPropertyFormGUI $form, ?int $record_id = null): ilFormPropertyGUI
31 {
32 if ($this->getField()->getProperty(ilDclBaseFieldModel::PROP_N_REFERENCE)) {
33 $input = new ilMultiSelectInputGUI($this->getField()->getTitle(), 'field_' . $this->getField()->getId());
34 $input->setWidth(100);
35 $input->setWidthUnit('%');
36 } else {
37 $input = new ilSelectInputGUI($this->getField()->getTitle(), 'field_' . $this->getField()->getId());
38 }
39
40 $this->setupInputField($input, $this->getField());
41
42 $options = [];
43 if (!$this->getField()->getProperty(ilDclBaseFieldModel::PROP_N_REFERENCE)) {
44 $options[''] = $this->lng->txt('dcl_please_select');
45 }
46
47 $fieldref = (int) $this->getField()->getProperty(ilDclBaseFieldModel::PROP_REFERENCE);
48 $reffield = ilDclCache::getFieldCache($fieldref);
49 $reftable = ilDclCache::getTableCache($reffield->getTableId());
50 foreach ($reftable->getRecords() as $record) {
51 $record_field = $record->getRecordField($fieldref);
52 switch ($reffield->getDatatypeId()) {
54 if ($record_field->getValue()) {
55 $file_obj = new ilObjFile($record_field->getValue(), false);
56 $options[$record->getId()] = $file_obj->getFileName();
57 }
58 break;
60 $media_obj = new ilObjMediaObject($record_field->getValue());
61 $options[$record->getId()] = $media_obj->getTitle();
62 break;
64 $options[$record->getId()] = strtotime($record->getRecordFieldSingleHTML($fieldref));
65 $options2[$record->getId()] = $record->getRecordFieldSingleHTML($fieldref);
66 break;
68 $value = $record_field->getValue();
69 if ($record->getRecordField((int) $fieldref)->getField()->hasProperty(ilDclBaseFieldModel::PROP_URL)) {
70 if (!is_array($value)) {
71 $value = ['title' => '', 'link' => $value];
72 }
73 $value = $value['title'] ?: $value['link'];
74 }
75 $options[$record->getId()] = $value;
76 break;
78 $value = $record_field->getValue();
79 $options[$record->getId()] = ilObject::_lookupTitle(ilObject::_lookupObjectId($value)) . ' [' . $value . ']';
80 break;
81 default:
82 $options[$record->getId()] = $record_field->getExportValue();
83 break;
84 }
85 }
86 asort($options, SORT_NATURAL | SORT_FLAG_CASE);
87
88 // TT #0019091: restore the actual values after sorting with timestamp
89 if ($reffield->getDatatypeId() === ilDclDatatype::INPUTFORMAT_DATE) {
90 foreach ($options as $key => $opt) {
91 if ($key != "" && isset($options2) && is_array($options2)) {
92 $options[$key] = $options2[$key];
93 }
94 }
95 // the option 'please select' messes with the order, therefore we reset it
96 unset($options[""]);
97 $options = ["" => $this->lng->txt('dcl_please_select')] + $options;
98 }
99
100 $input->setOptions($options);
101 if ($input instanceof ilMultiSelectInputGUI) {
102 $input->setHeight(32 * min(5, max(1, count($options))));
103 }
104
105
106 $ref_id = $this->http->wrapper()->query()->retrieve('ref_id', $this->refinery->kindlyTo()->int());
107
109 $input->addCustomAttribute('data-ref="1"');
110 $input->addCustomAttribute('data-ref-table-id="' . $reftable->getId() . '"');
111 $input->addCustomAttribute('data-ref-field-id="' . $reffield->getId() . '"');
112 }
113
114 return $input;
115 }
116
121 {
122 $input = $table->addFilterItemByMetaType(
123 "filter_" . $this->getField()->getId(),
125 false,
126 $this->getField()->getId()
127 );
128 $ref_field_id = (int) $this->getField()->getProperty(ilDclBaseFieldModel::PROP_REFERENCE);
129 $ref_field = ilDclCache::getFieldCache($ref_field_id);
130 $ref_table = ilDclCache::getTableCache($ref_field->getTableId());
131 $options = [];
132 foreach ($ref_table->getRecords() as $record) {
133 $options[$record->getId()] = $record->getRecordField($ref_field_id)->getPlainText();
134 }
135 // Sort by values ASC
136 asort($options);
137 $options = ['' => $this->lng->txt('dcl_all_entries')]
138 + $options
139 + ['none' => $this->lng->txt('dcl_no_entry')];
140 $input->setOptions($options);
141
142 $this->setupFilterInputField($input);
143
144 return $this->getFilterInputFieldValue($input);
145 }
146
150 public function passThroughFilter(ilDclBaseRecordModel $record, $filter): bool
151 {
152 $value = $record->getRecordFieldValue($this->getField()->getId());
153
154 $pass = false;
155 if ($filter && $this->getField()->getProperty(ilDclBaseFieldModel::PROP_N_REFERENCE) && is_array($value) && in_array(
156 $filter,
157 $value
158 )) {
159 $pass = true;
160 }
161 if (!$filter || $filter == $value) {
162 $pass = true;
163 }
164
165 return $pass;
166 }
167
168 protected function buildFieldCreationInput(ilObjDataCollection $dcl, string $mode = 'create'): ilRadioOption
169 {
170 $opt = parent::buildFieldCreationInput($dcl, $mode);
171
172 $options = [];
173 // Get Tables
174 $tables = $dcl->getTables();
175 foreach ($tables as $table) {
176 foreach ($table->getRecordFields() as $field) {
177 //referencing references may lead to endless loops.
179 $options[$field->getId()] = $table->getTitle() . self::REFERENCE_SEPARATOR . $field->getTitle();
180 }
181 }
182 }
183 $prop_table_selection = new ilSelectInputGUI(
184 $this->lng->txt('dcl_reference_title'),
186 );
187 $prop_table_selection->setOptions($options);
188 $prop_table_selection->setInfo($this->lng->txt('dcl_reference_title_desc'));
189
190 $opt->addSubItem($prop_table_selection);
191
192 $prop_ref_link = new ilDclCheckboxInputGUI(
193 $this->lng->txt('dcl_reference_link'),
195 );
196 $prop_ref_link->setInfo($this->lng->txt('dcl_reference_link_info'));
197 $opt->addSubItem($prop_ref_link);
198
199 $prop_multi_select = new ilDclCheckboxInputGUI(
200 $this->lng->txt('dcl_multiple_selection'),
202 );
203 $opt->addSubItem($prop_multi_select);
204
205 return $opt;
206 }
207}
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)
buildFieldCreationInput(ilObjDataCollection $dcl, string $mode='create')
Build the creation-input-field.
This class represents a property in a property form.
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.