ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
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
33 {
34 parent::__construct($record, $field);
35 $dclTable = ilDclCache::getTableCache($this->getField()->getTableId());
36 $this->dcl_obj_id = $dclTable->getObjId();
37 }
38
42 public function getExportValue()
43 {
44 $value = $this->getValue();
45 if ($value) {
46 if ($this->getField()->getProperty(ilDclBaseFieldModel::PROP_N_REFERENCE)) {
47 if (!is_array($value)) {
48 $value = array($value);
49 }
50 foreach ($value as $val) {
51 if ($val) {
52 $ref_rec = ilDclCache::getRecordCache($val);
53 $ref_record_field = $ref_rec->getRecordField($this->getField()->getProperty(ilDclBaseFieldModel::PROP_REFERENCE));
54 if ($ref_record_field) {
55 $exp_value = $ref_record_field->getExportValue();
56 $names[] = is_array($exp_value) ? array_shift($exp_value) : $exp_value;
57 }
58 }
59 }
60 return implode('; ', $names);
61 } else {
62 $ref_rec = ilDclCache::getRecordCache($this->getValue());
63 $ref_record_field = $ref_rec->getRecordField($this->getField()->getProperty(ilDclBaseFieldModel::PROP_REFERENCE));
64
65 $exp_value = "";
66 if ($ref_record_field) {
67 $exp_value = $ref_record_field->getExportValue();
68 }
69
70 return (is_array($exp_value) ? array_shift($exp_value) : $exp_value);
71 }
72 } else {
73 return "";
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 return array('warning' => $warning);
95 }
96
97 return $value;
98 }
99
107 protected function getReferencesFromString($stringValues)
108 {
109 $delimiter = strpos($stringValues, '; ') ? '; ' : ', ';
110 $slicedStrings = explode($delimiter, $stringValues);
111 $slicedReferences = array();
112 $resolved = 0;
113 for ($i = 0; $i < count($slicedStrings); $i++) {
114 //try to find a reference since the last resolved value separated by a comma.
115 // $i = 1; $resolved = 0; $string = "hello, world, gaga" -> try to match "hello, world".
116 $searchString = implode(array_slice($slicedStrings, $resolved, $i - $resolved + 1));
117 if ($ref = $this->getReferenceFromValue($searchString)) {
118 $slicedReferences[] = $ref;
119 $resolved = $i;
120 continue;
121 }
122
123 //try to find a reference with the current index.
124 // $i = 1; $resolved = 0; $string = "hello, world, gaga" -> try to match "world".
125 $searchString = $slicedStrings[$i];
126 if ($ref = $this->getReferenceFromValue($searchString)) {
127 $slicedReferences[] = $ref;
128 $resolved = $i;
129 continue;
130 }
131 }
132 return $slicedReferences;
133 }
134
142 {
144 $table = ilDclCache::getTableCache($field->getTableId());
145 $record_id = 0;
146 foreach ($table->getRecords() as $record) {
147 $record_value = $record->getRecordField($field->getId())->getExportValue();
148 // in case of a url-field
149 if (is_array($record_value) && !is_array($value)) {
150 $record_value = array_shift($record_value);
151 }
152 if ($record_value == $value) {
153 $record_id = $record->getId();
154 }
155 }
156
157 return $record_id;
158 }
159
160
161 public function afterClone()
162 {
163 $field_clone = ilDclCache::getCloneOf($this->getField()->getId(), ilDclCache::TYPE_FIELD);
165
166 if ($field_clone && $record_clone) {
167 $record_field_clone = ilDclCache::getRecordFieldCache($record_clone, $field_clone);
168 $clone_reference = $record_field_clone->getValue();
169 $reference_record = ilDclCache::getCloneOf($clone_reference, ilDclCache::TYPE_RECORD);
170 if ($reference_record) {
171 $this->setValue($reference_record->getId()); // reference fields store the id of the reference's record as their value
172 $this->doUpdate();
173 }
174 }
175 }
176}
$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
if(empty($password)) $table
Definition: pwgen.php:24
$old
global $DIC
Definition: saml.php:7
$delimiter
Definition: showstats.php:16