From fa33d79b7d0981e35607fb22d7fa2a63a4dc5362 Mon Sep 17 00:00:00 2001 From: yarik Date: Wed, 9 Nov 2016 15:37:21 +0200 Subject: [PATCH] Namespaces --- CatalogUrlManager.php | 229 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ models/ProductSearch.php | 24 +++++++++++------------- 2 files changed, 240 insertions(+), 13 deletions(-) create mode 100644 CatalogUrlManager.php diff --git a/CatalogUrlManager.php b/CatalogUrlManager.php new file mode 100644 index 0000000..91a7ca8 --- /dev/null +++ b/CatalogUrlManager.php @@ -0,0 +1,229 @@ +getPathInfo(); + $paths = explode('/', $pathInfo); + + if (!array_key_exists($paths[ 0 ], $this->route_map)) { + return false; + } + + $params = []; + if ($paths[ 0 ] == 'catalog') { + $route = 'catalog/category'; + + // Category + if (!empty( $paths[ 1 ] )) { + $category = Category::find() + ->joinWith('lang') + ->where( + [ + 'category_lang.alias' => $paths[ 1 ], + ] + ) + ->one(); + if (empty( $category )) { + \Yii::$app->response->redirect('/'); + } + $params[ 'category' ] = $category; + } + if (!empty( $paths[ 2 ] )) { + // Filter + + if (strpos($paths[ 2 ], 'filters:') === 0) { + if (!isset( $paths[ 3 ] )) { + $this->parseFilter($paths[ 2 ], $params); + } else { + throw new HttpException(404, 'Page not found'); + } + + } else { + throw new HttpException(404, 'Page not found'); + } + } + } elseif ($paths[ 0 ] == 'product') { + + if (!empty( $paths[ 3 ] )) { + throw new HttpException(404, 'Page not found'); + } + $product = Product::find() + ->joinWith('lang') + ->where([ 'product_lang.alias' => $paths[ 1 ] ]) + ->one(); + $variant = ProductVariant::find() + ->joinWith('lang') + ->where([ 'sku' => $paths[ 2 ] ]) + ->one(); + + if (empty( $variant->id ) || empty( $product->id )) { + throw new HttpException(404, 'Page not found'); + } + $route = 'catalog/product'; + $params = [ + 'product' => $product, + 'variant' => $variant, + ]; + } + + return [ + $route, + $params, + ]; + } + /** + * Creates a URL according to the given route and parameters. + * + * @param \yii\web\UrlManager $manager the URL manager + * @param string $route the route. It should not have slashes at the beginning or the end. + * @param array $params the parameters + * + * @return string|boolean the created URL, or false if this rule cannot be used for creating this URL. + */ + public function createUrl($manager, $route, $params) + { + + if (!in_array($route, $this->route_map)) { + return false; + } + + switch ($route) { + case 'catalog/category': + if (!empty( $params[ 'category' ] )) { + $category_alias = is_object( + $params[ 'category' ] + ) ? $params[ 'category' ]->lang->alias : strtolower( + $params[ 'category' ] + ); + $url = 'catalog/' . $category_alias . '/'; + unset( $params[ 'category' ] ); + } else { + $url = 'catalog/'; + } + + $this->setFilterUrl($params, $url); + + foreach ($params as $key => $param) { + if (empty( $params[ $key ] )) { + unset( $params[ $key ] ); + } + } + + if (!empty( $params ) && ( $query = http_build_query($params) ) !== '') { + $url .= '?' . $query; + } + + return $url; + break; + + case 'catalog/product': + if (!empty( $params[ 'product' ] )) { + $product_alias = is_object($params[ 'product' ]) ? $params[ 'product' ]->alias : strtolower( + $params[ 'product' ] + ); + unset( $params[ 'product' ] ); + } + $url = 'product/' . $product_alias; + + if (!empty( $params ) && ( $query = http_build_query($params) ) !== '') { + $url .= '?' . $query; + } + + return $url; + break; + + } + } + + private function option_value_encode($value) + { + return str_replace( + [ + ',', + '/', + ], + [ + '~', + '&s;', + ], + $value + ); + } + + private function setFilterUrl(&$params, &$url) + { + $filter = []; + if (!empty( $params[ 'filters' ] )) { + foreach ($params[ 'filters' ] as $key => $values) { + switch ($key) { + case 'prices': + $filter[] = $key . '=' . implode(':', $values); + break; + + default: + foreach ($values as &$value) { + $value = $this->option_value_encode($value); + if (empty( $value )) { + unset( $value ); + } + } + $filter[] = $key . '=' . implode(',', $values); + break; + } + } + if (!empty( $filter )) { + $url .= 'filters:' . implode(';', $filter); + } + unset( $params[ 'filters' ] ); + } + } + + private function parseFilter($paths, &$params) + { + $params[ 'filters' ] = []; + $filter_str = substr($paths, 8); + $filter_options = explode(';', $filter_str); + foreach ($filter_options as $filter_option) { + $filter_exp = explode('=', $filter_option); + if (!empty( $filter_exp[ 1 ] )) { + list( $filter_key, $filter_option ) = explode('=', $filter_option); + if ($filter_key == 'prices') { // price-interval section + $prices = explode(':', $filter_option); + $params[ 'filters' ][ $filter_key ] = [ + 'min' => floatval($prices[ 0 ]), + 'max' => floatval($prices[ 1 ]), + ]; + } else { // brands and other sections + $params[ 'filters' ][ $filter_key ] = explode(',', $filter_option); + } + } else { + throw new HttpException(404, 'Page not found'); + } + } + } + + } \ No newline at end of file diff --git a/models/ProductSearch.php b/models/ProductSearch.php index 19e1bef..c92ef20 100755 --- a/models/ProductSearch.php +++ b/models/ProductSearch.php @@ -101,17 +101,14 @@ 'categories', 'lang', ] - ) - ->joinWith( - [ - 'brand' => function ($query) { + )// ->joinWith( + // [ + // 'brand.lang', + // ] + // ) /** * @var ActiveQuery $query */ - $query->joinWith('lang'); - }, - ] - ) ->joinWith('variants'); $query->groupBy( @@ -135,10 +132,11 @@ 'asc' => [ 'product_lang.title' => SORT_ASC ], 'desc' => [ 'product_lang.title' => SORT_DESC ], ], - 'brand_id' => [ - 'asc' => [ 'brand_lang.title' => SORT_ASC ], - 'desc' => [ 'brand_lang.title' => SORT_DESC ], - ], + // 'brand_id' => [ + // 'asc' => [ 'brand_lang.title' => SORT_ASC ], + // 'desc' => [ 'brand_lang.title' => SORT_DESC ], + // 'default' => SORT_DESC, + // ], ], ] ); @@ -174,7 +172,7 @@ } $query->andFilterWhere( [ - 'product.brand_id' => $this->brand_id, + // 'product.brand_id' => $this->brand_id, 'product.id' => $this->id, 'product_category.category_id' => $this->categoryId, ] -- libgit2 0.21.4