Beispiel #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);
 }
 /**
  * Обработать мультиязычную загрузку файла
  * и получить подготовленные данные для сохранения
  * @param string $value - значение пришедшее из формы
  * @param string $field - обрабатываемое поле
  * @param string $locale - локаль
  * @param object $entity - сущность которая сохраняется
  * @return array|null
  */
 protected function processFileUpload($value, $field, $locale, $entity)
 {
     $result = false;
     if (isset($value['delete']) && $value['delete']) {
         $this->deleteFileByOldData($value, $field, $entity);
         //чтобы фронт не ломался и не было NOTICE в iPhp
         $result = ['fileName' => null, 'originalName' => null, 'mimeType' => null, 'size' => null, 'path' => null];
     } elseif (isset($value['file']) && $value['file'] instanceof File) {
         // Локаль указвыается в сущности для того,
         // чтобы её можно было использовать
         // при формировании имени файла
         $entity->setLocale($locale);
         $fileMapping = $this->getFileMappings($entity)[$field];
         $fileData = $this->fileStorage->upload($fileMapping, $value['file']);
         //Сбрасываем локаль
         $entity->setLocale(null);
         $result = $fileData;
     } elseif (isset($value['oldData'])) {
         $result = unserialize($value['oldData']);
     }
     return $result;
 }
 /**
  * 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);
         }
     }
 }