ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
class.ilAssOrderingFormValuesObjectsConverter.php
Go to the documentation of this file.
1 <?php
2 
26 {
27  public const INDENTATIONS_POSTVAR_SUFFIX = '_ordering';
28  public const INDENTATIONS_POSTVAR_SUFFIX_JS = '__default';
29 
30  public const CONTEXT_MAINTAIN_ELEMENT_TEXT = 'maintainItemText';
31  public const CONTEXT_MAINTAIN_ELEMENT_IMAGE = 'maintainItemImage';
32  public const CONTEXT_MAINTAIN_HIERARCHY = 'maintainHierarchy';
33 
37  protected $context = null;
38 
42  protected $postVar = null;
43 
47  protected $imageRemovalCommand = null;
48 
52  protected $imageUrlPath;
53 
57  protected $imageFsPath;
58 
62  protected $thumbnailPrefix;
63 
67  public function getContext(): ?string
68  {
69  return $this->context;
70  }
71 
75  public function setContext($context): void
76  {
77  $this->context = $context;
78  }
79 
83  public function getPostVar(): ?string
84  {
85  return $this->postVar;
86  }
87 
91  public function setPostVar($postVar): void
92  {
93  $this->postVar = $postVar;
94  }
95 
96  public function getImageRemovalCommand(): ?string
97  {
99  }
100 
102  {
103  $this->imageRemovalCommand = $imageRemovalCommand;
104  }
105 
106  public function getImageUrlPath(): string
107  {
108  return $this->imageUrlPath;
109  }
110 
114  public function setImageUrlPath($imageUrlPath): void
115  {
116  $this->imageUrlPath = $imageUrlPath;
117  }
118 
122  public function getImageFsPath(): string
123  {
124  return $this->imageFsPath;
125  }
126 
130  public function setImageFsPath($imageFsPath): void
131  {
132  $this->imageFsPath = $imageFsPath;
133  }
134 
138  public function getThumbnailPrefix(): string
139  {
140  return $this->thumbnailPrefix;
141  }
142 
146  public function setThumbnailPrefix($thumbnailPrefix): void
147  {
148  $this->thumbnailPrefix = $thumbnailPrefix;
149  }
150 
151  public function getIndentationsPostVar(): string
152  {
153  $postVar = $this->getPostVar();
154  $postVar .= self::INDENTATIONS_POSTVAR_SUFFIX;
155  $postVar .= self::INDENTATIONS_POSTVAR_SUFFIX_JS;
156 
157  return $postVar;
158  }
159 
160  protected function needsConvertToValues($elements_or_values): bool
161  {
162  if (!count($elements_or_values)) {
163  return false;
164  }
165 
166  return (current($elements_or_values) instanceof ilAssOrderingElement);
167  }
168 
169  public function manipulateFormInputValues(array $input_values): array
170  {
171  if ($this->needsConvertToValues($input_values)) {
172  $input_values = $this->collectValuesFromElements($input_values);
173  }
174 
175  return $input_values;
176  }
177 
178  protected function collectValuesFromElements(array $elements): array
179  {
180  $values = array();
181 
182  foreach ($elements as $identifier => $orderingElement) {
183  switch ($this->getContext()) {
184  case self::CONTEXT_MAINTAIN_ELEMENT_TEXT:
185 
186  $values[$identifier] = $this->getTextContentValueFromObject($orderingElement);
187  break;
188 
189  case self::CONTEXT_MAINTAIN_ELEMENT_IMAGE:
190 
191  $values[$identifier] = $this->getImageContentValueFromObject($orderingElement);
192  break;
193 
194  case self::CONTEXT_MAINTAIN_HIERARCHY:
195 
196  $values[$identifier] = $this->getStructValueFromObject($orderingElement);
197  break;
198 
199  default:
200  throw new ilFormException('unsupported context: ' . $this->getContext());
201  }
202  }
203 
204  return $values;
205  }
206 
207  protected function getTextContentValueFromObject(ilAssOrderingElement $element): ?string
208  {
209  return $element->getContent();
210  }
211 
212  protected function getImageContentValueFromObject(ilAssOrderingElement $element): array
213  {
214  $element->setImagePathWeb($this->getImageUrlPath());
215  $element->setImagePathFs($this->getImageFsPath());
216  $element->setImageThumbnailPrefix($this->getThumbnailPrefix());
217 
218  return array(
219  'title' => $element->getContent(),
220  'src' => $element->getPresentationImageUrl()
221  );
222  }
223 
224  protected function getStructValueFromObject(ilAssOrderingElement $element): array
225  {
226  return array(
227  'answer_id' => $element->getId(),
228  'random_id' => $element->getRandomIdentifier(),
229  'content' => (string) $element->getContent(),
230  'ordering_position' => $element->getPosition(),
231  'ordering_indentation' => $element->getIndentation()
232  );
233  }
234 
235  protected function needsConvertToElements($valuesOrElements): bool
236  {
237  if (!count($valuesOrElements)) {
238  return false;
239  }
240 
241  return !(current($valuesOrElements) instanceof ilAssOrderingElement);
242  }
243 
244  public function manipulateFormSubmitValues(array $submitValues): array
245  {
246  if ($this->needsConvertToElements($submitValues)) {
247  $submitValues = $this->constructElementsFromValues($submitValues);
248  }
249 
250  return $submitValues;
251  }
252 
253  public function constructElementsFromValues(array $values): array
254  {
255  $elements = array();
256 
257  $position = 0;
258 
259  if (array_key_exists('content', $values)) {
260  $values = $values['content'];
261  }
262  foreach ($values as $identifier => $value) {
263  $element = new ilAssOrderingElement();
264 
265  $element->setRandomIdentifier($identifier);
266 
267  $element->setPosition($position++);
268 
269  if ($this->getContext() == self::CONTEXT_MAINTAIN_HIERARCHY) {
270  $element->setIndentation((int)$value);
271  } else {
272  $element->setContent($value);
273  }
274 
275  if ($this->getContext() == self::CONTEXT_MAINTAIN_ELEMENT_IMAGE) {
276  $element->setUploadImageName($this->fetchSubmittedImageFilename($identifier));
277  $element->setUploadImageFile($this->fetchSubmittedUploadFilename($identifier));
278 
279  $element->setImageRemovalRequest($this->wasImageRemovalRequested($identifier));
280  }
281 
282  $elements[$identifier] = $element;
283  }
284 
285  return $elements;
286  }
287 
288  protected function fetchSubmittedImageFilename($identifier)
289  {
290  $fileUpload = $this->fetchElementFileUpload($identifier);
291  return $this->fetchSubmittedFileUploadProperty($fileUpload, 'name');
292  }
293 
294  protected function fetchSubmittedUploadFilename($identifier)
295  {
296  $fileUpload = $this->fetchElementFileUpload($identifier);
297  return $this->fetchSubmittedFileUploadProperty($fileUpload, 'tmp_name');
298  }
299 
300  protected function fetchSubmittedFileUploadProperty($fileUpload, $property)
301  {
302  if (!array_key_exists($property, $fileUpload)) {
303  return null;
304  }
305 
306  return $fileUpload[$property];
307  }
308 
309  protected function fetchElementFileUpload($identifier)
310  {
311  $uploadFiles = $this->fetchSubmittedUploadFiles();
312 
313  if (!isset($uploadFiles[$identifier])) {
314  return array();
315  }
316 
317  return $uploadFiles[$identifier];
318  }
319 
320  protected function fetchSubmittedUploadFiles(): array
321  {
322  $submittedUploadFiles = $this->getFileSubmitDataRestructuredByIdentifiers();
323  //$submittedUploadFiles = $this->getFileSubmitsHavingActualUpload($submittedUploadFiles);
324  return $submittedUploadFiles;
325  }
326 
327  protected function getFileSubmitsHavingActualUpload($submittedUploadFiles)
328  {
329  foreach ($submittedUploadFiles as $identifier => $uploadProperties) {
330  if (!isset($uploadProperties['tmp_name'])) {
331  unset($submittedUploadFiles[$identifier]);
332  continue;
333  }
334 
335  if (!strlen($uploadProperties['tmp_name'])) {
336  unset($submittedUploadFiles[$identifier]);
337  continue;
338  }
339 
340  if (!is_uploaded_file($uploadProperties['tmp_name'])) {
341  unset($submittedUploadFiles[$identifier]);
342  continue;
343  }
344  }
345 
346  return $submittedUploadFiles;
347  }
348 
352  protected function getFileSubmitDataRestructuredByIdentifiers(): array
353  {
354  $submittedUploadFiles = array();
355 
356  foreach ($this->getFileSubmitData() as $uploadProperty => $valueElement) {
357  foreach ($valueElement as $elementIdentifier => $uploadValue) {
358  if (!isset($submittedUploadFiles[$elementIdentifier])) {
359  $submittedUploadFiles[$elementIdentifier] = array();
360  }
361 
362  $submittedUploadFiles[$elementIdentifier][$uploadProperty] = $uploadValue;
363  }
364  }
365 
366  return $submittedUploadFiles;
367  }
368 
369  protected function getFileSubmitData()
370  {
371  if (!isset($_FILES[$this->getPostVar()])) {
372  return array();
373  }
374 
375  return $_FILES[$this->getPostVar()];
376  }
377 
383  protected function wasImageRemovalRequested($identifier): bool
384  {
385  if (!$this->getImageRemovalCommand()) {
386  return false;
387  }
388 
389  if (!isset($_POST['cmd']) || !is_array($_POST['cmd'])) {
390  return false;
391  }
392 
393  $cmdArr = $_POST['cmd'];
394 
395  if (!isset($cmdArr[$this->getImageRemovalCommand()])) {
396  return false;
397  }
398 
399  $fieldArr = $cmdArr[$this->getImageRemovalCommand()];
400 
401  if (!isset($fieldArr[$this->getPostVar()])) {
402  return false;
403  }
404 
405  $identifierArr = $fieldArr[$this->getPostVar()];
406 
407  $requested_identfier = key($identifierArr);
408 
409  // The code actually relied on a manipulation of $_POST by ilIdentifiedMultiValuesJsPositionIndexRemover
410  return (string) str_replace(
412  '',
413  (string) $requested_identfier
414  ) === (string) $identifier;
415  }
416 }
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...
wasImageRemovalRequested($identifier)
TODO: Instead of accessing post, the complete ilFormValuesManipulator should be aware of a server req...
setImageThumbnailPrefix($imageThumbnailPrefix)