ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
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';
8require_once("./Services/Link/classes/class.ilLink.php");
9require_once("./Modules/DataCollection/classes/class.ilDataCollectionImporter.php");
10
23
27 protected $dcl_obj_id;
28
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 $value = $this->getValue();
44 if ($value) {
45 if ($this->getField()->getProperty(ilDclBaseFieldModel::PROP_N_REFERENCE)) {
46 if (!is_array($value)) {
47 $value = array($value);
48 }
49 foreach ($value as $val) {
50 if ($val) {
51 $ref_rec = ilDclCache::getRecordCache($val);
52 $ref_record_field = $ref_rec->getRecordField($this->getField()->getProperty(ilDclBaseFieldModel::PROP_REFERENCE));
53 if($ref_record_field) {
54 $exp_value = $ref_record_field->getExportValue();
55 $names[] = is_array($exp_value) ? array_shift($exp_value) : $exp_value;
56 }
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 global $DIC;
79 $lng = $DIC['lng'];
80 $value = parent::getValueFromExcel($excel, $row, $col);
81 $old = $value;
82 if ($this->getField()->hasProperty(ilDclBaseFieldModel::PROP_N_REFERENCE)) {
84 $has_value = count($value);
85 } else {
87 $has_value = $value;
88 }
89
90 if (!$has_value && $old) {
91 $warning = "(" . $row . ", " . ilDataCollectionImporter::getExcelCharForInteger($col+1) . ") " . $lng->txt("dcl_no_such_reference") . " "
92 . $old;
93 return array('warning' => $warning);
94 }
95
96 return $value;
97 }
98
106 protected function getReferencesFromString($stringValues) {
107 $slicedStrings = explode(", ", $stringValues);
108 $slicedReferences = array();
109 $resolved = 0;
110 for($i = 0; $i < count($slicedStrings); $i++) {
111 //try to find a reference since the last resolved value separated by a comma.
112 // $i = 1; $resolved = 0; $string = "hello, world, gaga" -> try to match "hello, world".
113 $searchString = implode(array_slice($slicedStrings, $resolved, $i - $resolved + 1));
114 if($ref = $this->getReferenceFromValue($searchString)){
115 $slicedReferences[] = $ref;
116 $resolved = $i;
117 continue;
118 }
119
120 //try to find a reference with the current index.
121 // $i = 1; $resolved = 0; $string = "hello, world, gaga" -> try to match "world".
122 $searchString = $slicedStrings[$i];
123 if($ref = $this->getReferenceFromValue($searchString)){
124 $slicedReferences[] = $ref;
125 $resolved = $i;
126 continue;
127 }
128 }
129 return $slicedReferences;
130 }
131
138 public function getReferenceFromValue($value) {
140 $table = ilDclCache::getTableCache($field->getTableId());
141 $record_id = 0;
142 foreach ($table->getRecords() as $record) {
143 $record_value = $record->getRecordField($field->getId())->getValue();
144 // in case of a url-field
145 if (is_array($record_value) && !is_array($value)) {
146 $record_value = array_shift($record_value);
147 }
148 if ($record_value == $value) {
149 $record_id = $record->getId();
150 }
151 }
152
153 return $record_id;
154 }
155
156
157 public function afterClone() {
158 $field_clone = ilDclCache::getCloneOf($this->getField()->getId(), ilDclCache::TYPE_FIELD);
160
161 if ($field_clone && $record_clone) {
162 $record_field_clone = ilDclCache::getRecordFieldCache($record_clone, $field_clone);
163 $clone_reference = $record_field_clone->getValue();
164 $reference_record = ilDclCache::getCloneOf($clone_reference, ilDclCache::TYPE_RECORD);
165 if ($reference_record) {
166 $this->setValue($reference_record->getId()); // reference fields store the id of the reference's record as their value
167 $this->doUpdate();
168 }
169 }
170 }
171}
172
173?>
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.
$old
global $DIC