Beispiel #1
0
 /**
  * Automatically upload files for $domainObject based on $propertyName. Uses
  * uploadfolder from $domainObject if specified. Can be overridden with
  * $basePath - which means files will be uploaded to that path AND THE FULL
  * RELATIVE PATH IS SAVED to the $domainObject $propertyName.
  * Returns TRUE on upload success.
  * If you do not specify a particular $propertyName then the fields are taken
  * from your DomainObject using the @file (className of resource type) annotation.
  *
  * See documentation for further instructions on integrating files.
  *
  * @param Tx_Extbase_DomainObject_DomainObjectInterface $domainObject
  * @param string $propertyName
  * @param string $basePath
  * @throws Exception
  * @return boolean
  * @api
  */
 public function autoUpload(Tx_Extbase_DomainObject_DomainObjectInterface &$domainObject, $propertyName = NULL, $basePath = NULL)
 {
     if ($propertyName === NULL) {
         $propertyNames = $this->domainService->getPropertiesByAnnotation($domainObject, 'file', TRUE, FALSE);
         foreach ($propertyNames as $propertyName) {
             if ($this->autoUpload($domainObject, $propertyName) === FALSE) {
                 return FALSE;
             }
         }
         return TRUE;
     }
     $uploadFolder = $this->domainService->getUploadFolder($domainObject, $propertyName);
     $uploadedFileObjectStorage = $this->objectManager->get('Tx_Tool_Resource_FileResourceObjectStorage');
     $uploadedFileObjectStorage->setBasePath($uploadFolder);
     $objectType = array_pop($this->domainService->getAnnotationValuesByProperty($domainObject, $propertyName, 'file'));
     if (!$objectType) {
         $objectType = 'Tx_Tool_Resource_FileResource';
     }
     $fileObjectStorage = $this->getUploadedFiles($domainObject, $propertyName, $objectType);
     foreach ($fileObjectStorage as $fileObject) {
         $source = $fileObject->getAbsolutePath();
         $destination = $uploadFolder . '/' . $fileObject->getTargetFilename();
         $newFilename = $this->move($source, $destination);
         $fileObject->setAbsolutePath(PATH_site . $newFilename);
         $uploadedFileObjectStorage->attach($fileObject);
     }
     $setter = 'set' . ucfirst($propertyName);
     $valueToSet = (string) $uploadedFileObjectStorage;
     $domainObject->{$setter}($valueToSet);
     return TRUE;
 }
 /**
  * Define the source parent of this FileResourceObjectStorage - enables automatic
  * detection of $objectType and $basePath based on $propertyName and TCA.
  *
  * @param Tx_Extbase_DomainObject_AbstractDomainObject $associatedDomainObject
  * @param string $propertyName Name of the property in which this object is contained
  */
 public function setAssociatedDomainObject(Tx_Extbase_DomainObject_AbstractDomainObject $associatedDomainObject, $propertyName)
 {
     $annotationValues = $this->domainService->getAnnotationValuesByProperty($associatedDomainObject, $propertyName, 'file');
     // use collected data to set necessary precursor variables
     $this->objectType = array_pop($annotationValues);
     $this->associatedDomainObject = $associatedDomainObject;
     $this->associatedPropertyName = $propertyName;
     $this->basePath = $this->domainService->getUploadFolder($associatedDomainObject, $propertyName);
 }