ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
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($elementsOrValues)
157 {
158 if (!count($elementsOrValues)) {
159 return false;
160 }
161
162 return (current($elementsOrValues) 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 $element->setRandomIdentifier($identifier);
258
259 $element->setPosition($position++);
260
261 if ($this->getContext() == self::CONTEXT_MAINTAIN_HIERARCHY) {
262 $element->setIndentation($value);
263 } else {
264 $element->setContent($value);
265 }
266
267 if ($this->getContext() == self::CONTEXT_MAINTAIN_ELEMENT_IMAGE) {
268 $element->setUploadImageName($this->fetchSubmittedImageFilename($identifier));
269 $element->setUploadImageFile($this->fetchSubmittedUploadFilename($identifier));
270
271 $element->setImageRemovalRequest($this->wasImageRemovalRequested($identifier));
272 }
273
274 $elements[$identifier] = $element;
275 }
276
277 return $elements;
278 }
279
280 protected function fetchSubmittedImageFilename($identifier)
281 {
282 $fileUpload = $this->fetchElementFileUpload($identifier);
283 return $this->fetchSubmittedFileUploadProperty($fileUpload, 'name');
284 }
285
286 protected function fetchSubmittedUploadFilename($identifier)
287 {
288 $fileUpload = $this->fetchElementFileUpload($identifier);
289 return $this->fetchSubmittedFileUploadProperty($fileUpload, 'tmp_name');
290 }
291
292 protected function fetchSubmittedFileUploadProperty($fileUpload, $property)
293 {
294 if (!isset($fileUpload[$property]) || !strlen($fileUpload[$property])) {
295 return null;
296 }
297
298 return $fileUpload[$property];
299 }
300
301 protected function fetchElementFileUpload($identifier)
302 {
303 $uploadFiles = $this->fetchSubmittedUploadFiles();
304
305 if (!isset($uploadFiles[$identifier])) {
306 return array();
307 }
308
309 return $uploadFiles[$identifier];
310 }
311
312 protected function fetchSubmittedUploadFiles()
313 {
314 $submittedUploadFiles = $this->getFileSubmitDataRestructuredByIdentifiers();
315 //$submittedUploadFiles = $this->getFileSubmitsHavingActualUpload($submittedUploadFiles);
316 return $submittedUploadFiles;
317 }
318
319 protected function getFileSubmitsHavingActualUpload($submittedUploadFiles)
320 {
321 foreach ($submittedUploadFiles as $identifier => $uploadProperties) {
322 if (!isset($uploadProperties['tmp_name'])) {
323 unset($submittedUploadFiles[$identifier]);
324 continue;
325 }
326
327 if (!strlen($uploadProperties['tmp_name'])) {
328 unset($submittedUploadFiles[$identifier]);
329 continue;
330 }
331
332 if (!is_uploaded_file($uploadProperties['tmp_name'])) {
333 unset($submittedUploadFiles[$identifier]);
334 continue;
335 }
336 }
337
338 return $submittedUploadFiles;
339 }
340
345 {
346 $submittedUploadFiles = array();
347
348 foreach ($this->getFileSubmitData() as $uploadProperty => $valueElement) {
349 foreach ($valueElement as $elementIdentifier => $uploadValue) {
350 if (!isset($submittedUploadFiles[$elementIdentifier])) {
351 $submittedUploadFiles[$elementIdentifier] = array();
352 }
353
354 $submittedUploadFiles[$elementIdentifier][$uploadProperty] = $uploadValue;
355 }
356 }
357
358 return $submittedUploadFiles;
359 }
360
361 protected function getFileSubmitData()
362 {
363 if (!isset($_FILES[$this->getPostVar()])) {
364 return array();
365 }
366
367 return $_FILES[$this->getPostVar()];
368 }
369
370 protected function wasImageRemovalRequested($identifier)
371 {
372 if (!$this->getImageRemovalCommand()) {
373 return false;
374 }
375
376 if (!isset($_POST['cmd']) || !is_array($_POST['cmd'])) {
377 return false;
378 }
379
380 $cmdArr = $_POST['cmd'];
381
382 if (!isset($cmdArr[$this->getImageRemovalCommand()])) {
383 return false;
384 }
385
386 $fieldArr = $cmdArr[$this->getImageRemovalCommand()];
387
388 if (!isset($fieldArr[$this->getPostVar()])) {
389 return false;
390 }
391
392 $identifierArr = $fieldArr[$this->getPostVar()];
393
394 return key($identifierArr) == $identifier;
395 }
396}
$_POST["username"]
An exception for terminatinating execution or to throw for unit testing.
setImageThumbnailPrefix($imageThumbnailPrefix)
Class ilFormException.