ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
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
5include_once("./Services/Table/interfaces/interface.ilTableFilterItem.php");
6include_once("./Services/Form/classes/class.ilSubEnabledFormPropertyGUI.php");
7include_once 'Services/UIComponent/Toolbar/interfaces/interface.ilToolbarItem.php';
8include_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 foreach($this->getSubItems() as $item)
90 {
91 $item->setValueByArray($a_values);
92 }
93 }
94
100 function checkInput()
101 {
102 global $lng;
103
104 $valid = true;
105 if(!$this->getMulti())
106 {
108 if($this->getRequired() && trim($_POST[$this->getPostVar()]) == "")
109 {
110 $valid = false;
111 }
112 if(!array_key_exists($_POST[$this->getPostVar()], (array) $this->getOptions()))
113 {
114 $this->setAlert($lng->txt('msg_invalid_post_input'));
115 return false;
116 }
117 }
118 else
119 {
120 foreach($_POST[$this->getPostVar()] as $idx => $value)
121 {
122 $_POST[$this->getPostVar()][$idx] = ilUtil::stripSlashes($value);
123 if(!array_key_exists($value, (array) $this->getOptions()))
124 {
125 $this->setAlert($lng->txt('msg_invalid_post_input'));
126 return false;
127 }
128 }
129 $_POST[$this->getPostVar()] = array_unique($_POST[$this->getPostVar()]);
130
131 if($this->getRequired() && !trim(implode("", $_POST[$this->getPostVar()])))
132 {
133 $valid = false;
134 }
135 }
136 if (!$valid)
137 {
138 $this->setAlert($lng->txt("msg_input_is_required"));
139 return false;
140 }
141 return $this->checkSubItemsInput();
142 }
143
144 public function addCustomAttribute($a_attr)
145 {
146 $this->cust_attr[] = $a_attr;
147 }
148
149 public function getCustomAttributes()
150 {
151 return (array) $this->cust_attr;
152 }
153
157 function render($a_mode = "")
158 {
159 $tpl = new ilTemplate("tpl.prop_select.html", true, true, "Services/Form");
160
161 foreach($this->getCustomAttributes() as $attr)
162 {
163 $tpl->setCurrentBlock('cust_attr');
164 $tpl->setVariable('CUSTOM_ATTR',$attr);
165 $tpl->parseCurrentBlock();
166 }
167
168 // determin value to select. Due to accessibility reasons we
169 // should always select a value (per default the first one)
170 $first = true;
171 foreach($this->getOptions() as $option_value => $option_text)
172 {
173 if ($first)
174 {
175 $sel_value = $option_value;
176 }
177 $first = false;
178 if ((string) $option_value == (string) $this->getValue())
179 {
180 $sel_value = $option_value;
181 }
182 }
183 foreach($this->getOptions() as $option_value => $option_text)
184 {
185 $tpl->setCurrentBlock("prop_select_option");
186 $tpl->setVariable("VAL_SELECT_OPTION", ilUtil::prepareFormOutput($option_value));
187 if((string) $sel_value == (string) $option_value)
188 {
189 $tpl->setVariable("CHK_SEL_OPTION",
190 'selected="selected"');
191 }
192 $tpl->setVariable("TXT_SELECT_OPTION", $option_text);
193 $tpl->parseCurrentBlock();
194 }
195 $tpl->setVariable("ID", $this->getFieldId());
196
197 $postvar = $this->getPostVar();
198 if($this->getMulti() && substr($postvar, -2) != "[]")
199 {
200 $postvar .= "[]";
201 }
202
203 if ($this->getDisabled())
204 {
205 if($this->getMulti())
206 {
207 $value = $this->getMultiValues();
208 $hidden = "";
209 if(is_array($value))
210 {
211 foreach($value as $item)
212 {
213 $hidden .= $this->getHiddenTag($postvar, $item);
214 }
215 }
216 }
217 else
218 {
219 $hidden = $this->getHiddenTag($postvar, $this->getValue());
220 }
221 if($hidden)
222 {
223 $tpl->setVariable("DISABLED", " disabled=\"disabled\"");
224 $tpl->setVariable("HIDDEN_INPUT", $hidden);
225 }
226 }
227 else
228 {
229 $tpl->setVariable("POST_VAR", $postvar);
230 }
231
232 // multi icons
233 if($this->getMulti() && !$a_mode && !$this->getDisabled())
234 {
235 $tpl->touchBlock("inline_in_bl");
236 $tpl->setVariable("MULTI_ICONS", $this->getMultiIconsHTML());
237 }
238
239 return $tpl->get();
240 }
241
247 function insert($a_tpl)
248 {
249 $a_tpl->setCurrentBlock("prop_generic");
250 $a_tpl->setVariable("PROP_GENERIC", $this->render());
251 $a_tpl->parseCurrentBlock();
252 }
253
258 {
259 $html = $this->render();
260 return $html;
261 }
262
266 function getToolbarHTML()
267 {
268 $html = $this->render("toolbar");
269 return $html;
270 }
271
279 function setHideSubForm($a_value, $a_condition = null)
280 {
281 $this->hide_sub = (bool)$a_value;
282
283 if($a_condition)
284 {
285 $this->addCustomAttribute('onchange="if(this.value '.$a_condition.')'.
286 ' { il.Form.showSubForm(\'subform_'.$this->getFieldId().'\', \'il_prop_cont_'.$this->getFieldId().'\'); }'.
287 ' else { il.Form.hideSubForm(\'subform_'.$this->getFieldId().'\'); };"');
288 }
289 }
290
291 function hideSubForm()
292 {
293 return (bool)$this->hide_sub;
294 }
295
296}
global $tpl
Definition: ilias.php:8
$_POST["username"]
An exception for terminatinating execution or to throw for unit testing.
getMultiIconsHTML()
Get HTML for multiple value icons.
setType($a_type)
Set Type.
getPostVar()
Get Post Variable.
getHiddenTag($a_post_var, $a_value)
Get hidden tag (used for disabled properties)
setAlert($a_alert)
Set Alert Text.
getMultiValues()
Get multi values.
setMultiValues(array $a_values)
Set multi values.
getFieldId()
Get Post Variable.
This class represents a selection list property in a property form.
__construct($a_title="", $a_postvar="")
Constructor.
setValue($a_value)
Set Value.
setOptions($a_options)
Set Options.
getTableFilterHTML()
Get HTML for table filter.
checkInput()
Check input, strip slashes etc.
render($a_mode="")
Render item.
setValueByArray($a_values)
Set value by array.
insert($a_tpl)
Insert property html.
setHideSubForm($a_value, $a_condition=null)
Set initial sub form visibility, optionally add dynamic value-based condition.
getToolbarHTML()
Get HTML for toolbar.
This class represents a property that may include a sub form.
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
static prepareFormOutput($a_str, $a_strip=false)
prepares string output for html forms @access public
$valid
$html
Definition: example_001.php:87
Interface for multi values support.
Interface for property form input GUI classes that can be used in table filters.
Interface for property form input GUI classes that can be used in ilToolbarGUI.
global $lng
Definition: privfeed.php:17