ILIAS  release_8 Revision v8.24
class.ilDclReferenceRecordFieldModel.php
Go to the documentation of this file.
1<?php
2
20{
21 protected ?int $dcl_obj_id;
22
28 {
30 $dclTable = ilDclCache::getTableCache($this->getField()->getTableId());
31 $this->dcl_obj_id = $dclTable->getObjId();
32 }
33
37 public function getExportValue()
38 {
39 $value = $this->getValue();
40 if ($value) {
41 if ($this->getField()->getProperty(ilDclBaseFieldModel::PROP_N_REFERENCE)) {
42 if (!is_array($value)) {
43 $value = [$value];
44 }
45 foreach ($value as $val) {
46 if ($val) {
47 $ref_rec = ilDclCache::getRecordCache($val);
48 $ref_record_field = $ref_rec->getRecordField($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 } else {
56 $ref_rec = ilDclCache::getRecordCache($this->getValue());
57 $ref_record_field = $ref_rec->getRecordField($this->getField()->getProperty(ilDclBaseFieldModel::PROP_REFERENCE));
58 $exp_value = $ref_record_field->getExportValue();
59
60 return (is_array($exp_value) ? array_shift($exp_value) : $exp_value);
61 }
62 } else {
63 return "";
64 }
65 }
66
67 public function getValueFromExcel(ilExcel $excel, int $row, int $col)
68 {
69 $value = parent::getValueFromExcel($excel, $row, $col);
70 $old = $value;
71 if ($this->getField()->hasProperty(ilDclBaseFieldModel::PROP_N_REFERENCE)) {
73 $has_value = count($value);
74 } else {
76 $has_value = $value;
77 }
78
79 if (!$has_value && $old) {
80 $warning = "(" . $row . ", " . ilDataCollectionImporter::getExcelCharForInteger($col + 1) . ") " . $this->lng->txt("dcl_no_such_reference") . " "
81 . $old;
82
83 return ['warning' => $warning];
84 }
85
86 return $value;
87 }
88
95 protected function getReferencesFromString(string $stringValues): array
96 {
97 $delimiter = strpos($stringValues, '; ') ? '; ' : ', ';
98 $slicedStrings = explode($delimiter, $stringValues);
99 $slicedReferences = [];
100 $resolved = 0;
101 for ($i = 0; $i < count($slicedStrings); $i++) {
102 //try to find a reference since the last resolved value separated by a comma.
103 // $i = 1; $resolved = 0; $string = "hello, world, gaga" -> try to match "hello, world".
104 $searchString = implode(array_slice($slicedStrings, $resolved, $i - $resolved + 1));
105 if ($ref = $this->getReferenceFromValue($searchString)) {
106 $slicedReferences[] = $ref;
107 $resolved = $i;
108 continue;
109 }
110
111 //try to find a reference with the current index.
112 // $i = 1; $resolved = 0; $string = "hello, world, gaga" -> try to match "world".
113 $searchString = $slicedStrings[$i];
114 if ($ref = $this->getReferenceFromValue($searchString)) {
115 $slicedReferences[] = $ref;
116 $resolved = $i;
117 }
118 }
119
120 return $slicedReferences;
121 }
122
124 {
127 $record_id = 0;
128 foreach ($table->getRecords() as $record) {
129 $record_value = $record->getRecordField($field->getId())->getExportValue();
130 // in case of a url-field
131 if (is_array($record_value) && !is_array($value)) {
132 $record_value = array_shift($record_value);
133 }
134 if ($record_value == $value) {
135 $record_id = $record->getId();
136 }
137 }
138
139 return $record_id;
140 }
141
142 public function afterClone(): void
143 {
144 $field_clone = ilDclCache::getCloneOf($this->getField()->getId(), ilDclCache::TYPE_FIELD);
146
147 if ($field_clone && $record_clone) {
148 $record_field_clone = ilDclCache::getRecordFieldCache($record_clone, $field_clone);
149 $clone_references = $record_field_clone->getValue();
150
151 $value = [];
152 if (is_array($clone_references)) {
153 foreach ($clone_references as $clone_reference) {
154 if (!is_null($temp_value = $this->getCloneRecordId($clone_reference))) {
155 $value[] = $temp_value;
156 }
157 }
158 } elseif (!is_null($temp_value = $this->getCloneRecordId($clone_references))) {
159 $value = $temp_value;
160 }
161
162 $this->setValue($value, true); // reference fields store the id of the reference's record as their value
163 $this->doUpdate();
164 }
165 }
166
167 protected function getCloneRecordId(?string $clone_reference): ?string
168 {
169 $reference_record = ilDclCache::getCloneOf((int) $clone_reference, ilDclCache::TYPE_RECORD);
170 if ($reference_record) {
171 return (string) $reference_record->getId();
172 }
173 return null;
174 }
175}
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
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 getFieldCache(int $field_id=0)
static getTableCache(int $table_id=null)
static getCloneOf(int $id, string $type)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
__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)
$i
Definition: metadata.php:41
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc