|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace app\controllers; |
|
4
|
|
|
|
|
5
|
|
|
use app\actions\SubmitFormAction; |
|
6
|
|
|
use app\components\search\SearchEvent; |
|
7
|
|
|
use app\models\Search; |
|
8
|
|
|
use app\modules\shop\models\Product; |
|
9
|
|
|
use Yii; |
|
10
|
|
|
use yii\helpers\Url; |
|
11
|
|
|
use yii\web\Controller; |
|
12
|
|
|
use yii\web\Response; |
|
13
|
|
|
|
|
14
|
|
|
class DefaultController extends Controller |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* @inheritdoc |
|
18
|
|
|
*/ |
|
19
|
|
|
public function actions() |
|
20
|
|
|
{ |
|
21
|
|
|
return [ |
|
22
|
|
|
'error' => [ |
|
23
|
|
|
'class' => 'app\actions\ErrorAction', |
|
24
|
|
|
], |
|
25
|
|
|
'captcha' => [ |
|
26
|
|
|
'class' => 'yii\captcha\CaptchaAction', |
|
27
|
|
|
'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null, |
|
28
|
|
|
], |
|
29
|
|
|
'submit-form' => [ |
|
30
|
|
|
'class' => SubmitFormAction::className(), |
|
|
|
|
|
|
31
|
|
|
], |
|
32
|
|
|
]; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
View Code Duplication |
public function behaviors() |
|
36
|
|
|
{ |
|
37
|
|
|
return [ |
|
38
|
|
|
'verbs' => [ |
|
39
|
|
|
'class' => \yii\filters\VerbFilter::className(), |
|
|
|
|
|
|
40
|
|
|
'actions' => [ |
|
41
|
|
|
'submit-form' => ['post'], |
|
42
|
|
|
], |
|
43
|
|
|
] |
|
44
|
|
|
]; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
public function actionIndex() |
|
48
|
|
|
{ |
|
49
|
|
|
return $this->render('index'); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
|
|
53
|
|
|
public function actionSearch() |
|
54
|
|
|
{ |
|
55
|
|
|
$model = new Search(); |
|
56
|
|
|
$model->load(Yii::$app->request->get()); |
|
57
|
|
|
return $this->render( |
|
58
|
|
|
'search', |
|
59
|
|
|
[ |
|
60
|
|
|
'model' => $model, |
|
61
|
|
|
] |
|
62
|
|
|
); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @param $term |
|
67
|
|
|
* @return string JSON |
|
|
|
|
|
|
68
|
|
|
*/ |
|
69
|
|
|
public function actionAutoCompleteSearch($term) |
|
70
|
|
|
{ |
|
71
|
|
|
Yii::$app->response->format = Response::FORMAT_JSON; |
|
72
|
|
|
$search = new Search(); |
|
73
|
|
|
$search->q = $term; |
|
74
|
|
|
$search->on(Search::QUERY_SEARCH_PRODUCTS_BY_DESCRIPTION, function (SearchEvent $event) { |
|
75
|
|
|
$event->setFunctionSearch(function ($activeQuery) { |
|
76
|
|
|
$activeQuery->limit(Yii::$app->getModule('core')->autoCompleteResultsCount); |
|
77
|
|
|
$product = Yii::$container->get(Product::class); |
|
78
|
|
|
return $product::find() |
|
79
|
|
|
->select(['id', 'name', 'main_category_id', 'slug', 'sku']) |
|
80
|
|
|
->where(['id' => $activeQuery->all()]) |
|
81
|
|
|
->all(); |
|
82
|
|
|
}); |
|
83
|
|
|
}); |
|
84
|
|
|
$products = $search->searchProductsByDescription(); |
|
85
|
|
|
$result = []; |
|
86
|
|
|
|
|
87
|
|
|
foreach ($products as $product) { |
|
88
|
|
|
/** @var Product $product */ |
|
89
|
|
|
$result[] = [ |
|
90
|
|
|
'id' => $product->id, |
|
91
|
|
|
'name' => $product->name, |
|
92
|
|
|
'url' => Url::toRoute( |
|
93
|
|
|
[ |
|
94
|
|
|
'@product', |
|
95
|
|
|
'model' => $product, |
|
96
|
|
|
'category_group_id' => $product->getMainCategory()->category_group_id, |
|
97
|
|
|
], |
|
98
|
|
|
true |
|
99
|
|
|
), |
|
100
|
|
|
]; |
|
101
|
|
|
} |
|
102
|
|
|
return $result; |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|
This method has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.