ILIAS  Release_5_0_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilDataCollectionNReferenceField.php
Go to the documentation of this file.
1 <?php
2 
10 
14  protected $max_reference_length = 20;
15 
16 
17  public function doUpdate() {
18  global $ilDB;
19 
20  $values = $this->getValue();
21  if (!is_array($values)) {
22  $values = array( $values );
23  }
24  $datatype = $this->field->getDatatype();
25 
26  $query = "DELETE FROM il_dcl_stloc" . $datatype->getStorageLocation() . "_value WHERE record_field_id = "
27  . $ilDB->quote($this->id, "integer");
28  $ilDB->manipulate($query);
29 
30  if (!count($values) || $values[0] == 0) {
31  return;
32  }
33 
34  $query = "INSERT INTO il_dcl_stloc" . $datatype->getStorageLocation() . "_value (value, record_field_id, id) VALUES";
35  foreach ($values as $value) {
36  $next_id = $ilDB->nextId("il_dcl_stloc" . $datatype->getStorageLocation() . "_value");
37  $query .= " (" . $ilDB->quote($value, $datatype->getDbType()) . ", " . $ilDB->quote($this->getId(), "integer") . ", "
38  . $ilDB->quote($next_id, "integer") . "),";
39  }
40  $query = substr($query, 0, - 1);
41  $ilDB->manipulate($query);
42  }
43 
44 
45  protected function loadValue() {
46  if ($this->value === NULL) {
47  global $ilDB;
48  $datatype = $this->field->getDatatype();
49  $query = "SELECT * FROM il_dcl_stloc" . $datatype->getStorageLocation() . "_value WHERE record_field_id = "
50  . $ilDB->quote($this->id, "integer");
51  $set = $ilDB->query($query);
52  $this->value = array();
53  while ($rec = $ilDB->fetchAssoc($set)) {
54  $this->value[] = $rec['value'];
55  }
56  }
57  }
58 
59 
65  public function getSingleHTML($options = NULL) {
66 
67  // if we are in a record view and the n-ref should be displayed as a link to it's reference
68  $values = $this->getValue();
69  $record_field = $this;
70 
71  if (!$values || !count($values)) {
72  return "";
73  }
74 
75  $tpl = $this->buildTemplate($record_field, $values, $options);
76 
77  return $tpl->get();
78  }
79 
80 
88  public function buildTemplate($record_field, $values, $options) {
89  $tpl = new ilTemplate("tpl.reference_list.html", true, true, "Modules/DataCollection");
90  $tpl->setCurrentBlock("reference_list");
91  foreach ($values as $value) {
92  $ref_record = ilDataCollectionCache::getRecordCache($value);
93  if (!$ref_record->getTableId() || !$record_field->getField() || !$record_field->getField()->getTableId()) {
94  //the referenced record_field does not seem to exist.
95  $record_field->setValue(0);
96  $record_field->doUpdate();
97  } else {
98  $tpl->setCurrentBlock("reference");
99  if (!$options) {
100  $tpl->setVariable("CONTENT", $ref_record->getRecordFieldHTML($this->getField()->getFieldRef()));
101  } else {
102  $tpl->setVariable("CONTENT", $this->getLinkHTML($options['link']['name'], $value));
103  }
104  $tpl->parseCurrentBlock();
105  }
106  }
107  $tpl->parseCurrentBlock();
108 
109  return $tpl;
110  }
111 
112 
119  protected function getLinkHTML($link, $value) {
120  if ($link == "[" . $this->getField()->getTitle() . "]") {
121  $link = NULL;
122  }
123 
124  return parent::getLinkHTML($link, $value);
125  }
126 
127 
131  public function getHTML() {
132  $values = $this->getValue();
133  $record_field = $this;
134 
135  if (!$values OR !count($values)) {
136  return "";
137  }
138 
139  $html = "";
140  $cut = false;
141  $tpl = new ilTemplate("tpl.reference_hover.html", true, true, "Modules/DataCollection");
142  $tpl->setCurrentBlock("reference_list");
143  $elements = array();
144  foreach ($values as $value) {
145  $ref_record = ilDataCollectionCache::getRecordCache($value);
146  if (!$ref_record->getTableId() OR !$record_field->getField() OR !$record_field->getField()->getTableId()) {
147  //the referenced record_field does not seem to exist.
148  $record_field->setValue(NULL);
149  $record_field->doUpdate();
150  } else {
151  $elements[] = array('value' => $ref_record->getRecordFieldHTML($this->getField()->getFieldRef()),
152  'sort' => $ref_record->getRecordFieldSortingValue($this->getField()->getFieldRef()));
153  }
154  }
155 
156  //sort fetched elements
157  $is_numeric = false;
158  $ref_field = new ilDataCollectionField($record_field->getField()->getFieldRef());
159  switch ($ref_field->getDatatypeId()) {
162  $is_numeric = true;
163  break;
164  }
165  $elements = ilUtil::sortArray($elements, 'sort', 'asc', $is_numeric);
166 
167  //concat
168  foreach($elements as $element) {
169  if ((strlen($html) < $this->max_reference_length)) {
170  $html .= $element['value'] . ", ";
171  } else {
172  $cut = true;
173  }
174  $tpl->setCurrentBlock("reference");
175  $tpl->setVariable("CONTENT", $element['value']);
176  $tpl->parseCurrentBlock();
177  }
178 
179  $html = substr($html, 0, - 2);
180  if ($cut) {
181  $html .= "...";
182  }
183  $tpl->setVariable("RECORD_ID", $this->getRecord()->getId());
184  $tpl->setVariable("ALL", $html);
185  $tpl->parseCurrentBlock();
186 
187  return $tpl->get();
188  }
189 
190 
194  public function getExportValue() {
195  $values = $this->getValue();
196  $names = array();
197  foreach ($values as $value) {
198  if ($value) {
199  $ref_rec = ilDataCollectionCache::getRecordCache($value);
200  $names[] = $ref_rec->getRecordField($this->getField()->getFieldRef())->getValue();
201  }
202  }
203  $string = "";
204  foreach ($names as $name) {
205  $string .= $name . ", ";
206  }
207  if (!count($names)) {
208  return "";
209  }
210  $string = substr($string, 0, - 2);
211 
212  return $string;
213  }
214 }
215 
216 ?>