ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
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  public function __construct($a_title = "", $a_postvar = "")
30  {
31  global $DIC;
32 
33  $this->lng = $DIC->language();
34  parent::__construct($a_title, $a_postvar);
35  $this->setType("select");
36  }
37 
43  public function setOptions($a_options)
44  {
45  $this->options = $a_options;
46  }
47 
53  public function getOptions()
54  {
55  return $this->options ? $this->options : array();
56  }
57 
63  public function setValue($a_value)
64  {
65  if ($this->getMulti() && is_array($a_value)) {
66  $this->setMultiValues($a_value);
67  $a_value = array_shift($a_value);
68  }
69  $this->value = $a_value;
70  }
71 
77  public function getValue()
78  {
79  return $this->value;
80  }
81 
82 
88  public function setValueByArray($a_values)
89  {
90  $this->setValue($a_values[$this->getPostVar()]);
91  foreach ($this->getSubItems() as $item) {
92  $item->setValueByArray($a_values);
93  }
94  }
95 
101  public function checkInput()
102  {
103  $lng = $this->lng;
104 
105  $valid = true;
106  if (!$this->getMulti()) {
107  $_POST[$this->getPostVar()] = ilUtil::stripSlashes($_POST[$this->getPostVar()]);
108  if ($this->getRequired() && trim($_POST[$this->getPostVar()]) == "") {
109  $valid = false;
110  } elseif (!array_key_exists($_POST[$this->getPostVar()], (array) $this->getOptions())) {
111  $this->setAlert($lng->txt('msg_invalid_post_input'));
112  return false;
113  }
114  } else {
115  foreach ($_POST[$this->getPostVar()] as $idx => $value) {
116  $_POST[$this->getPostVar()][$idx] = ilUtil::stripSlashes($value);
117  if (!array_key_exists($value, (array) $this->getOptions())) {
118  $this->setAlert($lng->txt('msg_invalid_post_input'));
119  return false;
120  }
121  }
122  $_POST[$this->getPostVar()] = array_unique($_POST[$this->getPostVar()]);
123 
124  if ($this->getRequired() && !trim(implode("", $_POST[$this->getPostVar()]))) {
125  $valid = false;
126  }
127  }
128  if (!$valid) {
129  $this->setAlert($lng->txt("msg_input_is_required"));
130  return false;
131  }
132  return $this->checkSubItemsInput();
133  }
134 
135  public function addCustomAttribute($a_attr)
136  {
137  $this->cust_attr[] = $a_attr;
138  }
139 
140  public function getCustomAttributes()
141  {
142  return (array) $this->cust_attr;
143  }
144 
148  public function render($a_mode = "")
149  {
150  $tpl = new ilTemplate("tpl.prop_select.html", true, true, "Services/Form");
151 
152  foreach ($this->getCustomAttributes() as $attr) {
153  $tpl->setCurrentBlock('cust_attr');
154  $tpl->setVariable('CUSTOM_ATTR', $attr);
155  $tpl->parseCurrentBlock();
156  }
157 
158  // determin value to select. Due to accessibility reasons we
159  // should always select a value (per default the first one)
160  $first = true;
161  foreach ($this->getOptions() as $option_value => $option_text) {
162  if ($first) {
163  $sel_value = $option_value;
164  }
165  $first = false;
166  if ((string) $option_value == (string) $this->getValue()) {
167  $sel_value = $option_value;
168  }
169  }
170  foreach ($this->getOptions() as $option_value => $option_text) {
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  $tpl->setVariable(
175  "CHK_SEL_OPTION",
176  'selected="selected"'
177  );
178  }
179  $tpl->setVariable("TXT_SELECT_OPTION", $option_text);
180  $tpl->parseCurrentBlock();
181  }
182  $tpl->setVariable("ID", $this->getFieldId());
183 
184  $postvar = $this->getPostVar();
185  if ($this->getMulti() && substr($postvar, -2) != "[]") {
186  $postvar .= "[]";
187  }
188 
189  if ($this->getDisabled()) {
190  if ($this->getMulti()) {
191  $value = $this->getMultiValues();
192  $hidden = "";
193  if (is_array($value)) {
194  foreach ($value as $item) {
195  $hidden .= $this->getHiddenTag($postvar, $item);
196  }
197  }
198  } else {
199  $hidden = $this->getHiddenTag($postvar, $this->getValue());
200  }
201  if ($hidden) {
202  $tpl->setVariable("DISABLED", " disabled=\"disabled\"");
203  $tpl->setVariable("HIDDEN_INPUT", $hidden);
204  }
205  } else {
206  $tpl->setVariable("POST_VAR", $postvar);
207  }
208 
209  // multi icons
210  if ($this->getMulti() && !$a_mode && !$this->getDisabled()) {
211  $tpl->touchBlock("inline_in_bl");
212  $tpl->setVariable("MULTI_ICONS", $this->getMultiIconsHTML());
213  }
214 
215  $tpl->setVariable("ARIA_LABEL", ilUtil::prepareFormOutput($this->getTitle()));
216 
217  return $tpl->get();
218  }
219 
225  public function insert($a_tpl)
226  {
227  $a_tpl->setCurrentBlock("prop_generic");
228  $a_tpl->setVariable("PROP_GENERIC", $this->render());
229  $a_tpl->parseCurrentBlock();
230  }
231 
235  public function getTableFilterHTML()
236  {
237  $html = $this->render();
238  return $html;
239  }
240 
244  public function getToolbarHTML()
245  {
246  $html = $this->render("toolbar");
247  return $html;
248  }
249 
257  public function setHideSubForm($a_value, $a_condition = null)
258  {
259  $this->hide_sub = (bool) $a_value;
260 
261  if ($a_condition) {
262  $this->addCustomAttribute('onchange="if(this.value ' . $a_condition . ')' .
263  ' { il.Form.showSubForm(\'subform_' . $this->getFieldId() . '\', \'il_prop_cont_' . $this->getFieldId() . '\'); }' .
264  ' else { il.Form.hideSubForm(\'subform_' . $this->getFieldId() . '\'); };"');
265  }
266  }
267 
268  public function hideSubForm()
269  {
270  return (bool) $this->hide_sub;
271  }
272 }
static prepareFormOutput($a_str, $a_strip=false)
prepares string output for html forms public
getToolbarHTML()
Get HTML for toolbar.
getHiddenTag($a_post_var, $a_value)
Get hidden tag (used for disabled properties)
Interface for property form input GUI classes that can be used in table filters.
This class represents a selection list property in a property form.
setHideSubForm($a_value, $a_condition=null)
Set initial sub form visibility, optionally add dynamic value-based condition.
global $DIC
Definition: saml.php:7
$tpl
Definition: ilias.php:10
getPostVar()
Get Post Variable.
$valid
Interface for property form input GUI classes that can be used in ilToolbarGUI.
render($a_mode="")
Render item.
getMultiIconsHTML()
Get HTML for multiple value icons.
setMultiValues(array $a_values)
Set multi values.
__construct($a_title="", $a_postvar="")
Constructor.
setAlert($a_alert)
Set Alert Text.
setType($a_type)
Set Type.
getMultiValues()
Get multi values.
checkInput()
Check input, strip slashes etc.
setValue($a_value)
Set Value.
getFieldId()
Get Post Variable.
special template class to simplify handling of ITX/PEAR
static stripSlashes($a_str, $a_strip_html=true, $a_allow="")
strip slashes if magic qoutes is enabled
setOptions($a_options)
Set Options.
Interface for multi values support.
insert($a_tpl)
Insert property html.
This class represents a property that may include a sub form.
setValueByArray($a_values)
Set value by array.
$_POST["username"]
$html
Definition: example_001.php:87
getTableFilterHTML()
Get HTML for table filter.