From 8f02e664a73e5ebe4ff634f6f6ccabbf8565a4a1 Mon Sep 17 00:00:00 2001 From: Mihail Date: Wed, 23 Dec 2015 13:06:46 +0200 Subject: [PATCH] add multisort functional in goods controller --- common/models/GoodsViewSearch.php | 9 +++++---- console/migrations/m151219_103804_goods_view.php | 4 ++-- frontend/controllers/GoodsController.php | 70 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++-------------- frontend/views/goods/index.php | 29 ++++++++++++++--------------- frontend/views/goods/one_item.php | 6 +++--- frontend/web/css/BC2_catalog_zapchasti.css | 16 ---------------- frontend/web/js/goods.js | 30 +++++++++++++++--------------- frontend/web/js/script.js | 5 ----- 8 files changed, 95 insertions(+), 74 deletions(-) diff --git a/common/models/GoodsViewSearch.php b/common/models/GoodsViewSearch.php index 7d8ff75..8bfce17 100644 --- a/common/models/GoodsViewSearch.php +++ b/common/models/GoodsViewSearch.php @@ -38,7 +38,7 @@ class GoodsViewSearch extends GoodsView * * @return ActiveDataProvider */ - public function search($params) + public function search( $params, $sort ) { $query = GoodsView::find(); @@ -53,7 +53,8 @@ class GoodsViewSearch extends GoodsView 'brand' => $this->brand, ]); - $query->andWhere(['or', 'box > 0', 'add_box > 0'])->orderBy(['price' => SORT_DESC]); + + $query->andWhere(['or', 'box > 0', 'add_box > 0'])->orderBy( $sort ); $dataProvider = new ActiveDataProvider([ 'query' => $query, @@ -62,7 +63,7 @@ class GoodsViewSearch extends GoodsView return $dataProvider; } - public function searchCrosses($params) + public function searchCrosses( $params, $sort ) { $query = GoodsView::find()->innerJoin('w_details_crosses', '`w_details_crosses`.`ARTICLE` = `name` and `w_details_crosses`.`BRAND` = w_goods_view.`brand`')->select('w_goods_view.*, w_details_crosses.CROSS_BRAND as crosses' ); @@ -77,7 +78,7 @@ class GoodsViewSearch extends GoodsView 'w_details_crosses.CROSS_BRAND' => $this->brand, ]); - $query->andWhere(['or', 'box > 0', 'add_box > 0'])->orderBy(['price' => SORT_DESC]); + $query->andWhere(['or', 'box > 0', 'add_box > 0'])->orderBy( $sort ); $dataProvider = new ActiveDataProvider([ 'query' => $query, diff --git a/console/migrations/m151219_103804_goods_view.php b/console/migrations/m151219_103804_goods_view.php index e626a6c..3c642b8 100644 --- a/console/migrations/m151219_103804_goods_view.php +++ b/console/migrations/m151219_103804_goods_view.php @@ -40,9 +40,9 @@ class m151219_103804_goods_view extends Migration if (`w_details_description`.`image` = '', if (`w_details_description`.`tecdoc_image` = '', '', - concat('ital_origin/images/tecdoc/big/',`w_details_description`.`tecdoc_image`) + concat('/storage/images/tecdoc/big/',`w_details_description`.`tecdoc_image`) ), - concat('ital_origin/images/goods/big/',`w_details_description`.`image`) + concat('/storage/images/goods/big/',`w_details_description`.`image`) ) as `image`, `w_details_description`.`tecdoc_id`, diff --git a/frontend/controllers/GoodsController.php b/frontend/controllers/GoodsController.php index 64f36ac..7096111 100644 --- a/frontend/controllers/GoodsController.php +++ b/frontend/controllers/GoodsController.php @@ -16,49 +16,91 @@ use Yii; use yii\web\Controller; use common\models\Details; -class GoodsController extends Controller { +class GoodsController extends Controller +{ public $layout = '/internal'; - public function actionIndex( $name , $id ) + public function actionIndex($name, $id) { - - $arr_name = explode('_',$name); + $arr_name = explode('_', $name); $brand = $arr_name[0]; $article = $arr_name[1]; + $params = Yii::$app->request->getQueryParams(); + + // получим параметры сортировки из гет запроса + $sort_params_or = $this->getSortParams($params, 'or'); + $sort_params_cross = $this->getSortParams($params, 'cross'); $arr_values = ['name' => $article, - 'brand' => $brand]; + 'brand' => $brand]; $arr = ['GoodsViewSearch' => $arr_values]; - if(empty( Yii::$app->user->identity )){ + if (empty(Yii::$app->user->identity)) { $margin_id = 1; - }else{ + } else { $margin_id = Yii::$app->user->identity->margin_id; } -// $arr = ['GoodsViewSearch' => ['name' => '0092S40090', -// 'brand' => 'BOSCH']]; - - Yii::$app->session->setFlash('price_currency_id', 1); Yii::$app->session->setFlash('price_margin_id', $margin_id); $searchModel = new GoodsViewSearch(); - $goods_provider = $searchModel->search($arr); + $goods_provider = $searchModel->search($arr, $sort_params_or); - $crosses_provider = $searchModel->searchCrosses($arr); + $crosses_provider = $searchModel->searchCrosses($arr, $sort_params_cross); // для заголовка таблицы (карточка товара) $detailsModel = GoodsView::findOne($arr_values); - return $this->render('index',[ + return $this->render('index', [ 'goods_data_provider' => $goods_provider, 'crosses_data_provider' => $crosses_provider, 'detailsModel' => $detailsModel, ]); } + /** + * @param $params - массив параметров для сортировки + * @param $prefix - строка, перфикс таблицы сортировки (_or / _cross ) + * @return массив - итоговый массив с параемтрами для сортировки который подставляется в запрос + */ + protected function getSortParams($params, $prefix) + { + $default_sort_params = ['price' => SORT_DESC]; + $sort_params = []; + + if ( empty( $params['sort'] ) ) { + // нет параметров - сортируем по цене по убыванию + $sort_params = $default_sort_params; + + } else { + // разбираем парметры + $sort_arr = explode('_', $params['sort']); + if ($sort_arr[1] === $prefix) { + if ($sort_arr[0][0] === '-') { + // если первый символ "-", то по убыванию + $sort_arr[0] = str_replace('-', '', $sort_arr[0]); + $sort_params[$sort_arr[0]] = SORT_ASC; + } else { + $sort_params[$sort_arr[0]] = SORT_DESC; + } + } + if ( empty( $sort_params ) ) { + // если так и ненашли подходящих параметров, значит сортируем другую таблицу + // а для этой вытащим из кеша или дефолтная сортировка + if ( Yii::$app->cache->exists( "sort_{$prefix}" ) ) { + $sort_params = Yii::$app->cache->get( "sort_{$prefix}" ); + } else { + $sort_params = $default_sort_params; + } + } else { + Yii::$app->cache->set( "sort_{$prefix}", $sort_params, 300 ); + } + } + + return $sort_params; + } } \ No newline at end of file diff --git a/frontend/views/goods/index.php b/frontend/views/goods/index.php index 5c9882e..51fe7d2 100644 --- a/frontend/views/goods/index.php +++ b/frontend/views/goods/index.php @@ -4,12 +4,13 @@ use \yii\helpers\Html; $sort = new Sort([ 'attributes' => [ - 'box' => ['label' => 'Наличие'], - 'delivery' => ['label' => 'Срок'], - 'price' => [ - 'asc' => ['price' => SORT_ASC ], - 'desc' => ['price' => SORT_DESC], - 'default' => SORT_DESC, + 'box_or' => ['label' => 'Наличие'], + 'delivery_or' => ['label' => 'Срок'], + 'price_or' => [ + 'label' => 'Цена'], + 'box_cross' => ['label' => 'Наличие'], + 'delivery_cross' => ['label' => 'Срок'], + 'price_cross' => [ 'label' => 'Цена'], ], ]); @@ -108,9 +109,9 @@ $this->params['breadcrumbs'][] = $this->title; Номер детали Описание - link('box') ?> - link('delivery') ?> - link('price') ?> + link('box_or') ?> + link('delivery_or') ?> + link('price_or') ?> params['breadcrumbs'][] = $this->title;

Замены искомого производителя

-

UAH

-

USD

-

EUR

+

@@ -138,9 +137,9 @@ $this->params['breadcrumbs'][] = $this->title; - - - + + + crosses) )? '': '_cross';
Номер детали Описание
delivery?> дн.
- + -
formatter->asDecimal( $model->outputPriceUSD )?>