示例#1
0
 /**
  * Updates an existing Product model.
  * If update is successful, the browser will be redirected to the 'view' page.
  * @param integer $id
  * @return mixed
  */
 public function actionUpdate($id)
 {
     $model = $this->findModel($id);
     $modelCategory = new Category();
     $categoriesArray = ArrayHelper::map(Category::find()->all(), 'id', 'name');
     $modelLinkProduct = LinkProduct::find()->where(['id_product' => $id])->orderBy('id_category')->all();
     $subCategoriesArray = SubCategory::find()->all();
     if ($model->load(Yii::$app->request->post()) && $model->save()) {
         LinkProduct::deleteAll(['id_product' => $id]);
         foreach (Yii::$app->request->post()['category'] as $key => $id) {
             //var_dump($id.'---');
             if (Yii::$app->request->post()['subCategory' . $id]) {
                 foreach (Yii::$app->request->post()['subCategory' . $id] as $keySub => $idSub) {
                     //$linkProductArray = array($model->id,$id,$idSub);
                     $modelLinkProduct = new LinkProduct();
                     //var_dump($keySub.$idSub );echo "<br>";
                     $modelLinkProduct->id_product = $model->id;
                     $modelLinkProduct->id_category = $id;
                     $modelLinkProduct->id_sub_category = $idSub;
                     $modelLinkProduct->save();
                 }
             } else {
                 $modelLinkProduct = new LinkProduct();
                 //var_dump($keySub.$idSub );echo "<br>";
                 $modelLinkProduct->id_product = $model->id;
                 $modelLinkProduct->id_category = $id;
                 $modelLinkProduct->id_sub_category = 0;
                 $modelLinkProduct->save();
             }
         }
         return $this->redirect(['view', 'id' => $model->id]);
     } else {
         $modelLinkProduct = LinkProduct::find()->where(['id_product' => $id])->orderBy('id_category')->all();
         //            var_dump(ArrayHelper::map($modelLinkProduct,'id','id_product'));die();
         return $this->render('update', ['model' => $model, 'categoriesArray' => $categoriesArray, 'subCategoriesArray' => $subCategoriesArray, 'modelLinkProductCategory' => ArrayHelper::map($modelLinkProduct, 'id', 'id_category'), 'modelLinkProductSubCategory' => ArrayHelper::map($modelLinkProduct, 'id', 'id_sub_category')]);
     }
 }
示例#2
0
    public function actionShowInfoProduct()
    {
        $id = (int) $_POST['id'];
        $out = '<div class="wrap-block">';
        $modelProduct = Product::findOne(['id' => $id]);
        $modelLinkProduct = LinkProduct::find()->where(['id_product' => $id])->all();
        //var_dump(count($modelLinkProduct));die();
        // $out = "<b>" . $modelProduct->name . "</b>";
        $modelCategory = null;
        foreach ($modelLinkProduct as $linkProduct) {
            //ar_dump($modelCategory->id.'----'.$linkProduct->id_category);
            //var_dump($modelCategory->id != $linkProduct->id_category);
            if ($modelCategory->id != $linkProduct->id_category) {
                $modelCategory = Category::findOne(['id' => $linkProduct->id_category]);
                $out .= "<div class='container-info border-con'>\n\t\t\t\t\t\t\t\t<p>" . $modelCategory->name . "</p></div>";
                $out .= "<input type='hidden' name='category[]" . "' value='" . $modelCategory->id . "'>";
                switch ($modelCategory->id_type) {
                    case 0:
                        $out .= "<div class=\"container-1 border-con\"><p><select name='sub_category_" . $modelCategory->id . "'><option>Выбрать</option>";
                        $modelLinkProduct2 = LinkProduct::find()->where(['id_product' => $id])->all();
                        foreach ($modelLinkProduct2 as $linkProduct2) {
                            if ($modelCategory->id == $linkProduct2->id_category) {
                                //var_dump($modelCategory->name);die();
                                $modelSubCategory = SubCategory::findOne(['id' => $linkProduct2->id_sub_category]);
                                $out .= "<option value='" . $modelSubCategory->id . "'>" . $modelSubCategory->name . "</option>";
                            } else {
                                continue;
                            }
                        }
                        $out .= "</select></p></div>";
                        break;
                    case 1:
                        $modelSubCategory = SubCategory::findOne(['id' => $linkProduct->id_sub_category]);
                        //var_dump($modelSubCategory);
                        $out .= "<input type='text' name='sub_category_" . $modelCategory->id . "' value='" . $modelSubCategory->id . "'>" . $modelSubCategory->name;
                        break;
                    case 2:
                        $modelLinkProduct2 = LinkProduct::find()->where(['id_product' => $id])->all();
                        foreach ($modelLinkProduct2 as $linkProduct2) {
                            //var_dump($modelCategory->id.$linkProduct2->id_category);
                            if ($modelCategory->id == $linkProduct2->id_category) {
                                $modelSubCategory = SubCategory::findOne(['id' => $linkProduct2->id_sub_category]);
                                //var_dump($modelSubCategory);
                                $out .= "<input type='radio' name='sub_category_" . $modelCategory->id . "' value='" . $modelSubCategory->id . "'>" . $modelSubCategory->name;
                            } else {
                                continue;
                            }
                        }
                        break;
                    case 3:
                        foreach ($modelLinkProduct as $linkProduct2) {
                            if ($modelCategory->id == $linkProduct2->id_category) {
                                $modelSubCategory = SubCategory::findOne(['id' => $linkProduct2->id_sub_category]);
                                $out .= "<input type='checkbox' name='sub_category_" . $modelCategory->id . "[]' value='" . $modelSubCategory->id . "'>" . $modelSubCategory->name;
                            } else {
                                continue;
                            }
                        }
                        break;
                }
                $out .= '</div>';
            }
        }
        $out .= '<div class="form-group field-order-comment has-success">
<label class="control-label" for="order-comment">Комментарий</label>
<textarea id="order-comment" class="form-control" name="Order[comment]"></textarea>
<div class="help-block"></div>
</div><div class="form-group field-order-file has-success">
<label class="control-label" for="order-file">Файлы</label>
<input type="hidden" name="Order[file]" value=""><input type="file" id="order-file" name="Order[file]">

<div class="help-block"></div>
</div></div>';
        return $out;
    }