ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
class.ilMultiSelectInputGUI.php
Go to the documentation of this file.
1 <?php
2 /*
3  +-----------------------------------------------------------------------------+
4  | ILIAS open source |
5  +-----------------------------------------------------------------------------+
6  | Copyright (c) 1998-2007 ILIAS open source, University of Cologne |
7  | |
8  | This program is free software; you can redistribute it and/or |
9  | modify it under the terms of the GNU General Public License |
10  | as published by the Free Software Foundation; either version 2 |
11  | of the License, or (at your option) any later version. |
12  | |
13  | This program is distributed in the hope that it will be useful, |
14  | but WITHOUT ANY WARRANTY; without even the implied warranty of |
15  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16  | GNU General Public License for more details. |
17  | |
18  | You should have received a copy of the GNU General Public License |
19  | along with this program; if not, write to the Free Software |
20  | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
21  +-----------------------------------------------------------------------------+
22 */
23 
24 include_once("./Services/Table/interfaces/interface.ilTableFilterItem.php");
25 include_once("./Services/Form/classes/class.ilFormPropertyGUI.php");
26 
35 {
36  protected $options;
37  protected $value;
38  protected $select_all; // [bool]
39  protected $selected_first; // [bool]
40 
47  private $width = 160;
48 
55  private $height = 100;
56 
60  protected $widthUnit = 'px';
61 
65  protected $heightUnit = 'px';
66 
70  protected $custom_attributes = array();
71 
78  public function __construct($a_title = "", $a_postvar = "")
79  {
80  global $DIC;
81 
82  $this->lng = $DIC->language();
83  parent::__construct($a_title, $a_postvar);
84  $this->setType("multi_select");
85  $this->setValue(array());
86  }
87 
94  public function setWidth($a_width)
95  {
96  $this->width = (int) $a_width;
97  }
98 
105  public function getWidth()
106  {
107  return $this->width;
108  }
109 
116  public function setHeight($a_height)
117  {
118  $this->height = (int) $a_height;
119  }
120 
127  public function getHeight()
128  {
129  return $this->height;
130  }
131 
137  public function setOptions($a_options)
138  {
139  $this->options = $a_options;
140  }
141 
147  public function getOptions()
148  {
149  return $this->options;
150  }
151 
157  public function setValue($a_array)
158  {
159  $this->value = $a_array;
160  }
161 
167  public function getValue()
168  {
169  return is_array($this->value) ? $this->value : array();
170  }
171 
177  public function setValueByArray($a_values)
178  {
179  $this->setValue($a_values[$this->getPostVar()]);
180  }
181 
182 
183  public function enableSelectAll($a_value)
184  {
185  $this->select_all = (bool) $a_value;
186  }
187 
188  public function enableSelectedFirst($a_value)
189  {
190  $this->selected_first = (bool) $a_value;
191  }
192 
193 
199  public function checkInput()
200  {
201  $lng = $this->lng;
202 
203  if (is_array($_POST[$this->getPostVar()])) {
204  foreach ($_POST[$this->getPostVar()] as $k => $v) {
205  $_POST[$this->getPostVar()][$k] =
207  }
208  } else {
209  $_POST[$this->getPostVar()] = array();
210  }
211  if ($this->getRequired() && count($_POST[$this->getPostVar()]) == 0) {
212  $this->setAlert($lng->txt("msg_input_is_required"));
213 
214  return false;
215  }
216  if (count($_POST[$this->getPostVar()]) > 0) {
217  $options = array_map(function ($k) {
218  return (string) $k;
219  }, array_keys($this->getOptions()));
220  foreach ($_POST[$this->getPostVar()] as $key => $val) {
221  if ($key != 0 || $val != "") {
222  if (!in_array((string) $val, $options)) {
223  $this->setAlert($lng->txt("msg_unknown_value"));
224  return false;
225  }
226  }
227  }
228  }
229 
230  return true;
231  }
232 
236  public function render()
237  {
238  $lng = $this->lng;
239 
240  $tpl = new ilTemplate("tpl.prop_multi_select.html", true, true, "Services/Form");
241  $values = $this->getValue();
242 
243  $options = $this->getOptions();
244  if ($options) {
245  if ($this->select_all) {
246  // enable select all toggle
247  $tpl->setCurrentBlock("item");
248  $tpl->setVariable("VAL", "");
249  $tpl->setVariable("ID_VAL", ilUtil::prepareFormOutput("all__toggle"));
250  $tpl->setVariable("IID", $this->getFieldId());
251  $tpl->setVariable("TXT_OPTION", "<em>" . $lng->txt("select_all") . "</em>");
252  $tpl->setVariable("POST_VAR", $this->getPostVar());
253  $tpl->parseCurrentBlock();
254 
255  $tpl->setVariable("TOGGLE_FIELD_ID", $this->getFieldId());
256  $tpl->setVariable("TOGGLE_ALL_ID", $this->getFieldId() . "_all__toggle");
257  $tpl->setVariable("TOGGLE_ALL_CBOX_ID", $this->getFieldId() . "_");
258  }
259 
260  if ($this->selected_first) {
261  // move selected values to top
262  $tmp_checked = $tmp_unchecked = array();
263  foreach ($options as $option_value => $option_text) {
264  if (in_array($option_value, $values)) {
265  $tmp_checked[$option_value] = $option_text;
266  } else {
267  $tmp_unchecked[$option_value] = $option_text;
268  }
269  }
270  $options = $tmp_checked + $tmp_unchecked;
271  unset($tmp_checked);
272  unset($tmp_unchecked);
273  }
274 
275  foreach ($options as $option_value => $option_text) {
276  $tpl->setCurrentBlock("item");
277  if ($this->getDisabled()) {
278  $tpl->setVariable(
279  "DISABLED",
280  " disabled=\"disabled\""
281  );
282  }
283  if (in_array($option_value, $values)) {
284  $tpl->setVariable(
285  "CHECKED",
286  " checked=\"checked\""
287  );
288  }
289 
290  $tpl->setVariable("VAL", ilUtil::prepareFormOutput($option_value));
291  $tpl->setVariable("ID_VAL", ilUtil::prepareFormOutput($option_value));
292  $tpl->setVariable("IID", $this->getFieldId());
293  $tpl->setVariable("TXT_OPTION", $option_text);
294  $tpl->setVariable("POST_VAR", $this->getPostVar());
295  $tpl->parseCurrentBlock();
296  }
297  }
298 
299  $tpl->setVariable("ID", $this->getFieldId());
300  $tpl->setVariable("CUSTOM_ATTRIBUTES", implode(' ', $this->getCustomAttributes()));
301 
302  if ($this->getWidth()) {
303  $tpl->setVariable("WIDTH", $this->getWidth() . ($this->getWidthUnit()?$this->getWidthUnit():''));
304  }
305  if ($this->getHeight()) {
306  $tpl->setVariable("HEIGHT", $this->getHeight() . ($this->getHeightUnit()?$this->getHeightUnit():''));
307  }
308 
309  return $tpl->get();
310  }
311 
317  public function insert($a_tpl)
318  {
319  $a_tpl->setCurrentBlock("prop_generic");
320  $a_tpl->setVariable("PROP_GENERIC", $this->render());
321  $a_tpl->parseCurrentBlock();
322  }
323 
327  public function getTableFilterHTML()
328  {
329  $html = $this->render();
330  return $html;
331  }
332 
333 
337  public function getCustomAttributes()
338  {
340  }
341 
342 
347  {
348  $this->custom_attributes = $custom_attributes;
349  }
350 
351 
355  public function addCustomAttribute($custom_attribute)
356  {
357  $this->custom_attributes[] = $custom_attribute;
358  }
359 
363  public function getWidthUnit()
364  {
365  return $this->widthUnit;
366  }
367 
371  public function setWidthUnit($widthUnit)
372  {
373  $this->widthUnit = $widthUnit;
374  }
375 
379  public function getHeightUnit()
380  {
381  return $this->heightUnit;
382  }
383 
387  public function setHeightUnit($heightUnit)
388  {
389  $this->heightUnit = $heightUnit;
390  }
391 }
insert($a_tpl)
Insert property html.
static prepareFormOutput($a_str, $a_strip=false)
prepares string output for html forms public
Interface for property form input GUI classes that can be used in table filters.
setOptions($a_options)
Set Options.
setHeight($a_height)
Sets the height of this field.
global $DIC
Definition: saml.php:7
$tpl
Definition: ilias.php:10
getPostVar()
Get Post Variable.
setValueByArray($a_values)
Set value by array.
setAlert($a_alert)
Set Alert Text.
setCustomAttributes($custom_attributes)
setType($a_type)
Set Type.
This class represents a multi selection list property in a property form.
getHeight()
Returns the height currently set for this field.
getWidth()
Returns the width currently set for this field.
getFieldId()
Get Post Variable.
special template class to simplify handling of ITX/PEAR
getTableFilterHTML()
Get HTML for table filter.
static stripSlashes($a_str, $a_strip_html=true, $a_allow="")
strip slashes if magic qoutes is enabled
Create styles array
The data for the language used.
This class represents a property in a property form.
checkInput()
Check input, strip slashes etc.
__construct($a_title="", $a_postvar="")
Constructor.
setWidth($a_width)
Sets the width of this field.
$key
Definition: croninfo.php:18
$_POST["username"]
$html
Definition: example_001.php:87