[CLEANUP] Rework/simplify copyright header and remove @package
[Packages/TYPO3.CMS.git] / typo3 / sysext / fluid / Classes / ViewHelpers / Form / UploadViewHelper.php
1 <?php
2 namespace TYPO3\CMS\Fluid\ViewHelpers\Form;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is backported from the TYPO3 Flow package "TYPO3.Fluid".
8 *
9 * It is free software; you can redistribute it and/or modify it under
10 * the terms of the GNU General Public License, either version 2
11 * of the License, or any later version.
12 *
13 * For the full copyright and license information, please read the
14 * LICENSE.txt file that was distributed with this source code.
15 *
16 * The TYPO3 project - inspiring people to share!
17 */
18
19 /**
20 * A view helper which generates an <input type="file"> HTML element.
21 * Make sure to set enctype="multipart/form-data" on the form!
22 *
23 * = Examples =
24 *
25 * <code title="Example">
26 * <f:form.upload name="file" />
27 * </code>
28 * <output>
29 * <input type="file" name="file" />
30 * </output>
31 *
32 * @api
33 */
34 class UploadViewHelper extends \TYPO3\CMS\Fluid\ViewHelpers\Form\AbstractFormFieldViewHelper {
35
36 /**
37 * @var string
38 */
39 protected $tagName = 'input';
40
41 /**
42 * Initialize the arguments.
43 *
44 * @return void
45 * @api
46 */
47 public function initializeArguments() {
48 parent::initializeArguments();
49 $this->registerTagAttribute('disabled', 'string', 'Specifies that the input element should be disabled when the page loads');
50 $this->registerTagAttribute('multiple', 'string', 'Specifies that the file input element should allow multiple selection of files');
51 $this->registerArgument('errorClass', 'string', 'CSS class to set if there are errors for this view helper', FALSE, 'f3-form-error');
52 $this->registerUniversalTagAttributes();
53 }
54
55 /**
56 * Renders the upload field.
57 *
58 * @return string
59 * @api
60 */
61 public function render() {
62 $name = $this->getName();
63 $allowedFields = array('name', 'type', 'tmp_name', 'error', 'size');
64 foreach ($allowedFields as $fieldName) {
65 $this->registerFieldNameForFormTokenGeneration($name . '[' . $fieldName . ']');
66 }
67 $this->tag->addAttribute('type', 'file');
68
69 if (isset($this->arguments['multiple'])) {
70 $this->tag->addAttribute('name', $name . '[]');
71 } else {
72 $this->tag->addAttribute('name', $name);
73 }
74
75 $this->setErrorClassAttribute();
76 return $this->tag->render();
77 }
78
79 }