ILIAS  release_4-3 Revision
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilImageWizardInputGUI.php
Go to the documentation of this file.
1 <?php
2 /*
3  +-----------------------------------------------------------------------------+
4  | ILIAS open source |
5  +-----------------------------------------------------------------------------+
6  | Copyright (c) 1998-2007 ILIAS open source, University of Cologne |
7  | |
8  | This program is free software; you can redistribute it and/or |
9  | modify it under the terms of the GNU General Public License |
10  | as published by the Free Software Foundation; either version 2 |
11  | of the License, or (at your option) any later version. |
12  | |
13  | This program is distributed in the hope that it will be useful, |
14  | but WITHOUT ANY WARRANTY; without even the implied warranty of |
15  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16  | GNU General Public License for more details. |
17  | |
18  | You should have received a copy of the GNU General Public License |
19  | along with this program; if not, write to the Free Software |
20  | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
21  +-----------------------------------------------------------------------------+
22 */
23 
24 require_once 'Services/Form/classes/class.ilTextInputGUI.php';
25 
34 {
35  protected $values = array();
36  protected $allowMove = false;
37  protected $qstObject = null;
38  protected $suffixes = array();
39 
46  function __construct($a_title = "", $a_postvar = "")
47  {
48  parent::__construct($a_title, $a_postvar);
49  $this->setSuffixes(array("jpg", "jpeg", "png", "gif"));
50  $this->setSize('25');
51  $this->validationRegexp = "";
52  }
53 
59  function setValue($a_value)
60  {
61  $this->values = array();
62  if (is_array($a_value))
63  {
64  if (is_array($a_value['count']))
65  {
66  foreach ($a_value['count'] as $index => $value)
67  {
68  array_push($this->values, $a_value['imagename'][$index]);
69  }
70  }
71  }
72  }
73 
79  function setSuffixes($a_suffixes)
80  {
81  $this->suffixes = $a_suffixes;
82  }
83 
89  function getSuffixes()
90  {
91  return $this->suffixes;
92  }
93 
99  function setValues($a_values)
100  {
101  $this->values = $a_values;
102  }
103 
109  function getValues()
110  {
111  return $this->values;
112  }
113 
119  function setQuestionObject($a_value)
120  {
121  $this->qstObject =& $a_value;
122  }
123 
129  function getQuestionObject()
130  {
131  return $this->qstObject;
132  }
133 
139  function setAllowMove($a_allow_move)
140  {
141  $this->allowMove = $a_allow_move;
142  }
143 
149  function getAllowMove()
150  {
151  return $this->allowMove;
152  }
153 
159  function checkInput()
160  {
161  global $lng;
162 
163  if (is_array($_POST[$this->getPostVar()])) $_POST[$this->getPostVar()] = ilUtil::stripSlashesRecursive($_POST[$this->getPostVar()]);
164  if (is_array($_FILES[$this->getPostVar()]['error']['image']))
165  {
166  foreach ($_FILES[$this->getPostVar()]['error']['image'] as $index => $error)
167  {
168  // error handling
169  if ($error > 0)
170  {
171  switch ($error)
172  {
173  case UPLOAD_ERR_INI_SIZE:
174  $this->setAlert($lng->txt("form_msg_file_size_exceeds"));
175  return false;
176  break;
177 
178  case UPLOAD_ERR_FORM_SIZE:
179  $this->setAlert($lng->txt("form_msg_file_size_exceeds"));
180  return false;
181  break;
182 
183  case UPLOAD_ERR_PARTIAL:
184  $this->setAlert($lng->txt("form_msg_file_partially_uploaded"));
185  return false;
186  break;
187 
188  case UPLOAD_ERR_NO_FILE:
189  if ($this->getRequired())
190  {
191  if (!strlen($_POST[$this->getPostVar()]['imagename'][$index]))
192  {
193  $this->setAlert($lng->txt("form_msg_file_no_upload"));
194  return false;
195  }
196  }
197  break;
198 
199  case UPLOAD_ERR_NO_TMP_DIR:
200  $this->setAlert($lng->txt("form_msg_file_missing_tmp_dir"));
201  return false;
202  break;
203 
204  case UPLOAD_ERR_CANT_WRITE:
205  $this->setAlert($lng->txt("form_msg_file_cannot_write_to_disk"));
206  return false;
207  break;
208 
209  case UPLOAD_ERR_EXTENSION:
210  $this->setAlert($lng->txt("form_msg_file_upload_stopped_ext"));
211  return false;
212  break;
213  }
214  }
215  }
216  }
217  else
218  {
219  if ($this->getRequired())
220  {
221  $this->setAlert($lng->txt("form_msg_file_no_upload"));
222  return false;
223  }
224  }
225 
226  if (is_array($_FILES[$this->getPostVar()]['tmp_name']['image']))
227  {
228  foreach ($_FILES[$this->getPostVar()]['tmp_name']['image'] as $index => $tmpname)
229  {
230  $filename = $_FILES[$this->getPostVar()]['name']['image'][$index];
231  $filename_arr = pathinfo($filename);
232  $suffix = $filename_arr["extension"];
233  $mimetype = $_FILES[$this->getPostVar()]['type']['image'][$index];
234  $size_bytes = $_FILES[$this->getPostVar()]['size']['image'][$index];
235  // check suffixes
236  if (strlen($tmpname) && is_array($this->getSuffixes()))
237  {
238  if (!in_array(strtolower($suffix), $this->getSuffixes()))
239  {
240  $this->setAlert($lng->txt("form_msg_file_wrong_file_type"));
241  return false;
242  }
243  }
244  }
245  }
246 
247  if (is_array($_FILES[$this->getPostVar()]['tmp_name']['image']))
248  {
249  foreach ($_FILES[$this->getPostVar()]['tmp_name']['image'] as $index => $tmpname)
250  {
251  $filename = $_FILES[$this->getPostVar()]['name']['image'][$index];
252  $filename_arr = pathinfo($filename);
253  $suffix = $filename_arr["extension"];
254  $mimetype = $_FILES[$this->getPostVar()]['type']['image'][$index];
255  $size_bytes = $_FILES[$this->getPostVar()]['size']['image'][$index];
256  // virus handling
257  if (strlen($tmpname))
258  {
259  $vir = ilUtil::virusHandling($tmpname, $filename);
260  if ($vir[0] == false)
261  {
262  $this->setAlert($lng->txt("form_msg_file_virus_found")."<br />".$vir[1]);
263  return false;
264  }
265  }
266  }
267  }
268 
269  return $this->checkSubItemsInput();
270  }
271 
277  function insert(&$a_tpl)
278  {
279  global $lng;
280 
281  $tpl = new ilTemplate("tpl.prop_imagewizardinput.html", true, true, "Modules/TestQuestionPool");
282  $i = 0;
283  foreach ($this->values as $value)
284  {
285  if (strlen($value))
286  {
287  $imagename = $this->qstObject->getImagePathWeb() . $value;
288  if ($this->qstObject->getThumbSize())
289  {
290  if (@file_exists($this->qstObject->getImagePath() . $this->qstObject->getThumbPrefix() . $value))
291  {
292  $imagename = $this->qstObject->getImagePathWeb() . $this->qstObject->getThumbPrefix() . $value;
293  }
294  }
295  $tpl->setCurrentBlock('image');
296  $tpl->setVariable('SRC_IMAGE', $imagename);
297  $tpl->setVariable('IMAGE_NAME', $value);
298  $tpl->setVariable('ALT_IMAGE', ilUtil::prepareFormOutput($value));
299  $tpl->setVariable("TXT_DELETE_EXISTING", $lng->txt("delete_existing_file"));
300  $tpl->setVariable("IMAGE_ROW_NUMBER", $i);
301  $tpl->setVariable("IMAGE_POST_VAR", $this->getPostVar());
302  $tpl->parseCurrentBlock();
303  }
304  $tpl->setCurrentBlock('addimage');
305  $tpl->setVariable("IMAGE_ID", $this->getPostVar() . "[image][$i]");
306  $tpl->setVariable("IMAGE_SUBMIT", $lng->txt("upload"));
307  $tpl->setVariable("IMAGE_ROW_NUMBER", $i);
308  $tpl->setVariable("IMAGE_POST_VAR", $this->getPostVar());
309  $tpl->parseCurrentBlock();
310 
311  if ($this->getAllowMove())
312  {
313  $tpl->setCurrentBlock("move");
314  $tpl->setVariable("CMD_UP", "cmd[up" . $this->getFieldId() . "][$i]");
315  $tpl->setVariable("CMD_DOWN", "cmd[down" . $this->getFieldId() . "][$i]");
316  $tpl->setVariable("ID", $this->getPostVar() . "[$i]");
317  $tpl->setVariable("UP_BUTTON", ilUtil::getImagePath('a_up.png'));
318  $tpl->setVariable("DOWN_BUTTON", ilUtil::getImagePath('a_down.png'));
319  $tpl->parseCurrentBlock();
320  }
321  $tpl->setCurrentBlock("row");
322  $class = ($i % 2 == 0) ? "even" : "odd";
323  if ($i == 0) $class .= " first";
324  if ($i == count($this->values)-1) $class .= " last";
325  $tpl->setVariable("ROW_CLASS", $class);
326  $tpl->setVariable("POST_VAR", $this->getPostVar());
327  $tpl->setVariable("ROW_NUMBER", $i);
328  $tpl->setVariable("ID", $this->getPostVar() . "[answer][$i]");
329  $tpl->setVariable("CMD_ADD", "cmd[add" . $this->getFieldId() . "][$i]");
330  $tpl->setVariable("CMD_REMOVE", "cmd[remove" . $this->getFieldId() . "][$i]");
331  $tpl->setVariable("ADD_BUTTON", ilUtil::getImagePath('edit_add.png'));
332  $tpl->setVariable("REMOVE_BUTTON", ilUtil::getImagePath('edit_remove.png'));
333  $tpl->parseCurrentBlock();
334  $i++;
335  }
336 
337  if (is_array($this->getSuffixes()))
338  {
339  $suff_str = $delim = "";
340  foreach($this->getSuffixes() as $suffix)
341  {
342  $suff_str.= $delim.".".$suffix;
343  $delim = ", ";
344  }
345  $tpl->setCurrentBlock('allowed_image_suffixes');
346  $tpl->setVariable("TXT_ALLOWED_SUFFIXES", $lng->txt("file_allowed_suffixes")." ".$suff_str);
347  $tpl->parseCurrentBlock();
348  }
349  /*
350  $tpl->setCurrentBlock("image_heading");
351  $tpl->setVariable("ANSWER_IMAGE", $lng->txt('answer_image'));
352  $tpl->parseCurrentBlock();
353  */
354 
355  $tpl->setVariable("TXT_MAX_SIZE", ilUtil::getFileSizeInfo());
356  $tpl->setVariable("ELEMENT_ID", $this->getPostVar());
357  $tpl->setVariable("TEXT_YES", $lng->txt('yes'));
358  $tpl->setVariable("TEXT_NO", $lng->txt('no'));
359  $tpl->setVariable("DELETE_IMAGE_HEADER", $lng->txt('delete_image_header'));
360  $tpl->setVariable("DELETE_IMAGE_QUESTION", $lng->txt('delete_image_question'));
361  $tpl->setVariable("ANSWER_TEXT", $lng->txt('answer_text'));
362  $tpl->setVariable("COMMANDS_TEXT", $lng->txt('actions'));
363 
364  $a_tpl->setCurrentBlock("prop_generic");
365  $a_tpl->setVariable("PROP_GENERIC", $tpl->get());
366  $a_tpl->parseCurrentBlock();
367 
368  global $tpl;
369  include_once "./Services/YUI/classes/class.ilYuiUtil.php";
371  $tpl->addJavascript("./Modules/TestQuestionPool/templates/default/imagewizard.js");
372  }
373 }