3
3
namespace FullscreenInteractive \ManyField ;
4
4
5
5
use Dotenv \Exception \ValidationException ;
6
+ use Exception ;
6
7
use SilverStripe \Forms \GridField \GridField ;
7
8
use FullscreenInteractive \ManyField \ManyField ;
9
+ use Psr \Log \LoggerInterface ;
8
10
use SilverStripe \Assets \Upload ;
11
+ use SilverStripe \Core \Injector \Injector ;
9
12
use SilverStripe \Forms \FieldList ;
10
13
use SilverStripe \Forms \GridField \GridFieldButtonRow ;
11
14
use SilverStripe \Forms \GridField \GridFieldConfig ;
@@ -156,9 +159,18 @@ public function getValueFromData($data)
156
159
157
160
foreach ($ this ->Children () as $ field )
158
161
{
159
- foreach ($ incoming [$ field ->Name ] as $ i => $ value ) {
160
- if ($ value && !empty ($ value )) {
161
- $ rowHasValue [$ i ] = true ;
162
+ if (isset ($ incoming [$ field ->Name ])) {
163
+ foreach ($ incoming [$ field ->Name ] as $ i => $ value ) {
164
+ if ($ value && !empty ($ value )) {
165
+ $ rowHasValue [$ i ] = true ;
166
+ }
167
+ }
168
+ } elseif (isset ($ incoming ['name ' ]) && isset ($ incoming ['name ' ][$ field ->Name ])) {
169
+ // handle multi-part
170
+ foreach ($ incoming ['name ' ][$ field ->Name ] as $ i => $ value ) {
171
+ if ($ value && !empty ($ value )) {
172
+ $ rowHasValue [$ i ] = true ;
173
+ }
162
174
}
163
175
}
164
176
}
@@ -167,21 +179,37 @@ public function getValueFromData($data)
167
179
168
180
foreach ($ this ->Children () as $ field )
169
181
{
170
- foreach ($ incoming [$ field ->Name ] as $ i => $ value ) {
171
- if (!isset ($ rowHasValue [$ i ])) {
172
- // empty row;
173
- continue ;
174
- }
182
+ if (isset ($ incoming [$ field ->Name ])) {
183
+ foreach ($ incoming [$ field ->Name ] as $ i => $ value ) {
184
+ if (!isset ($ rowHasValue [$ i ])) {
185
+ // empty row;
186
+ continue ;
187
+ }
188
+
189
+ if (!isset ($ rows [$ i ])) {
190
+ $ rows [$ i ] = [];
191
+ }
175
192
176
- if (!isset ($ rows [$ i ])) {
177
- $ rows [$ i ] = [];
193
+ $ submittedField = $ this ->createNestedSubmittedFormField ($ field , [
194
+ $ field ->Name => $ value
195
+ ]);
196
+
197
+ $ rows [$ i ][$ field ->Name ] = $ submittedField ->ID ;
178
198
}
199
+ } elseif (isset ($ incoming ['name ' ]) && isset ($ incoming ['name ' ][$ field ->Name ])) {
200
+ // handle multi-part
201
+ foreach ($ incoming ['name ' ][$ field ->Name ] as $ i => $ value ) {
202
+ if (!isset ($ rowHasValue [$ i ])) {
203
+ // empty row;
204
+ continue ;
205
+ }
179
206
180
- $ submittedField = $ this ->createNestedSubmittedFormField ($ field , [
181
- $ field ->Name => $ value
182
- ]);
207
+ $ submittedField = $ this ->createNestedSubmittedFormField ($ field , [
208
+ $ field ->Name => $ value
209
+ ]);
183
210
184
- $ rows [$ i ][$ field ->Name ] = $ submittedField ->ID ;
211
+ $ rows [$ i ][$ field ->Name ] = $ submittedField ->ID ;
212
+ }
185
213
}
186
214
}
187
215
@@ -204,26 +232,42 @@ public function createNestedSubmittedFormField(EditableFormField $field, $data)
204
232
}
205
233
}
206
234
207
- if (!empty ($ data [$ field ->Name ])) {
208
- if (in_array (EditableFileField::class, $ field ->getClassAncestry ())) {
209
- if (!empty ($ _FILES [$ field ->Name ]['name ' ])) {
210
- $ foldername = $ field ->getFormField ()->getFolderName ();
211
- $ upload = Upload::create ();
235
+ $ file = null ;
236
+ if (!empty ($ _FILES [$ field ->Name ]['name ' ])) {
237
+ $ file = $ _FILES [$ field ->Name ];
238
+ } else if (!empty ($ _FILES [$ this ->Name ]['name ' ])) {
239
+ if (!empty ($ _FILES [$ this ->Name ]['name ' ][$ field ->Name ])) {
240
+ $ file = [
241
+ 'tmp_name ' => $ _FILES [$ this ->Name ]['tmp_name ' ][$ field ->Name ][0 ],
242
+ 'type ' => $ _FILES [$ this ->Name ]['type ' ][$ field ->Name ][0 ],
243
+ 'name ' => $ _FILES [$ this ->Name ]['name ' ][$ field ->Name ][0 ],
244
+ 'error ' => $ _FILES [$ this ->Name ]['error ' ][$ field ->Name ][0 ],
245
+ 'size ' => $ _FILES [$ this ->Name ]['size ' ][$ field ->Name ][0 ]
246
+ ];
247
+ }
248
+ }
212
249
213
- try {
214
- $ upload ->loadIntoFile ($ _FILES [$ field ->Name ], null , $ foldername );
250
+ if ($ file ) {
251
+ $ foldername = $ field ->getFormField ()->getFolderName ();
252
+ $ upload = Upload::create ();
215
253
216
- /** @var AssetContainer|File $file */
217
- $ file = $ upload ->getFile ();
218
- $ file ->ShowInSearch = 0 ;
219
- $ file ->UserFormUpload = UserFormFileExtension::USER_FORM_UPLOAD_TRUE ;
220
- $ file ->write ();
254
+ try {
255
+ $ result = $ upload ->loadIntoFile ($ file , null , $ foldername );
221
256
222
- $ submittedField -> UploadedFileID = $ file -> ID ;
223
- } catch ( ValidationException $ e ) {
257
+ /** @var AssetContainer|File $fileObj */
258
+ $ fileObj = $ upload -> getFile ();
224
259
225
- }
260
+ if ($ result && $ fileObj ) {
261
+ $ fileObj ->ShowInSearch = 0 ;
262
+ $ fileObj ->UserFormUpload = UserFormFileExtension::USER_FORM_UPLOAD_TRUE ;
263
+ $ fileObj ->write ();
264
+
265
+ $ submittedField ->UploadedFileID = $ fileObj ->ID ;
266
+ } else {
267
+ throw new Exception ('Could not upload files: %s ' , implode ($ upload ->getErrors ()));
226
268
}
269
+ } catch (ValidationException $ e ) {
270
+ Injector::inst ()->get (LoggerInterface::class)->error ($ e );
227
271
}
228
272
}
229
273
0 commit comments