ILIAS  Release_4_0_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 /*
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 
32 {
33  protected $values = array();
34  protected $allowMove = false;
35  protected $qstObject = null;
36  protected $suffixes = array();
37 
44  function __construct($a_title = "", $a_postvar = "")
45  {
46  parent::__construct($a_title, $a_postvar);
47  $this->setSuffixes(array("jpg", "jpeg", "png", "gif"));
48  $this->setSize('25');
49  $this->validationRegexp = "";
50  }
51 
57  function setValue($a_value)
58  {
59  $this->values = array();
60  if (is_array($a_value))
61  {
62  if (is_array($a_value['count']))
63  {
64  foreach ($a_value['count'] as $index => $value)
65  {
66  array_push($this->values, $a_value['imagename'][$index]);
67  }
68  }
69  }
70  }
71 
77  function setSuffixes($a_suffixes)
78  {
79  $this->suffixes = $a_suffixes;
80  }
81 
87  function getSuffixes()
88  {
89  return $this->suffixes;
90  }
91 
97  function setValues($a_values)
98  {
99  $this->values = $a_values;
100  }
101 
107  function getValues()
108  {
109  return $this->values;
110  }
111 
117  function setQuestionObject($a_value)
118  {
119  $this->qstObject =& $a_value;
120  }
121 
127  function getQuestionObject()
128  {
129  return $this->qstObject;
130  }
131 
137  function setAllowMove($a_allow_move)
138  {
139  $this->allowMove = $a_allow_move;
140  }
141 
147  function getAllowMove()
148  {
149  return $this->allowMove;
150  }
151 
157  function checkInput()
158  {
159  global $lng;
160 
161  include_once "./Services/AdvancedEditing/classes/class.ilObjAdvancedEditing.php";
162  if (is_array($_POST[$this->getPostVar()])) $_POST[$this->getPostVar()] = ilUtil::stripSlashesRecursive($_POST[$this->getPostVar()], false, ilObjAdvancedEditing::_getUsedHTMLTagsAsString("assessment"));
163  if (is_array($_FILES[$this->getPostVar()]['error']['image']))
164  {
165  foreach ($_FILES[$this->getPostVar()]['error']['image'] as $index => $error)
166  {
167  // error handling
168  if ($error > 0)
169  {
170  switch ($error)
171  {
172  case UPLOAD_ERR_INI_SIZE:
173  $this->setAlert($lng->txt("form_msg_file_size_exceeds"));
174  return false;
175  break;
176 
177  case UPLOAD_ERR_FORM_SIZE:
178  $this->setAlert($lng->txt("form_msg_file_size_exceeds"));
179  return false;
180  break;
181 
182  case UPLOAD_ERR_PARTIAL:
183  $this->setAlert($lng->txt("form_msg_file_partially_uploaded"));
184  return false;
185  break;
186 
187  case UPLOAD_ERR_NO_FILE:
188  if ($this->getRequired())
189  {
190  if (!strlen($_POST[$this->getPostVar()]['imagename'][$index]))
191  {
192  $this->setAlert($lng->txt("form_msg_file_no_upload"));
193  return false;
194  }
195  }
196  break;
197 
198  case UPLOAD_ERR_NO_TMP_DIR:
199  $this->setAlert($lng->txt("form_msg_file_missing_tmp_dir"));
200  return false;
201  break;
202 
203  case UPLOAD_ERR_CANT_WRITE:
204  $this->setAlert($lng->txt("form_msg_file_cannot_write_to_disk"));
205  return false;
206  break;
207 
208  case UPLOAD_ERR_EXTENSION:
209  $this->setAlert($lng->txt("form_msg_file_upload_stopped_ext"));
210  return false;
211  break;
212  }
213  }
214  }
215  }
216  else
217  {
218  if ($this->getRequired())
219  {
220  $this->setAlert($lng->txt("form_msg_file_no_upload"));
221  return false;
222  }
223  }
224 
225  if (is_array($_FILES[$this->getPostVar()]['tmp_name']['image']))
226  {
227  foreach ($_FILES[$this->getPostVar()]['tmp_name']['image'] as $index => $tmpname)
228  {
229  $filename = $_FILES[$this->getPostVar()]['name']['image'][$index];
230  $filename_arr = pathinfo($filename);
231  $suffix = $filename_arr["extension"];
232  $mimetype = $_FILES[$this->getPostVar()]['type']['image'][$index];
233  $size_bytes = $_FILES[$this->getPostVar()]['size']['image'][$index];
234  // check suffixes
235  if (strlen($tmpname) && is_array($this->getSuffixes()))
236  {
237  if (!in_array(strtolower($suffix), $this->getSuffixes()))
238  {
239  $this->setAlert($lng->txt("form_msg_file_wrong_file_type"));
240  return false;
241  }
242  }
243  }
244  }
245 
246  if (is_array($_FILES[$this->getPostVar()]['tmp_name']['image']))
247  {
248  foreach ($_FILES[$this->getPostVar()]['tmp_name']['image'] as $index => $tmpname)
249  {
250  $filename = $_FILES[$this->getPostVar()]['name']['image'][$index];
251  $filename_arr = pathinfo($filename);
252  $suffix = $filename_arr["extension"];
253  $mimetype = $_FILES[$this->getPostVar()]['type']['image'][$index];
254  $size_bytes = $_FILES[$this->getPostVar()]['size']['image'][$index];
255  // virus handling
256  if (strlen($tmpname))
257  {
258  $vir = ilUtil::virusHandling($tmpname, $filename);
259  if ($vir[0] == false)
260  {
261  $this->setAlert($lng->txt("form_msg_file_virus_found")."<br />".$vir[1]);
262  return false;
263  }
264  }
265  }
266  }
267 
268  return $this->checkSubItemsInput();
269  }
270 
276  function insert(&$a_tpl)
277  {
278  global $lng;
279 
280  $tpl = new ilTemplate("tpl.prop_imagewizardinput.html", true, true, "Modules/TestQuestionPool");
281  $i = 0;
282  foreach ($this->values as $value)
283  {
284  if (strlen($value))
285  {
286  $imagename = $this->qstObject->getImagePathWeb() . $value;
287  if ($this->qstObject->getThumbSize())
288  {
289  if (@file_exists($this->qstObject->getImagePath() . $this->qstObject->getThumbPrefix() . $value))
290  {
291  $imagename = $this->qstObject->getImagePathWeb() . $this->qstObject->getThumbPrefix() . $value;
292  }
293  }
294  $tpl->setCurrentBlock('image');
295  $tpl->setVariable('SRC_IMAGE', $imagename);
296  $tpl->setVariable('IMAGE_NAME', $value);
297  $tpl->setVariable('ALT_IMAGE', ilUtil::prepareFormOutput($value));
298  $tpl->setVariable("TXT_DELETE_EXISTING", $lng->txt("delete_existing_file"));
299  $tpl->setVariable("IMAGE_ROW_NUMBER", $i);
300  $tpl->setVariable("IMAGE_POST_VAR", $this->getPostVar());
301  $tpl->parseCurrentBlock();
302  }
303  $tpl->setCurrentBlock('addimage');
304  $tpl->setVariable("IMAGE_ID", $this->getPostVar() . "[image][$i]");
305  $tpl->setVariable("IMAGE_SUBMIT", $lng->txt("upload"));
306  $tpl->setVariable("IMAGE_ROW_NUMBER", $i);
307  $tpl->setVariable("IMAGE_POST_VAR", $this->getPostVar());
308  $tpl->parseCurrentBlock();
309 
310  if ($this->getAllowMove())
311  {
312  $tpl->setCurrentBlock("move");
313  $tpl->setVariable("CMD_UP", "cmd[up" . $this->getFieldId() . "][$i]");
314  $tpl->setVariable("CMD_DOWN", "cmd[down" . $this->getFieldId() . "][$i]");
315  $tpl->setVariable("ID", $this->getPostVar() . "[$i]");
316  $tpl->setVariable("UP_BUTTON", ilUtil::getImagePath('a_up.gif'));
317  $tpl->setVariable("DOWN_BUTTON", ilUtil::getImagePath('a_down.gif'));
318  $tpl->parseCurrentBlock();
319  }
320  $tpl->setCurrentBlock("row");
321  $class = ($i % 2 == 0) ? "even" : "odd";
322  if ($i == 0) $class .= " first";
323  if ($i == count($this->values)-1) $class .= " last";
324  $tpl->setVariable("ROW_CLASS", $class);
325  $tpl->setVariable("POST_VAR", $this->getPostVar());
326  $tpl->setVariable("ROW_NUMBER", $i);
327  $tpl->setVariable("ID", $this->getPostVar() . "[answer][$i]");
328  $tpl->setVariable("CMD_ADD", "cmd[add" . $this->getFieldId() . "][$i]");
329  $tpl->setVariable("CMD_REMOVE", "cmd[remove" . $this->getFieldId() . "][$i]");
330  $tpl->setVariable("ADD_BUTTON", ilUtil::getImagePath('edit_add.png'));
331  $tpl->setVariable("REMOVE_BUTTON", ilUtil::getImagePath('edit_remove.png'));
332  $tpl->parseCurrentBlock();
333  $i++;
334  }
335 
336  if (is_array($this->getSuffixes()))
337  {
338  $suff_str = $delim = "";
339  foreach($this->getSuffixes() as $suffix)
340  {
341  $suff_str.= $delim.".".$suffix;
342  $delim = ", ";
343  }
344  $tpl->setCurrentBlock('allowed_image_suffixes');
345  $tpl->setVariable("TXT_ALLOWED_SUFFIXES", $lng->txt("file_allowed_suffixes")." ".$suff_str);
346  $tpl->parseCurrentBlock();
347  }
348  $tpl->setCurrentBlock("image_heading");
349  $tpl->setVariable("ANSWER_IMAGE", $lng->txt('answer_image'));
350  $tpl->setVariable("TXT_MAX_SIZE", ilUtil::getFileSizeInfo());
351  $tpl->parseCurrentBlock();
352 
353  $tpl->setVariable("ELEMENT_ID", $this->getPostVar());
354  $tpl->setVariable("TEXT_YES", $lng->txt('yes'));
355  $tpl->setVariable("TEXT_NO", $lng->txt('no'));
356  $tpl->setVariable("DELETE_IMAGE_HEADER", $lng->txt('delete_image_header'));
357  $tpl->setVariable("DELETE_IMAGE_QUESTION", $lng->txt('delete_image_question'));
358  $tpl->setVariable("ANSWER_TEXT", $lng->txt('answer_text'));
359  $tpl->setVariable("COMMANDS_TEXT", $lng->txt('actions'));
360 
361  $a_tpl->setCurrentBlock("prop_generic");
362  $a_tpl->setVariable("PROP_GENERIC", $tpl->get());
363  $a_tpl->parseCurrentBlock();
364 
365  global $tpl;
366  include_once "./Services/YUI/classes/class.ilYuiUtil.php";
368  $tpl->addJavascript("./Modules/TestQuestionPool/templates/default/imagewizard.js");
369  }
370 }