Blame view

frontend/controllers/IamController.php 5.6 KB
ecf49b1b   Administrator   second
1
2
3
4
  <?php
  
  namespace frontend\controllers;
  
1c8ebc91   Karnovsky A   Share functional
5
  use common\models\ShareSearch;
ecf49b1b   Administrator   second
6
7
8
9
10
11
12
  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;
7d9935e2   Administrator   проапдейтил роли
13
  use common\models\Orders;
ecf49b1b   Administrator   second
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
  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()
      {
ecf49b1b   Administrator   second
48
  		return $this->render(Yii::$app->user->identity->role, [
7ba4acc5   Administrator   after marge
49
                  'model' => Yii::$app->user->identity,
ecf49b1b   Administrator   second
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
              ]);
      }
  	
      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(){
             
  
7d9935e2   Administrator   проапдейтил роли
76
          $model = Orders::find()->where(['user_id'=>Yii::$app->user->id])->orderBy('id DESC')->all();
ecf49b1b   Administrator   second
77
78
79
80
81
82
83
  
         return $this->render('myorders',['model'=>$model]);
                  
      }
      
      public function actionShow_order()
      {
7d9935e2   Administrator   проапдейтил роли
84
  		$model = Orders::findOne($_GET['id']);
ecf49b1b   Administrator   second
85
86
87
88
89
90
91
92
93
94
95
96
97
                  
  
                  
                  $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(){
1c8ebc91   Karnovsky A   Share functional
98
                  if(Yii::$app->request->get('id')) {
ecf49b1b   Administrator   second
99
100
101
                      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;
                      
1c8ebc91   Karnovsky A   Share functional
102
                      $model->product_id = Yii::$app->request->get('id');
ecf49b1b   Administrator   second
103
104
105
106
107
                      $model->save();
                      
                      Yii::$app->getSession()->setFlash('success', 'Этот товар добавлен в закладку!');
                      return $this->redirect(Yii::$app->request->referrer);
                  }
1c8ebc91   Karnovsky A   Share functional
108
                  else {
ecf49b1b   Administrator   second
109
110
111
112
113
114
                     /* $dataProvider = new ActiveDataProvider([
                              'query' => Share::find()->where(['user_id'=>Yii::$app->user->id])->orderBy('date_time DESC'),
                              'pagination' => [
                                      'pageSize' => 20,
                              ],
                      ]);*/
1c8ebc91   Karnovsky A   Share functional
115
116
                      if(Yii::$app->request->get('deleteID')) {
                          $model = Share::find()->where(['user_id'=>Yii::$app->user->id,'id'=>Yii::$app->request->get('deleteID')])->one();
ecf49b1b   Administrator   second
117
118
119
                          $model->delete();
                          return $this->redirect(Yii::$app->request->referrer);
                      }
1c8ebc91   Karnovsky A   Share functional
120
121
122
123
124
125
126
  
                      $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');
ecf49b1b   Administrator   second
127
128
129
130
131
132
                      $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)
1c8ebc91   Karnovsky A   Share functional
133
134
                          ->all();*/
                  return $this->render('share', ['items' => $items]);
ecf49b1b   Administrator   second
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
                  }        
      }
      
      
      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]);
                  }        
      }    
      
  }