예제 #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())) {
         $data = Yii::$app->request->post('Item');
         if (Yii::$app->request->isAjax) {
             Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
             return ActiveForm::validate($model);
         } else {
             $model->category_id = $category->primaryKey;
             $model->data = Yii::$app->request->post('Data');
             if (isset($_FILES) && $this->module->settings['itemThumb']) {
                 $model->image = UploadedFile::getInstance($model, 'image');
                 if ($model->image && $model->validate(['image'])) {
                     $model->image = Image::upload($model->image, 'customers');
                 } else {
                     $model->image = '';
                 }
             }
             if ($model->save()) {
                 //save related items
                 foreach (Yii::$app->request->post('products') as $item) {
                     $CustomerData = new CustomerItems();
                     $CustomerData->item_id = $item;
                     $CustomerData->customer_id = $model->item_id;
                     $CustomerData->save();
                 }
                 $this->flash('success', Yii::t('easyii/customers', 'Item 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 {
         $selected = '';
         return $this->render('create', ['model' => $model, 'category' => $category, 'selectedData' => $selected, 'dataForm' => $this->generateForm($category->fields)]);
     }
 }