public function actionEdit($id)
 {
     $model = Products::findOne(['id' => $id]);
     $all_cats = ModArendaTree::find()->asArray()->all();
     if ($model->load(Yii::$app->request->post())) {
         $model->imageFile = UploadedFile::getInstance($model, 'imageFile');
         if ($model->validate()) {
             if (!empty($model->imageFile)) {
                 $model->upload();
                 $model->image = md5($model->imageFile->baseName . date("Y-m-d-H-i-s")) . '.' . $model->imageFile->extension;
             }
             if ($model->save()) {
                 $last_id = $_GET['id'];
                 foreach (Yii::$app->request->post('character') as $key => $value) {
                     $model1 = CharacteristicsProducts::findOne(['product_id' => $last_id, 'character_id' => $key]);
                     if (!$model1) {
                         $model1 = new CharacteristicsProducts();
                         $model1->product_id = $last_id;
                     }
                     $model1->character_id = $key;
                     $model1->value = empty($value) ? 'none' : $value;
                     $model1->save();
                 }
             }
             // form inputs are valid, do something here
         }
     }
     $result = CharacteristicsForCats::find()->select(['characteristics_for_cats.*', 'characteristics.name AS characteristic_name'])->where(['cat_id' => $model->cat_id])->leftJoin('characteristics', 'characteristics_for_cats.character_id=characteristics.id')->leftJoin('characteristics_products', 'characteristics_for_cats.character_id=characteristics_products.character_id')->asArray()->all();
     if (empty($result)) {
         $cat = ModArendaTree::findOne(['id' => $model->cat_id]);
         while ($cat->parent_id != 0) {
             $result = CharacteristicsForCats::find()->select(['characteristics_for_cats.*', 'characteristics.name AS characteristic_name', 'characteristics_products.value AS VALUE'])->where(['cat_id' => $cat->parent_id])->leftJoin('characteristics', 'characteristics_for_cats.character_id=characteristics.id')->leftJoin('characteristics_products', 'characteristics_for_cats.character_id=characteristics_products.character_id AND characteristics_products.product_id="' . $_GET['id'] . '"')->asArray()->all();
             if (!empty($result)) {
                 break;
             }
             $cat = ModArendaTree::findOne(['id' => $cat->parent_id]);
         }
     } else {
     }
     $html = '';
     if (!empty($result)) {
         foreach ($result as $key => $value) {
             $VAL = !empty($value['VALUE']) ? $value['VALUE'] : '';
             $html .= '
             <div class="form-group">
                 <label><p>' . $value['characteristic_name'] . '</p>
                 <input value="' . $VAL . '" type="text" class="form-control" name="character[' . $value['character_id'] . ']" >
                 </label>
             </div>
             ';
         }
     }
     return $this->render('edit', ['model' => $model, 'all_cats' => $all_cats, 'html' => $html]);
 }
Example #2
0
    public function actionCatalog($item = '')
    {
        @session_start();
        $_SESSION['current_str'] = 'catalog';
        $url = parse_url(Yii::$app->request->url);
        $url = explode('/', $url['path']);
        if (strrpos($item, '/')) {
            $item = trim(substr($item, strrpos($item, '/') + 1, strlen($item) - strrpos($item, '/')));
        } else {
            $item = trim($item);
        }
        $result = ModArendaTree::findOne(['url' => $item]);
        //���� �� ������� ���������, �������������� ��� �����, ����� ������ ������
        if (!$result) {
            $kroshka = [];
            foreach ($url as $key => $value) {
                if (!empty($value) && $value !== 'catalog') {
                    if ($key != count($url) - 1) {
                        $cat = ModArendaTree::findOne(['url' => $value]);
                        if ($cat) {
                            $kroshka['cats'][] = $cat;
                        }
                    } else {
                        $id = trim(substr($value, strrpos($value, '-') + 1, strlen($value) - strrpos($value, '-')));
                        $tovar = Products::findOne(['id' => $id]);
                        $kroshka['tovars'][] = $tovar;
                    }
                }
            }
            if (strrpos($item, '-')) {
                $id = trim(substr($item, strrpos($item, '-') + 1, strlen($item) - strrpos($item, '-')));
                $tovar = Products::findOne(['id' => $id]);
                $characteristics = CharacteristicsProducts::find()->select(['characteristics_products.*', 'characteristics.name AS characteristic_name'])->where(['product_id' => $id])->leftJoin('characteristics', 'characteristics_products.character_id=characteristics.id')->asArray()->all();
                $comments = Comments::find()->where(['product_id' => $id])->andWhere(['moder' => 1])->asArray()->all();
                $pohozhie = Products::find()->where(['cat_id' => $tovar->cat_id])->andWhere('id<>' . $tovar->id)->orderBy(['id' => SORT_DESC])->asArray()->all();
                return $this->render('tovar', ['data' => $tovar, 'characteristics' => $characteristics, 'kroshka' => $kroshka, 'comments' => $comments, 'pohozhie' => $pohozhie]);
            } else {
                return $this->render('main_cats');
            }
        }
        $kroshka = [];
        foreach ($url as $key => $value) {
            if (!empty($value) && $value !== 'catalog') {
                $cat = ModArendaTree::findOne(['url' => $value]);
                if ($cat) {
                    $kroshka['cats'][] = $cat;
                }
            }
        }
        $result_parent = ModArendaTree::find()->select(['mod_arenda_tree.*', 'images_for_cats.url AS image'])->leftJoin('images_for_cats', 'images_for_cats.cat_id=mod_arenda_tree.id')->groupBy('mod_arenda_tree.id')->where(['parent_id' => $result->id])->asArray()->all();
        if ($result_parent) {
            return $this->render('cats', ['data' => $result_parent, 'kroshka' => $kroshka]);
        } else {
            $cat = ModArendaTree::findOne(['url' => $item]);
            $char_for_cats = CharacteristicsForCats::find()->select(['characteristics_for_cats.*', 'characteristics.name AS char_name'])->where(['cat_id' => $cat->id])->leftJoin('characteristics', 'characteristics.id=characteristics_for_cats.character_id')->orderBy(['character_id' => SORT_ASC])->asArray()->all();
            if (empty($char_for_cats)) {
                while ($cat->parent_id != 0) {
                    $char_for_cats = CharacteristicsForCats::find()->select(['characteristics_for_cats.*', 'characteristics.name AS char_name'])->where(['cat_id' => $cat->parent_id])->leftJoin('characteristics', 'characteristics.id=characteristics_for_cats.character_id')->orderBy(['character_id' => SORT_ASC])->asArray()->all();
                    $cat = ModArendaTree::findOne(['id' => $cat->parent_id]);
                }
            }
            if ($char_for_cats) {
                //тут будет код если все охуенно
            }
            if (Yii::$app->request->post()) {
                $post = Yii::$app->request->post();
                unset($post['_csrf']);
                $q = '';
                foreach ($post as $key => $value) {
                    if ($value == 'all') {
                        continue;
                    }
                    if (is_array($value)) {
                        if (empty($value[0]) || empty($value[1])) {
                            continue;
                        }
                        $chid = substr($key, 3);
                        // возвращает "bcdef"
                        $q .= 'INNER JOIN characteristics_products AS ' . $key . ' ON (' . $key . '.product_id=P.id AND (' . $key . '.character_id=' . $chid . ' AND (' . $key . '.value>=' . $value[0] . ' AND ' . $key . '.value<=' . $value[1] . ' )))';
                    } else {
                        $chid = substr($key, 3);
                        // возвращает "bcdef"
                        $q .= 'INNER JOIN characteristics_products AS ' . $key . ' ON (' . $key . '.product_id=P.id AND (' . $key . '.character_id=' . $chid . ' AND ' . $key . '.value="' . $value . '"))';
                    }
                }
                $tovars = Yii::$app->db->createCommand('
                    SELECT P.*
                    FROM products AS P
                    ' . $q . '
                     WHERE cat_id=' . $result->id . '

                    ')->queryAll();
            } else {
                $tovars = Yii::$app->db->createCommand('
                    SELECT P.*
                    FROM products AS P
                    WHERE cat_id=' . $result->id . '

                    ')->queryAll();
            }
            foreach ($tovars as $key => $value) {
                $chars = CharacteristicsProducts::find()->where(['product_id' => $value['id']])->orderBy(['character_id' => SORT_ASC])->asArray()->all();
                $tovars[$key]['chars'] = $chars;
            }
            //ЗАПРО ДЛЯ ФИЛЬТРА
            $tovars1 = Yii::$app->db->createCommand('
                    SELECT P.*
                    FROM products AS P
                    WHERE cat_id=' . $result->id . '

                    ')->queryAll();
            foreach ($tovars1 as $key => $value) {
                $chars = CharacteristicsProducts::find()->where(['product_id' => $value['id']])->orderBy(['character_id' => SORT_ASC])->asArray()->all();
                $tovars1[$key]['chars'] = $chars;
            }
            //ЗАПРО ДЛЯ ФИЛЬТРА
            //делаем проверку на типы фильтров
            $CHARACTERISTICS = [];
            foreach ($tovars1 as $key => $value) {
                foreach ($value['chars'] as $key1 => $value1) {
                    if (isset($CHARACTERISTICS[$key1])) {
                        if (!in_array(strtolower($value1['value']), $CHARACTERISTICS[$key1]) && $value1['value'] != 'none') {
                            $CHARACTERISTICS[$key1][] = mb_strtolower($value1['value']);
                        }
                    } else {
                        if ($value1['value'] != 'none') {
                            $CHARACTERISTICS[$key1][] = is_numeric($value1['value']) ? $value1['value'] : mb_strtolower($value1['value']);
                        }
                    }
                    if (!isset($CHARACTER[$key1])) {
                        $CHARACTER[$key1] = 'number';
                    }
                    if (!is_numeric($value1['value'])) {
                        $CHARACTER[$key1] = 'string';
                    }
                }
            }
            foreach ($CHARACTERISTICS as $key3 => $value3) {
                $CHARACTERISTICS[$key3]['type'] = $CHARACTER[$key3];
            }
            foreach ($char_for_cats as $key3 => $value3) {
                if (!isset($CHARACTERISTICS[$key3])) {
                    unset($char_for_cats[$key3]);
                } else {
                    $char_for_cats[$key3]['chars'] = $CHARACTERISTICS[$key3];
                }
            }
            return $this->render('tovars', ['data' => $tovars, 'kroshka' => $kroshka, 'chars' => $char_for_cats]);
        }
    }