/** * @param GetResponseEvent $event * @return GetResponseEvent */ public function onRequestHandler(GetResponseEvent $event) { $request = $event->getRequest(); if ($request->attributes->get('_route') == $this->routeName) { if ($request->getMethod() == 'POST') { if (!($propertyPath = $request->request->get('propertyPath'))) { throw new NotFoundHttpException(); } $files = $this->propertyAccessor->getValue($request->files->all(), $this->fixPropertyPath($propertyPath)); $files = $this->formatResponse($this->storage->storeFiles($files)); $event->setResponse(new JsonResponse($files)); } else { // We can avoid that if we force the method in the route declaration throw new NotFoundHttpException(); } } return $event; }
public function onSubmit(FormEvent $event) { $form = $event->getForm(); $data = $form->getParent()->getData(); // save original files collection for postSubmit event $getter = 'get' . ucfirst($this->propertyName); $this->originalFiles = $data->{$getter}(); if ($this->allow_delete) { // remove files not present in submitted pk $pkGetter = 'get' . ucfirst($this->primary_key); foreach ($data->{$getter}() as $file) { if (!in_array($file->{$pkGetter}(), $this->submitted_pk)) { $data->{$getter}()->removeElement($file); } } } if ($this->allow_add) { // create file entites for each file foreach ($this->uploads as $upload) { if (!is_object($upload) && !is_null($this->storage)) { // read submitted editable $editable = array_key_exists($upload, $this->editable) ? $this->editable[$upload] : array(); $upload = $this->storage->getFile($upload); } if ($upload === null) { continue; } $file = new $this->dataClass(); if (!$file instanceof UploadCollectionFileInterface) { throw new UnexpectedTypeException($file, '\\Admingenerator\\FormExtensionsBundle\\Form\\Model\\UploadCollectionFileInterface'); } $file->setFile($upload); $file->setParent($data); foreach ($editable as $editableField => $editableValue) { $setEditable = 'set' . ucfirst($editableField); $file->{$setEditable}($editableValue); } // if nameable field specified - set normalized name if ($this->nameable && $this->nameable_field) { $setNameable = 'set' . ucfirst($this->nameable_field); // this value is unsafe $name = $upload->getClientOriginalName(); // normalize string $safeName = $this->normalizeUtf8String($name); $file->{$setNameable}($safeName); } $data->{$getter}()->add($file); } } $event->setData($data->{$getter}()); }