Blame view

frontend/controllers/OrdersController.php 5.38 KB
cea5c45d   Administrator   21.03.16 Versrka
1
2
3
4
  <?php
  
  namespace frontend\controllers;
  
f7aa643c   Administrator   21.03.16 Versrka
5
  use common\widgets\BasketModal;
cea5c45d   Administrator   21.03.16 Versrka
6
7
8
9
10
11
12
13
  use Yii;
  
  use yii\web\Controller;
  use yii\web\NotFoundHttpException;
  use yii\data\ArrayDataProvider;
  /**
   * OrderController implements the CRUD actions for Order model.
   */
f7aa643c   Administrator   21.03.16 Versrka
14
  class OrdersController extends Controller
cea5c45d   Administrator   21.03.16 Versrka
15
16
  {
  
f7aa643c   Administrator   21.03.16 Versrka
17
18
19
20
21
22
23
24
25
26
27
28
  
      /**
       * @inheritdoc
       */
      public function beforeAction($action)
      {
          if ($action->id == 'buy-items' || $action->id == 'delete') {
              Yii::$app->controller->enableCsrfValidation = false;
          }
  
          return true;
      }
cea5c45d   Administrator   21.03.16 Versrka
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
  
      /**
       * Lists all Order models.
       * @return mixed
       */
      public function actionIndex()
      {
  
          if (Yii::$app->request->post()) {
              $item = $items_id = array();
  
              $i = 0;
              $order = Yii::$app->request->post();
  
              $orderData['Order'] = $order['OrderForm'];
  
              foreach($order['OrderForm']['one_item'] as $k => $v ){
                  $item[$k]['num'] = $v['num'];
                  $items_id[] = $k;
                  $i++;
              }
  
              $items = Items::find()->where(['id'=>$items_id])->all();
  
  
              $orderModel = new Order();
              $orderModel->load($orderData);
              $orderModel->save();
  
  
              foreach($items as $one_item){
                  $ItemOrderModel = new ItemOrder();
                  $ItemOrderModel->order_id = $orderModel->id;
                  $ItemOrderModel->num = $item[$one_item->id]['num'];
                  $ItemOrderModel->item_id = $one_item->id;
                  $ItemOrderModel->price = $one_item->price * $item[$one_item->id]['num'];
                  $ItemOrderModel->item_name = $one_item->name;
                  $ItemOrderModel->save();
              }
              Yii::$app->session->set('order', [] );
              return $this->redirect(['order/complete']);
          }
          $total_price = 0;
  
          $items_id = [];
  
          $orders = Yii::$app->session->get('order');
  
          if(!empty($orders)){
              foreach($orders as $k => $v) {
                  $items_id[] = $k;
              }
          }
  
  
          $items = Items::find()->where(['id'=>$items_id])->all();
  
          foreach($items as $item) {
              $total_price += $orders[$item['id']]['num'] * $item['price'];
          }
  
  
          $dataProvider = new ArrayDataProvider([
              'allModels' => $items
          ]);
  
          return $this->render('index', [
              'dataProvider' => $dataProvider,
              'total_price'=> $total_price,
              'model' => new OrderForm()
          ]);
      }
  
  
      public function actionComplete()
      {
          return $this->render('complete', [
          ]);
      }
  
      public function actionBuyItems(){
          $data = Yii::$app->request->post();
          $sessionData = Yii::$app->session->get('order');
          if(isset($sessionData) && !array_search($data['id'],Yii::$app->session->get('order')) ){
              $array = Yii::$app->session->get('order');
              $array[$data['id']] = $data;
              Yii::$app->session->set('order', $array );
          } else {
              $array[$data['id']] = $data;
              Yii::$app->session->set('order', $array );
          }
f7aa643c   Administrator   21.03.16 Versrka
120
          echo BasketModal::widget([]);
cea5c45d   Administrator   21.03.16 Versrka
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
  
      }
      /**
       * Displays a single Order model.
       * @param integer $id
       * @return mixed
       */
      public function actionView($id)
      {
          return $this->render('view', [
              'model' => $this->findModel($id),
          ]);
      }
  
      /**
       * Creates a new Order model.
       * If creation is successful, the browser will be redirected to the 'view' page.
       * @return mixed
       */
      public function actionCreate()
      {
          $model = new Order();
  
          if ($model->load(Yii::$app->request->post()) && $model->save()) {
              return $this->redirect(['view', 'id' => $model->id]);
          } else {
              return $this->render('create', [
                  'model' => $model,
              ]);
          }
      }
  
      /**
       * Updates an existing Order model.
       * If update is successful, the browser will be redirected to the 'view' page.
       * @param integer $id
       * @return mixed
       */
      public function actionUpdate($id)
      {
          $model = $this->findModel($id);
  
          if ($model->load(Yii::$app->request->post()) && $model->save()) {
              return $this->redirect(['view', 'id' => $model->id]);
          } else {
              return $this->render('update', [
                  'model' => $model,
              ]);
          }
      }
  
      /**
       * Deletes an existing Order model.
       * If deletion is successful, the browser will be redirected to the 'index' page.
       * @param integer $id
       * @return mixed
       */
      public function actionDelete()
      {
          $data = Yii::$app->request->post();
          $sessionData = Yii::$app->session->get('order');
          unset($sessionData[$data['id']]);
          Yii::$app->session->set('order', $sessionData);
          return count(Yii::$app->session->get('order'));
      }
  
      /**
       * Finds the Order model based on its primary key value.
       * If the model is not found, a 404 HTTP exception will be thrown.
       * @param integer $id
       * @return Order the loaded model
       * @throws NotFoundHttpException if the model cannot be found
       */
      protected function findModel($id)
      {
          if (($model = Order::findOne($id)) !== null) {
              return $model;
          } else {
              throw new NotFoundHttpException('The requested page does not exist.');
          }
      }
  }