Пример #1
0
 /**
  * @return \app\modules\document\models\Attachment|bool
  * @throws \yii\base\Exception
  */
 public function upload()
 {
     if ($this->validate()) {
         $root = \Yii::getAlias('@webroot');
         $path = '/uploads/' . date('Y-m-d');
         $uploadPath = $root . $path;
         $file = $this->file;
         //check for the existence of a folder
         if (!file_exists($uploadPath)) {
             FileHelper::createDirectory($uploadPath, 0777, true);
         }
         //check for file with same name is exists
         $srcPath = $path . '/' . $file->name;
         $savePath = $uploadPath . '/' . $file->name;
         if (file_exists($savePath)) {
             $t = time();
             $savePath = $uploadPath . '/' . $t . "_" . $file->name;
             $srcPath = $path . '/' . $t . "_" . $file->name;
         }
         //move uploaded file and save it into database
         if ($file->saveAs($savePath)) {
             return Attachment::createNew($file, $srcPath);
         }
     }
     return false;
 }
Пример #2
0
 /**
  * Delete attachment from server and db.
  *
  * @var $attachment_id
  *
  * @return bool
  * @throws \Exception
  */
 public function actionDelete()
 {
     if (\Yii::$app->request->isPost && \Yii::$app->request->isAjax) {
         $attachment_id = \Yii::$app->request->post('attachment_id');
         //if attachment with id is exists
         if ($attachment = Attachment::findOne(["id" => $attachment_id])) {
             /* @var Attachment $attachment */
             $attachment->delete();
             echo \yii\helpers\Json::encode(['status' => '1']);
         }
     }
     return false;
 }
Пример #3
0
 /**
  * @return int|string
  */
 public function getAttachmentsCount()
 {
     return $this->hasMany(Attachment::className(), ['document_id' => 'id'])->count();
 }