ILIAS  release_8 Revision v8.24
class.ilMultipleImagesInputGUI.php
Go to the documentation of this file.
1<?php
2
23{
24 public const RENDERING_TEMPLATE = 'tpl.prop_multi_image_inp.html';
25
26 public const ITERATOR_SUBFIELD_NAME = 'iteratorfield';
27 public const STORED_IMAGE_SUBFIELD_NAME = 'storedimage';
28 public const IMAGE_UPLOAD_SUBFIELD_NAME = 'imageupload';
29
30 public const FILE_DATA_INDEX_DODGING_FILE = 'dodging_file';
31
36
40 protected $editElementOrderEnabled = false;
41
42 protected stdClass $dodging_files;
43
47 protected $suffixes = array();
48
49 protected $imageRemovalCommand = 'removeImage';
50
51 protected $imageUploadCommand = 'uploadImage';
52
53 protected ilLanguage $lng;
55
60 public function __construct($a_title = "", $a_postvar = "")
61 {
63 global $DIC;
64
65 $this->lng = $DIC->language();
66 $this->tpl = $DIC->ui()->mainTemplate();
67 parent::__construct($a_title, $a_postvar);
68
69 $this->setSuffixes(["jpg", "jpeg", "png", "gif"]);
70 $this->setSize(25);
71 $this->validationRegexp = "";
72
74 $manipulator->setPostVar($this->getPostVar());
75 $this->addFormValuesManipulator($manipulator);
76
77 $this->dodging_files = new stdClass();
78
79 $manipulator = new ilMultipleImagesFileSubmissionDataCompletion($this->dodging_files);
80 $this->addFormValuesManipulator($manipulator);
81
83 $manipulator->setPostVar($this->getPostVar());
84 $this->addFormValuesManipulator($manipulator);
85
87 $manipulator->setPostVar($this->getPostVar());
88 $this->addFormValuesManipulator($manipulator);
89 }
90
96 public function setSuffixes($a_suffixes): void
97 {
98 $this->suffixes = $a_suffixes;
99 }
100
101 public function getSuffixes(): array
102 {
103 return $this->suffixes;
104 }
105
106 public function getImageRemovalCommand(): string
107 {
109 }
110
115 {
116 $this->imageRemovalCommand = $imageRemovalCommand;
117 }
118
119 public function getImageUploadCommand(): string
120 {
122 }
123
128 {
129 $this->imageUploadCommand = $imageUploadCommand;
130 }
131
132 public function isEditElementOccuranceEnabled(): bool
133 {
135 }
136
141 {
142 $this->editElementOccuranceEnabled = $editElementOccuranceEnabled;
143 }
144
145 public function isEditElementOrderEnabled(): bool
146 {
148 }
149
154 {
155 $this->editElementOrderEnabled = $editElementOrderEnabled;
156 }
157
158 public function onCheckInput(): bool
159 {
160 $F = $_FILES[$this->getPostVar()];
161
162 $submittedElements = $this->getInput();
163
164 if ($F && ((array) $this->dodging_files) !== []) {
165 $F = array_merge([self::FILE_DATA_INDEX_DODGING_FILE => (array) $this->dodging_files], $F);
166 }
167
168 if ($this->getRequired() && !is_array($F['error'])) {
169 $this->setAlert($this->lng->txt("form_msg_file_no_upload"));
170 return false;
171 } else {
172 foreach ($F['error'] as $index => $error) {
173 // error handling
174 if ($error > 0) {
175 switch ($error) {
176 case UPLOAD_ERR_FORM_SIZE:
177 case UPLOAD_ERR_INI_SIZE:
178 $this->setAlert($this->lng->txt("form_msg_file_size_exceeds"));
179 return false;
180 break;
181
182 case UPLOAD_ERR_PARTIAL:
183 $this->setAlert($this->lng->txt("form_msg_file_partially_uploaded"));
184 return false;
185 break;
186
187 case UPLOAD_ERR_NO_FILE:
188 if (!$this->getRequired()) {
189 break;
190 } elseif (isset($F[self::FILE_DATA_INDEX_DODGING_FILE][$index]) && $F[self::FILE_DATA_INDEX_DODGING_FILE][$index] !== '') {
191 break;
192 }
193 $this->setAlert($this->lng->txt("form_msg_file_no_upload"));
194 return false;
195 break;
196
197 case UPLOAD_ERR_NO_TMP_DIR:
198 $this->setAlert($this->lng->txt("form_msg_file_missing_tmp_dir"));
199 return false;
200 break;
201
202 case UPLOAD_ERR_CANT_WRITE:
203 $this->setAlert($this->lng->txt("form_msg_file_cannot_write_to_disk"));
204 return false;
205 break;
206
207 case UPLOAD_ERR_EXTENSION:
208 $this->setAlert($this->lng->txt("form_msg_file_upload_stopped_ext"));
209 return false;
210 break;
211 }
212 }
213 }
214 }
215
216 if (is_array($F['tmp_name'])) {
217 foreach ($F['tmp_name'] as $index => $tmpname) {
218 $filename = $F['name'][$index];
219 if (is_array($filename)) {
220 $filename = array_shift($filename);
221 $tmpname = array_shift($tmpname);
222 }
223 $filename_arr = pathinfo($filename);
224 $suffix = $filename_arr["extension"] ?? '';
225 $mimetype = $F['type'][$index];
226 $size_bytes = $F['size'][$index];
227 // check suffixes
228 if (strlen($tmpname) && is_array($this->getSuffixes())) {
229 if (!in_array(strtolower($suffix), $this->getSuffixes())) {
230 $this->setAlert($this->lng->txt("form_msg_file_wrong_file_type"));
231 return false;
232 }
233 }
234 }
235 }
236
237 foreach ($F['tmp_name'] as $index => $tmpname) {
238 $filename = $F['name'][$index];
239 if (is_array($filename)) {
240 $filename = array_shift($filename);
241 $tmpname = array_shift($tmpname);
242 }
243 $filename_arr = pathinfo($filename);
244 $suffix = $filename_arr["extension"] ?? '';
245 $mimetype = $F['type'][$index];
246 $size_bytes = $F['size'][$index];
247 // virus handling
248 if (strlen($tmpname)) {
250 if ($vir[0] == false) {
251 $this->setAlert($this->lng->txt("form_msg_file_virus_found") . "<br />" . $vir[1]);
252 return false;
253 }
254 }
255 }
256
257 return $this->checkSubItemsInput();
258 }
259
260 public function render(string $a_mode = ""): string
261 {
263
264 $tpl = $this->getTemplate();
265 $i = 0;
266
268 foreach ($identified_multi_values as $identifier => $value) {
270 $tpl->setCurrentBlock('image');
271
272 $tpl->setVariable('STORED_IMAGE_SRC', $this->fetchContentImageSourceFromValue($value));
273 $tpl->setVariable(
274 'STORED_IMAGE_ALT',
276 );
277 $tpl->setVariable('STORED_IMAGE_FILENAME', $this->fetchContentImageTitleFromValue($value));
278 $tpl->setVariable("STORED_IMAGE_POST_VAR", $this->getMultiValuePostVarSubFieldPosIndexed($identifier, self::STORED_IMAGE_SUBFIELD_NAME, $i));
279
280 $tpl->setVariable("TXT_DELETE_EXISTING", $lng->txt("delete_existing_file"));
281 $tpl->setVariable("IMAGE_CMD_REMOVE", $this->buildMultiValueSubmitVar($identifier, $i, $this->getImageRemovalCommand()));
282
283 $tpl->parseCurrentBlock();
284 }
285
286 $tpl->setCurrentBlock('addimage');
287
288 $tpl->setVariable("IMAGE_BROWSE", $lng->txt('select_file'));
289 $tpl->setVariable("IMAGE_ID", $this->getMultiValuePosIndexedSubFieldId($identifier, self::IMAGE_UPLOAD_SUBFIELD_NAME, $i));
290 $tpl->setVariable("TXT_IMAGE_SUBMIT", $lng->txt("upload"));
291 $tpl->setVariable("IMAGE_CMD_UPLOAD", $this->buildMultiValueSubmitVar($identifier, $i, $this->getImageUploadCommand()));
292 $tpl->setVariable("UPLOAD_IMAGE_POST_VAR", $this->getMultiValuePostVarSubFieldPosIndexed($identifier, self::IMAGE_UPLOAD_SUBFIELD_NAME, $i));
293 $tpl->setVariable("COUNT_POST_VAR", $this->getMultiValuePostVarSubFieldPosIndexed($identifier, self::ITERATOR_SUBFIELD_NAME, $i));
294
296
297 if ($this->isEditElementOrderEnabled()) {
298 $tpl->setCurrentBlock("move");
299 $tpl->setVariable("ID_UP", $this->getMultiValuePosIndexedSubFieldId($identifier, 'up', $i));
300 $tpl->setVariable("ID_DOWN", $this->getMultiValuePosIndexedSubFieldId($identifier, 'down', $i));
301 $tpl->setVariable("CMD_UP", $this->buildMultiValueSubmitVar($identifier, $i, 'up'));
302 $tpl->setVariable("CMD_DOWN", $this->buildMultiValueSubmitVar($identifier, $i, 'down'));
306 }
307
308 if ($this->isEditElementOccuranceEnabled()) {
309 $tpl->setCurrentBlock("row");
310 $tpl->setVariable("ID_ADD", $this->getMultiValuePosIndexedSubFieldId($identifier, 'add', $i));
311 $tpl->setVariable("ID_REMOVE", $this->getMultiValuePosIndexedSubFieldId($identifier, 'remove', $i));
312 $tpl->setVariable("CMD_ADD", $this->buildMultiValueSubmitVar($identifier, $i, 'add'));
313 $tpl->setVariable("CMD_REMOVE", $this->buildMultiValueSubmitVar($identifier, $i, 'remove'));
317 }
318
319 $i++;
320 }
321
322 if (is_array($this->getSuffixes())) {
323 $suff_str = $delim = "";
324 foreach ($this->getSuffixes() as $suffix) {
325 $suff_str .= $delim . "." . $suffix;
326 $delim = ", ";
327 }
328 $tpl->setCurrentBlock('allowed_image_suffixes');
329 $tpl->setVariable("TXT_ALLOWED_SUFFIXES", $lng->txt("file_allowed_suffixes") . " " . $suff_str);
331 }
332
334 $tpl->setVariable("ELEMENT_ID", $this->getPostVar());
335 $tpl->setVariable("TEXT_YES", $lng->txt('yes'));
336 $tpl->setVariable("TEXT_NO", $lng->txt('no'));
337 $tpl->setVariable("DELETE_IMAGE_HEADER", $lng->txt('delete_image_header'));
338 $tpl->setVariable("DELETE_IMAGE_QUESTION", $lng->txt('delete_image_question'));
339 $tpl->setVariable("ANSWER_TEXT", $lng->txt('answer_text'));
340 $tpl->setVariable("COMMANDS_TEXT", $lng->txt('actions'));
341
342 if (!$this->getDisabled()) {
343 $iterator_subfield_name = self::ITERATOR_SUBFIELD_NAME;
344 $image_upload_subfield_name = self::IMAGE_UPLOAD_SUBFIELD_NAME;
345
346 $init_code = <<<JS
347$.extend({}, ilWizardInput, ilIdentifiedWizardInputExtend).init(
348 {
349 'fieldContainerSelector': '.ilWzdContainerImage',
350 'reindexingRequiredElementsSelectors': [
351 'input:hidden[name*="[{$iterator_subfield_name}]"]',
352 'input:file[id*="__{$image_upload_subfield_name}__"]',
353 'input:submit[name*="[{$this->getImageUploadCommand()}]"]',
354 'input:submit[name*="[{$this->getImageRemovalCommand()}]"]',
355 'button'
356 ],
357 'handleRowCleanUpCallback': function(rowElem) {
358 $(rowElem).find('div.imagepresentation').remove();
359 $(rowElem).find('input[type=text]').val('');
360 }
361 }
362);
363JS;
364
365 $this->tpl->addJavascript("./Services/Form/js/ServiceFormWizardInput.js");
366 $this->tpl->addJavascript("./Services/Form/js/ServiceFormIdentifiedWizardInputExtend.js");
367 $this->tpl->addOnLoadCode($init_code);
368 }
369
370 return $tpl->get();
371 }
372
376 protected function valueHasContentImageSource($value): bool
377 {
378 return is_array($value)
379 && array_key_exists('src', $value)
380 && $value['src'] !== '';
381 }
382
386 protected function fetchContentImageSourceFromValue($value): ?string
387 {
389 return $value['src'];
390 }
391
392 return null;
393 }
394
398 protected function valueHasContentImageTitle($value): bool
399 {
400 return isset($value['title']) && strlen($value['title']);
401 }
402
403 protected function fetchContentImageTitleFromValue($value): ?string
404 {
405 if ($this->valueHasContentImageTitle($value)) {
406 return $value['title'];
407 }
408
410 }
411
412 protected function getTemplate(): ilTemplate
413 {
414 return new ilTemplate(self::RENDERING_TEMPLATE, true, true, "Modules/TestQuestionPool");
415 }
416}
$filename
Definition: buildRTE.php:78
static getFileSizeInfo()
static get(string $a_glyph, string $a_text="")
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
addFormValuesManipulator(ilFormValuesManipulator $manipulator)
getMultiValuePostVarSubFieldPosIndexed($identifier, $subFieldIndex, $positionIndex)
getMultiValuePosIndexedSubFieldId($identifier, $subFieldIndex, $positionIndex)
buildMultiValueSubmitVar($identifier, $positionIndex, $submitCommand)
language handling
txt(string $a_topic, string $a_default_lang_fallback_mod="")
gets the text for a given topic if the topic is not in the list, the topic itself with "-" will be re...
static prepareFormOutput($a_str, bool $a_strip=false)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
setEditElementOccuranceEnabled($editElementOccuranceEnabled)
setEditElementOrderEnabled($editElementOrderEnabled)
setSuffixes($a_suffixes)
Set Accepted Suffixes.
special template class to simplify handling of ITX/PEAR
static virusHandling(string $a_file, string $a_orig_name='', bool $a_clean=true)
global $DIC
Definition: feed.php:28
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
setCurrentBlock(string $part=self::DEFAULT_BLOCK)
Sets the template to the given block.
parseCurrentBlock(string $block_name=self::DEFAULT_BLOCK)
Parses the given block.
setVariable(string $variable, $value='')
Sets the given variable to the given value.
get(string $part=self::DEFAULT_BLOCK)
Renders the given block and returns the html string.
$index
Definition: metadata.php:145
$i
Definition: metadata.php:41
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc