Пример #1
0
 /**
  * upload new media files and link to a given content category id
  * @menuLabel __HIDDEN__
  * @return string
  */
 public function actionUpload($targetCategoryId, $mediaType = null)
 {
     if ($mediaType != null && MediaController::$MEDIA_TYPE_AUDIO != $mediaType && MediaController::$MEDIA_TYPE_IMAGE != $mediaType && MediaController::$MEDIA_TYPE_VIDEO != $mediaType) {
         throw new InvalidParamException('The media type ' . $mediaType . ' is not known.');
     }
     $model = new MediaBrowserImageUpload();
     $targetCategoryId = intval($targetCategoryId);
     $model->targetCategoryId = $targetCategoryId;
     $msg = "";
     if (Yii::$app->request->isPost) {
         $model->file = UploadedFile::getInstances($model, 'file');
         //check if folder exists
         $cagtegory = CmsContentCategory::findOne($targetCategoryId);
         if ($cagtegory == null) {
             throw new InvalidValueException('the given catgeory id is not valid. Upload failed');
         }
         if ($model->file && $model->validate()) {
             foreach ($model->file as $file) {
                 /* @VAR $file UploadedFile */
                 $targetPath = $this->getFullUploadPathForFile($file);
                 if ($file->saveAs($targetPath)) {
                     $pathInfo = pathinfo($targetPath);
                     $content = new CmsContentMedia();
                     $content->init();
                     $content->mime_type = BaseFileHelper::getMimeType($targetPath);
                     $content->file_name = $pathInfo['basename'];
                     $content->file_path = $pathInfo['dirname'];
                     $content->filesize_bytes = $file->size;
                     $content->content_category_id = $targetCategoryId;
                     $content->media_type = $this->module->getMediaTypeForMimeType($content->mime_type);
                     if ($content->media_type == MediaController::$MEDIA_TYPE_IMAGE) {
                         $dimensions = $this->getImageDimensions($targetPath);
                         if ($dimensions != null) {
                             $content->dimension_width = $dimensions['width'];
                             $content->dimension_height = $dimensions['height'];
                         } else {
                             $msg .= 'Unable to detect image dimensions for image ' . $content->file_name;
                         }
                     }
                     $content->created_datetime = new Expression('NOW()');
                     $content->createdby_userid = Yii::$app->user->id;
                     if (!$content->insert(true)) {
                         $this->layout = 'modalLayout';
                         return $this->render('fileUpload', ['model' => $model, 'errors' => $content->errors, 'mediaType' => $mediaType, 'msg' => $msg]);
                         //throw new Exception('The upload of one or more files failed. Most likely validation of properties failed');
                     }
                 } else {
                     throw new \Exception('The upload of one or more files failed.');
                 }
             }
             //reload browser for current folder
             return $this->redirect(Url::toRoute(['media/mediabrowser', 'mediatype' => $mediaType, 'activeCategoryId' => $targetCategoryId]));
         }
     }
     $this->layout = 'modalLayout';
     return $this->render('fileUpload', ['model' => $model, 'mediaType' => $mediaType, 'msg' => $msg]);
 }