Exemple #1
0
 public function actionCreate($id)
 {
     if (!($category = Carousel::findOne($id))) {
         return $this->redirect(['/admin/' . $this->module->id]);
     }
     $model = new ItemCarousel();
     if ($model->load(Yii::$app->request->post())) {
         if (Yii::$app->request->isAjax) {
             Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
             return ActiveForm::validate($model);
         } else {
             $model->carousel_id = $category->primaryKey;
             if ($fileInstanse = UploadedFile::getInstance($model, 'image')) {
                 $model->image = $fileInstanse;
                 if ($model->validate(['image'])) {
                     $model->image = Image::upload($model->image, 'carousel');
                     $model->status = Carousel::STATUS_ON;
                     if ($model->save()) {
                         $this->flash('success', Yii::t('easyii/carousel', 'Carousel created'));
                         return $this->redirect(['/admin/' . $this->module->id]);
                     } else {
                         $this->flash('error', Yii::t('easyii', 'Create error. {0}', $model->formatErrors()));
                     }
                 } else {
                     $this->flash('error', Yii::t('easyii', 'Create error. {0}', $model->formatErrors()));
                 }
             } else {
                 $this->flash('error', Yii::t('yii', '{attribute} cannot be blank.', ['attribute' => $model->getAttributeLabel('image')]));
             }
             return $this->refresh();
         }
     } else {
         return $this->render('create', ['model' => $model, 'category' => $category]);
     }
 }