-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathUploadedResourceViewHelper.php
More file actions
189 lines (165 loc) · 6.32 KB
/
Copy pathUploadedResourceViewHelper.php
File metadata and controls
189 lines (165 loc) · 6.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
<?php
declare(strict_types=1);
/*
* This file is part of the TYPO3 CMS project.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* The TYPO3 project - inspiring people to share!
*/
namespace WapplerSystems\FormExtended\ViewHelpers\Form;
use TYPO3\CMS\Core\Crypto\HashService;
use TYPO3\CMS\Extbase\Domain\Model\FileReference;
use TYPO3\CMS\Extbase\Property\PropertyMapper;
use TYPO3\CMS\Fluid\ViewHelpers\Form\AbstractFormFieldViewHelper;
use TYPO3\CMS\Form\Security\HashScope;
/**
* This ViewHelper makes the specified Image object available for its
* childNodes.
* In case the form is redisplayed because of validation errors, a previously
* uploaded image will be correctly used.
*
* Scope: frontend
*/
class UploadedResourceViewHelper extends AbstractFormFieldViewHelper
{
protected $tagName = 'input';
/**
* @var HashService
*/
protected $hashService;
/**
* @var \TYPO3\CMS\Extbase\Property\PropertyMapper
*/
protected $propertyMapper;
/**
* @param HashService $hashService
* @internal
*/
public function injectHashService(HashService $hashService)
{
$this->hashService = $hashService;
}
/**
* @param \TYPO3\CMS\Extbase\Property\PropertyMapper $propertyMapper
* @internal
*/
public function injectPropertyMapper(PropertyMapper $propertyMapper)
{
$this->propertyMapper = $propertyMapper;
}
/**
* Initialize the arguments.
*
* @internal
*/
public function initializeArguments(): void
{
parent::initializeArguments();
$this->registerTagAttribute('disabled', 'string', 'Specifies that the input element should be disabled when the page loads');
$this->registerTagAttribute('multiple', 'string', 'Specifies that the file input element should allow multiple selection of files');
$this->registerArgument('accept', 'array', 'Values for the accept attribute', false, []);
$this->registerArgument('errorClass', 'string', 'CSS class to set if there are errors for this ViewHelper', false, 'f3-form-error');
$this->registerUniversalTagAttributes();
$this->registerArgument('as', 'string', '');
}
/**
* @return string
*/
public function render(): string
{
$output = '';
$as = $this->arguments['as'];
$accept = $this->arguments['accept'];
$resources = $this->getUploadedResources();
if (!empty($accept)) {
$this->tag->addAttribute('accept', implode(',', $accept));
}
$name = $this->getName();
$allowedFields = ['name', 'type', 'tmp_name', 'error', 'size'];
foreach ($allowedFields as $fieldName) {
$this->registerFieldNameForFormTokenGeneration($name . '[' . $fieldName . ']');
}
$this->tag->addAttribute('type', 'file');
if (isset($this->arguments['multiple'])) {
$multiple = true;
$this->tag->addAttribute('name', $name . '[]');
} else {
$this->tag->addAttribute('name', $name);
$multiple = false;
}
$this->setErrorClassAttribute();
$output .= $this->tag->render();
if ($resources !== null) {
if ($resources instanceof FileReference) {
$resources = [$resources];
}
foreach ($resources as $key => $resource) {
$resourcePointerIdAttribute = '';
if ($this->hasArgument('id')) {
$resourcePointerIdAttribute = ' id="' . htmlspecialchars($this->arguments['id']) . '-file-reference-' . $key . '"';
}
$resourcePointerValue = $resource->getUid();
if ($resourcePointerValue === null) {
// Newly created file reference which is not persisted yet.
// Use the file UID instead, but prefix it with "file:" to communicate this to the type converter
$resourcePointerValue = 'file:' . $resource->getOriginalResource()->getOriginalFile()->getUid();
}
$output .= '<input type="hidden" name="' . htmlspecialchars($this->getName()) . '[submittedFile][resourcePointer][]" value="' . htmlspecialchars($this->hashService->appendHmac((string)$resourcePointerValue, HashScope::ResourcePointer->prefix())) . '"' . $resourcePointerIdAttribute . ' />';
}
if ($multiple == false) {
$resources = reset($resources);
}
$this->templateVariableContainer->add($as, $resources);
$output .= $this->renderChildren();
$this->templateVariableContainer->remove($as);
}
return $output;
}
/**
* Return a previously uploaded resource.
* Return NULL if errors occurred during property mapping for this property.
*
* @return array
*/
protected function getUploadedResources()
{
if ($this->getMappingResultsForProperty()->hasErrors()) {
return null;
}
$return = [];
$resources = $this->getValueAttribute();
if (is_array($resources)) {
foreach ($resources as $resource) {
if ($resource === null) continue;
if ($resource instanceof FileReference) {
$return[] = $resource;
continue;
}
$ex = $this->propertyMapper->convert($resource, FileReference::class);
$return = array_merge($return, $ex);
}
} elseif ($resources instanceof FileReference) {
return $resources;
}
return $return;
}
protected function getValueAttribute()
{
$value = null;
if ($this->respectSubmittedDataValue) {
$value = $this->getValueFromSubmittedFormData($value);
} elseif ($this->hasArgument('value')) {
$value = $this->arguments['value'];
} elseif ($this->isObjectAccessorMode()) {
$value = $this->getPropertyValue();
}
$value = $this->convertToPlainValue($value);
return $value;
}
}