Пример #1
0
 /**
  * Creates a new Items model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  * @return mixed
  */
 public function actionCreate()
 {
     $model = new Items();
     if ($model->load(Yii::$app->request->post())) {
         // Set Modified as actual date
         $model->modified = date("Y-m-d H:i:s");
         // If alias is not set, generate it
         if ($_POST['Items']['alias'] == "") {
             $model->alias = $model->generateAlias($model->title);
         }
         // Upload Image and Thumb if is not Null
         $imagePath = Yii::getAlias('@webroot') . "/" . Yii::$app->controller->module->itemImagePath;
         $thumbPath = Yii::getAlias('@webroot') . "/" . Yii::$app->controller->module->itemThumbPath;
         $imgNameType = Yii::$app->controller->module->imageNameType;
         $imgOptions = Yii::$app->controller->module->thumbOptions;
         $imgName = $model->title;
         $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
                 $thumb = $model->createThumbImages($image, $imagePath, $imgOptions, $thumbPath);
             }
             // Set Success Message
             Yii::$app->session->setFlash('success', Yii::t('articles.message', 'Item has been created!'));
             return $this->redirect(['view', 'id' => $model->id]);
         } else {
             // Set Error Message
             Yii::$app->session->setFlash('error', Yii::t('articles.message', 'Item could not be saved!'));
             return $this->render('create', ['model' => $model]);
         }
     } else {
         Yii::$app->session->setFlash('error', Yii::t('articles.message', 'Item could not be saved!'));
         return $this->render('create', ['model' => $model]);
     }
 }
Пример #2
0
 /**
  * Creates a new Items 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 Items();
         if ($model->load(Yii::$app->request->post())) {
             // Set Modified as actual date
             $model->modified = "0000-00-00 00:00:00";
             // If alias is not set, generate it
             if ($_POST['Items']['alias'] == "") {
                 $model->alias = $model->generateAlias($model->title);
             }
             // Upload Image and Thumb if is not Null
             $imagePath = Yii::getAlias(Yii::$app->controller->module->itemImagePath);
             $thumbPath = Yii::getAlias(Yii::$app->controller->module->itemThumbPath);
             $imgNameType = Yii::$app->controller->module->imageNameType;
             $imgOptions = Yii::$app->controller->module->thumbOptions;
             $imgName = $model->title;
             $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', 'Item has been created!'));
                 return $this->redirect(['index']);
             } else {
                 // Set Error Message
                 Yii::$app->session->setFlash('error', Yii::t('articles', 'Item could not be saved!'));
                 return $this->render('create', ['model' => $model]);
             }
         } else {
             return $this->render('create', ['model' => $model]);
         }
     } else {
         throw new ForbiddenHttpException();
     }
 }