コード例 #1
0
 /**
  * Creates a new Categories model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  *
  * @return mixed
  * @throws ForbiddenHttpException
  */
 public function actionCreate()
 {
     // Check RBAC Permission
     if ($this->userCanCreate()) {
         $model = new Categories();
         if ($model->load(Yii::$app->request->post())) {
             // If alias is not set, generate it
             if ($_POST['Categories']['alias'] == "") {
                 $model->alias = $model->generateAlias($model->name);
             }
             // Genarate Json Params
             $params = ['categoriesImageWidth' => $_POST['categoriesImageWidth'], 'categoriesViewData' => $_POST['categoriesViewData'], 'categoryImageWidth' => $_POST['categoryImageWidth'], 'categoryViewData' => $_POST['categoryViewData'], 'itemImageWidth' => $_POST['itemImageWidth'], 'itemViewData' => $_POST['itemViewData']];
             $params = $model->generateJsonParams($params);
             $model->params = $params;
             // Upload Image and Thumb if is not Null
             $imagePath = Yii::getAlias(Yii::$app->controller->module->categoryImagePath);
             $thumbPath = Yii::getAlias(Yii::$app->controller->module->categoryThumbPath);
             $imgNameType = Yii::$app->controller->module->imageNameType;
             $imgOptions = Yii::$app->controller->module->thumbOptions;
             $imgName = $model->name;
             $fileField = "image";
             // Create UploadFile Instance
             $image = $model->uploadFile($imgName, $imgNameType, $imagePath, $fileField);
             if ($model->save()) {
                 // upload only if valid uploaded file instance found
                 if ($image !== false) {
                     // save thumbs to thumbPaths
                     $model->createThumbImages($image, $imagePath, $imgOptions, $thumbPath);
                 }
                 // Set Success Message
                 Yii::$app->session->setFlash('success', Yii::t('articles', 'Category has been created!'));
                 return $this->redirect(['index']);
             } else {
                 // Set Error Message
                 Yii::$app->session->setFlash('error', Yii::t('articles', 'Category could not be saved!'));
                 return $this->render('create', ['model' => $model]);
             }
         } else {
             return $this->render('create', ['model' => $model]);
         }
     } else {
         throw new ForbiddenHttpException();
     }
 }