/** * @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 // TODO: use PropertyPath $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); $keysToRemove = array(); $dataFiles = $data->{$getter}(); foreach ($dataFiles as $key => $file) { if (!in_array($file->{$pkGetter}(), $this->submitted_pk)) { $keysToRemove[] = $key; } } foreach ($keysToRemove as $key) { $dataFiles->remove($key); } } if ($this->allow_add) { // create file entites for each file foreach ($this->uploads as $upload) { $loadAdditionalContentUid = null; if (!is_object($upload) && !is_null($this->storage)) { $loadAdditionalContentUid = $upload; $upload = $this->storage->getFile($upload); } if ($upload === null) { continue; } $fileOwner = new $this->dataClass(); if (!$fileOwner instanceof UploadCollectionFileInterface) { throw new UnexpectedTypeException($fileOwner, '\\Avocode\\FormExtensionsBundle\\Form\\Model\\UploadCollectionFileInterface'); } $fileOwner->setFile($upload); $fileOwner->setParent($data); // if nameable field specified - set normalized name if ($this->nameable && $this->nameable_field) { // TODO: use PropertyPath $setNameable = 'set' . ucfirst($this->nameable_field); // this value is unsafe $name = $upload->getClientOriginalName(); // normalize string $safeName = $this->normalizeUtf8String($name); $fileOwner->{$setNameable}($safeName); } $this->loadAdditionalContent($loadAdditionalContentUid, $fileOwner); $data->{$getter}()->add($fileOwner); } } $event->setData($data->{$getter}()); }