ILIAS  Release_4_4_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
10 
11  private $max_reference_length = 20;
12  /*
13  * doUpdate
14  */
15  public function doUpdate()
16  {
17  global $ilDB;
18 
19  $values = $this->getValue();
20  if(!is_array($values))
21  $values = array($values);
22  $datatype = $this->field->getDatatype();
23 
24  $query = "DELETE FROM il_dcl_stloc".$datatype->getStorageLocation()."_value WHERE record_field_id = ".$ilDB->quote($this->id, "integer");
25  $ilDB->manipulate($query);
26 
27  if(!count($values)|| $values[0] == 0)
28  return;
29 
30  $query = "INSERT INTO il_dcl_stloc".$datatype->getStorageLocation()."_value (value, record_field_id, id) VALUES";
31  foreach($values as $value){
32  $next_id = $ilDB->nextId("il_dcl_stloc".$datatype->getStorageLocation()."_value");
33  $query .= " (".$ilDB->quote($value, $datatype->getDbType()).", ".$ilDB->quote($this->getId(), "integer").", ".$ilDB->quote($next_id, "integer")."),";
34  }
35  $query = substr($query, 0, -1);
36  $ilDB->manipulate($query);
37  }
38 
39  /*
40  * loadValue
41  */
42  protected function loadValue()
43  {
44  if($this->value === NULL)
45  {
46  global $ilDB;
47  $datatype = $this->field->getDatatype();
48  $query = "SELECT * FROM il_dcl_stloc".$datatype->getStorageLocation()."_value WHERE record_field_id = ".$ilDB->quote($this->id, "integer");
49  $set = $ilDB->query($query);
50  while($rec = $ilDB->fetchAssoc($set))
51  $this->value[] = $rec['value'];
52  }
53  }
54 
59  public function getSingleHTML($options = NULL){
60 
61  // if we are in a record view and the n-ref should be displayed as a link to it's reference
62  $values = $this->getValue();
63  $record_field = $this;
64 
65  if(!$values || !count($values)){
66  return "";
67  }
68 
69  $tpl = $this->buildTemplate($record_field, $values, $options);
70 
71  return $tpl->get();
72  }
73 
74  public function buildTemplate($record_field, $values, $options)
75  {
76  $tpl = new ilTemplate("tpl.reference_list.html", true, true, "Modules/DataCollection");
77  $tpl->setCurrentBlock("reference_list");
78  foreach ($values as $value) {
79  $ref_record = ilDataCollectionCache::getRecordCache($value);
80  if (!$ref_record->getTableId() || !$record_field->getField() || !$record_field->getField()->getTableId()) {
81  //the referenced record_field does not seem to exist.
82  $record_field->setValue(0);
83  $record_field->doUpdate();
84  } else {
85  $tpl->setCurrentBlock("reference");
86  if (!$options)
87  $tpl->setVariable("CONTENT", $ref_record->getRecordFieldHTML($this->getField()->getFieldRef()));
88  else
89  $tpl->setVariable("CONTENT", $this->getLinkHTML($options['link']['name'], $value));
90  $tpl->parseCurrentBlock();
91  }
92  }
93  $tpl->parseCurrentBlock();
94  return $tpl;
95  }
96 
97  protected function getLinkHTML($link, $value){
98  if($link == "[".$this->getField()->getTitle()."]"){
99  $link = null;
100  }
101  return parent::getLinkHTML($link, $value);
102  }
103 
104  /*
105  * getHTML
106  *
107  * @param array $options
108  * @return array
109  */
110  public function getHTML(array $options = array()){
111  global $ilCtrl;
112 
113  $values = $this->getValue();
114  $record_field = $this;
115 
116  if(!$values || !count($values)){
117  return "";
118  }
119 
120  $html = "";
121  $tpl = new ilTemplate("tpl.reference_hover.html",true, true, "Modules/DataCollection");
122  $tpl->setCurrentBlock("reference_list");
123  foreach($values as $value){
124  $ref_record = ilDataCollectionCache::getRecordCache($value);
125  if(!$ref_record->getTableId() || !$record_field->getField() || !$record_field->getField()->getTableId()){
126  //the referenced record_field does not seem to exist.
127  $record_field->setValue(NULL);
128  $record_field->doUpdate();
129  }else{
130  if((strlen($html) < $this->max_reference_length))
131  $html .= $ref_record->getRecordFieldHTML($this->getField()->getFieldRef()).", ";
132  else
133  $cut = true;
134  $tpl->setCurrentBlock("reference");
135  $tpl->setVariable("CONTENT", $ref_record->getRecordFieldHTML($this->getField()->getFieldRef()));
136  $tpl->parseCurrentBlock();
137  }
138  }
139  $html = substr($html, 0, -2);
140  if($cut){
141  $html .= "...";
142  }
143  $tpl->setVariable("RECORD_ID", $this->getRecord()->getId());
144  $tpl->setVariable("ALL", $html);
145  $tpl->parseCurrentBlock();
146 
147  return $tpl->get();
148  }
149 
150  /*
151  * getExportValue
152  */
153  public function getExportValue()
154  {
155  $values = $this->getValue();
156  $names = array();
157  foreach($values as $value){
158  if($value){
159  $ref_rec = ilDataCollectionCache::getRecordCache($value);
160  $names[] = $ref_rec->getRecordField($this->getField()->getFieldRef())->getValue();
161  }
162  }
163  $string = "";
164  foreach($names as $name){
165  $string.=$name.", ";
166  }
167  if(!count($names))
168  return "";
169  $string = substr($string, 0, -2);
170  return $string;
171  }
172 }
173 
174 ?>