ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
class.ilAssOrderingFormValuesObjectsConverter.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/interfaces/interface.ilFormValuesManipulator.php';
5 
13 {
14  const INDENTATIONS_POSTVAR_SUFFIX = '_ordering';
15  const INDENTATIONS_POSTVAR_SUFFIX_JS = '__default';
16 
17  const CONTEXT_MAINTAIN_ELEMENT_TEXT = 'maintainItemText';
18  const CONTEXT_MAINTAIN_ELEMENT_IMAGE = 'maintainItemImage';
19  const CONTEXT_MAINTAIN_HIERARCHY = 'maintainHierarchy';
20 
24  protected $context = null;
25 
29  protected $postVar = null;
30 
34  protected $imageRemovalCommand = null;
35 
39  protected $imageUrlPath;
40 
44  protected $imageFsPath;
45 
49  protected $thumbnailPrefix;
50 
54  public function getContext()
55  {
56  return $this->context;
57  }
58 
62  public function setContext($context)
63  {
64  $this->context = $context;
65  }
66 
70  public function getPostVar()
71  {
72  return $this->postVar;
73  }
74 
78  public function setPostVar($postVar)
79  {
80  $this->postVar = $postVar;
81  }
82 
86  public function getImageRemovalCommand()
87  {
89  }
90 
95  {
96  $this->imageRemovalCommand = $imageRemovalCommand;
97  }
98 
102  public function getImageUrlPath()
103  {
104  return $this->imageUrlPath;
105  }
106 
111  {
112  $this->imageUrlPath = $imageUrlPath;
113  }
114 
118  public function getImageFsPath()
119  {
120  return $this->imageFsPath;
121  }
122 
126  public function setImageFsPath($imageFsPath)
127  {
128  $this->imageFsPath = $imageFsPath;
129  }
130 
134  public function getThumbnailPrefix()
135  {
136  return $this->thumbnailPrefix;
137  }
138 
143  {
144  $this->thumbnailPrefix = $thumbnailPrefix;
145  }
146 
147  public function getIndentationsPostVar()
148  {
149  $postVar = $this->getPostVar();
150  $postVar .= self::INDENTATIONS_POSTVAR_SUFFIX;
151  $postVar .= self::INDENTATIONS_POSTVAR_SUFFIX_JS;
152 
153  return $postVar;
154  }
155 
156  protected function needsConvertToValues($elementsOrValues)
157  {
158  if( !count($elementsOrValues) )
159  {
160  return false;
161  }
162 
163  return ( current($elementsOrValues) instanceof ilAssOrderingElement );
164  }
165 
166  public function manipulateFormInputValues($elementsOrValues)
167  {
168  if( $this->needsConvertToValues($elementsOrValues) )
169  {
170  $elementsOrValues = $this->collectValuesFromElements($elementsOrValues);
171  }
172 
173  return $elementsOrValues;
174  }
175 
176  protected function collectValuesFromElements(array $elements)
177  {
178  $values = array();
179 
180  foreach($elements as $identifier => $orderingElement)
181  {
182  switch( $this->getContext() )
183  {
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 
208  {
209  return $element->getContent();
210  }
211 
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)
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)
236  {
237  if( !count($valuesOrElements) )
238  {
239  return false;
240  }
241 
242  return !( current($valuesOrElements) instanceof ilAssOrderingElement );
243  }
244 
245  public function manipulateFormSubmitValues($valuesOrElements)
246  {
247  if( $this->needsConvertToElements($valuesOrElements) )
248  {
249  $valuesOrElements = $this->constructElementsFromValues($valuesOrElements);
250  }
251 
252  return $valuesOrElements;
253  }
254 
255  public function constructElementsFromValues(array $values)
256  {
257  $elements = array();
258 
259  $position = 0;
260 
261  foreach($values as $identifier => $value)
262  {
263  $element = new ilAssOrderingElement();
264  $element->setRandomIdentifier($identifier);
265 
266  $element->setPosition($position++);
267 
268  if( $this->getContext() == self::CONTEXT_MAINTAIN_HIERARCHY )
269  {
270  $element->setIndentation($value);
271  }
272  else
273  {
274  $element->setContent($value);
275  }
276 
277  if( $this->getContext() == self::CONTEXT_MAINTAIN_ELEMENT_IMAGE )
278  {
279  $element->setUploadImageName($this->fetchSubmittedImageFilename($identifier));
280  $element->setUploadImageFile($this->fetchSubmittedUploadFilename($identifier));
281 
282  $element->setImageRemovalRequest($this->wasImageRemovalRequested($identifier));
283  }
284 
285  $elements[$identifier] = $element;
286  }
287 
288  return $elements;
289  }
290 
291  protected function fetchSubmittedImageFilename($identifier)
292  {
293  $fileUpload = $this->fetchElementFileUpload($identifier);
294  return $this->fetchSubmittedFileUploadProperty($fileUpload, 'name');
295  }
296 
297  protected function fetchSubmittedUploadFilename($identifier)
298  {
299  $fileUpload = $this->fetchElementFileUpload($identifier);
300  return $this->fetchSubmittedFileUploadProperty($fileUpload, 'tmp_name');
301  }
302 
303  protected function fetchSubmittedFileUploadProperty($fileUpload, $property)
304  {
305  if( !isset($fileUpload[$property]) || !strlen($fileUpload[$property]) )
306  {
307  return null;
308  }
309 
310  return $fileUpload[$property];
311  }
312 
313  protected function fetchElementFileUpload($identifier)
314  {
315  $uploadFiles = $this->fetchSubmittedUploadFiles();
316 
317  if( !isset($uploadFiles[$identifier]) )
318  {
319  return array();
320  }
321 
322  return $uploadFiles[$identifier];
323  }
324 
325  protected function fetchSubmittedUploadFiles()
326  {
327  $submittedUploadFiles = $this->getFileSubmitDataRestructuredByIdentifiers();
328  //$submittedUploadFiles = $this->getFileSubmitsHavingActualUpload($submittedUploadFiles);
329  return $submittedUploadFiles;
330  }
331 
332  protected function getFileSubmitsHavingActualUpload($submittedUploadFiles)
333  {
334  foreach($submittedUploadFiles as $identifier => $uploadProperties)
335  {
336  if( !isset($uploadProperties['tmp_name']) )
337  {
338  unset($submittedUploadFiles[$identifier]);
339  continue;
340  }
341 
342  if( !strlen($uploadProperties['tmp_name']) )
343  {
344  unset($submittedUploadFiles[$identifier]);
345  continue;
346  }
347 
348  if( !is_uploaded_file($uploadProperties['tmp_name']) )
349  {
350  unset($submittedUploadFiles[$identifier]);
351  continue;
352  }
353  }
354 
355  return $submittedUploadFiles;
356  }
357 
362  {
363  $submittedUploadFiles = array();
364 
365  foreach( $this->getFileSubmitData() as $uploadProperty => $valueElement )
366  {
367  foreach( $valueElement as $elementIdentifier => $uploadValue )
368  {
369  if( !isset( $submittedUploadFiles[$elementIdentifier] ) )
370  {
371  $submittedUploadFiles[$elementIdentifier] = array();
372  }
373 
374  $submittedUploadFiles[$elementIdentifier][$uploadProperty] = $uploadValue;
375  }
376  }
377 
378  return $submittedUploadFiles;
379  }
380 
381  protected function getFileSubmitData()
382  {
383  if( !isset($_FILES[$this->getPostVar()]) )
384  {
385  return array();
386  }
387 
388  return $_FILES[$this->getPostVar()];
389  }
390 
391  protected function wasImageRemovalRequested($identifier)
392  {
393  if( !$this->getImageRemovalCommand() )
394  {
395  return false;
396  }
397 
398  if( !isset($_POST['cmd']) || !is_array($_POST['cmd']) )
399  {
400  return false;
401  }
402 
403  $cmdArr = $_POST['cmd'];
404 
405  if( !isset($cmdArr[$this->getImageRemovalCommand()]) )
406  {
407  return false;
408  }
409 
410  $fieldArr = $cmdArr[$this->getImageRemovalCommand()];
411 
412  if( !isset($fieldArr[$this->getPostVar()]) )
413  {
414  return false;
415  }
416 
417  $identifierArr = $fieldArr[$this->getPostVar()];
418 
419  return key($identifierArr) == $identifier;
420  }
421 }
Add rich text string
The name of the decorator.
Class ilFormException.
Create styles array
The data for the language used.
setImageThumbnailPrefix($imageThumbnailPrefix)
$_POST["username"]