1<?php
2/* Copyright (c) 1998-2009 ILIAS open source, Extended GPL, see docs/LICENSE */
3
4/**
5 * Input GUI for the configuration of select input elements. E.g course custum field,
6 * udf field, ...
7 *
8 * @author Stefan Meyer <smeyer.ilias@gmx.de>
9 * @version $Id$
10 * @ingroup	ServicesForm
11 */
12class ilSelectBuilderInputGUI extends ilTextWizardInputGUI
13{
14    /**
15     * @var ilTemplate
16     */
17    protected $tpl;
18
19
20    protected $open_answer_indexes = array();
21
22
23    // constructor
24    public function __construct($a_title = "", $a_postvar = "")
25    {
26        global $DIC;
27
28        $this->lng = $DIC->language();
29        $this->tpl = $DIC["tpl"];
30        parent::__construct($a_title, $a_postvar);
31    }
32
33
34    /**
35     * Get open answer indexes
36     */
37    public function getOpenAnswerIndexes()
38    {
39        return $this->open_answer_indexes;
40    }
41
42    /**
43     * Set open answer indexes
44     */
45    public function setOpenAnswerIndexes($a_indexes)
46    {
47        $this->open_answer_indexes = $a_indexes;
48    }
49
50    /**
51     * Mark an index as open answer
52     */
53    public function addOpenAnswerIndex($a_idx)
54    {
55        $this->open_answer_indexes[] = $a_idx;
56    }
57
58    /**
59     * Check if an index is an open answer index
60     * @param type $a_idx
61     * @return type
62     */
63    public function isOpenAnswerIndex($a_idx)
64    {
65        return in_array($a_idx, (array) $this->open_answer_indexes);
66    }
67
68    /**
69    * Check input, strip slashes etc. set alert, if input is not ok.
70    *
71    * @return	boolean		Input ok, true/false
72    */
73    public function checkInput()
74    {
75        $lng = $this->lng;
76
77        $foundvalues = $_POST[$this->getPostVar()];
78
79
80
81        $this->setOpenAnswerIndexes(array());
82        if (is_array($foundvalues)) {
83            foreach ($foundvalues as $idx => $value) {
84                $_POST[$this->getPostVar()][$idx] = ilUtil::stripSlashes($value);
85                if ($this->getRequired() && trim($value) == "") {
86                    $this->setAlert($lng->txt("msg_input_is_required"));
87
88                    return false;
89                } elseif (strlen($this->getValidationRegexp())) {
90                    if (!preg_match($this->getValidationRegexp(), $value)) {
91                        $this->setAlert($lng->txt("msg_wrong_format"));
92                        return false;
93                    }
94                }
95            }
96        } else {
97            $this->setAlert($lng->txt("msg_input_is_required"));
98            return false;
99        }
100
101        foreach ((array) $_POST[$this->getPostVar() . '_open'] as $oindex => $ovalue) {
102            $this->addOpenAnswerIndex($oindex);
103        }
104
105
106        return $this->checkSubItemsInput();
107    }
108
109    public function setValueByArray($a_values)
110    {
111        parent::setValueByArray($a_values);
112
113        foreach ((array) $_POST[$this->getPostVar() . '_open'] as $oindex => $ovalue) {
114            $this->addOpenAnswerIndex($oindex);
115        }
116    }
117
118    /**
119    * Insert property html
120    *
121    * @return	int	Size
122    */
123    public function insert($a_tpl)
124    {
125        $lng = $this->lng;
126
127        $tpl = new ilTemplate("tpl.prop_selectbuilder.html", true, true, "Services/Form");
128        $i = 0;
129        foreach ($this->values as $value) {
130            if (!is_string($value)) {
131                continue;
132            }
133
134            if (strlen((string) $value)) {
135                $tpl->setCurrentBlock("prop_text_propval");
136                $tpl->setVariable("PROPERTY_VALUE", ilUtil::prepareFormOutput((string) $value));
137                $tpl->parseCurrentBlock();
138            }
139            if ($this->getAllowMove()) {
140                $tpl->setCurrentBlock("move");
141                $tpl->setVariable("CMD_UP", "cmd[up" . $this->getFieldId() . "][$i]");
142                $tpl->setVariable("CMD_DOWN", "cmd[down" . $this->getFieldId() . "][$i]");
143                $tpl->setVariable("ID", $this->getFieldId() . "[$i]");
144                include_once("./Services/UIComponent/Glyph/classes/class.ilGlyphGUI.php");
145                $tpl->setVariable("UP_BUTTON", ilGlyphGUI::get(ilGlyphGUI::UP));
146                $tpl->setVariable("DOWN_BUTTON", ilGlyphGUI::get(ilGlyphGUI::DOWN));
147
148                $tpl->parseCurrentBlock();
149            }
150            $tpl->setCurrentBlock("row");
151            $tpl->setVariable("POST_VAR", $this->getPostVar() . "[$i]");
152            #$tpl->setVariable('POST_VAR_OPEN',$this->getPostVar().'[open]'.'['.$i.']');
153            $tpl->setVariable('POST_VAR_OPEN', $this->getPostVar() . '_open' . '[' . $i . ']');
154            $tpl->setVariable('POST_VAR_OPEN_ID', $this->getPostVar() . '_open[' . $i . ']');
155            $tpl->setVariable('TXT_OPEN', $lng->txt("form_open_answer"));
156
157            if ($this->isOpenAnswerIndex($i)) {
158                $tpl->setVariable('PROP_OPEN_CHECKED', 'checked="checked"');
159            }
160            if ($this->getDisabled()) {
161                $tpl->setVariable('PROP_OPEN_DISABLED', 'disabled="disabled"');
162            }
163
164            $tpl->setVariable("ID", $this->getFieldId() . "[$i]");
165            $tpl->setVariable("CMD_ADD", "cmd[add" . $this->getFieldId() . "][$i]");
166            $tpl->setVariable("CMD_REMOVE", "cmd[remove" . $this->getFieldId() . "][$i]");
167            $tpl->setVariable("SIZE", $this->getSize());
168            $tpl->setVariable("MAXLENGTH", $this->getMaxLength());
169            if ($this->getDisabled()) {
170                $tpl->setVariable(
171                    "DISABLED",
172                    " disabled=\"disabled\""
173                );
174            }
175            include_once("./Services/UIComponent/Glyph/classes/class.ilGlyphGUI.php");
176            $tpl->setVariable("ADD_BUTTON", ilGlyphGUI::get(ilGlyphGUI::ADD));
177            $tpl->setVariable("REMOVE_BUTTON", ilGlyphGUI::get(ilGlyphGUI::REMOVE));
178            $tpl->parseCurrentBlock();
179            $i++;
180        }
181        $tpl->setVariable("ELEMENT_ID", $this->getFieldId());
182
183        $a_tpl->setCurrentBlock("prop_generic");
184        $a_tpl->setVariable("PROP_GENERIC", $tpl->get());
185        $a_tpl->parseCurrentBlock();
186
187        $tpl = $this->tpl;
188        $tpl->addJavascript("./Services/Form/js/ServiceFormWizardInput.js");
189        $tpl->addJavascript("./Services/Form/templates/default/textwizard.js");
190    }
191}
192