Blame view

frontend/controllers/IamController.php 5.63 KB
ecf49b1b   Administrator   second
1
2
3
4
  <?php
  
  namespace frontend\controllers;
  
23ef7292   Administrator   14.09.16
5
  use common\models\Customer;
1c8ebc91   Karnovsky A   Share functional
6
  use common\models\ShareSearch;
ecf49b1b   Administrator   second
7
8
9
10
11
12
13
  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   проапдейтил роли
14
  use common\models\Orders;
ecf49b1b   Administrator   second
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
  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
49
  		return $this->render(Yii::$app->user->identity->role, [
7ba4acc5   Administrator   after marge
50
                  'model' => Yii::$app->user->identity,
ecf49b1b   Administrator   second
51
52
53
54
55
56
57
              ]);
      }
  	
      public function actionEdit()
      {
              
  
23ef7292   Administrator   14.09.16
58
  		$model = Customer::findOne(Yii::$app->user->id);
ecf49b1b   Administrator   second
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
  		
  		$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   проапдейтил роли
77
          $model = Orders::find()->where(['user_id'=>Yii::$app->user->id])->orderBy('id DESC')->all();
ecf49b1b   Administrator   second
78
79
80
81
82
83
84
  
         return $this->render('myorders',['model'=>$model]);
                  
      }
      
      public function actionShow_order()
      {
7d9935e2   Administrator   проапдейтил роли
85
  		$model = Orders::findOne($_GET['id']);
ecf49b1b   Administrator   second
86
87
88
89
90
91
92
93
94
95
96
97
98
                  
  
                  
                  $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
99
                  if(Yii::$app->request->get('id')) {
ecf49b1b   Administrator   second
100
101
102
                      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
103
                      $model->product_id = Yii::$app->request->get('id');
ecf49b1b   Administrator   second
104
105
106
107
108
                      $model->save();
                      
                      Yii::$app->getSession()->setFlash('success', 'Этот товар добавлен в закладку!');
                      return $this->redirect(Yii::$app->request->referrer);
                  }
1c8ebc91   Karnovsky A   Share functional
109
                  else {
ecf49b1b   Administrator   second
110
111
112
113
114
115
                     /* $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
116
117
                      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
118
119
120
                          $model->delete();
                          return $this->redirect(Yii::$app->request->referrer);
                      }
1c8ebc91   Karnovsky A   Share functional
121
122
123
124
125
126
127
  
                      $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
128
129
130
131
132
133
                      $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
134
135
                          ->all();*/
                  return $this->render('share', ['items' => $items]);
ecf49b1b   Administrator   second
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
162
                  }        
      }
      
      
      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]);
                  }        
      }    
      
  }