Created
May 13, 2015 03:31
-
-
Save Alex-Bond/9690a801fdf99348d3bc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Created by Alex Bond at UPDG. | |
* Date: 2/19/15 6:59 PM | |
*/ | |
namespace frontend\modules\store\controllers; | |
use common\models\Categories; | |
use common\models\Products; | |
use frontend\components\Controller; | |
use yii\data\ActiveDataProvider; | |
use yii\web\NotFoundHttpException; | |
class CategoryController extends Controller | |
{ | |
public function actionView( $id, $alias ) | |
{ | |
$model = Categories::findOne( $id ); | |
if ( ! $model) { | |
throw new NotFoundHttpException( "Category not found." ); | |
} | |
if ($model->alias != $alias) { | |
return $this->redirect( $model->getUrl() ); | |
} | |
$query = Products::find(); | |
$dataProvider = new ActiveDataProvider( [ | |
'query' => $query | |
] ); | |
$query->innerJoinWith( [ 'categories', 'attributesModel', 'attributesModel.attributeModel' ] ); | |
$query->where( [ 'productCategories.category_id' => $model->id ] ); | |
if (isset( $_GET['f'] ) && is_array( $_GET['f'] )) { | |
foreach (\Yii::$app->request->get( 'f' ) as $key => $filter) { | |
foreach ($filter as $value) { | |
$query->where( [ 'attributes.attribute_id' => $key, 'attributes.value' => $value ] ); | |
} | |
} | |
} | |
switch (\Yii::$app->request->get( 'order', 0 )) { | |
case 0: | |
$query->orderBy( [ 'id' => 'DESC' ] ); | |
break; | |
case 1: | |
$query->orderBy( [ 'products.name' => 'ASC' ] ); | |
break; | |
case 2: | |
$query->orderBy( [ 'products.name' => 'DESC' ] ); | |
break; | |
case 3: | |
$query->orderBy( [ 'products.price' => 'ASC' ] ); | |
break; | |
case 4: | |
$query->orderBy( [ 'products.price' => 'DESC' ] ); | |
break; | |
case 5: | |
$query->orderBy( [ 'products.rating' => 'ASC' ] ); | |
break; | |
case 6: | |
$query->orderBy( [ 'products.rating' => 'DESC' ] ); | |
break; | |
} | |
$dataProvider->getPagination()->setPageSize( \Yii::$app->request->get( 'pageSize', 9 ) ); | |
return $this->render( "view", [ 'model' => $model, 'productsDataProvider' => $dataProvider ] ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment