/**
  * @inheritdoc
  */
 public function run()
 {
     if ($this->getRequest()->isPost) {
         $result = [];
         $modelClass = $this->modelClass;
         /** @var ActiveRecord $model */
         $model = new $modelClass();
         /** @var AttachBehavior $behavior */
         $behavior = $model->getBehavior(AttachBehavior::NAME);
         /** @var UploadedFile $file */
         $file = Manager::getUploadedFile();
         if ($behavior && $file) {
             /**
              * @var  AttachmentFile $instance
              */
             $instance = $behavior->getAttach($this->attribute);
             if ($instance) {
                 $instance->setFile($file)->setUser($this->getUser());
                 $status = $instance->save();
                 $result = $this->formatFile($instance, $status);
             }
         } else {
             throw new NotSupportedException('Upload for this model not supported, ensure you attach behavior');
         }
         Yii::$app->response->format = Response::FORMAT_JSON;
         return $result;
     } else {
         throw new BadRequestHttpException('Only POST is allowed');
     }
 }
 public function init()
 {
     parent::init();
     /** @var Manager $attachment */
     $attachment = Manager::getInstance();
     if (!$attachment instanceof Manager) {
         throw new InvalidConfigException('Attachment Manager component not defined');
     }
     $this->attachmentTable = $attachment->attachmentFileTable;
 }
Esempio n. 3
0
 /**
  * @inheritdoc
  */
 public function attach($owner)
 {
     parent::attach($owner);
     if (!is_array($this->models) || empty($this->models)) {
         throw new InvalidParamException('Invalid or empty models array.');
     } else {
         foreach ($this->models as $relationName => $config) {
             Manager::getInstance()->addAttachmentModel($this->owner, $relationName, $config);
             $this->checkRelationExistence($relationName);
         }
     }
 }
Esempio n. 4
0
 /**
  * @return AttachmentFile
  */
 public function getAttachmentModel()
 {
     return Manager::getInstance()->getAttachmentModel($this->modelClass, $this->attribute);
 }
Esempio n. 5
0
 /**
  * @inheritdoc
  */
 public function beforeSave($insert)
 {
     if (parent::beforeSave($insert)) {
         if ($insert) {
             $this->type = static::TYPE;
             if (!is_numeric($this->status_id)) {
                 $this->status_id = self::STATUS_TEMPORARY;
             }
             Manager::getInstance()->createDirectory($this->getDirPath());
             if (!$this->file->saveAs($this->getFilePath())) {
                 throw new ErrorException("File @storage/{$this->uri} cannot be saved.");
             }
         }
         return true;
     } else {
         return false;
     }
 }
Esempio n. 6
0
<?php

use artkost\attachment\Manager;
return ['class' => Manager::className()];