ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
class.ilMultipleImagesInputGUI.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.ilIdentifiedMultiValuesInputGUI.php';
5 require_once 'Services/UIComponent/Glyph/classes/class.ilGlyphGUI.php';
6 
14 {
15  const RENDERING_TEMPLATE = 'tpl.prop_multi_image_inp.html';
16 
17  const ITERATOR_SUBFIELD_NAME = 'iteratorfield';
18  const STORED_IMAGE_SUBFIELD_NAME = 'storedimage';
19  const IMAGE_UPLOAD_SUBFIELD_NAME = 'imageupload';
20 
21  const FILE_DATA_INDEX_DODGING_FILE = 'dodging_file';
25  protected $editElementOccuranceEnabled = false;
26 
30  protected $editElementOrderEnabled = false;
31 
35  protected $suffixes = array();
36 
37  protected $imageRemovalCommand = 'removeImage';
38 
39  protected $imageUploadCommand = 'uploadImage';
40 
47  function __construct($a_title = "", $a_postvar = "")
48  {
49  parent::__construct($a_title, $a_postvar);
50 
51  $this->setSuffixes(array("jpg", "jpeg", "png", "gif"));
52  $this->setSize('25');
53  $this->validationRegexp = "";
54 
55  require_once 'Services/Form/classes/class.ilMultipleImagesAdditionalIndexLevelRemover.php';
57  $manipulator->setPostVar($this->getPostVar());
58  $this->addFormValuesManipulator($manipulator);
59 
60  require_once 'Services/Form/classes/class.ilMultipleImagesFileSubmissionDataCompletion.php';
62  $manipulator->setPostVar($this->getPostVar());
63  $this->addFormValuesManipulator($manipulator);
64 
65  require_once 'Services/Form/classes/class.ilIdentifiedMultiFilesJsPositionIndexRemover.php';
67  $manipulator->setPostVar($this->getPostVar());
68  $this->addFormValuesManipulator($manipulator);
69 
70  require_once 'Services/Form/classes/class.ilMultiFilesSubmitRecursiveSlashesStripper.php';
71  $manipulator = new ilMultiFilesSubmitRecursiveSlashesStripper();
72  $manipulator->setPostVar($this->getPostVar());
73  $this->addFormValuesManipulator($manipulator);
74  }
75 
81  function setSuffixes($a_suffixes)
82  {
83  $this->suffixes = $a_suffixes;
84  }
85 
91  function getSuffixes()
92  {
93  return $this->suffixes;
94  }
95 
99  public function getImageRemovalCommand()
100  {
102  }
103 
108  {
109  $this->imageRemovalCommand = $imageRemovalCommand;
110  }
111 
115  public function getImageUploadCommand()
116  {
118  }
119 
124  {
125  $this->imageUploadCommand = $imageUploadCommand;
126  }
127 
132  {
134  }
135 
140  {
141  $this->editElementOccuranceEnabled = $editElementOccuranceEnabled;
142  }
143 
147  public function isEditElementOrderEnabled()
148  {
150  }
151 
156  {
157  $this->editElementOrderEnabled = $editElementOrderEnabled;
158  }
159 
164  abstract protected function isValidFilenameInput($filenameInput);
165 
171  function onCheckInput()
172  {
173  $lng = $GLOBALS['DIC'] ? $GLOBALS['DIC']['lng'] : $GLOBALS['lng'];
174  $F = $_FILES[$this->getPostVar()];
175 
176  if( $this->getRequired() && !is_array($F['error']) )
177  {
178  $this->setAlert($lng->txt("form_msg_file_no_upload"));
179  return false;
180  }
181  else foreach( $F['error'] as $index => $error )
182  {
183  // error handling
184  if ($error > 0)
185  {
186  switch ($error)
187  {
188  case UPLOAD_ERR_INI_SIZE:
189  $this->setAlert($lng->txt("form_msg_file_size_exceeds"));
190  return false;
191  break;
192 
193  case UPLOAD_ERR_FORM_SIZE:
194  $this->setAlert($lng->txt("form_msg_file_size_exceeds"));
195  return false;
196  break;
197 
198  case UPLOAD_ERR_PARTIAL:
199  $this->setAlert($lng->txt("form_msg_file_partially_uploaded"));
200  return false;
201  break;
202 
203  case UPLOAD_ERR_NO_FILE:
204  if( !$this->getRequired() )
205  {
206  break;
207  }
208  elseif( strlen($F[self::FILE_DATA_INDEX_DODGING_FILE][$index]) )
209  {
210  break;
211  }
212  $this->setAlert($lng->txt("form_msg_file_no_upload"));
213  return false;
214  break;
215 
216  case UPLOAD_ERR_NO_TMP_DIR:
217  $this->setAlert($lng->txt("form_msg_file_missing_tmp_dir"));
218  return false;
219  break;
220 
221  case UPLOAD_ERR_CANT_WRITE:
222  $this->setAlert($lng->txt("form_msg_file_cannot_write_to_disk"));
223  return false;
224  break;
225 
226  case UPLOAD_ERR_EXTENSION:
227  $this->setAlert($lng->txt("form_msg_file_upload_stopped_ext"));
228  return false;
229  break;
230  }
231  }
232  }
233 
234  if (is_array($F['tmp_name']))
235  {
236  foreach ($F['tmp_name'] as $index => $tmpname)
237  {
238  $filename = $F['name'][$index];
239  $filename_arr = pathinfo($filename);
240  $suffix = $filename_arr["extension"];
241  $mimetype = $F['type'][$index];
242  $size_bytes = $F['size'][$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  foreach ($F['tmp_name'] as $index => $tmpname)
256  {
257  $filename = $F['name'][$index];
258  $filename_arr = pathinfo($filename);
259  $suffix = $filename_arr["extension"];
260  $mimetype = $F['type'][$index];
261  $size_bytes = $F['size'][$index];
262  // virus handling
263  if (strlen($tmpname))
264  {
265  $vir = ilUtil::virusHandling($tmpname, $filename);
266  if ($vir[0] == false)
267  {
268  $this->setAlert($lng->txt("form_msg_file_virus_found")."<br />".$vir[1]);
269  return false;
270  }
271  }
272  }
273 
274  return $this->checkSubItemsInput();
275  }
276 
281  public function render($a_mode = "")
282  {
283  global $lng;
284 
285  $tpl = $this->getTemplate();
286  $i = 0;
287  foreach ($this->getIdentifiedMultiValues() as $identifier => $value)
288  {
289  if( $this->valueHasContentImageSource($value) )
290  {
291  $tpl->setCurrentBlock('image');
292 
293  $tpl->setVariable('STORED_IMAGE_SRC', $this->fetchContentImageSourceFromValue($value));
294  $tpl->setVariable('STORED_IMAGE_ALT', ilUtil::prepareFormOutput($this->fetchContentImageTitleFromValue($value)));
295  $tpl->setVariable('STORED_IMAGE_FILENAME', $this->fetchContentImageTitleFromValue($value));
296  $tpl->setVariable("STORED_IMAGE_POST_VAR", $this->getMultiValuePostVarSubFieldPosIndexed($identifier, self::STORED_IMAGE_SUBFIELD_NAME, $i));
297 
298  $tpl->setVariable("TXT_DELETE_EXISTING", $lng->txt("delete_existing_file"));
299  $tpl->setVariable("IMAGE_CMD_REMOVE", $this->buildMultiValueSubmitVar($identifier, $i, $this->getImageRemovalCommand()));
300 
301  $tpl->parseCurrentBlock();
302  }
303 
304  $tpl->setCurrentBlock('addimage');
305 
306  $tpl->setVariable("IMAGE_BROWSE", $lng->txt('select_file'));
307  $tpl->setVariable("IMAGE_ID", $this->getMultiValuePosIndexedSubFieldId($identifier, self::IMAGE_UPLOAD_SUBFIELD_NAME, $i));
308  $tpl->setVariable("TXT_IMAGE_SUBMIT", $lng->txt("upload"));
309  $tpl->setVariable("IMAGE_CMD_UPLOAD", $this->buildMultiValueSubmitVar($identifier, $i, $this->getImageUploadCommand()));
310  $tpl->setVariable("UPLOAD_IMAGE_POST_VAR", $this->getMultiValuePostVarSubFieldPosIndexed($identifier, self::IMAGE_UPLOAD_SUBFIELD_NAME, $i));
311  $tpl->setVariable("COUNT_POST_VAR", $this->getMultiValuePostVarSubFieldPosIndexed($identifier, self::ITERATOR_SUBFIELD_NAME, $i));
312 
313  $tpl->parseCurrentBlock();
314 
315  if( $this->isEditElementOrderEnabled() )
316  {
317  $tpl->setCurrentBlock("move");
318  $tpl->setVariable("ID_UP", $this->getMultiValuePosIndexedSubFieldId($identifier, 'up', $i));
319  $tpl->setVariable("ID_DOWN", $this->getMultiValuePosIndexedSubFieldId($identifier, 'down', $i));
320  $tpl->setVariable("CMD_UP", $this->buildMultiValueSubmitVar($identifier, $i, 'up'));
321  $tpl->setVariable("CMD_DOWN", $this->buildMultiValueSubmitVar($identifier, $i, 'down'));
322  $tpl->setVariable("UP_BUTTON", ilGlyphGUI::get(ilGlyphGUI::UP));
323  $tpl->setVariable("DOWN_BUTTON", ilGlyphGUI::get(ilGlyphGUI::DOWN));
324  $tpl->parseCurrentBlock();
325  }
326 
327  if( $this->isEditElementOccuranceEnabled() )
328  {
329  $tpl->setCurrentBlock("row");
330  $tpl->setVariable("ID_ADD", $this->getMultiValuePosIndexedSubFieldId($identifier, 'add', $i));
331  $tpl->setVariable("ID_REMOVE", $this->getMultiValuePosIndexedSubFieldId($identifier, 'remove', $i));
332  $tpl->setVariable("CMD_ADD", $this->buildMultiValueSubmitVar($identifier, $i, 'add'));
333  $tpl->setVariable("CMD_REMOVE", $this->buildMultiValueSubmitVar($identifier, $i, 'remove'));
334  $tpl->setVariable("ADD_BUTTON", ilGlyphGUI::get(ilGlyphGUI::ADD));
335  $tpl->setVariable("REMOVE_BUTTON", ilGlyphGUI::get(ilGlyphGUI::REMOVE));
336  $tpl->parseCurrentBlock();
337  }
338 
339  $i++;
340  }
341 
342  if (is_array($this->getSuffixes()))
343  {
344  $suff_str = $delim = "";
345  foreach($this->getSuffixes() as $suffix)
346  {
347  $suff_str.= $delim.".".$suffix;
348  $delim = ", ";
349  }
350  $tpl->setCurrentBlock('allowed_image_suffixes');
351  $tpl->setVariable("TXT_ALLOWED_SUFFIXES", $lng->txt("file_allowed_suffixes")." ".$suff_str);
352  $tpl->parseCurrentBlock();
353  }
354  /*
355  $tpl->setCurrentBlock("image_heading");
356  $tpl->setVariable("ANSWER_IMAGE", $lng->txt('answer_image'));
357  $tpl->parseCurrentBlock();
358  */
359 
360  $tpl->setVariable("TXT_MAX_SIZE", ilUtil::getFileSizeInfo());
361  $tpl->setVariable("ELEMENT_ID", $this->getPostVar());
362  $tpl->setVariable("TEXT_YES", $lng->txt('yes'));
363  $tpl->setVariable("TEXT_NO", $lng->txt('no'));
364  $tpl->setVariable("DELETE_IMAGE_HEADER", $lng->txt('delete_image_header'));
365  $tpl->setVariable("DELETE_IMAGE_QUESTION", $lng->txt('delete_image_question'));
366  $tpl->setVariable("ANSWER_TEXT", $lng->txt('answer_text'));
367  $tpl->setVariable("COMMANDS_TEXT", $lng->txt('actions'));
368 
369  if (!$this->getDisabled())
370  {
371  $tpl->setCurrentBlock('js_engine_initialisation');
372  $tpl->setVariable('UPLOAD_CMD', $this->getImageUploadCommand());
373  $tpl->setVariable('REMOVE_CMD', $this->getImageRemovalCommand());
374  $tpl->setVariable('ITERATOR', self::ITERATOR_SUBFIELD_NAME);
375  $tpl->setVariable('STORED_IMAGE_POSTVAR', self::STORED_IMAGE_SUBFIELD_NAME);
376  $tpl->setVariable('UPLOAD_IMAGE_POSTVAR', self::IMAGE_UPLOAD_SUBFIELD_NAME);
377  $tpl->parseCurrentBlock();
378 
379  $globalTpl = $GLOBALS['DIC'] ? $GLOBALS['DIC']['tpl'] : $GLOBALS['tpl'];
380  $globalTpl->addJavascript("./Services/Form/js/ServiceFormWizardInput.js");
381  $globalTpl->addJavascript("./Services/Form/js/ServiceFormIdentifiedWizardInputExtend.js");
382  //$globalTpl->addJavascript("./Services/Form/js/ServiceFormIdentifiedImageWizardInputConcrete.js");
383  }
384 
385  return $tpl->get();
386  }
387 
392  protected function valueHasContentImageSource($value)
393  {
394  return isset($value['src']) && strlen($value['src']);
395  }
396 
402  {
403  if( $this->valueHasContentImageSource($value) )
404  {
405  return $value['src'];
406  }
407 
408  return null;
409  }
410 
415  protected function valueHasContentImageTitle($value)
416  {
417  return isset($value['title']) && strlen($value['title']);
418  }
419 
424  {
425  if( $this->valueHasContentImageTitle($value) )
426  {
427  return $value['title'];
428  }
429 
431  }
432 
436  protected function getTemplate()
437  {
438  return new ilTemplate(self::RENDERING_TEMPLATE, true, true, "Services/Form");
439  }
440 }
getMultiValuePostVarSubFieldPosIndexed($identifier, $subFieldIndex, $positionIndex)
static prepareFormOutput($a_str, $a_strip=false)
prepares string output for html forms public
$error
Definition: Error.php:17
getMultiValuePosIndexedSubFieldId($identifier, $subFieldIndex, $positionIndex)
static virusHandling($a_file, $a_orig_name="", $a_clean=true)
scan file for viruses and clean files if possible
onCheckInput()
Check input, strip slashes etc.
getPostVar()
Get Post Variable.
$GLOBALS['loaded']
Global hash that tracks already loaded includes.
static get($a_glyph, $a_text="")
Get glyph html.
__construct($a_title="", $a_postvar="")
Constructor.
static getFileSizeInfo()
setSuffixes($a_suffixes)
Set Accepted Suffixes.
setAlert($a_alert)
Set Alert Text.
global $tpl
Definition: ilias.php:8
buildMultiValueSubmitVar($identifier, $positionIndex, $submitCommand)
special template class to simplify handling of ITX/PEAR
setSize($a_size)
Set Size.
Create styles array
The data for the language used.
isValidFilenameInput($filenameInput)
setEditElementOrderEnabled($editElementOrderEnabled)
global $lng
Definition: privfeed.php:17
setEditElementOccuranceEnabled($editElementOccuranceEnabled)
addFormValuesManipulator(ilFormValuesManipulator $manipulator)