ILIAS  release_7 Revision v7.30-3-g800a261c036
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
4require_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
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
127 {
128 $this->imageFsPath = $imageFsPath;
129 }
130
134 public function getThumbnailPrefix()
135 {
137 }
138
143 {
144 $this->thumbnailPrefix = $thumbnailPrefix;
145 }
146
147 public function getIndentationsPostVar()
148 {
149 $postVar = $this->getPostVar();
152
153 return $postVar;
154 }
155
156 protected function needsConvertToValues($elements_or_values)
157 {
158 if (!count($elements_or_values)) {
159 return false;
160 }
161
162 return (current($elements_or_values) instanceof ilAssOrderingElement);
163 }
164
165 public function manipulateFormInputValues($elementsOrValues)
166 {
167 if ($this->needsConvertToValues($elementsOrValues)) {
168 $elementsOrValues = $this->collectValuesFromElements($elementsOrValues);
169 }
170
171 return $elementsOrValues;
172 }
173
174 protected function collectValuesFromElements(array $elements)
175 {
176 $values = array();
177
178 foreach ($elements as $identifier => $orderingElement) {
179 switch ($this->getContext()) {
181
182 $values[$identifier] = $this->getTextContentValueFromObject($orderingElement);
183 break;
184
186
187 $values[$identifier] = $this->getImageContentValueFromObject($orderingElement);
188 break;
189
191
192 $values[$identifier] = $this->getStructValueFromObject($orderingElement);
193 break;
194
195 default:
196 throw new ilFormException('unsupported context: ' . $this->getContext());
197 }
198 }
199
200 return $values;
201 }
202
204 {
205 return $element->getContent();
206 }
207
209 {
210 $element->setImagePathWeb($this->getImageUrlPath());
211 $element->setImagePathFs($this->getImageFsPath());
213
214 return array(
215 'title' => $element->getContent(),
216 'src' => $element->getPresentationImageUrl()
217 );
218 }
219
221 {
222 return array(
223 'answer_id' => $element->getId(),
224 'random_id' => $element->getRandomIdentifier(),
225 'content' => (string) $element->getContent(),
226 'ordering_position' => $element->getPosition(),
227 'ordering_indentation' => $element->getIndentation()
228 );
229 }
230
231 protected function needsConvertToElements($valuesOrElements)
232 {
233 if (!count($valuesOrElements)) {
234 return false;
235 }
236
237 return !(current($valuesOrElements) instanceof ilAssOrderingElement);
238 }
239
240 public function manipulateFormSubmitValues($valuesOrElements)
241 {
242 if ($this->needsConvertToElements($valuesOrElements)) {
243 $valuesOrElements = $this->constructElementsFromValues($valuesOrElements);
244 }
245
246 return $valuesOrElements;
247 }
248
249 public function constructElementsFromValues(array $values)
250 {
251 $elements = array();
252
253 $position = 0;
254
255 foreach ($values as $identifier => $value) {
256 $element = new ilAssOrderingElement();
257
258 $element->setRandomIdentifier($identifier);
259
260 $element->setPosition($position++);
261
262 if ($this->getContext() == self::CONTEXT_MAINTAIN_HIERARCHY) {
263 $element->setIndentation($value);
264 } else {
265 $element->setContent($value);
266 }
267
268 if ($this->getContext() == self::CONTEXT_MAINTAIN_ELEMENT_IMAGE) {
269 $element->setUploadImageName($this->fetchSubmittedImageFilename($identifier));
270 $element->setUploadImageFile($this->fetchSubmittedUploadFilename($identifier));
271
272 $element->setImageRemovalRequest($this->wasImageRemovalRequested($identifier));
273 }
274
275 $elements[$identifier] = $element;
276 }
277
278 return $elements;
279 }
280
281 protected function fetchSubmittedImageFilename($identifier)
282 {
283 $fileUpload = $this->fetchElementFileUpload($identifier);
284 return $this->fetchSubmittedFileUploadProperty($fileUpload, 'name');
285 }
286
287 protected function fetchSubmittedUploadFilename($identifier)
288 {
289 $fileUpload = $this->fetchElementFileUpload($identifier);
290 return $this->fetchSubmittedFileUploadProperty($fileUpload, 'tmp_name');
291 }
292
293 protected function fetchSubmittedFileUploadProperty($fileUpload, $property)
294 {
295 if (!array_key_exists($property, $fileUpload)) {
296 return null;
297 }
298
299 return $fileUpload[$property];
300 }
301
302 protected function fetchElementFileUpload($identifier)
303 {
304 $uploadFiles = $this->fetchSubmittedUploadFiles();
305
306 if (!isset($uploadFiles[$identifier])) {
307 return array();
308 }
309
310 return $uploadFiles[$identifier];
311 }
312
313 protected function fetchSubmittedUploadFiles()
314 {
315 $submittedUploadFiles = $this->getFileSubmitDataRestructuredByIdentifiers();
316 //$submittedUploadFiles = $this->getFileSubmitsHavingActualUpload($submittedUploadFiles);
317 return $submittedUploadFiles;
318 }
319
320 protected function getFileSubmitsHavingActualUpload($submittedUploadFiles)
321 {
322 foreach ($submittedUploadFiles as $identifier => $uploadProperties) {
323 if (!isset($uploadProperties['tmp_name'])) {
324 unset($submittedUploadFiles[$identifier]);
325 continue;
326 }
327
328 if (!strlen($uploadProperties['tmp_name'])) {
329 unset($submittedUploadFiles[$identifier]);
330 continue;
331 }
332
333 if (!is_uploaded_file($uploadProperties['tmp_name'])) {
334 unset($submittedUploadFiles[$identifier]);
335 continue;
336 }
337 }
338
339 return $submittedUploadFiles;
340 }
341
346 {
347 $submittedUploadFiles = array();
348
349 foreach ($this->getFileSubmitData() as $uploadProperty => $valueElement) {
350 foreach ($valueElement as $elementIdentifier => $uploadValue) {
351 if (!isset($submittedUploadFiles[$elementIdentifier])) {
352 $submittedUploadFiles[$elementIdentifier] = array();
353 }
354
355 $submittedUploadFiles[$elementIdentifier][$uploadProperty] = $uploadValue;
356 }
357 }
358
359 return $submittedUploadFiles;
360 }
361
362 protected function getFileSubmitData()
363 {
364 if (!isset($_FILES[$this->getPostVar()])) {
365 return array();
366 }
367
368 return $_FILES[$this->getPostVar()];
369 }
370
371 protected function wasImageRemovalRequested($identifier)
372 {
373 if (!$this->getImageRemovalCommand()) {
374 return false;
375 }
376
377 if (!isset($_POST['cmd']) || !is_array($_POST['cmd'])) {
378 return false;
379 }
380
381 $cmdArr = $_POST['cmd'];
382
383 if (!isset($cmdArr[$this->getImageRemovalCommand()])) {
384 return false;
385 }
386
387 $fieldArr = $cmdArr[$this->getImageRemovalCommand()];
388
389 if (!isset($fieldArr[$this->getPostVar()])) {
390 return false;
391 }
392
393 $identifierArr = $fieldArr[$this->getPostVar()];
394
395 return key($identifierArr) == $identifier;
396 }
397}
$_POST["username"]
An exception for terminatinating execution or to throw for unit testing.
setImageThumbnailPrefix($imageThumbnailPrefix)
Class ilFormException.