ILIAS  release_5-1 Revision 5.0.0-5477-g43f3e3fab5f
class.ilADTFormBridge.php
Go to the documentation of this file.
1<?php
2/* Copyright (c) 1998-2009 ILIAS open source, Extended GPL, see docs/LICENSE */
3
11abstract class ilADTFormBridge
12{
13 protected $adt; // [ilADT]
14 protected $form; // [ilPropertyFormGUI]
15 protected $id; // [string]
16 protected $title; // [string]
17 protected $info; // [string]
18 protected $parent_element; // [string|array]
19 protected $required; // [bool]
20 protected $disabled; // [bool]
21
28 public function __construct(ilADT $a_adt)
29 {
30 $this->setADT($a_adt);
31 }
32
33
34 //
35 // properties
36 //
37
46 abstract protected function isValidADT(ilADT $a_adt);
47
54 protected function setADT(ilADT $a_adt)
55 {
56 if(!$this->isValidADT($a_adt))
57 {
58 throw new Exception('ADTFormBridge Type mismatch.');
59 }
60
61 $this->adt = $a_adt;
62 }
63
69 public function getADT()
70 {
71 return $this->adt;
72 }
73
79 public function setForm(ilPropertyFormGUI $a_form)
80 {
81 $this->form = $a_form;
82 }
83
89 public function getForm()
90 {
91 return $this->form;
92 }
93
99 public function setElementId($a_value)
100 {
101 $this->id = (string)$a_value;
102 }
103
109 public function getElementId()
110 {
111 return $this->id;
112 }
113
119 public function setTitle($a_value)
120 {
121 $this->title = trim($a_value);
122 }
123
129 public function getTitle()
130 {
131 return $this->title;
132 }
133
139 public function setInfo($a_value)
140 {
141 $this->info = trim($a_value);
142 }
143
149 public function getInfo()
150 {
151 return $this->info;
152 }
153
159 public function setParentElement($a_value)
160 {
161 if(!is_array($a_value))
162 {
163 $a_value = (string)$a_value;
164 }
165 $this->parent = $a_value;
166 }
167
173 public function getParentElement()
174 {
175 return $this->parent;
176 }
177
183 public function setDisabled($a_value)
184 {
185 $this->disabled = (bool)$a_value;
186 }
187
193 public function isDisabled()
194 {
195 return $this->disabled;
196 }
197
203 public function setRequired($a_value)
204 {
205 $this->required = (bool)$a_value;
206 }
207
213 public function isRequired()
214 {
215 return $this->required;
216 }
217
218
219 //
220 // form
221 //
222
229 protected function addBasicFieldProperties(ilFormPropertyGUI $a_field, ilADTDefinition $a_def)
230 {
231 if((bool)$this->isDisabled())
232 {
233 $a_field->setDisabled(true);
234 }
235 else if($this->isRequired())
236 {
237 $a_field->setRequired(true);
238 }
239
240 $info = $this->getInfo();
241 if($info)
242 {
243 $a_field->setInfo($info);
244 }
245 }
246
252 protected function findParentElementInForm()
253 {
254 $parent_def = $this->getParentElement();
255 if($parent_def)
256 {
257 if(is_array($parent_def))
258 {
259 $parent_option = $parent_def[1];
260 $parent_def = $parent_def[0];
261 }
262
263 // :TODO: throw exception on invalid definition ?!
264
265 $parent_field = $this->getForm()->getItemByPostVar($parent_def);
266 if($parent_field instanceof ilSubEnabledFormPropertyGUI)
267 {
268 // radio/checkbox group
269 if($parent_option && method_exists($parent_field, "getOptions"))
270 {
271 foreach($parent_field->getOptions() as $option)
272 {
273 if($option->getValue() == $parent_option)
274 {
275 $parent_field = $option;
276 break;
277 }
278 }
279 }
280 }
281
282 if($parent_field)
283 {
284 return $parent_field;
285 }
286 }
287 }
288
294 protected function addToParentElement(ilFormPropertyGUI $a_field)
295 {
296 $field = $this->findParentElementInForm();
297 if($field)
298 {
299 $field->addSubItem($a_field);
300 }
301 else if($this->getForm() instanceof ilPropertyFormGUI)
302 {
303 $this->getForm()->addItem($a_field);
304 }
305 }
306
310 abstract public function addToForm();
311
317 public function addJS(ilTemplate $a_tpl)
318 {
319
320 }
321
328 protected function isActiveForSubItems($a_parent_option = null)
329 {
330 return !$this->getADT()->isNull();
331 }
332
339 public function shouldBeImportedFromPost(ilADTFormBridge $a_parent_adt = null)
340 {
341 if($this->isDisabled())
342 {
343 return false;
344 }
345
346 // inactive parent elements disable importing
347 if($a_parent_adt)
348 {
349 $parent_option = null;
351 if(is_array($parent_element))
352 {
353 $parent_option = $parent_element[1];
354 }
355 return $a_parent_adt->isActiveForSubItems($parent_option);
356 }
357
358 return true;
359 }
360
364 abstract public function importFromPost();
365
371 public function validate()
372 {
373 global $lng;
374
375 // ilADTFormBridge->isRequired() != ilADT->allowNull()
376 if($this->isRequired() && $this->getADT()->isNull())
377 {
378 $field = $this->getForm()->getItemByPostvar($this->getElementId());
379 $field->setAlert($lng->txt("msg_input_is_required"));
380 return false;
381 }
382 // no need to further validate if no value given
383 else if(!$this->getADT()->isValid())
384 {
385 $tmp = array();
386
387 $mess = $this->getADT()->getValidationErrors();
388 foreach($mess as $error_code)
389 {
390 $tmp[] = $this->getADT()->translateErrorCode($error_code);
391 }
392
393 $field = $this->getForm()->getItemByPostvar($this->getElementId());
394 $field->setAlert(implode("<br />", $tmp));
395
396 return false;
397 }
398
399 return true;
400 }
401
402 public function setExternalErrors($a_errors)
403 {
404 $field = $this->getForm()->getItemByPostvar($this->getElementId());
405 $field->setAlert(implode("<br />", $a_errors));
406 }
407}
408
409?>
ADT definition base class.
ADT form bridge base class.
__construct(ilADT $a_adt)
Constructor.
setADT(ilADT $a_adt)
Set ADT.
isValidADT(ilADT $a_adt)
Check if given ADT is valid.
addBasicFieldProperties(ilFormPropertyGUI $a_field, ilADTDefinition $a_def)
Helper method to handle generic properties like setRequired(), setInfo()
addJS(ilTemplate $a_tpl)
Add ADT-specific JS-files to template.
findParentElementInForm()
Try to find parent element in form (could be option)
getElementId()
Get element id.
setInfo($a_value)
Set info (aka form field info text)
addToParentElement(ilFormPropertyGUI $a_field)
Add form field to parent element.
setTitle($a_value)
Set title (aka form field caption)
importFromPost()
Import values from form request POST data.
isActiveForSubItems($a_parent_option=null)
Check if element is currently active for subitem(s)
isRequired()
Get required.
setRequired($a_value)
Set required.
addToForm()
Add ADT-specific fields to form.
setDisabled($a_value)
Set disabled.
setElementId($a_value)
Set element id (aka form field)
isDisabled()
Get disabled.
validate()
Validate ADT and parse error codes.
setParentElement($a_value)
Set parent element.
getParentElement()
Get parent element.
setForm(ilPropertyFormGUI $a_form)
Set form.
shouldBeImportedFromPost(ilADTFormBridge $a_parent_adt=null)
Check if incoming values should be imported at all.
ADT base class.
Definition: class.ilADT.php:12
This class represents a property in a property form.
setInfo($a_info)
Set Information Text.
setRequired($a_required)
Set Required.
setDisabled($a_disabled)
Set Disabled.
This class represents a property form user interface.
This class represents a property that may include a sub form.
special template class to simplify handling of ITX/PEAR
global $lng
Definition: privfeed.php:40