1<?php
2/* Copyright (c) 1998-2009 ILIAS open source, Extended GPL, see docs/LICENSE */
3
4/**
5* This class represents a property in a property form.
6*
7* @author Alex Killing <alex.killing@gmx.de>
8* @version $Id$
9* @ingroup	ServicesForm
10*/
11class ilFormPropertyGUI
12{
13    /**
14     * @var ilCtrl
15     */
16    protected $ctrl;
17
18    /**
19     * @var ilLanguage
20     */
21    protected $lng;
22
23    protected $type;
24    protected $title;
25    protected $postvar;
26    protected $info;
27    protected $alert;
28    protected $required = false;
29    protected $parentgui;
30    protected $parentform;
31    protected $hidden_title = "";
32    protected $multi = false;
33    protected $multi_sortable = false;
34    protected $multi_addremove = true;
35    protected $multi_values;
36
37    /**
38    * Constructor
39    *
40    * @param	string	$a_title	Title
41    * @param	string	$a_postvar	Post Variable
42    */
43    public function __construct($a_title = "", $a_postvar = "")
44    {
45        global $DIC;
46
47        $this->ctrl = $DIC->ctrl();
48        $this->lng = $DIC->language();
49        $this->setTitle($a_title);
50        $this->setPostVar($a_postvar);
51        $this->setDisabled(false);
52    }
53
54    /**
55    * Execute command.
56    */
57    public function executeCommand()
58    {
59        $ilCtrl = $this->ctrl;
60
61        $next_class = $ilCtrl->getNextClass($this);
62        $cmd = $ilCtrl->getCmd();
63
64        return $this->$cmd();
65    }
66
67    /**
68    * Set Type.
69    *
70    * @param	string	$a_type	Type
71    */
72    protected function setType($a_type)
73    {
74        $this->type = $a_type;
75    }
76
77    /**
78    * Get Type.
79    *
80    * @return	string	Type
81    */
82    public function getType()
83    {
84        return $this->type;
85    }
86
87    /**
88    * Set Title.
89    *
90    * @param	string	$a_title	Title
91    */
92    public function setTitle($a_title)
93    {
94        $this->title = $a_title;
95    }
96
97    /**
98    * Get Title.
99    *
100    * @return	string	Title
101    */
102    public function getTitle()
103    {
104        return $this->title;
105    }
106
107    /**
108    * Set Post Variable.
109    *
110    * @param	string	$a_postvar	Post Variable
111    */
112    public function setPostVar($a_postvar)
113    {
114        $this->postvar = $a_postvar;
115    }
116
117    /**
118    * Get Post Variable.
119    *
120    * @return	string	Post Variable
121    */
122    public function getPostVar()
123    {
124        return $this->postvar;
125    }
126
127    /**
128    * Get Post Variable.
129    *
130    * @return	string	Post Variable
131    */
132    public function getFieldId()
133    {
134        $id = str_replace("[", "__", $this->getPostVar());
135        $id = str_replace("]", "__", $id);
136
137        return $id;
138    }
139
140    /**
141    * Set Information Text.
142    *
143    * @param	string	$a_info	Information Text
144    */
145    public function setInfo($a_info)
146    {
147        $this->info = $a_info;
148    }
149
150    /**
151    * Get Information Text.
152    *
153    * @return	string	Information Text
154    */
155    public function getInfo()
156    {
157        return $this->info;
158    }
159
160    /**
161    * Set Alert Text.
162    *
163    * @param	string	$a_alert	Alert Text
164    */
165    public function setAlert($a_alert)
166    {
167        $this->alert = $a_alert;
168    }
169
170    /**
171    * Get Alert Text.
172    *
173    * @return	string	Alert Text
174    */
175    public function getAlert()
176    {
177        return $this->alert;
178    }
179
180    /**
181    * Set Required.
182    *
183    * @param	boolean	$a_required	Required
184    */
185    public function setRequired($a_required)
186    {
187        $this->required = $a_required;
188    }
189
190    /**
191    * Get Required.
192    *
193    * @return	boolean	Required
194    */
195    public function getRequired()
196    {
197        return $this->required;
198    }
199
200    /**
201    * Set Disabled.
202    *
203    * @param	boolean	$a_disabled	Disabled
204    */
205    public function setDisabled($a_disabled)
206    {
207        $this->disabled = $a_disabled;
208    }
209
210    /**
211    * Get Disabled.
212    *
213    * @return	boolean	Disabled
214    */
215    public function getDisabled()
216    {
217        return $this->disabled;
218    }
219
220    /**
221    * Check input, strip slashes etc. set alert, if input is not ok.
222    *
223    * @return	boolean		Input ok, true/false
224    */
225    public function checkInput()
226    {
227        return false;		// please overwrite
228    }
229
230    /**
231    * Set Parent Form.
232    *
233    * @param	object	$a_parentform	Parent Form
234    */
235    public function setParentForm($a_parentform)
236    {
237        $this->setParent($a_parentform);
238    }
239
240    /**
241    * Get Parent Form.
242    *
243    * @return	object	Parent Form
244    */
245    public function getParentForm()
246    {
247        return $this->getParent();
248    }
249
250    /**
251    * Set Parent GUI object.
252    *
253    * @param	object	parent gui object
254    */
255    public function setParent($a_val)
256    {
257        $this->parent_gui = $a_val;
258    }
259
260    /**
261    * Get  Parent GUI object.
262    *
263    * @return	object	parent gui object
264    */
265    public function getParent()
266    {
267        return $this->parent_gui;
268    }
269
270    /**
271    * Get sub form html
272    *
273    */
274    public function getSubForm()
275    {
276        return "";
277    }
278
279    /**
280    * Sub form hidden on init?
281    *
282    */
283    public function hideSubForm()
284    {
285        return false;
286    }
287
288    /**
289    * Set hidden title (for screenreaders)
290    *
291    * @param	string	hidden title
292    */
293    public function setHiddenTitle($a_val)
294    {
295        $this->hidden_title = $a_val;
296    }
297
298    /**
299    * Get hidden title
300    *
301    * @return	string	hidden title
302    */
303    public function getHiddenTitle()
304    {
305        return $this->hidden_title;
306    }
307
308    /**
309    * Get item by post var
310    *
311    * @return	mixed	false or item object
312    */
313    public function getItemByPostVar($a_post_var)
314    {
315        if ($this->getPostVar() == $a_post_var) {
316            return $this;
317        }
318
319        return false;
320    }
321
322    /**
323    * serialize data
324    */
325    public function serializeData()
326    {
327        return serialize($this->getValue());
328    }
329
330    /**
331    * unserialize data
332    */
333    public function unserializeData($a_data)
334    {
335        $data = unserialize($a_data);
336
337        if ($data) {
338            $this->setValue($data);
339        } else {
340            $this->setValue(false);
341        }
342    }
343
344    /**
345    * Write to session
346    */
347    public function writeToSession()
348    {
349        $parent = $this->getParent();
350        if (!is_object($parent)) {
351            die("You must set parent for " . get_class($this) . " to use serialize feature.");
352        }
353        $_SESSION["form_" . $parent->getId()][$this->getFieldId()] =
354            $this->serializeData();
355    }
356
357    /**
358    * Clear session value
359    */
360    public function clearFromSession()
361    {
362        $parent = $this->getParent();
363        if (!is_object($parent)) {
364            die("You must set parent for " . get_class($this) . " to use serialize feature.");
365        }
366        $_SESSION["form_" . $parent->getId()][$this->getFieldId()] = false;
367    }
368
369    /**
370    * Read from session
371    */
372    public function readFromSession()
373    {
374        $parent = $this->getParent();
375        if (!is_object($parent)) {
376            die("You must set parent for " . get_class($this) . " to use serialize feature.");
377        }
378        $this->unserializeData($_SESSION["form_" . $parent->getId()][$this->getFieldId()]);
379    }
380
381    /**
382     * Get hidden tag (used for disabled properties)
383     */
384    public function getHiddenTag($a_post_var, $a_value)
385    {
386        return '<input type="hidden" name="' . $a_post_var . '" value="' . ilUtil::prepareFormOutput($a_value) . '" />';
387    }
388
389    /**
390     * Set Multi
391     *
392     * @param	bool	$a_multi	Multi
393     */
394    public function setMulti($a_multi, $a_sortable = false, $a_addremove = true)
395    {
396        if (!$this instanceof ilMultiValuesItem) {
397            require_once 'Services/Form/exceptions/class.ilFormException.php';
398            throw new ilFormException(sprintf(
399                "%s not supported for form property type %s",
400                __FUNCTION__,
401                get_class($this)
402            ));
403        }
404
405        $this->multi = (bool) $a_multi;
406        $this->multi_sortable = (bool) $a_sortable;
407        $this->multi_addremove = (bool) $a_addremove;
408    }
409
410    /**
411     * Get Multi
412     *
413     * @return	bool	Multi
414     */
415    public function getMulti()
416    {
417        return $this->multi;
418    }
419
420    /**
421     * Set multi values
422     *
423     * @param array $a_values
424     */
425    public function setMultiValues(array $a_values)
426    {
427        $this->multi_values = array_unique($a_values);
428    }
429
430    /**
431     * Get multi values
432     *
433     * @return array
434     */
435    public function getMultiValues()
436    {
437        return $this->multi_values;
438    }
439
440    /**
441     * Get HTML for multiple value icons
442     *
443     * @param bool $a_sortable
444     * @return string;
445     */
446    protected function getMultiIconsHTML()
447    {
448        $lng = $this->lng;
449
450        $id = $this->getFieldId();
451
452        $tpl = new ilTemplate("tpl.multi_icons.html", true, true, "Services/Form");
453
454        $html = "";
455        if ($this->multi_addremove) {
456            $tpl->setCurrentBlock("addremove");
457            $tpl->setVariable("ID", $id);
458            $tpl->setVariable("TXT_ADD", $lng->txt("add"));
459            $tpl->setVariable("TXT_REMOVE", $lng->txt("remove"));
460            include_once("./Services/UIComponent/Glyph/classes/class.ilGlyphGUI.php");
461            $tpl->setVariable("SRC_ADD", ilGlyphGUI::get(ilGlyphGUI::ADD));
462            $tpl->setVariable("SRC_REMOVE", ilGlyphGUI::get(ilGlyphGUI::REMOVE));
463            $tpl->parseCurrentBlock();
464        }
465
466        if ($this->multi_sortable) {
467            $tpl->setCurrentBlock("sortable");
468            $tpl->setVariable("ID", $id);
469            $tpl->setVariable("TXT_DOWN", $lng->txt("down"));
470            $tpl->setVariable("TXT_UP", $lng->txt("up"));
471            include_once("./Services/UIComponent/Glyph/classes/class.ilGlyphGUI.php");
472            $tpl->setVariable("SRC_UP", ilGlyphGUI::get(ilGlyphGUI::UP));
473            $tpl->setVariable("SRC_DOWN", ilGlyphGUI::get(ilGlyphGUI::DOWN));
474            $tpl->parseCurrentBlock();
475        }
476
477        return $tpl->get();
478    }
479
480    /**
481     * Get content that has to reside outside of the parent form tag, e.g. panels/layers
482     *
483     * @return string
484     */
485    public function getContentOutsideFormTag()
486    {
487    }
488
489    /**
490     * Remove prohibited characters
491     * see #19159
492     *
493     * @param string $a_text
494     * @return string
495     */
496    public static function removeProhibitedCharacters($a_text)
497    {
498        return str_replace("\x0B", "", $a_text);
499    }
500
501    /**
502     * Strip slashes with add space fallback, see https://www.ilias.de/mantis/view.php?id=19727
503     *
504     * @param string $a_str string
505     * @return string
506     */
507    public function stripSlashesAddSpaceFallback($a_str)
508    {
509        $str = ilUtil::stripSlashes($a_str);
510        if ($str != $a_str) {
511            $str = ilUtil::stripSlashes(str_replace("<", "< ", $a_str));
512        }
513        return $str;
514    }
515
516    /**
517     * Get label "for" attribute value for filter
518     * @return string
519     */
520    public function getTableFilterLabelFor() {
521        return $this->getFieldId();
522    }
523
524    /**
525     * Get label "for" attribute value for form
526     * @return string
527     */
528    public function getFormLabelFor() {
529        return $this->getFieldId();
530    }
531
532}
533