1<?php
2/* Copyright (c) 1998-2013 ILIAS open source, Extended GPL, see docs/LICENSE */
3
4require_once 'Services/Form/classes/class.ilTextInputGUI.php';
5require_once 'Services/UIComponent/Glyph/classes/class.ilGlyphGUI.php';
6
7/**
8* This class represents a single choice wizard property in a property form.
9*
10* @author Helmut Schottmüller <ilias@aurealis.de>
11* @version $Id$
12* @ingroup	ServicesForm
13*/
14class ilImageWizardInputGUI extends ilTextInputGUI
15{
16    protected $values = array();
17    protected $allowMove = false;
18    protected $qstObject = null;
19    protected $suffixes = array();
20
21    /**
22    * Constructor
23    *
24    * @param	string	$a_title	Title
25    * @param	string	$a_postvar	Post Variable
26    */
27    public function __construct($a_title = "", $a_postvar = "")
28    {
29        parent::__construct($a_title, $a_postvar);
30        $this->setSuffixes(assQuestion::getAllowedImageMaterialFileExtensions());
31        $this->setSize('25');
32        $this->validationRegexp = "";
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['count'])) {
45                foreach ($a_value['count'] as $index => $value) {
46                    array_push($this->values, $a_value['imagename'][$index]);
47                }
48            }
49        }
50    }
51
52    /**
53    * Set Accepted Suffixes.
54    *
55    * @param	array	$a_suffixes	Accepted Suffixes
56    */
57    public function setSuffixes($a_suffixes)
58    {
59        $this->suffixes = $a_suffixes;
60    }
61
62    /**
63    * Get Accepted Suffixes.
64    *
65    * @return	array	Accepted Suffixes
66    */
67    public function getSuffixes()
68    {
69        return $this->suffixes;
70    }
71
72    /**
73    * Set Values
74    *
75    * @param	array	$a_value	Value
76    */
77    public function setValues($a_values)
78    {
79        $this->values = $a_values;
80    }
81
82    /**
83    * Get Values
84    *
85    * @return	array	Values
86    */
87    public function getValues()
88    {
89        return $this->values;
90    }
91
92    /**
93    * Set question object
94    *
95    * @param	object	$a_value	test object
96    */
97    public function setQuestionObject($a_value)
98    {
99        $this->qstObject = &$a_value;
100    }
101
102    /**
103    * Get question object
104    *
105    * @return	object	Value
106    */
107    public function getQuestionObject()
108    {
109        return $this->qstObject;
110    }
111
112    /**
113    * Set allow move
114    *
115    * @param	boolean	$a_allow_move Allow move
116    */
117    public function setAllowMove($a_allow_move)
118    {
119        $this->allowMove = $a_allow_move;
120    }
121
122    /**
123    * Get allow move
124    *
125    * @return	boolean	Allow move
126    */
127    public function getAllowMove()
128    {
129        return $this->allowMove;
130    }
131
132    /**
133    * Check input, strip slashes etc. set alert, if input is not ok.
134    *
135    * @return	boolean		Input ok, true/false
136    */
137    public function checkInput()
138    {
139        global $DIC;
140        $lng = $DIC['lng'];
141
142        if (is_array($_POST[$this->getPostVar()])) {
143            $_POST[$this->getPostVar()] = ilUtil::stripSlashesRecursive($_POST[$this->getPostVar()]);
144        }
145        if (is_array($_FILES[$this->getPostVar()]['error']['image'])) {
146            foreach ($_FILES[$this->getPostVar()]['error']['image'] as $index => $error) {
147                // error handling
148                if ($error > 0) {
149                    switch ($error) {
150                        case UPLOAD_ERR_INI_SIZE:
151                            $this->setAlert($lng->txt("form_msg_file_size_exceeds"));
152                            return false;
153                            break;
154
155                        case UPLOAD_ERR_FORM_SIZE:
156                            $this->setAlert($lng->txt("form_msg_file_size_exceeds"));
157                            return false;
158                            break;
159
160                        case UPLOAD_ERR_PARTIAL:
161                            $this->setAlert($lng->txt("form_msg_file_partially_uploaded"));
162                            return false;
163                            break;
164
165                        case UPLOAD_ERR_NO_FILE:
166                            if ($this->getRequired()) {
167                                if (!strlen($_POST[$this->getPostVar()]['imagename'][$index])) {
168                                    $this->setAlert($lng->txt("form_msg_file_no_upload"));
169                                    return false;
170                                }
171                            }
172                            break;
173
174                        case UPLOAD_ERR_NO_TMP_DIR:
175                            $this->setAlert($lng->txt("form_msg_file_missing_tmp_dir"));
176                            return false;
177                            break;
178
179                        case UPLOAD_ERR_CANT_WRITE:
180                            $this->setAlert($lng->txt("form_msg_file_cannot_write_to_disk"));
181                            return false;
182                            break;
183
184                        case UPLOAD_ERR_EXTENSION:
185                            $this->setAlert($lng->txt("form_msg_file_upload_stopped_ext"));
186                            return false;
187                            break;
188                    }
189                }
190            }
191        } else {
192            if ($this->getRequired()) {
193                $this->setAlert($lng->txt("form_msg_file_no_upload"));
194                return false;
195            }
196        }
197
198        if (is_array($_FILES[$this->getPostVar()]['tmp_name']['image'])) {
199            foreach ($_FILES[$this->getPostVar()]['tmp_name']['image'] as $index => $tmpname) {
200                $filename = $_FILES[$this->getPostVar()]['name']['image'][$index];
201                $filename_arr = pathinfo($filename);
202                $suffix = $filename_arr["extension"];
203                $mimetype = $_FILES[$this->getPostVar()]['type']['image'][$index];
204                $size_bytes = $_FILES[$this->getPostVar()]['size']['image'][$index];
205                // check suffixes
206                if (strlen($tmpname) && is_array($this->getSuffixes())) {
207                    if (!in_array(strtolower($suffix), $this->getSuffixes())) {
208                        $this->setAlert($lng->txt("form_msg_file_wrong_file_type"));
209                        return false;
210                    }
211                }
212            }
213        }
214
215        if (is_array($_FILES[$this->getPostVar()]['tmp_name']['image'])) {
216            foreach ($_FILES[$this->getPostVar()]['tmp_name']['image'] as $index => $tmpname) {
217                $filename = $_FILES[$this->getPostVar()]['name']['image'][$index];
218                $filename_arr = pathinfo($filename);
219                $suffix = $filename_arr["extension"];
220                $mimetype = $_FILES[$this->getPostVar()]['type']['image'][$index];
221                $size_bytes = $_FILES[$this->getPostVar()]['size']['image'][$index];
222                // virus handling
223                if (strlen($tmpname)) {
224                    $vir = ilUtil::virusHandling($tmpname, $filename);
225                    if ($vir[0] == false) {
226                        $this->setAlert($lng->txt("form_msg_file_virus_found") . "<br />" . $vir[1]);
227                        return false;
228                    }
229                }
230            }
231        }
232
233        return $this->checkSubItemsInput();
234    }
235
236    /**
237    * Insert property html
238    *
239    * @return	int	Size
240    */
241    public function insert($a_tpl)
242    {
243        global $DIC;
244        $lng = $DIC['lng'];
245
246        $tpl = new ilTemplate("tpl.prop_imagewizardinput.html", true, true, "Modules/TestQuestionPool");
247        $i = 0;
248        foreach ($this->values as $value) {
249            if (strlen($value)) {
250                $imagename = $this->qstObject->getImagePathWeb() . $value;
251                if ($this->qstObject->getThumbSize()) {
252                    if (@file_exists($this->qstObject->getImagePath() . $this->qstObject->getThumbPrefix() . $value)) {
253                        $imagename = $this->qstObject->getImagePathWeb() . $this->qstObject->getThumbPrefix() . $value;
254                    }
255                }
256                $tpl->setCurrentBlock('image');
257                $tpl->setVariable('SRC_IMAGE', $imagename);
258                $tpl->setVariable('IMAGE_NAME', $value);
259                $tpl->setVariable('ALT_IMAGE', ilUtil::prepareFormOutput($value));
260                $tpl->setVariable("TXT_DELETE_EXISTING", $lng->txt("delete_existing_file"));
261                $tpl->setVariable("IMAGE_ROW_NUMBER", $i);
262                $tpl->setVariable("IMAGE_POST_VAR", $this->getPostVar());
263                $tpl->parseCurrentBlock();
264            }
265            $tpl->setCurrentBlock('addimage');
266            $tpl->setVariable("IMAGE_BROWSE", $lng->txt('select_file'));
267            $tpl->setVariable("IMAGE_ID", $this->getPostVar() . "[image][$i]");
268            $tpl->setVariable("IMAGE_SUBMIT", $lng->txt("upload"));
269            $tpl->setVariable("IMAGE_ROW_NUMBER", $i);
270            $tpl->setVariable("IMAGE_POST_VAR", $this->getPostVar());
271            $tpl->parseCurrentBlock();
272
273            if ($this->getAllowMove()) {
274                $tpl->setCurrentBlock("move");
275                $tpl->setVariable("CMD_UP", "cmd[up" . $this->getFieldId() . "][$i]");
276                $tpl->setVariable("CMD_DOWN", "cmd[down" . $this->getFieldId() . "][$i]");
277                $tpl->setVariable("ID", $this->getPostVar() . "[$i]");
278                $tpl->setVariable("UP_BUTTON", ilGlyphGUI::get(ilGlyphGUI::UP));
279                $tpl->setVariable("DOWN_BUTTON", ilGlyphGUI::get(ilGlyphGUI::DOWN));
280                $tpl->parseCurrentBlock();
281            }
282            $tpl->setCurrentBlock("row");
283            $tpl->setVariable("POST_VAR", $this->getPostVar());
284            $tpl->setVariable("ROW_NUMBER", $i);
285            $tpl->setVariable("ID", $this->getPostVar() . "[answer][$i]");
286            $tpl->setVariable("CMD_ADD", "cmd[add" . $this->getFieldId() . "][$i]");
287            $tpl->setVariable("CMD_REMOVE", "cmd[remove" . $this->getFieldId() . "][$i]");
288            $tpl->setVariable("ADD_BUTTON", ilGlyphGUI::get(ilGlyphGUI::ADD));
289            $tpl->setVariable("REMOVE_BUTTON", ilGlyphGUI::get(ilGlyphGUI::REMOVE));
290            $tpl->parseCurrentBlock();
291            $i++;
292        }
293
294        if (is_array($this->getSuffixes())) {
295            $suff_str = $delim = "";
296            foreach ($this->getSuffixes() as $suffix) {
297                $suff_str .= $delim . "." . $suffix;
298                $delim = ", ";
299            }
300            $tpl->setCurrentBlock('allowed_image_suffixes');
301            $tpl->setVariable("TXT_ALLOWED_SUFFIXES", $lng->txt("file_allowed_suffixes") . " " . $suff_str);
302            $tpl->parseCurrentBlock();
303        }
304        /*
305        $tpl->setCurrentBlock("image_heading");
306        $tpl->setVariable("ANSWER_IMAGE", $lng->txt('answer_image'));
307        $tpl->parseCurrentBlock();
308        */
309
310        $tpl->setVariable("TXT_MAX_SIZE", ilUtil::getFileSizeInfo());
311        $tpl->setVariable("ELEMENT_ID", $this->getPostVar());
312        $tpl->setVariable("TEXT_YES", $lng->txt('yes'));
313        $tpl->setVariable("TEXT_NO", $lng->txt('no'));
314        $tpl->setVariable("DELETE_IMAGE_HEADER", $lng->txt('delete_image_header'));
315        $tpl->setVariable("DELETE_IMAGE_QUESTION", $lng->txt('delete_image_question'));
316        $tpl->setVariable("ANSWER_TEXT", $lng->txt('answer_text'));
317        $tpl->setVariable("COMMANDS_TEXT", $lng->txt('actions'));
318
319        $a_tpl->setCurrentBlock("prop_generic");
320        $a_tpl->setVariable("PROP_GENERIC", $tpl->get());
321        $a_tpl->parseCurrentBlock();
322
323        global $DIC;
324        $tpl = $DIC['tpl'];
325        $tpl->addJavascript("./Services/Form/js/ServiceFormWizardInput.js");
326        $tpl->addJavascript("./Modules/TestQuestionPool/templates/default/imagewizard.js");
327    }
328}
329