ILIAS  Release_4_4_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;
56 
63  function __construct($a_title = "", $a_postvar = "")
64  {
65  parent::__construct($a_title, $a_postvar);
66  $this->setType("multi_select");
67  $this->setValue(array());
68  }
69 
76  public function setWidth($a_width)
77  {
78  $this->width = (int)$a_width;
79  }
80 
87  public function getWidth()
88  {
89  return $this->width;
90  }
91 
98  public function setHeight($a_height)
99  {
100  $this->height = (int)$a_height;
101  }
102 
109  public function getHeight()
110  {
111  return $this->height;
112  }
113 
119  function setOptions($a_options)
120  {
121  $this->options = $a_options;
122  }
123 
129  function getOptions()
130  {
131  return $this->options;
132  }
133 
139  function setValue($a_array)
140  {
141  $this->value = $a_array;
142  }
143 
149  function getValue()
150  {
151  return is_array($this->value) ? $this->value : array();
152  }
153 
159  function setValueByArray($a_values)
160  {
161  $this->setValue($a_values[$this->getPostVar()]);
162  }
163 
164 
165  function enableSelectAll($a_value)
166  {
167  $this->select_all = (bool)$a_value;
168  }
169 
170  function enableSelectedFirst($a_value)
171  {
172  $this->selected_first = (bool)$a_value;
173  }
174 
175 
181  function checkInput()
182  {
183  global $lng;
184 
185  if (is_array($_POST[$this->getPostVar()]))
186  {
187  foreach ($_POST[$this->getPostVar()] as $k => $v)
188  {
189  $_POST[$this->getPostVar()][$k] =
191  }
192  }
193  else
194  {
195  $_POST[$this->getPostVar()] = array();
196  }
197  if ($this->getRequired() && count($_POST[$this->getPostVar()]) == 0)
198  {
199  $this->setAlert($lng->txt("msg_input_is_required"));
200 
201  return false;
202  }
203  return true;
204  }
205 
209  function render()
210  {
211  global $lng;
212 
213  $tpl = new ilTemplate("tpl.prop_multi_select.html", true, true, "Services/Form");
214  $values = $this->getValue();
215 
216  $options = $this->getOptions();
217  if($options)
218  {
219  if($this->select_all)
220  {
221  // enable select all toggle
222  $tpl->setCurrentBlock("item");
223  $tpl->setVariable("VAL", "");
224  $tpl->setVariable("ID_VAL", ilUtil::prepareFormOutput("all__toggle"));
225  $tpl->setVariable("IID", $this->getFieldId());
226  $tpl->setVariable("TXT_OPTION" ,"<em>".$lng->txt("select_all")."</em>");
227  $tpl->setVariable("POST_VAR", $this->getPostVar());
228  $tpl->parseCurrentBlock();
229 
230  $tpl->setVariable("TOGGLE_FIELD_ID", $this->getFieldId());
231  $tpl->setVariable("TOGGLE_ALL_ID", $this->getFieldId()."_all__toggle");
232  $tpl->setVariable("TOGGLE_ALL_CBOX_ID", $this->getFieldId()."_");
233  }
234 
235  if($this->selected_first)
236  {
237  // move selected values to top
238  $tmp_checked = $tmp_unchecked = array();
239  foreach($options as $option_value => $option_text)
240  {
241  if (in_array($option_value, $values))
242  {
243  $tmp_checked[$option_value] = $option_text;
244  }
245  else
246  {
247  $tmp_unchecked[$option_value] = $option_text;
248  }
249  }
250  $options = $tmp_checked + $tmp_unchecked;
251  unset($tmp_checked);
252  unset($tmp_unchecked);
253  }
254 
255  foreach($options as $option_value => $option_text)
256  {
257  $tpl->setCurrentBlock("item");
258  if ($this->getDisabled())
259  {
260  $tpl->setVariable("DISABLED",
261  " disabled=\"disabled\"");
262  }
263  if (in_array($option_value, $values))
264  {
265  $tpl->setVariable("CHECKED",
266  " checked=\"checked\"");
267  }
268 
269  $tpl->setVariable("VAL", ilUtil::prepareFormOutput($option_value));
270  $tpl->setVariable("ID_VAL", ilUtil::prepareFormOutput($option_value));
271  $tpl->setVariable("IID", $this->getFieldId());
272  $tpl->setVariable("TXT_OPTION", $option_text);
273  $tpl->setVariable("POST_VAR", $this->getPostVar());
274  $tpl->parseCurrentBlock();
275  }
276  }
277 
278  $tpl->setVariable("ID", $this->getFieldId());
279 
280  $tpl->setVariable("WIDTH", $this->getWidth());
281  $tpl->setVariable("HEIGHT", $this->getHeight());
282 
283  return $tpl->get();
284  }
285 
291  function insert(&$a_tpl)
292  {
293  $a_tpl->setCurrentBlock("prop_generic");
294  $a_tpl->setVariable("PROP_GENERIC", $this->render());
295  $a_tpl->parseCurrentBlock();
296  }
297 
302  {
303  $html = $this->render();
304  return $html;
305  }
306 
307 }