Ejemplo n.º 1
0
 public function actionCreate()
 {
     if (yii::$app->request->post('list')) {
         $list = array_map('trim', explode("\n", yii::$app->request->post('list')));
         foreach ($list as $variant) {
             $model = new FilterVariant();
             $model->value = htmlspecialchars($variant);
             $model->filter_id = (int) yii::$app->request->post('FilterVariant')['filter_id'];
             $model->save();
         }
         if (isset($model)) {
             return $this->redirect(['/filter/filter/update', 'id' => $model->filter_id]);
         }
     } else {
         $json = [];
         $model = new FilterVariant();
         $post = yii::$app->request->post('FilterVariant');
         //Если такой вариант уже есть у этого товара, просто выставляем его выделение
         if ($have = $model::find()->where(['value' => $post['value'], 'filter_id' => $post['filter_id']])->one()) {
             $json['result'] = 'success';
             $json['value'] = $have->value;
             $json['id'] = $have->id;
             $json['new'] = false;
             //Если варианта нет, создаем
         } else {
             if ($model->load(yii::$app->request->post()) && $model->save()) {
                 $json['result'] = 'success';
                 $json['value'] = $model->value;
                 $json['id'] = $model->id;
                 $json['new'] = true;
             } else {
                 $json['result'] = 'fail';
             }
         }
         return json_encode($json);
     }
 }