ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
class.ilDclReferenceRecordFieldModel.php
Go to the documentation of this file.
1<?php
2
3/* Copyright (c) 1998-2009 ILIAS open source, Extended GPL, see docs/LICENSE */
4
17{
18
22 protected $dcl_obj_id;
23
24
30 {
32 $dclTable = ilDclCache::getTableCache($this->getField()->getTableId());
33 $this->dcl_obj_id = $dclTable->getObjId();
34 }
35
36
40 public function getExportValue()
41 {
42 $value = $this->getValue();
43 if ($value) {
44 if ($this->getField()->getProperty(ilDclBaseFieldModel::PROP_N_REFERENCE)) {
45 if (!is_array($value)) {
46 $value = array($value);
47 }
48 foreach ($value as $val) {
49 if ($val) {
50 $ref_rec = ilDclCache::getRecordCache($val);
51 $ref_record_field = $ref_rec->getRecordField($this->getField()->getProperty(ilDclBaseFieldModel::PROP_REFERENCE));
52 if ($ref_record_field) {
53 $exp_value = $ref_record_field->getExportValue();
54 $names[] = is_array($exp_value) ? array_shift($exp_value) : $exp_value;
55 }
56 }
57 }
58
59 return implode('; ', $names);
60 } else {
61 $ref_rec = ilDclCache::getRecordCache($this->getValue());
62 $ref_record_field = $ref_rec->getRecordField($this->getField()->getProperty(ilDclBaseFieldModel::PROP_REFERENCE));
63
64 $exp_value = "";
65 if ($ref_record_field) {
66 $exp_value = $ref_record_field->getExportValue();
67 }
68
69 return (is_array($exp_value) ? array_shift($exp_value) : $exp_value);
70 }
71 } else {
72 return "";
73 }
74 }
75
76
77 public function getValueFromExcel($excel, $row, $col)
78 {
79 global $DIC;
80 $lng = $DIC['lng'];
81 $value = parent::getValueFromExcel($excel, $row, $col);
82 $old = $value;
83 if ($this->getField()->hasProperty(ilDclBaseFieldModel::PROP_N_REFERENCE)) {
85 $has_value = count($value);
86 } else {
88 $has_value = $value;
89 }
90
91 if (!$has_value && $old) {
92 $warning = "(" . $row . ", " . ilDataCollectionImporter::getExcelCharForInteger($col + 1) . ") " . $lng->txt("dcl_no_such_reference") . " "
93 . $old;
94
95 return array('warning' => $warning);
96 }
97
98 return $value;
99 }
100
101
111 protected function getReferencesFromString($stringValues)
112 {
113 $delimiter = strpos($stringValues, '; ') ? '; ' : ', ';
114 $slicedStrings = explode($delimiter, $stringValues);
115 $slicedReferences = array();
116 $resolved = 0;
117 for ($i = 0; $i < count($slicedStrings); $i++) {
118 //try to find a reference since the last resolved value separated by a comma.
119 // $i = 1; $resolved = 0; $string = "hello, world, gaga" -> try to match "hello, world".
120 $searchString = implode(array_slice($slicedStrings, $resolved, $i - $resolved + 1));
121 if ($ref = $this->getReferenceFromValue($searchString)) {
122 $slicedReferences[] = $ref;
123 $resolved = $i;
124 continue;
125 }
126
127 //try to find a reference with the current index.
128 // $i = 1; $resolved = 0; $string = "hello, world, gaga" -> try to match "world".
129 $searchString = $slicedStrings[$i];
130 if ($ref = $this->getReferenceFromValue($searchString)) {
131 $slicedReferences[] = $ref;
132 $resolved = $i;
133 continue;
134 }
135 }
136
137 return $slicedReferences;
138 }
139
140
148 {
150 $table = ilDclCache::getTableCache($field->getTableId());
151 $record_id = 0;
152 foreach ($table->getRecords() as $record) {
153 $record_value = $record->getRecordField($field->getId())->getExportValue();
154 // in case of a url-field
155 if (is_array($record_value) && !is_array($value)) {
156 $record_value = array_shift($record_value);
157 }
158 if ($record_value == $value) {
159 $record_id = $record->getId();
160 }
161 }
162
163 return $record_id;
164 }
165
166
167 public function afterClone()
168 {
169 $field_clone = ilDclCache::getCloneOf($this->getField()->getId(), ilDclCache::TYPE_FIELD);
171
172 if ($field_clone && $record_clone) {
173 $record_field_clone = ilDclCache::getRecordFieldCache($record_clone, $field_clone);
174 $clone_reference = $record_field_clone->getValue();
175 $reference_record = ilDclCache::getCloneOf($clone_reference, ilDclCache::TYPE_RECORD);
176 if ($reference_record) {
177 $this->setValue($reference_record->getId()); // reference fields store the id of the reference's record as their value
178 $this->doUpdate();
179 }
180 }
181 }
182}
An exception for terminatinating execution or to throw for unit testing.
Class ilDclBaseFieldModel.
setValue($value, $omit_parsing=false)
Set value for record field.
Class ilDclBaseRecordModel.
static getRecordFieldCache($record, $field)
static getTableCache($table_id=0)
static getRecordCache($record_id=0)
static getCloneOf($id, $type)
static getFieldCache($field_id=0)
__construct(ilDclBaseRecordModel $record, ilDclBaseFieldModel $field)
getReferencesFromString($stringValues)
This method tries to get as many valid references out of a string separated by commata.
$i
Definition: metadata.php:24
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc
$DIC
Definition: xapitoken.php:46