ILIAS  release_4-3 Revision
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilCheckboxGroupInputGUI.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/Form/classes/class.ilCheckboxOption.php");
25 
34 {
35  protected $options = array();
36  protected $value;
37  protected $use_values_as_keys = false;
38 
39 
46  function __construct($a_title = "", $a_postvar = "")
47  {
48  parent::__construct($a_title, $a_postvar);
49  $this->setType("checkbox");
50  }
51 
57  function setUseValuesAsKeys($a_val)
58  {
59  $this->use_values_as_keys = $a_val;
60  }
61 
67  function getUseValuesAsKeys()
68  {
70  }
71 
77  function addOption($a_option)
78  {
79  $this->options[] = $a_option;
80  }
81 
87  function setOptions($a_options)
88  {
89  foreach($a_options as $key => $label) {
90  if (is_string($label)) {
91  $chb = new ilCheckboxInputGUI($label, $key);
92  $this->options[] = $chb;
93  }
94  else if ($label instanceof ilCheckboxInputGUI) {
95  $this->options[] = $label;
96  }
97  }
98  }
99 
105  function getOptions()
106  {
107  return $this->options;
108  }
109 
115  function setValue($a_value)
116  {
117  $this->value = $a_value;
118  }
119 
125  function getValue()
126  {
127  return $this->value;
128  }
129 
135  function setValueByArray($a_values)
136  {
137  $this->setValue($a_values[$this->getPostVar()]);
138  foreach($this->getOptions() as $option)
139  {
140  foreach($option->getSubItems() as $item)
141  {
142  $item->setValueByArray($a_values);
143  }
144  }
145  }
146 
152  function checkInput()
153  {
154  global $lng;
155 
156  if ($this->getRequired() && count($_POST[$this->getPostVar()]) == 0)
157  {
158  $this->setAlert($lng->txt("msg_input_is_required"));
159 
160  return false;
161  }
162 
163  $ok = true;
164  foreach($this->getOptions() as $option)
165  {
166  foreach($option->getSubItems() as $item)
167  {
168  $item_ok = $item->checkInput();
169  if (!$item_ok && in_array($option->getValue(), $_POST[$this->getPostVar()]))
170  {
171  $ok = false;
172  }
173  }
174  }
175  return $ok;
176 
177  }
178 
184  function insert(&$a_tpl)
185  {
186  $tpl = new ilTemplate("tpl.prop_checkbox_group.html", true, true, "Services/Form");
187 
188  foreach($this->getOptions() as $option)
189  {
190  // information text for option
191  if ($option->getInfo() != "")
192  {
193  $tpl->setCurrentBlock("checkbox_option_desc");
194  $tpl->setVariable("CHECKBOX_OPTION_DESC", $option->getInfo());
195  $tpl->parseCurrentBlock();
196  }
197 
198 
199  if (count($option->getSubItems()) > 0)
200  {
201  $tpl->setCurrentBlock("checkbox_option_subform");
202  $pf = new ilPropertyFormGUI();
203  $pf->setMode("subform");
204  $pf->setItems($option->getSubItems());
205  $tpl->setVariable("SUB_FORM", $pf->getContent());
206  $tpl->setVariable("SOP_ID", $this->getFieldId()."_".$option->getValue());
207  if ($pf->getMultipart())
208  {
209  $this->getParentForm()->setMultipart(true);
210  }
211  $tpl->parseCurrentBlock();
212  if ($pf->getMultipart())
213  {
214  $this->getParentForm()->setMultipart(true);
215  }
216  }
217 
218  $tpl->setCurrentBlock("prop_checkbox_option");
219 
220  if (!$this->getUseValuesAsKeys())
221  {
222  $tpl->setVariable("POST_VAR", $this->getPostVar() . '[]');
223  $tpl->setVariable("VAL_CHECKBOX_OPTION", $option->getValue());
224  }
225  else
226  {
227  $tpl->setVariable("POST_VAR", $this->getPostVar().'['.$option->getValue().']');
228  $tpl->setVariable("VAL_CHECKBOX_OPTION", "1");
229  }
230 
231  $tpl->setVariable("OP_ID", $this->getFieldId()."_".$option->getValue());
232  $tpl->setVariable("FID", $this->getFieldId());
233 
234  if($this->getDisabled() or $option->getDisabled())
235  {
236  $tpl->setVariable('DISABLED','disabled="disabled" ');
237  }
238 
239  if (is_array($this->getValue()))
240  {
241  if (!$this->getUseValuesAsKeys())
242  {
243  if (in_array($option->getValue(), $this->getValue()))
244  {
245  $tpl->setVariable("CHK_CHECKBOX_OPTION",
246  'checked="checked"');
247  }
248  }
249  else
250  {
251  $cval = $this->getValue();
252  if ($cval[$option->getValue()] == 1)
253  {
254  $tpl->setVariable("CHK_CHECKBOX_OPTION",
255  'checked="checked"');
256  }
257  }
258  }
259  $tpl->setVariable("TXT_CHECKBOX_OPTION", $option->getTitle());
260 
261 
262  $tpl->parseCurrentBlock();
263  }
264  $tpl->setVariable("ID", $this->getFieldId());
265 
266  $a_tpl->setCurrentBlock("prop_generic");
267  $a_tpl->setVariable("PROP_GENERIC", $tpl->get());
268  $a_tpl->parseCurrentBlock();
269 
270  }
271 
277  function getItemByPostVar($a_post_var)
278  {
279  if ($this->getPostVar() == $a_post_var)
280  {
281  return $this;
282  }
283 
284  foreach($this->getOptions() as $option)
285  {
286  foreach($option->getSubItems() as $item)
287  {
288  if ($item->getType() != "section_header")
289  {
290  $ret = $item->getItemByPostVar($a_post_var);
291  if (is_object($ret))
292  {
293  return $ret;
294  }
295  }
296  }
297  }
298 
299  return false;
300  }
301 
302 }