ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilDclReferenceRecordFieldModel.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22{
23 protected ?int $dcl_obj_id;
24
30 {
32 $dclTable = ilDclCache::getTableCache($this->getField()->getTableId());
33 $this->dcl_obj_id = $dclTable->getObjId();
34 }
35
36 public function getExportValue(): string
37 {
38 $value = $this->getValue();
39 if ($value) {
40 if ($this->getField()->getProperty(ilDclBaseFieldModel::PROP_N_REFERENCE)) {
41 $names = [];
42 if (!is_array($value)) {
43 $value = [$value];
44 }
45 foreach ($value as $val) {
46 if ($val) {
47 $ref_rec = ilDclCache::getRecordCache((int) $val);
48 $ref_record_field = $ref_rec->getRecordField((int) $this->getField()->getProperty(ilDclBaseFieldModel::PROP_REFERENCE));
49 $exp_value = $ref_record_field->getExportValue();
50 $names[] = is_array($exp_value) ? array_shift($exp_value) : $exp_value;
51 }
52 }
53
54 return implode('; ', $names);
55 }
56
57 $ref_rec = ilDclCache::getRecordCache((int) $this->getValue());
58 $ref_record_field = $ref_rec->getRecordField((int) $this->getField()->getProperty(ilDclBaseFieldModel::PROP_REFERENCE));
59 $exp_value = $ref_record_field->getExportValue();
60
61 return (string) (is_array($exp_value) ? array_shift($exp_value) : $exp_value);
62 }
63 return "";
64 }
65
66 public function getValueFromExcel(ilExcel $excel, int $row, int $col)
67 {
68 $value = parent::getValueFromExcel($excel, $row, $col);
69 $old = $value;
70 if ($this->getField()->hasProperty(ilDclBaseFieldModel::PROP_N_REFERENCE)) {
72 $has_value = count($value);
73 } else {
75 $has_value = $value;
76 }
77
78 if (!$has_value && $old) {
79 $warning = "(" . $row . ", " . ilDataCollectionImporter::getExcelCharForInteger($col + 1) . ") " . $this->lng->txt("dcl_no_such_reference") . " "
80 . $old;
81
82 return ['warning' => $warning];
83 }
84
85 return $value;
86 }
87
94 protected function getReferencesFromString(string $stringValues): array
95 {
96 $delimiter = strpos($stringValues, '; ') ? '; ' : ', ';
97 $slicedStrings = explode($delimiter, $stringValues);
98 $slicedReferences = [];
99 $resolved = 0;
100 for ($i = 0; $i < count($slicedStrings); $i++) {
101 //try to find a reference since the last resolved value separated by a comma.
102 // $i = 1; $resolved = 0; $string = "hello, world, gaga" -> try to match "hello, world".
103 $searchString = implode(array_slice($slicedStrings, $resolved, $i - $resolved + 1));
104 if ($ref = $this->getReferenceFromValue($searchString)) {
105 $slicedReferences[] = $ref;
106 $resolved = $i;
107 continue;
108 }
109
110 //try to find a reference with the current index.
111 // $i = 1; $resolved = 0; $string = "hello, world, gaga" -> try to match "world".
112 $searchString = $slicedStrings[$i];
113 if ($ref = $this->getReferenceFromValue($searchString)) {
114 $slicedReferences[] = $ref;
115 $resolved = $i;
116 }
117 }
118
119 return $slicedReferences;
120 }
121
123 {
126 $record_id = 0;
127 foreach ($table->getRecords() as $record) {
128 $record_value = $record->getRecordField((int) $field->getId())->getExportValue();
129 // in case of a url-field
130 if (is_array($record_value) && !is_array($value)) {
131 $record_value = array_shift($record_value);
132 }
133 if ($record_value == $value) {
134 $record_id = $record->getId();
135 }
136 }
137
138 return $record_id;
139 }
140
141 public function afterClone(): void
142 {
143 $field_clone = ilDclCache::getCloneOf((int) $this->getField()->getId(), ilDclCache::TYPE_FIELD);
145
146 if ($field_clone && $record_clone) {
147 $record_field_clone = ilDclCache::getRecordFieldCache($record_clone, $field_clone);
148 $clone_references = $record_field_clone->getValue();
149 $value = [];
150 if (is_array($clone_references)) {
151
152 foreach ($clone_references as $clone_reference) {
153 if (!is_null($temp_value = $this->getCloneRecordId($clone_reference))) {
154 $value[] = $temp_value;
155 }
156 }
157 } elseif (!is_null($temp_value = $this->getCloneRecordId($clone_references))) {
158 $value = $temp_value;
159 }
160
161 $this->setValue($value, true); // reference fields store the id of the reference's record as their value
162 $this->doUpdate();
163 }
164 }
165
166 protected function getCloneRecordId(?string $clone_reference): ?string
167 {
168 $reference_record = ilDclCache::getCloneOf((int) $clone_reference, ilDclCache::TYPE_RECORD);
169 if ($reference_record) {
170 return (string) $reference_record->getId();
171 }
172 return null;
173 }
174}
setValue($value, bool $omit_parsing=false)
Set value for record field.
doUpdate()
Update object in database.
static getRecordCache(?int $record_id)
static getRecordFieldCache(object $record, object $field)
static getTableCache(?int $table_id=null)
static getFieldCache(int $field_id=0)
static getCloneOf(int $id, string $type)
__construct(ilDclBaseRecordModel $record, ilDclBaseFieldModel $field)
getReferencesFromString(string $stringValues)
This method tries to get as many valid references out of a string separated by commata.
getValueFromExcel(ilExcel $excel, int $row, int $col)
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc