ILIAS  Release_4_4_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilSelectInputGUI.php
Go to the documentation of this file.
1 <?php
2 
3 /* Copyright (c) 1998-2010 ILIAS open source, Extended GPL, see docs/LICENSE */
4 
5 include_once("./Services/Table/interfaces/interface.ilTableFilterItem.php");
6 include_once("./Services/Form/classes/class.ilSubEnabledFormPropertyGUI.php");
7 include_once 'Services/UIComponent/Toolbar/interfaces/interface.ilToolbarItem.php';
8 include_once 'Services/Form/interfaces/interface.ilMultiValuesItem.php';
9 
18 {
19  protected $cust_attr = array();
20  protected $options = array();
21  protected $value;
22 
29  function __construct($a_title = "", $a_postvar = "")
30  {
31  parent::__construct($a_title, $a_postvar);
32  $this->setType("select");
33  }
34 
40  function setOptions($a_options)
41  {
42  $this->options = $a_options;
43  }
44 
50  function getOptions()
51  {
52  return $this->options ? $this->options : array();
53  }
54 
60  function setValue($a_value)
61  {
62  if($this->getMulti() && is_array($a_value))
63  {
64  $this->setMultiValues($a_value);
65  $a_value = array_shift($a_value);
66  }
67  $this->value = $a_value;
68  }
69 
75  function getValue()
76  {
77  return $this->value;
78  }
79 
80 
86  function setValueByArray($a_values)
87  {
88  $this->setValue($a_values[$this->getPostVar()]);
89  }
90 
96  function checkInput()
97  {
98  global $lng;
99 
100  $valid = true;
101  if(!$this->getMulti())
102  {
103  $_POST[$this->getPostVar()] = ilUtil::stripSlashes($_POST[$this->getPostVar()]);
104  if($this->getRequired() && trim($_POST[$this->getPostVar()]) == "")
105  {
106  $valid = false;
107  }
108  }
109  else
110  {
111  foreach($_POST[$this->getPostVar()] as $idx => $value)
112  {
113  $_POST[$this->getPostVar()][$idx] = ilUtil::stripSlashes($value);
114  }
115  $_POST[$this->getPostVar()] = array_unique($_POST[$this->getPostVar()]);
116 
117  if($this->getRequired() && !trim(implode("", $_POST[$this->getPostVar()])))
118  {
119  $valid = false;
120  }
121  }
122  if (!$valid)
123  {
124  $this->setAlert($lng->txt("msg_input_is_required"));
125  return false;
126  }
127  return $this->checkSubItemsInput();
128  }
129 
130  public function addCustomAttribute($a_attr)
131  {
132  $this->cust_attr[] = $a_attr;
133  }
134 
135  public function getCustomAttributes()
136  {
137  return (array) $this->cust_attr;
138  }
139 
143  function render($a_mode = "")
144  {
145  $tpl = new ilTemplate("tpl.prop_select.html", true, true, "Services/Form");
146 
147  foreach($this->getCustomAttributes() as $attr)
148  {
149  $tpl->setCurrentBlock('cust_attr');
150  $tpl->setVariable('CUSTOM_ATTR',$attr);
151  $tpl->parseCurrentBlock();
152  }
153 
154  // determin value to select. Due to accessibility reasons we
155  // should always select a value (per default the first one)
156  $first = true;
157  foreach($this->getOptions() as $option_value => $option_text)
158  {
159  if ($first)
160  {
161  $sel_value = $option_value;
162  }
163  $first = false;
164  if ((string) $option_value == (string) $this->getValue())
165  {
166  $sel_value = $option_value;
167  }
168  }
169  foreach($this->getOptions() as $option_value => $option_text)
170  {
171  $tpl->setCurrentBlock("prop_select_option");
172  $tpl->setVariable("VAL_SELECT_OPTION", ilUtil::prepareFormOutput($option_value));
173  if((string) $sel_value == (string) $option_value)
174  {
175  $tpl->setVariable("CHK_SEL_OPTION",
176  'selected="selected"');
177  }
178  $tpl->setVariable("TXT_SELECT_OPTION", $option_text);
179  $tpl->parseCurrentBlock();
180  }
181  $tpl->setVariable("ID", $this->getFieldId());
182 
183  $postvar = $this->getPostVar();
184  if($this->getMulti() && substr($postvar, -2) != "[]")
185  {
186  $postvar .= "[]";
187  }
188 
189  if ($this->getDisabled())
190  {
191  if($this->getMulti())
192  {
193  $value = $this->getMultiValues();
194  $hidden = "";
195  if(is_array($value))
196  {
197  foreach($value as $item)
198  {
199  $hidden .= $this->getHiddenTag($postvar, $item);
200  }
201  }
202  }
203  else
204  {
205  $hidden = $this->getHiddenTag($postvar, $this->getValue());
206  }
207  if($hidden)
208  {
209  $tpl->setVariable("DISABLED", " disabled=\"disabled\"");
210  $tpl->setVariable("HIDDEN_INPUT", $hidden);
211  }
212  }
213  else
214  {
215  $tpl->setVariable("POST_VAR", $postvar);
216  }
217 
218  // multi icons
219  if($this->getMulti() && !$a_mode && !$this->getDisabled())
220  {
221  $tpl->setVariable("MULTI_ICONS", $this->getMultiIconsHTML());
222  }
223 
224  return $tpl->get();
225  }
226 
232  function insert(&$a_tpl)
233  {
234  $a_tpl->setCurrentBlock("prop_generic");
235  $a_tpl->setVariable("PROP_GENERIC", $this->render());
236  $a_tpl->parseCurrentBlock();
237  }
238 
243  {
244  $html = $this->render();
245  return $html;
246  }
247 
251  function getToolbarHTML()
252  {
253  $html = $this->render("toolbar");
254  return $html;
255  }
256 
257 }