IamController.php
5.6 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
<?php
namespace frontend\controllers;
use common\models\ShareSearch;
use Yii;
use yii\web\Controller;
use yii\filters\AccessControl;
use yii\filters\VerbFilter;
use yii\data\ActiveDataProvider;
use yii\data\Pagination;
use common\models\User;
use common\models\Orders;
use common\models\OrdersProducts;
use common\models\Share;
use common\models\Price;
class IamController extends Controller
{
public function behaviors()
{
return [
'access' => [
'class' => AccessControl::className(),
//'only' => ['person'],
'rules' => [
[
'actions' => ['index','edit','myorders','show_order','share','price'],
'allow' => true,
'roles' => ['@'],
],
],
],
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'logout' => ['post'],
],
],
];
}
public function actionIndex()
{
return $this->render(Yii::$app->user->identity->role, [
'model' => Yii::$app->user->identity,
]);
}
public function actionEdit()
{
$model = User::findOne(Yii::$app->user->id);
$model->scenario = 'edit_'.Yii::$app->user->identity->role;
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return Yii::$app->response->redirect(['/iam/index']);
}
return $this->render('edit_'.Yii::$app->user->identity->role, [
'model' => $model,
]);
}
public function actionMyorders(){
$model = Orders::find()->where(['user_id'=>Yii::$app->user->id])->orderBy('id DESC')->all();
return $this->render('myorders',['model'=>$model]);
}
public function actionShow_order()
{
$model = Orders::findOne($_GET['id']);
$dataProvider = new ActiveDataProvider([
'query' => OrdersProducts::find()->where(['order_id'=>$_GET['id']]),
'pagination' => [
'pageSize' => 20,
],
]);
return $this->render('show_order',['model'=>$model,'dataProvider'=>$dataProvider]);
}
public function actionShare(){
if(Yii::$app->request->get('id')) {
if(!$model = Share::find()->where('user_id=:user_id and product_id=:product_id',[':user_id'=>Yii::$app->user->id,':product_id'=>$_GET['id']])->one())
$model = new Share;
$model->product_id = Yii::$app->request->get('id');
$model->save();
Yii::$app->getSession()->setFlash('success', 'Этот товар добавлен в закладку!');
return $this->redirect(Yii::$app->request->referrer);
}
else {
/* $dataProvider = new ActiveDataProvider([
'query' => Share::find()->where(['user_id'=>Yii::$app->user->id])->orderBy('date_time DESC'),
'pagination' => [
'pageSize' => 20,
],
]);*/
if(Yii::$app->request->get('deleteID')) {
$model = Share::find()->where(['user_id'=>Yii::$app->user->id,'id'=>Yii::$app->request->get('deleteID')])->one();
$model->delete();
return $this->redirect(Yii::$app->request->referrer);
}
$items = [];
foreach(Share::find()->where(['user_id' => Yii::$app->user->id])->all() as $item) {
$items[$item->date][] = $item;
}
/* $query = Share::find()->where(['user_id'=>Yii::$app->user->id])->groupBy('date')->orderBy('date DESC');
$countQuery = clone $query;
$pages = new Pagination(['totalCount' => $countQuery->count(), 'pageSize'=>20]);
$pages->forcePageParam = false;
$pages->pageSizeParam = false;
$share = $query->offset($pages->offset)
->limit($pages->limit)
->all();*/
return $this->render('share', ['items' => $items]);
}
}
public function actionPrice(){
if(!empty($_GET['id'])){
if(!$model = Price::find()->where('user_id=:user_id and product_id=:product_id',[':user_id'=>Yii::$app->user->id,':product_id'=>$_GET['id']])->one())
$model = new Price;
$model->product_id = $_GET['id'];
$model->save();
Yii::$app->getSession()->setFlash('success', 'Этот товар добавлен в закладку Узнать о снижение цены!');
return $this->redirect(Yii::$app->request->referrer);
}
else{
$dataProvider = new ActiveDataProvider([
'query' => Price::find()->where(['user_id'=>Yii::$app->user->id])->orderBy('date_time DESC'),
'pagination' => [
'pageSize' => 20,
],
]);
return $this->render('price',['dataProvider'=>$dataProvider]);
}
}
}