/
CategoryController.php
76 lines (63 loc) · 2.25 KB
/
CategoryController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<?php
/**
* Created by PhpStorm.
* User: ivphpan
* Date: 28.07.15
* Time: 12:26
*/
namespace app\modules\store\controllers;
use app\modules\store\models\Category;
use app\modules\store\models\Manufacturer;
use app\modules\store\models\Product;
use yii\web\Controller;
use yii\web\HttpException;
class CategoryController extends Controller{
public function actionIndex()
{
$modelList = Category::find()->all();
return $this->render('index',['modelList'=>$modelList]);
}
public function actionView($alias)
{
/** @var $category Category */
$category = Category::find()->where(['alias'=>$alias])->one();
if(!$category)
{
throw new HttpException(404,'Категория товара не найдена');
}
$q = \Yii::$app->request->get('q');
if(!empty($q))
{
$products = Product::find()->where(['categoryId'=>$category->id])->visible()->andWhere(['like','title',$q])->all();
}
else
{
$products = $category->products;
}
$route = $this->route;
$title = 'Товары - Категории - '.$category->title;
$categoryList = Category::find()->with(['products'])->orderBy(['position'=>SORT_ASC])->all();
return $this->render('view',['category'=>$category,'products'=>$products,'q'=>$q,'route'=>$route,'title'=>$title,'categoryList'=>$categoryList]);
}
public function actionBrand($alias)
{
/** @var $category Category */
$category = Manufacturer::find()->where(['alias'=>$alias])->one();
if(!$category)
{
throw new HttpException(404,'Категория товара не найдена');
}
$q = \Yii::$app->request->get('q');
if(!empty($q))
{
$products = Product::find()->where(['manufacturerId'=>$category->id])->visible()->andWhere(['like','title',$q])->all();
}
else
{
$products = $category->products;
}
$route = $this->route;
$title = 'Товары - Производители - '.$category->title;
return $this->render('view',['category'=>$category,'products'=>$products,'q'=>$q,'route'=>$route,'title'=>$title]);
}
}