Ejemplo n.º 1
0
 public function actionCreate($id)
 {
     if (!($category = Category::findOne($id))) {
         return $this->redirect(['/admin/' . $this->module->id]);
     }
     $model = new Item();
     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->category_id = $category->primaryKey;
             if (isset($_FILES) && $this->module->settings['articleThumb']) {
                 $model->image = UploadedFile::getInstance($model, 'image');
                 if ($model->image && $model->validate(['image'])) {
                     $model->image = Image::upload($model->image, 'bookmark');
                 } else {
                     $model->image = '';
                 }
             }
             if ($model->save()) {
                 $this->flash('success', Yii::t('easyii/bookmark', 'Article created'));
                 return $this->redirect(['/admin/' . $this->module->id . '/items/edit', 'id' => $model->primaryKey]);
             } else {
                 $this->flash('error', Yii::t('easyii', 'Create error. {0}', $model->formatErrors()));
                 return $this->refresh();
             }
         }
     } else {
         return $this->render('create', ['model' => $model, 'category' => $category]);
     }
 }