Commit 4ee7051b44882bd647a50804c183f754dcec8a55

Authored by Yarik
1 parent 14782333

Sorting

frontend/controllers/CategoryController.php
1 <?php 1 <?php
  2 +
2 namespace frontend\controllers; 3 namespace frontend\controllers;
3 - 4 +
4 use artbox\catalog\helpers\FilterHelper; 5 use artbox\catalog\helpers\FilterHelper;
5 use artbox\catalog\models\Category; 6 use artbox\catalog\models\Category;
6 use artbox\core\components\SeoComponent; 7 use artbox\core\components\SeoComponent;
@@ -22,7 +23,6 @@ @@ -22,7 +23,6 @@
22 * Show category by ID 23 * Show category by ID
23 * 24 *
24 * @param string $category 25 * @param string $category
25 - *  
26 * @param string $filter 26 * @param string $filter
27 * 27 *
28 * @return string 28 * @return string
@@ -41,13 +41,55 @@ @@ -41,13 +41,55 @@
41 $query = $filterHelper->buildQuery() 41 $query = $filterHelper->buildQuery()
42 ->innerJoinWith('category', false) 42 ->innerJoinWith('category', false)
43 ->andWhere([ 'product_to_category.category_id' => $model->id ]) 43 ->andWhere([ 'product_to_category.category_id' => $model->id ])
44 - ->with('image', 'variants.image', 'lang'); 44 + ->innerJoinWith('lang')
  45 + ->innerJoinWith('variant')
  46 + ->with('image', 'variants.image');
45 $dataProvider = new ActiveDataProvider( 47 $dataProvider = new ActiveDataProvider(
46 [ 48 [
47 'query' => $query, 49 'query' => $query,
48 'pagination' => [ 50 'pagination' => [
49 'pageSize' => 18, 51 'pageSize' => 18,
50 ], 52 ],
  53 + 'sort' => [
  54 + 'attributes' => [
  55 + 'title_asc' => [
  56 + 'asc' => [
  57 + 'product_lang.title' => SORT_ASC,
  58 + ],
  59 + 'desc' => [
  60 + 'product_lang.title' => SORT_ASC,
  61 + ],
  62 + 'label' => \Yii::t('app', 'по имени от А до Я'),
  63 + ],
  64 + 'title_desc' => [
  65 + 'asc' => [
  66 + 'product_lang.title' => SORT_DESC,
  67 + ],
  68 + 'desc' => [
  69 + 'product_lang.title' => SORT_DESC,
  70 + ],
  71 + 'label' => \Yii::t('app', 'по имени от Я до А'),
  72 + ],
  73 + 'price_asc' => [
  74 + 'asc' => [
  75 + 'variant.price' => SORT_ASC,
  76 + ],
  77 + 'desc' => [
  78 + 'variant.price' => SORT_ASC,
  79 + ],
  80 + 'label' => \Yii::t('app', 'по цене по возрастанию'),
  81 + ],
  82 + 'price_desc' => [
  83 + 'asc' => [
  84 + 'variant.price' => SORT_DESC,
  85 + ],
  86 + 'desc' => [
  87 + 'variant.price' => SORT_DESC,
  88 + ],
  89 + 'label' => \Yii::t('app', 'по цене по убыванию'),
  90 + ],
  91 + ],
  92 + ],
51 ] 93 ]
52 ); 94 );
53 95
@@ -106,7 +148,7 @@ @@ -106,7 +148,7 @@
106 ) 148 )
107 ->one(); 149 ->one();
108 $seo->setAlias($model->lang->alias); 150 $seo->setAlias($model->lang->alias);
109 - if (!empty( $model )) { 151 + if (!empty($model)) {
110 if ($model->lang->alias_id !== $seo->aliasId) { 152 if ($model->lang->alias_id !== $seo->aliasId) {
111 throw new NotFoundHttpException('Wrong language'); 153 throw new NotFoundHttpException('Wrong language');
112 } 154 }
frontend/views/category/view.php
@@ -324,20 +324,48 @@ _________________________________________________________ --&gt; @@ -324,20 +324,48 @@ _________________________________________________________ --&gt;
324 _________________________________________________________ --> 324 _________________________________________________________ -->
325 325
326 <div class="col-sm-9"> 326 <div class="col-sm-9">
327 - <div class="col-md-12 sort-cat-wr">  
328 - <p>Сортировка:</p>  
329 - <div class="sort-cat">  
330 - <a id="category-sort" href="#3">  
331 - <span>по имени от А до Я</span>  
332 - <i class="fa fa-angle-down" aria-hidden="true"></i>  
333 - </a>  
334 - <ul class="sorter">  
335 - <li><a href="#1">по цене по уменьшению</a></li>  
336 - <li><a href="#2">по цене по возрастанию</a></li>  
337 - <li><a href="#4">по имени от Я до А</a></li>  
338 - </ul>  
339 - </div> 327 + <div class="col-md-12 sort-cat-wr">
  328 + <p><?php echo \Yii::t('app', 'Сортировка'); ?>:</p>
  329 + <div class="sort-cat">
  330 + <?php
  331 + $currentSortAttribute = array_keys($dataProvider->sort->attributes)[ 0 ];
  332 + $currentSort = \Yii::$app->request->get($dataProvider->sort->sortParam);
  333 + if (!empty($currentSort)) {
  334 + if (preg_match('/^-?(\w+)$/', $currentSort, $matches)) {
  335 + if (array_key_exists($matches[ 1 ], $dataProvider->sort->attributes)) {
  336 + $currentSortAttribute = $matches[ 1 ];
  337 + }
  338 + }
  339 + }
  340 + echo $dataProvider->sort->link(
  341 + $currentSortAttribute,
  342 + [
  343 + 'label' => Html::tag(
  344 + 'span',
  345 + $dataProvider->sort->attributes[ $currentSortAttribute ][ 'label' ]
  346 + ) . Html::tag(
  347 + 'i',
  348 + '',
  349 + [
  350 + 'class' => 'fa fa-angle-down',
  351 + 'aria-hidden' => true,
  352 + ]
  353 + ),
  354 + ]
  355 + );
  356 + ?>
  357 + <ul class="sorter">
  358 + <?php
  359 + foreach ($dataProvider->sort->attributes as $attribute => $sort) {
  360 + if ($currentSortAttribute == $attribute) {
  361 + continue;
  362 + }
  363 + echo Html::tag('li', $dataProvider->sort->link($attribute));
  364 + }
  365 + ?>
  366 + </ul>
340 </div> 367 </div>
  368 + </div>
341 <?php 369 <?php
342 echo ListView::widget( 370 echo ListView::widget(
343 [ 371 [