ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
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
5require_once './Modules/DataCollection/classes/Fields/Base/class.ilDclBaseRecordFieldModel.php';
6require_once './Modules/DataCollection/classes/Fields/Base/class.ilDclBaseRecordModel.php';
7require_once './Modules/DataCollection/classes/Fields/Base/class.ilDclBaseFieldModel.php';
8
21{
22
26 protected $dcl_obj_id;
27
28
34 {
35 parent::__construct($record, $field);
36 $dclTable = ilDclCache::getTableCache($this->getField()->getTableId());
37 $this->dcl_obj_id = $dclTable->getObjId();
38 }
39
40
44 public function getExportValue()
45 {
46 $value = $this->getValue();
47 if ($value) {
48 if ($this->getField()->getProperty(ilDclBaseFieldModel::PROP_N_REFERENCE)) {
49 if (!is_array($value)) {
50 $value = array($value);
51 }
52 foreach ($value as $val) {
53 if ($val) {
54 $ref_rec = ilDclCache::getRecordCache($val);
55 $ref_record_field = $ref_rec->getRecordField($this->getField()->getProperty(ilDclBaseFieldModel::PROP_REFERENCE));
56 if ($ref_record_field) {
57 $exp_value = $ref_record_field->getExportValue();
58 $names[] = is_array($exp_value) ? array_shift($exp_value) : $exp_value;
59 }
60 }
61 }
62
63 return implode('; ', $names);
64 } else {
65 $ref_rec = ilDclCache::getRecordCache($this->getValue());
66 $ref_record_field = $ref_rec->getRecordField($this->getField()->getProperty(ilDclBaseFieldModel::PROP_REFERENCE));
67
68 $exp_value = "";
69 if ($ref_record_field) {
70 $exp_value = $ref_record_field->getExportValue();
71 }
72
73 return (is_array($exp_value) ? array_shift($exp_value) : $exp_value);
74 }
75 } else {
76 return "";
77 }
78 }
79
80
81 public function getValueFromExcel($excel, $row, $col)
82 {
83 global $DIC;
84 $lng = $DIC['lng'];
85 $value = parent::getValueFromExcel($excel, $row, $col);
86 $old = $value;
87 if ($this->getField()->hasProperty(ilDclBaseFieldModel::PROP_N_REFERENCE)) {
89 $has_value = count($value);
90 } else {
92 $has_value = $value;
93 }
94
95 if (!$has_value && $old) {
96 $warning = "(" . $row . ", " . ilDataCollectionImporter::getExcelCharForInteger($col + 1) . ") " . $lng->txt("dcl_no_such_reference") . " "
97 . $old;
98
99 return array('warning' => $warning);
100 }
101
102 return $value;
103 }
104
105
115 protected function getReferencesFromString($stringValues)
116 {
117 $delimiter = strpos($stringValues, '; ') ? '; ' : ', ';
118 $slicedStrings = explode($delimiter, $stringValues);
119 $slicedReferences = array();
120 $resolved = 0;
121 for ($i = 0; $i < count($slicedStrings); $i++) {
122 //try to find a reference since the last resolved value separated by a comma.
123 // $i = 1; $resolved = 0; $string = "hello, world, gaga" -> try to match "hello, world".
124 $searchString = implode(array_slice($slicedStrings, $resolved, $i - $resolved + 1));
125 if ($ref = $this->getReferenceFromValue($searchString)) {
126 $slicedReferences[] = $ref;
127 $resolved = $i;
128 continue;
129 }
130
131 //try to find a reference with the current index.
132 // $i = 1; $resolved = 0; $string = "hello, world, gaga" -> try to match "world".
133 $searchString = $slicedStrings[$i];
134 if ($ref = $this->getReferenceFromValue($searchString)) {
135 $slicedReferences[] = $ref;
136 $resolved = $i;
137 continue;
138 }
139 }
140
141 return $slicedReferences;
142 }
143
144
152 {
154 $table = ilDclCache::getTableCache($field->getTableId());
155 $record_id = 0;
156 foreach ($table->getRecords() as $record) {
157 $record_value = $record->getRecordField($field->getId())->getExportValue();
158 // in case of a url-field
159 if (is_array($record_value) && !is_array($value)) {
160 $record_value = array_shift($record_value);
161 }
162 if ($record_value == $value) {
163 $record_id = $record->getId();
164 }
165 }
166
167 return $record_id;
168 }
169
170
171 public function afterClone()
172 {
173 $field_clone = ilDclCache::getCloneOf($this->getField()->getId(), ilDclCache::TYPE_FIELD);
175
176 if ($field_clone && $record_clone) {
177 $record_field_clone = ilDclCache::getRecordFieldCache($record_clone, $field_clone);
178 $clone_reference = $record_field_clone->getValue();
179 $reference_record = ilDclCache::getCloneOf($clone_reference, ilDclCache::TYPE_RECORD);
180 if ($reference_record) {
181 $this->setValue($reference_record->getId()); // reference fields store the id of the reference's record as their value
182 $this->doUpdate();
183 }
184 }
185 }
186}
$warning
Definition: X509warning.php:13
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: disco.tpl.php:19
$row
if(empty($password)) $table
Definition: pwgen.php:24
global $DIC
Definition: saml.php:7
$delimiter
Definition: showstats.php:16