|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace app\controllers; |
|
4
|
|
|
|
|
5
|
|
|
use Yii; |
|
6
|
|
|
use yii\data\Pagination; |
|
7
|
|
|
use yii\web\NotFoundHttpException; |
|
8
|
|
|
use yii\filters\{AccessControl, VerbFilter}; |
|
9
|
|
|
use yii\helpers\ArrayHelper; |
|
10
|
|
|
use app\models\{Category, Product}; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Class CategoryController |
|
14
|
|
|
* |
|
15
|
|
|
* @package app\controllers |
|
16
|
|
|
*/ |
|
17
|
|
|
class CategoryController extends BaseController |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* @return array |
|
21
|
|
|
*/ |
|
22
|
|
|
public function behaviors() |
|
23
|
|
|
{ |
|
24
|
|
|
return ArrayHelper::merge(parent::behaviors(), [ |
|
25
|
|
|
'access' => [ |
|
26
|
|
|
'class' => AccessControl::class, |
|
27
|
|
|
'rules' => [ |
|
28
|
|
|
[ |
|
29
|
|
|
'allow' => true, |
|
30
|
|
|
'actions' => ['view'], |
|
31
|
|
|
'roles' => ['?', '@'], |
|
32
|
|
|
], |
|
33
|
|
|
], |
|
34
|
|
|
], |
|
35
|
|
|
'verbs' => [ |
|
36
|
|
|
'class' => VerbFilter::class, |
|
37
|
|
|
'actions' => [ |
|
38
|
|
|
'view' => ['get'], |
|
39
|
|
|
], |
|
40
|
|
|
], |
|
41
|
|
|
]); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Displays Category. |
|
46
|
|
|
* |
|
47
|
|
|
* @param $alias |
|
48
|
|
|
* @return string |
|
49
|
|
|
* |
|
50
|
|
|
* @throws NotFoundHttpException |
|
51
|
|
|
*/ |
|
52
|
|
|
public function actionView($alias) |
|
53
|
|
|
{ |
|
54
|
|
|
$model = Category::find()->where([ |
|
55
|
|
|
'alias' => $alias |
|
56
|
|
|
])->andWhere([ |
|
57
|
|
|
'active' => 1 |
|
58
|
|
|
])->one(); |
|
59
|
|
|
|
|
60
|
|
|
if (null === $model) { |
|
61
|
|
|
throw new NotFoundHttpException('Category not fount with alias = '.$alias.'.'); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
$this->setMetaParams($model); |
|
|
|
|
|
|
65
|
|
|
|
|
66
|
|
|
$productsQuery = Product::find()->where([ |
|
67
|
|
|
'categoryId' => $model->id |
|
68
|
|
|
])->andWhere([ |
|
69
|
|
|
'active' => 1 |
|
70
|
|
|
]); |
|
71
|
|
|
|
|
72
|
|
|
$pagination = new Pagination([ |
|
73
|
|
|
'totalCount' => $productsQuery->count(), |
|
74
|
|
|
'defaultPageSize' => Yii::$app->params['defaultPageSize'] |
|
75
|
|
|
]); |
|
76
|
|
|
|
|
77
|
|
|
return $this->render('view', [ |
|
78
|
|
|
'model' => $model, |
|
79
|
|
|
'pagination' => $pagination, |
|
80
|
|
|
'products' => $productsQuery->offset($pagination->offset) |
|
81
|
|
|
->limit($pagination->limit) |
|
82
|
|
|
->all() |
|
83
|
|
|
]); |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|