ILIAS  Release_5_0_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
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;
59  protected $custom_attributes = array();
60 
67  function __construct($a_title = "", $a_postvar = "")
68  {
69  parent::__construct($a_title, $a_postvar);
70  $this->setType("multi_select");
71  $this->setValue(array());
72  }
73 
80  public function setWidth($a_width)
81  {
82  $this->width = (int)$a_width;
83  }
84 
91  public function getWidth()
92  {
93  return $this->width;
94  }
95 
102  public function setHeight($a_height)
103  {
104  $this->height = (int)$a_height;
105  }
106 
113  public function getHeight()
114  {
115  return $this->height;
116  }
117 
123  function setOptions($a_options)
124  {
125  $this->options = $a_options;
126  }
127 
133  function getOptions()
134  {
135  return $this->options;
136  }
137 
143  function setValue($a_array)
144  {
145  $this->value = $a_array;
146  }
147 
153  function getValue()
154  {
155  return is_array($this->value) ? $this->value : array();
156  }
157 
163  function setValueByArray($a_values)
164  {
165  $this->setValue($a_values[$this->getPostVar()]);
166  }
167 
168 
169  function enableSelectAll($a_value)
170  {
171  $this->select_all = (bool)$a_value;
172  }
173 
174  function enableSelectedFirst($a_value)
175  {
176  $this->selected_first = (bool)$a_value;
177  }
178 
179 
185  function checkInput()
186  {
187  global $lng;
188 
189  if (is_array($_POST[$this->getPostVar()]))
190  {
191  foreach ($_POST[$this->getPostVar()] as $k => $v)
192  {
193  $_POST[$this->getPostVar()][$k] =
195  }
196  }
197  else
198  {
199  $_POST[$this->getPostVar()] = array();
200  }
201  if ($this->getRequired() && count($_POST[$this->getPostVar()]) == 0)
202  {
203  $this->setAlert($lng->txt("msg_input_is_required"));
204 
205  return false;
206  }
207  return true;
208  }
209 
213  function render()
214  {
215  global $lng;
216 
217  $tpl = new ilTemplate("tpl.prop_multi_select.html", true, true, "Services/Form");
218  $values = $this->getValue();
219 
220  $options = $this->getOptions();
221  if($options)
222  {
223  if($this->select_all)
224  {
225  // enable select all toggle
226  $tpl->setCurrentBlock("item");
227  $tpl->setVariable("VAL", "");
228  $tpl->setVariable("ID_VAL", ilUtil::prepareFormOutput("all__toggle"));
229  $tpl->setVariable("IID", $this->getFieldId());
230  $tpl->setVariable("TXT_OPTION" ,"<em>".$lng->txt("select_all")."</em>");
231  $tpl->setVariable("POST_VAR", $this->getPostVar());
232  $tpl->parseCurrentBlock();
233 
234  $tpl->setVariable("TOGGLE_FIELD_ID", $this->getFieldId());
235  $tpl->setVariable("TOGGLE_ALL_ID", $this->getFieldId()."_all__toggle");
236  $tpl->setVariable("TOGGLE_ALL_CBOX_ID", $this->getFieldId()."_");
237  }
238 
239  if($this->selected_first)
240  {
241  // move selected values to top
242  $tmp_checked = $tmp_unchecked = array();
243  foreach($options as $option_value => $option_text)
244  {
245  if (in_array($option_value, $values))
246  {
247  $tmp_checked[$option_value] = $option_text;
248  }
249  else
250  {
251  $tmp_unchecked[$option_value] = $option_text;
252  }
253  }
254  $options = $tmp_checked + $tmp_unchecked;
255  unset($tmp_checked);
256  unset($tmp_unchecked);
257  }
258 
259  foreach($options as $option_value => $option_text)
260  {
261  $tpl->setCurrentBlock("item");
262  if ($this->getDisabled())
263  {
264  $tpl->setVariable("DISABLED",
265  " disabled=\"disabled\"");
266  }
267  if (in_array($option_value, $values))
268  {
269  $tpl->setVariable("CHECKED",
270  " checked=\"checked\"");
271  }
272 
273  $tpl->setVariable("VAL", ilUtil::prepareFormOutput($option_value));
274  $tpl->setVariable("ID_VAL", ilUtil::prepareFormOutput($option_value));
275  $tpl->setVariable("IID", $this->getFieldId());
276  $tpl->setVariable("TXT_OPTION", $option_text);
277  $tpl->setVariable("POST_VAR", $this->getPostVar());
278  $tpl->parseCurrentBlock();
279  }
280  }
281 
282  $tpl->setVariable("ID", $this->getFieldId());
283  $tpl->setVariable("CUSTOM_ATTRIBUTES", implode(' ', $this->getCustomAttributes()));
284 
285  if($this->getWidth())
286  {
287  $tpl->setVariable("WIDTH", $this->getWidth());
288  }
289  if($this->getHeight())
290  {
291  $tpl->setVariable("HEIGHT", $this->getHeight());
292  }
293 
294  return $tpl->get();
295  }
296 
302  function insert(&$a_tpl)
303  {
304  $a_tpl->setCurrentBlock("prop_generic");
305  $a_tpl->setVariable("PROP_GENERIC", $this->render());
306  $a_tpl->parseCurrentBlock();
307  }
308 
313  {
314  $html = $this->render();
315  return $html;
316  }
317 
318 
322  public function getCustomAttributes() {
324  }
325 
326 
331  $this->custom_attributes = $custom_attributes;
332  }
333 
334 
338  public function addCustomAttribute($custom_attribute) {
339  $this->custom_attributes[] = $custom_attribute;
340  }
341 
342 }