Пример #1
0
 /**
  * Update the mapped file for Entity (obj)
  *
  * @param \Doctrine\ORM\Event\PreUpdateEventArgs  $args
  */
 public function preUpdate(PreUpdateEventArgs $args)
 {
     $mappings = $this->mappingFactory->fromEventArgs($args);
     foreach ($mappings as $mapping) {
         //Uploaded or setted file
         $file = $mapping->getFilePropertyValue();
         $currentFileData = $args->hasChangedField($mapping->getPropertyName()) ? $args->getOldValue($mapping->getPropertyName()) : null;
         //If no new file
         if (is_null($file) || !$file instanceof File) {
             //Preserve old fileData if current file exists, else null
             if ($currentFileData) {
                 $mapping->setFileDataPropertyValue($this->fileStorage->checkFileExists($currentFileData) ? $currentFileData : null);
             }
         } else {
             if ($file instanceof \Iphp\FileStoreBundle\File\File && $file->isDeleted()) {
                 if ($this->fileStorage->removeFile($currentFileData)) {
                     $mapping->setFileDataPropertyValue(null);
                 }
             } else {
                 if ($currentFileData && !$this->fileStorage->isSameFile($file, $currentFileData)) {
                     $this->fileStorage->removeFile($currentFileData);
                 }
                 $fileData = $this->fileStorage->upload($mapping, $file);
                 $mapping->setFileDataPropertyValue($fileData);
             }
         }
     }
     $this->dataStorage->recomputeChangeSet($args);
 }
 public function preBind(FormEvent $event)
 {
     $form = $event->getForm();
     $propertyName = $form->getName();
     $obj = $form->getParent()->getData();
     //For oneToMany at SonataAdmin
     if (!$obj) {
         return;
     }
     $mapping = $this->mappingFactory->getMappingFromField($obj, $this->dataStorage->getReflectionClass($obj), $propertyName);
     if ($mapping) {
         /*        $form->add('file', 'file', array('required' => false))
                   ->add('delete', 'checkbox', array('required' => false));*/
         $this->transformer->setMapping($mapping, $mapping->getFileUploadPropertyName() == $propertyName ? FileDataTransformer::MODE_UPLOAD_FIELD : FileDataTransformer::MODE_FILEDATA_FIELD);
     }
 }
 function testPreBind()
 {
     $obj = new DummyEntity();
     $class = new \ReflectionClass($obj);
     $formEvent = $this->getMockBuilder('Symfony\\Component\\Form\\FormEvent')->disableOriginalConstructor()->getMock();
     $form = $this->getMockBuilder('Symfony\\Component\\Form\\Form')->disableOriginalConstructor()->getMock();
     $parentForm = $this->getMockBuilder('Symfony\\Component\\Form\\Form')->disableOriginalConstructor()->getMock();
     $formEvent->expects($this->once())->method('getForm')->will($this->returnValue($form));
     $form->expects($this->once())->method('getParent')->will($this->returnValue($parentForm));
     $parentForm->expects($this->once())->method('getData')->will($this->returnValue($obj));
     $this->dataStorage->expects($this->once())->method('getReflectionClass')->with($obj)->will($this->returnValue($class));
     $propertyMapping = Mocks::getPropertyMappingMock($this);
     $this->propertyMappingFactory->expects($this->once())->method('getMappingFromField')->with($obj, $class, 'file')->will($this->returnValue($propertyMapping));
     $form->expects($this->once())->method('getName')->will($this->returnValue('file'));
     $this->transformer->expects($this->once())->method('setMapping')->with($propertyMapping);
     $this->subscriber->preBind($formEvent);
 }
Пример #4
0
 /**
  * Creates the property mapping from the read annotation and configured mapping.
  *
  * @param  object                                          $obj   The object.
  * @param  \Iphp\FileStoreBundle\Mapping\Annotation\UploadableField $field The read annotation.
  * @return \Iphp\FileStoreBundle\Mapping\PropertyMapping     The property mapping.
  * @throws \InvalidArgumentException
  */
 protected function createMapping($obj, UploadableField $field)
 {
     $class = $this->dataStorage->getReflectionClass($obj);
     if (!array_key_exists($field->getMapping(), $this->mappings)) {
         throw new \InvalidArgumentException(sprintf('No mapping named "%s" configured.', $field->getMapping()));
     }
     $config = $this->mappings[$field->getMapping()];
     $mapping = new PropertyMapping($obj, $config, $this->container);
     $mapping->setProperty($class->getProperty($field->getPropertyName()));
     $mapping->setFileNameProperty($class->getProperty($field->getFileNameProperty()));
     $mapping->setMappingName($field->getMapping());
     return $mapping;
 }
 /**
  * Test the postFlush method has deferred files
  */
 public function testPostFlushHasDeferred()
 {
     $args = Mocks::getEventArgsMock($this);
     $obj = new DummyEntity();
     $file = Mocks::getFileMock($this);
     $propertyMapping = $this->createFilledPropertyMapping($file);
     $this->setDataStorageObjectMapping($obj, $propertyMapping);
     $listener = $this->getUploaderListener();
     $listener->prePersist(Mocks::getEventArgsMock($this));
     $this->assertEquals($listener->getDeferredObjectNum(), 1);
     $this->fileStorage->expects($this->once())->method('upload')->with($propertyMapping, $file)->will($this->returnValue(array('fileName' => 'NEW_NAME')));
     $propertyMapping->expects($this->once())->method('setFileDataPropertyValue')->with(array('fileName' => 'NEW_NAME'));
     $this->dataStorage->expects($this->once())->method('postFlush');
     $listener->postFlush($args);
     $this->assertEquals($listener->getDeferredObjectNum(), 0);
 }
 /**
  * upload field and file data field are SAME ($obj->file)
  * @param EventArgs $args
  * @param PropertyMapping $mapping
  */
 protected function updateSeparateProperties(\Doctrine\Common\EventArgs $args, PropertyMapping $mapping)
 {
     $uploadedFile = $mapping->getFileUploadPropertyValue();
     $currentFileData = $mapping->getFileDataPropertyValue();
     $previousFileData = $this->dataStorage->previusFieldDataIfChanged($mapping->getFileDataPropertyName(), $args);
     $currentFileName = $previousFileData ? $mapping->resolveFileName($previousFileData['fileName']) : null;
     //delete current file
     if ($previousFileData && (is_null($currentFileData) || $uploadedFile && $uploadedFile instanceof \Iphp\FileStoreBundle\File\File && $uploadedFile->isDeleted())) {
         if ($this->fileStorage->removeFile($currentFileName)) {
             $mapping->setFileDataPropertyValue(null);
         }
     } else {
         if ($uploadedFile && $uploadedFile instanceof File) {
             //Old value (file) exists and uploaded new file
             if ($currentFileName && !$this->fileStorage->isSameFile($uploadedFile, $currentFileName)) {
                 //before upload new file delete old file
                 $this->fileStorage->removeFile($currentFileName);
             }
             $fileData = $this->fileStorage->upload($mapping, $uploadedFile);
             $mapping->setFileDataPropertyValue($fileData);
         }
     }
 }