ILIAS  Release_4_2_x_branch Revision 61807
 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 
44  function __construct($a_title = "", $a_postvar = "")
45  {
46  parent::__construct($a_title, $a_postvar);
47  $this->setType("checkbox");
48  }
49 
55  function addOption($a_option)
56  {
57  $this->options[] = $a_option;
58  }
59 
65  function setOptions($a_options)
66  {
67  foreach($a_options as $key => $label) {
68  if (is_string($label)) {
69  $chb = new ilCheckboxInputGUI($label, $key);
70  $this->options[] = $chb;
71  }
72  else if ($label instanceof ilCheckboxInputGUI) {
73  $this->options[] = $label;
74  }
75  }
76  }
77 
83  function getOptions()
84  {
85  return $this->options;
86  }
87 
93  function setValue($a_value)
94  {
95  $this->value = $a_value;
96  }
97 
103  function getValue()
104  {
105  return $this->value;
106  }
107 
113  function setValueByArray($a_values)
114  {
115  $this->setValue($a_values[$this->getPostVar()]);
116  foreach($this->getOptions() as $option)
117  {
118  foreach($option->getSubItems() as $item)
119  {
120  $item->setValueByArray($a_values);
121  }
122  }
123  }
124 
130  function checkInput()
131  {
132  global $lng;
133 
134  if ($this->getRequired() && count($_POST[$this->getPostVar()]) == 0)
135  {
136  $this->setAlert($lng->txt("msg_input_is_required"));
137 
138  return false;
139  }
140 
141  $ok = true;
142  foreach($this->getOptions() as $option)
143  {
144  foreach($option->getSubItems() as $item)
145  {
146  $item_ok = $item->checkInput();
147  if (!$item_ok && in_array($option->getValue(), $_POST[$this->getPostVar()]))
148  {
149  $ok = false;
150  }
151  }
152  }
153  return $ok;
154 
155  }
156 
162  function insert(&$a_tpl)
163  {
164  $tpl = new ilTemplate("tpl.prop_checkbox_group.html", true, true, "Services/Form");
165 
166  foreach($this->getOptions() as $option)
167  {
168  // information text for option
169  if ($option->getInfo() != "")
170  {
171  $tpl->setCurrentBlock("checkbox_option_desc");
172  $tpl->setVariable("CHECKBOX_OPTION_DESC", $option->getInfo());
173  $tpl->parseCurrentBlock();
174  }
175 
176 
177  if (count($option->getSubItems()) > 0)
178  {
179  $tpl->setCurrentBlock("checkbox_option_subform");
180  $pf = new ilPropertyFormGUI();
181  $pf->setMode("subform");
182  $pf->setItems($option->getSubItems());
183  $tpl->setVariable("SUB_FORM", $pf->getContent());
184  $tpl->setVariable("SOP_ID", $this->getFieldId()."_".$option->getValue());
185  if ($pf->getMultipart())
186  {
187  $this->getParentForm()->setMultipart(true);
188  }
189  $tpl->parseCurrentBlock();
190  if ($pf->getMultipart())
191  {
192  $this->getParentForm()->setMultipart(true);
193  }
194  }
195 
196  $tpl->setCurrentBlock("prop_checkbox_option");
197  $tpl->setVariable("POST_VAR", $this->getPostVar() . '[]');
198  $tpl->setVariable("VAL_CHECKBOX_OPTION", $option->getValue());
199  $tpl->setVariable("OP_ID", $this->getFieldId()."_".$option->getValue());
200  $tpl->setVariable("FID", $this->getFieldId());
201  if($this->getDisabled() or $option->getDisabled())
202  {
203  $tpl->setVariable('DISABLED','disabled="disabled" ');
204  }
205  if (is_array($this->getValue()))
206  {
207  if (in_array($option->getValue(), $this->getValue()))
208  {
209  $tpl->setVariable("CHK_CHECKBOX_OPTION",
210  'checked="checked"');
211  }
212  }
213  $tpl->setVariable("TXT_CHECKBOX_OPTION", $option->getTitle());
214 
215 
216  $tpl->parseCurrentBlock();
217  }
218  $tpl->setVariable("ID", $this->getFieldId());
219 
220  $a_tpl->setCurrentBlock("prop_generic");
221  $a_tpl->setVariable("PROP_GENERIC", $tpl->get());
222  $a_tpl->parseCurrentBlock();
223 
224  }
225 
231  function getItemByPostVar($a_post_var)
232  {
233  if ($this->getPostVar() == $a_post_var)
234  {
235  return $this;
236  }
237 
238  foreach($this->getOptions() as $option)
239  {
240  foreach($option->getSubItems() as $item)
241  {
242  if ($item->getType() != "section_header")
243  {
244  $ret = $item->getItemByPostVar($a_post_var);
245  if (is_object($ret))
246  {
247  return $ret;
248  }
249  }
250  }
251  }
252 
253  return false;
254  }
255 
256 }