ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
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 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 {
104
105 $valid = true;
106 if (!$this->getMulti()) {
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 return $tpl->get();
216 }
217
223 public function insert($a_tpl)
224 {
225 $a_tpl->setCurrentBlock("prop_generic");
226 $a_tpl->setVariable("PROP_GENERIC", $this->render());
227 $a_tpl->parseCurrentBlock();
228 }
229
233 public function getTableFilterHTML()
234 {
235 $html = $this->render();
236 return $html;
237 }
238
242 public function getToolbarHTML()
243 {
244 $html = $this->render("toolbar");
245 return $html;
246 }
247
255 public function setHideSubForm($a_value, $a_condition = null)
256 {
257 $this->hide_sub = (bool) $a_value;
258
259 if ($a_condition) {
260 $this->addCustomAttribute('onchange="if(this.value ' . $a_condition . ')' .
261 ' { il.Form.showSubForm(\'subform_' . $this->getFieldId() . '\', \'il_prop_cont_' . $this->getFieldId() . '\'); }' .
262 ' else { il.Form.hideSubForm(\'subform_' . $this->getFieldId() . '\'); };"');
263 }
264 }
265
266 public function hideSubForm()
267 {
268 return (bool) $this->hide_sub;
269 }
270}
$tpl
Definition: ilias.php:10
$_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 $DIC
Definition: saml.php:7