Exemplo n.º 1
0
 public static function uploadFile(AttachedFile $attachedFile, $attributes, $modelType, $modelId, $fileInstance)
 {
     $attachedFile->attributes = $attributes;
     $attachedFile->modelType = $modelType;
     $attachedFile->modelId = $modelId;
     if ($attachedFile->attachType == AttachedFile::ATTACH_TYPE_FILE) {
         $attachedFile->file = $fileInstance;
         $basePath = Yii::getPathOfAlias('webroot') . '/public/attached';
         if (!file_exists($basePath)) {
             mkdir($basePath, 0777, true);
         }
         $path = $basePath . '/' . md5(time() . $attachedFile->file->getName());
         $attachedFile->filePath = $path;
         $attachedFile->fileName = $attachedFile->file->getName();
         $attachedFile->fileSize = $attachedFile->file->getSize();
         if ($attachedFile->validate()) {
             $attachedFile->file->saveAs($path);
         }
     }
     $attachedFile->save();
     return $attachedFile;
 }
 /**
  * Attach project file to this object
  *
  * @param ProjectFile $file
  * @return AttachedFiles
  */
 function attachFile(ProjectFile $file)
 {
     $manager_class = get_class($this->manager());
     $object_id = $this->getObjectId();
     $attached_file = AttachedFiles::findById(array('rel_object_manager' => $manager_class, 'rel_object_id' => $object_id, 'file_id' => $file->getId()));
     // findById
     if ($attached_file instanceof AttachedFile) {
         return $attached_file;
         // Already attached
     }
     // if
     $attached_file = new AttachedFile();
     $attached_file->setRelObjectManager($manager_class);
     $attached_file->setRelObjectId($object_id);
     $attached_file->setFileId($file->getId());
     $attached_file->save();
     if (!$file->getIsVisible()) {
         $file->setIsVisible(true);
         $file->setExpirationTime(EMPTY_DATETIME);
         $file->save();
     }
     // if
     return $attached_file;
 }