1<?php
2/* Copyright (c) 1998-2013 ILIAS open source, Extended GPL, see docs/LICENSE */
3
4require_once 'Services/UIComponent/Glyph/classes/class.ilGlyphGUI.php';
5
6/**
7* This class represents a key value pair wizard property in a property form.
8*
9* @author Helmut Schottmüller <ilias@aurealis.de>
10* @version $Id$
11* @ingroup	ServicesForm
12*/
13class ilKVPWizardInputGUI extends ilTextInputGUI
14{
15    protected $values = array();
16    protected $allowMove = false;
17    protected $key_size = 20;
18    protected $value_size = 20;
19    protected $key_maxlength = 255;
20    protected $value_maxlength = 255;
21    protected $key_name = "";
22    protected $value_name = "";
23
24    /**
25    * Constructor
26    *
27    * @param	string	$a_title	Title
28    * @param	string	$a_postvar	Post Variable
29    */
30    public function __construct($a_title = "", $a_postvar = "")
31    {
32        parent::__construct($a_title, $a_postvar);
33    }
34
35    /**
36    * Set Value.
37    *
38    * @param	string	$a_value	Value
39    */
40    public function setValue($a_value)
41    {
42        $this->values = array();
43        if (is_array($a_value)) {
44            if (is_array($a_value['key'])) {
45                foreach ($a_value['key'] as $idx => $key) {
46                    array_push($this->values, array($key, $a_value['value'][$idx]));
47                }
48            }
49        }
50    }
51
52    /**
53    * Set key size.
54    *
55    * @param	integer	$a_size	Key size
56    */
57    public function setKeySize($a_size)
58    {
59        $this->key_size = $a_size;
60    }
61
62    /**
63    * Get key size.
64    *
65    * @return	integer	Key size
66    */
67    public function getKeySize()
68    {
69        return $this->key_size;
70    }
71
72    /**
73    * Set value size.
74    *
75    * @param	integer	$a_size	value size
76    */
77    public function setValueSize($a_size)
78    {
79        $this->value_size = $a_size;
80    }
81
82    /**
83    * Get value size.
84    *
85    * @return	integer	value size
86    */
87    public function getValueSize()
88    {
89        return $this->value_size;
90    }
91
92    /**
93    * Set key maxlength.
94    *
95    * @param	integer	$a_size	Key maxlength
96    */
97    public function setKeyMaxlength($a_maxlength)
98    {
99        $this->key_maxlength = $a_maxlength;
100    }
101
102    /**
103    * Get key maxlength.
104    *
105    * @return	integer	Key maxlength
106    */
107    public function getKeyMaxlength()
108    {
109        return $this->key_maxlength;
110    }
111
112    /**
113    * Set value maxlength.
114    *
115    * @param	integer	$a_size	value maxlength
116    */
117    public function setValueMaxlength($a_maxlength)
118    {
119        $this->value_maxlength = $a_maxlength;
120    }
121
122    /**
123    * Get value maxlength.
124    *
125    * @return	integer	value maxlength
126    */
127    public function getValueMaxlength()
128    {
129        return $this->value_maxlength;
130    }
131
132    /**
133    * Set value name.
134    *
135    * @param	string	$a_name	value name
136    */
137    public function setValueName($a_name)
138    {
139        $this->value_name = $a_name;
140    }
141
142    /**
143    * Get value name.
144    *
145    * @return	string	value name
146    */
147    public function getValueName()
148    {
149        return $this->value_name;
150    }
151
152    /**
153    * Set key name.
154    *
155    * @param	string	$a_name	value name
156    */
157    public function setKeyName($a_name)
158    {
159        $this->key_name = $a_name;
160    }
161
162    /**
163    * Get key name.
164    *
165    * @return	string	value name
166    */
167    public function getKeyName()
168    {
169        return $this->key_name;
170    }
171
172    /**
173    * Set Values
174    *
175    * @param	array	$a_value	Value
176    */
177    public function setValues($a_values)
178    {
179        $this->values = $a_values;
180    }
181
182    /**
183    * Get Values
184    *
185    * @return	array	Values
186    */
187    public function getValues()
188    {
189        return $this->values;
190    }
191
192    /**
193    * Set allow move
194    *
195    * @param	boolean	$a_allow_move Allow move
196    */
197    public function setAllowMove($a_allow_move)
198    {
199        $this->allowMove = $a_allow_move;
200    }
201
202    /**
203    * Get allow move
204    *
205    * @return	boolean	Allow move
206    */
207    public function getAllowMove()
208    {
209        return $this->allowMove;
210    }
211
212    /**
213    * Check input, strip slashes etc. set alert, if input is not ok.
214    *
215    * @return	boolean		Input ok, true/false
216    */
217    public function checkInput()
218    {
219        global $DIC;
220        $lng = $DIC['lng'];
221
222        if (is_array($_POST[$this->getPostVar()])) {
223            $_POST[$this->getPostVar()] = ilUtil::stripSlashesRecursive($_POST[$this->getPostVar()]);
224        }
225        $foundvalues = $_POST[$this->getPostVar()];
226        if (is_array($foundvalues)) {
227            // check answers
228            if (is_array($foundvalues['key']) && is_array($foundvalues['value'])) {
229                foreach ($foundvalues['key'] as $val) {
230                    if ($this->getRequired() && (strlen($val)) == 0) {
231                        $this->setAlert($lng->txt("msg_input_is_required"));
232                        return false;
233                    }
234                }
235                foreach ($foundvalues['value'] as $val) {
236                    if ($this->getRequired() && (strlen($val)) == 0) {
237                        $this->setAlert($lng->txt("msg_input_is_required"));
238                        return false;
239                    }
240                }
241            } else {
242                if ($this->getRequired()) {
243                    $this->setAlert($lng->txt("msg_input_is_required"));
244                    return false;
245                }
246            }
247        } else {
248            if ($this->getRequired()) {
249                $this->setAlert($lng->txt("msg_input_is_required"));
250                return false;
251            }
252        }
253        return $this->checkSubItemsInput();
254    }
255
256    /**
257    * Insert property html
258    *
259    * @return	int	Size
260    */
261    public function insert($a_tpl)
262    {
263        global $DIC;
264        $lng = $DIC['lng'];
265
266        $tpl = new ilTemplate("tpl.prop_kvpwizardinput.html", true, true, "Modules/TestQuestionPool");
267        $i = 0;
268        foreach ($this->values as $value) {
269            if (is_array($value)) {
270                $tpl->setCurrentBlock("prop_key_propval");
271                $tpl->setVariable("PROPERTY_VALUE", ilUtil::prepareFormOutput($value[0]));
272                $tpl->parseCurrentBlock();
273                $tpl->setCurrentBlock("prop_value_propval");
274                $tpl->setVariable("PROPERTY_VALUE", ilUtil::prepareFormOutput($value[1]));
275                $tpl->parseCurrentBlock();
276            }
277            if ($this->getAllowMove()) {
278                $tpl->setCurrentBlock("move");
279                $tpl->setVariable("CMD_UP", "cmd[up" . $this->getFieldId() . "][$i]");
280                $tpl->setVariable("CMD_DOWN", "cmd[down" . $this->getFieldId() . "][$i]");
281                $tpl->setVariable("ID", $this->getPostVar() . "[$i]");
282                $tpl->setVariable("UP_BUTTON", ilGlyphGUI::get(ilGlyphGUI::UP));
283                $tpl->setVariable("DOWN_BUTTON", ilGlyphGUI::get(ilGlyphGUI::DOWN));
284                $tpl->parseCurrentBlock();
285            }
286
287            $tpl->setCurrentBlock("row");
288            $class = ($i % 2 == 0) ? "even" : "odd";
289            if ($i == 0) {
290                $class .= " first";
291            }
292            if ($i == count($this->values) - 1) {
293                $class .= " last";
294            }
295            $tpl->setVariable("ROW_CLASS", $class);
296            $tpl->setVariable("ROW_NUMBER", $i);
297
298            $tpl->setVariable("KEY_SIZE", $this->getKeySize());
299            $tpl->setVariable("KEY_ID", $this->getPostVar() . "[key][$i]");
300            $tpl->setVariable("KEY_MAXLENGTH", $this->getKeyMaxlength());
301
302            $tpl->setVariable("VALUE_SIZE", $this->getValueSize());
303            $tpl->setVariable("VALUE_ID", $this->getPostVar() . "[value][$i]");
304            $tpl->setVariable("VALUE_MAXLENGTH", $this->getValueMaxlength());
305
306            $tpl->setVariable("ID", $this->getPostVar() . "[$i]");
307            $tpl->setVariable("CMD_ADD", "cmd[add" . $this->getFieldId() . "][$i]");
308            $tpl->setVariable("CMD_REMOVE", "cmd[remove" . $this->getFieldId() . "][$i]");
309            $tpl->setVariable("ADD_BUTTON", ilGlyphGUI::get(ilGlyphGUI::ADD));
310            $tpl->setVariable("REMOVE_BUTTON", ilGlyphGUI::get(ilGlyphGUI::REMOVE));
311
312            $tpl->setVariable("POST_VAR", $this->getPostVar());
313
314            $tpl->parseCurrentBlock();
315
316            $i++;
317        }
318        $tpl->setVariable("ELEMENT_ID", $this->getPostVar());
319        $tpl->setVariable("KEY_TEXT", $this->getKeyName());
320        $tpl->setVariable("VALUE_TEXT", $this->getValueName());
321
322        $a_tpl->setCurrentBlock("prop_generic");
323        $a_tpl->setVariable("PROP_GENERIC", $tpl->get());
324        $a_tpl->parseCurrentBlock();
325
326        global $DIC;
327        $tpl = $DIC['tpl'];
328        $tpl->addJavascript("./Services/Form/js/ServiceFormWizardInput.js");
329        $tpl->addJavascript("./Modules/TestQuestionPool/templates/default/kvpwizard.js");
330    }
331}
332