ILIAS  Release_4_4_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilImageWizardInputGUI.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2013 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
4 require_once 'Services/Form/classes/class.ilTextInputGUI.php';
5 
14 {
15  protected $values = array();
16  protected $allowMove = false;
17  protected $qstObject = null;
18  protected $suffixes = array();
19 
20  protected $disable_text = false;
21  protected $disable_upload = false;
22  protected $disable_actions = false;
23 
30  function __construct($a_title = "", $a_postvar = "")
31  {
32  parent::__construct($a_title, $a_postvar);
33  $this->setSuffixes(array("jpg", "jpeg", "png", "gif"));
34  $this->setSize('25');
35  $this->validationRegexp = "";
36  }
37 
42  {
43  $this->disable_upload = $disable_upload;
44  }
45 
50  {
51  $this->disable_actions = $disable_actions;
52  }
53 
57  public function setDisableText($disable_text)
58  {
59  $this->disable_text = $disable_text;
60  }
61 
67  function setValue($a_value)
68  {
69  $this->values = array();
70  if (is_array($a_value))
71  {
72  if (is_array($a_value['count']))
73  {
74  foreach ($a_value['count'] as $index => $value)
75  {
76  array_push($this->values, $a_value['imagename'][$index]);
77  }
78  }
79  }
80  }
81 
87  function setSuffixes($a_suffixes)
88  {
89  $this->suffixes = $a_suffixes;
90  }
91 
97  function getSuffixes()
98  {
99  return $this->suffixes;
100  }
101 
107  function setValues($a_values)
108  {
109  $this->values = $a_values;
110  }
111 
117  function getValues()
118  {
119  return $this->values;
120  }
121 
127  function setQuestionObject($a_value)
128  {
129  $this->qstObject =& $a_value;
130  }
131 
137  function getQuestionObject()
138  {
139  return $this->qstObject;
140  }
141 
147  function setAllowMove($a_allow_move)
148  {
149  $this->allowMove = $a_allow_move;
150  }
151 
157  function getAllowMove()
158  {
159  return $this->allowMove;
160  }
161 
167  function checkInput()
168  {
169  global $lng;
170 
171  if (is_array($_POST[$this->getPostVar()])) $_POST[$this->getPostVar()] = ilUtil::stripSlashesRecursive($_POST[$this->getPostVar()]);
172  if (is_array($_FILES[$this->getPostVar()]['error']['image']))
173  {
174  foreach ($_FILES[$this->getPostVar()]['error']['image'] as $index => $error)
175  {
176  // error handling
177  if ($error > 0)
178  {
179  switch ($error)
180  {
181  case UPLOAD_ERR_INI_SIZE:
182  $this->setAlert($lng->txt("form_msg_file_size_exceeds"));
183  return false;
184  break;
185 
186  case UPLOAD_ERR_FORM_SIZE:
187  $this->setAlert($lng->txt("form_msg_file_size_exceeds"));
188  return false;
189  break;
190 
191  case UPLOAD_ERR_PARTIAL:
192  $this->setAlert($lng->txt("form_msg_file_partially_uploaded"));
193  return false;
194  break;
195 
196  case UPLOAD_ERR_NO_FILE:
197  if ($this->getRequired())
198  {
199  if (!strlen($_POST[$this->getPostVar()]['imagename'][$index]))
200  {
201  $this->setAlert($lng->txt("form_msg_file_no_upload"));
202  return false;
203  }
204  }
205  break;
206 
207  case UPLOAD_ERR_NO_TMP_DIR:
208  $this->setAlert($lng->txt("form_msg_file_missing_tmp_dir"));
209  return false;
210  break;
211 
212  case UPLOAD_ERR_CANT_WRITE:
213  $this->setAlert($lng->txt("form_msg_file_cannot_write_to_disk"));
214  return false;
215  break;
216 
217  case UPLOAD_ERR_EXTENSION:
218  $this->setAlert($lng->txt("form_msg_file_upload_stopped_ext"));
219  return false;
220  break;
221  }
222  }
223  }
224  }
225  else
226  {
227  if ($this->getRequired())
228  {
229  $this->setAlert($lng->txt("form_msg_file_no_upload"));
230  return false;
231  }
232  }
233 
234  if (is_array($_FILES[$this->getPostVar()]['tmp_name']['image']))
235  {
236  foreach ($_FILES[$this->getPostVar()]['tmp_name']['image'] as $index => $tmpname)
237  {
238  $filename = $_FILES[$this->getPostVar()]['name']['image'][$index];
239  $filename_arr = pathinfo($filename);
240  $suffix = $filename_arr["extension"];
241  $mimetype = $_FILES[$this->getPostVar()]['type']['image'][$index];
242  $size_bytes = $_FILES[$this->getPostVar()]['size']['image'][$index];
243  // check suffixes
244  if (strlen($tmpname) && is_array($this->getSuffixes()))
245  {
246  if (!in_array(strtolower($suffix), $this->getSuffixes()))
247  {
248  $this->setAlert($lng->txt("form_msg_file_wrong_file_type"));
249  return false;
250  }
251  }
252  }
253  }
254 
255  if (is_array($_FILES[$this->getPostVar()]['tmp_name']['image']))
256  {
257  foreach ($_FILES[$this->getPostVar()]['tmp_name']['image'] as $index => $tmpname)
258  {
259  $filename = $_FILES[$this->getPostVar()]['name']['image'][$index];
260  $filename_arr = pathinfo($filename);
261  $suffix = $filename_arr["extension"];
262  $mimetype = $_FILES[$this->getPostVar()]['type']['image'][$index];
263  $size_bytes = $_FILES[$this->getPostVar()]['size']['image'][$index];
264  // virus handling
265  if (strlen($tmpname))
266  {
267  $vir = ilUtil::virusHandling($tmpname, $filename);
268  if ($vir[0] == false)
269  {
270  $this->setAlert($lng->txt("form_msg_file_virus_found")."<br />".$vir[1]);
271  return false;
272  }
273  }
274  }
275  }
276 
277  return $this->checkSubItemsInput();
278  }
279 
285  function insert(&$a_tpl)
286  {
287  global $lng;
288 
289  $tpl = new ilTemplate("tpl.prop_imagewizardinput.html", true, true, "Modules/TestQuestionPool");
290  $i = 0;
291  foreach ($this->values as $value)
292  {
293  if (strlen($value))
294  {
295  $imagename = $this->qstObject->getImagePathWeb() . $value;
296  if ($this->qstObject->getThumbSize())
297  {
298  if (@file_exists($this->qstObject->getImagePath() . $this->qstObject->getThumbPrefix() . $value))
299  {
300  $imagename = $this->qstObject->getImagePathWeb() . $this->qstObject->getThumbPrefix() . $value;
301  }
302  }
303  $tpl->setCurrentBlock('image');
304  $tpl->setVariable('SRC_IMAGE', $imagename);
305  $tpl->setVariable('IMAGE_NAME', $value);
306  $tpl->setVariable('ALT_IMAGE', ilUtil::prepareFormOutput($value));
307  $tpl->setVariable("TXT_DELETE_EXISTING", $lng->txt("delete_existing_file"));
308  $tpl->setVariable("IMAGE_ROW_NUMBER", $i);
309  $tpl->setVariable("IMAGE_POST_VAR", $this->getPostVar());
310  if($this->disable_upload)
311  {
312  $tpl->setVariable('DISABLED_UPLOADS', ' disabled="disabled"');
313  }
314  $tpl->parseCurrentBlock();
315  }
316  $tpl->setCurrentBlock('addimage');
317  $tpl->setVariable("IMAGE_ID", $this->getPostVar() . "[image][$i]");
318  $tpl->setVariable("IMAGE_SUBMIT", $lng->txt("upload"));
319  $tpl->setVariable("IMAGE_ROW_NUMBER", $i);
320  $tpl->setVariable("IMAGE_POST_VAR", $this->getPostVar());
321  if($this->disable_upload)
322  {
323  $tpl->setVariable('DISABLED_UPLOADS', ' disabled="disabled"');
324  }
325  $tpl->parseCurrentBlock();
326 
327  if ($this->getAllowMove() && !$this->disable_actions)
328  {
329  $tpl->setCurrentBlock("move");
330  $tpl->setVariable("CMD_UP", "cmd[up" . $this->getFieldId() . "][$i]");
331  $tpl->setVariable("CMD_DOWN", "cmd[down" . $this->getFieldId() . "][$i]");
332  $tpl->setVariable("MOVE_ID", $this->getPostVar() . "[$i]");
333  $tpl->setVariable("UP_BUTTON", ilUtil::getImagePath('a_up.png'));
334  $tpl->setVariable("DOWN_BUTTON", ilUtil::getImagePath('a_down.png'));
335  $tpl->parseCurrentBlock();
336  }
337  $tpl->setCurrentBlock("row");
338  $class = ($i % 2 == 0) ? "even" : "odd";
339  if ($i == 0) $class .= " first";
340  if ($i == count($this->values)-1) $class .= " last";
341  $tpl->setVariable("ROW_CLASS", $class);
342  $tpl->setVariable("POST_VAR", $this->getPostVar());
343  $tpl->setVariable("ROW_NUMBER", $i);
344 
345  if(!$this->disable_actions)
346  {
347  //$tpl->setCurrentBlock('addremove');
348  $tpl->setVariable("ID", $this->getPostVar() . "[answer][$i]");
349  $tpl->setVariable("CMD_ADD", "cmd[add" . $this->getFieldId() . "][$i]");
350  $tpl->setVariable("CMD_REMOVE", "cmd[remove" . $this->getFieldId() . "][$i]");
351  $tpl->setVariable( "ADD_BUTTON", ilUtil::getImagePath( 'edit_add.png' ) );
352  $tpl->setVariable( "REMOVE_BUTTON", ilUtil::getImagePath( 'edit_remove.png' ) );
353  } else {
354  //$tpl->setVariable('DISABLED_ACTIONS', ' disabled="disabled"');
355  }
356 
357  $tpl->parseCurrentBlock();
358  $i++;
359  }
360 
361  if (is_array($this->getSuffixes()))
362  {
363  $suff_str = $delim = "";
364  foreach($this->getSuffixes() as $suffix)
365  {
366  $suff_str.= $delim.".".$suffix;
367  $delim = ", ";
368  }
369  $tpl->setCurrentBlock('allowed_image_suffixes');
370  $tpl->setVariable("TXT_ALLOWED_SUFFIXES", $lng->txt("file_allowed_suffixes")." ".$suff_str);
371  $tpl->parseCurrentBlock();
372  }
373  /*
374  $tpl->setCurrentBlock("image_heading");
375  $tpl->setVariable("ANSWER_IMAGE", $lng->txt('answer_image'));
376  $tpl->parseCurrentBlock();
377  */
378 
379  $tpl->setVariable("TXT_MAX_SIZE", ilUtil::getFileSizeInfo());
380  $tpl->setVariable("ELEMENT_ID", $this->getPostVar());
381  $tpl->setVariable("TEXT_YES", $lng->txt('yes'));
382  $tpl->setVariable("TEXT_NO", $lng->txt('no'));
383  $tpl->setVariable("DELETE_IMAGE_HEADER", $lng->txt('delete_image_header'));
384  $tpl->setVariable("DELETE_IMAGE_QUESTION", $lng->txt('delete_image_question'));
385  $tpl->setVariable("ANSWER_TEXT", $lng->txt('answer_text'));
386  $tpl->setVariable("COMMANDS_TEXT", $lng->txt('actions'));
387 
388  $a_tpl->setCurrentBlock("prop_generic");
389  $a_tpl->setVariable("PROP_GENERIC", $tpl->get());
390  $a_tpl->parseCurrentBlock();
391 
392  global $tpl;
393  include_once "./Services/YUI/classes/class.ilYuiUtil.php";
395  $tpl->addJavascript("./Modules/TestQuestionPool/templates/default/imagewizard.js");
396  }
397 }