예제 #1
0
 public function actionUpload($id, $type)
 {
     if ($type == 'catalog' or $type == 'article') {
         $success = null;
     } else {
         throw new NotFoundHttpException('The requested page does not exist.');
     }
     $photo = new Medias();
     if (Yii::$app->request->isAjax) {
         $photoUpload = UploadedFile::getInstance($photo, 'image');
         if ($photoUpload && $photo->validate(['image'])) {
             $exp = explode(".", $photoUpload->name);
             $ext = end($exp);
             $photoName = uniqid($id . '_') . '.' . $ext;
             $upload_path = Yii::getAlias('@webroot') . '/uploads/media/' . $photoName;
             $upload_path_thumb = Yii::getAlias('@webroot') . '/uploads/media/resized/' . $photoName;
             if ($photoUpload->saveAs($upload_path) and Image::thumbnail($upload_path, 460, 300, ManipulatorInterface::THUMBNAIL_INSET)->save($upload_path_thumb, ['quality' => 100])) {
                 $photo->file_title = $photoName;
                 $photo->file_type = $type;
                 $photo->file_url = '/uploads/media/' . $photoName;
                 $photo->file_url_thumb = '/uploads/media/resized/' . $photoName;
                 if ($photo->save()) {
                     if ($type == 'article') {
                         $content_medias = new ContentArticlesMedias();
                         $content_medias->content_articles_id = $id;
                         $content_medias->media_id = $photo->media_id;
                         $content_medias->save();
                     }
                     $success = ['message' => Yii::t('admin', 'Photo uploaded'), 'photo' => ['id' => $photo->media_id, 'image' => $photo->file_url, 'thumb' => $photo->file_url_thumb, 'description' => '']];
                 } else {
                     $this->flash('error', $photo->getErrors());
                 }
             }
         } else {
             $this->flash('error', Yii::t('admin', 'File is incorrect'));
         }
     } else {
         $this->flash('error', Yii::t('admin', 'Not ajax'));
     }
     return $this->formatResponse($success);
 }
예제 #2
0
 public function getArticlesMedias()
 {
     return $this->hasMany(ContentArticlesMedias::className(), ['content_articles_id' => 'id']);
 }