Blame view

src/app/frontend/controllers/AjaxController.php 9.58 KB
dce46e80   Alex Savenko   first push project
1
2
3
4
5
6
7
8
9
10
11
12
13
  <?php
  
  ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  
  namespace controllers;
  
  ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  use \Phalcon\Mvc\View;
  
  class AjaxController extends \controllers\ControllerBase
  {
      ///////////////////////////////////////////////////////////////////////////
  
15f3892a   Alex Savenko   tab
14
15
      public function getItemsAction() {
  
dce46e80   Alex Savenko   first push project
16
          header('Content-Type: application/json; charset=utf8');
dce46e80   Alex Savenko   first push project
17
  
15f3892a   Alex Savenko   tab
18
19
20
21
          $term   = $this->request->getPost('term', 'string', '');
  
          $items_ = $this->models->getItems()->getItemsByTermFromCatalog($term, 'items_dropdown', 1, $this->lang_id);
  
dce46e80   Alex Savenko   first push project
22
          if(!$items_) {
15f3892a   Alex Savenko   tab
23
24
25
  
              $items_ = $this->models->getItems()->getItemsByTerm($term, 'items_dropdown', 1, $this->lang_id);
  
dce46e80   Alex Savenko   first push project
26
27
          }
  
15f3892a   Alex Savenko   tab
28
29
30
31
32
          $items = [];
  
          if(!empty($items_)) {
  
              $items_ids = $this->common->array_column($items_, 'item_id');
dce46e80   Alex Savenko   first push project
33
  
816f861a   Alex Savenko   search
34
35
36
37
38
              //discount
              $discount = $this->models->getDiscount()->getActiveData();
              $discount = $this->models->getDiscount()->explodeGroupIds($discount);
              $discount = $discount[0];
  
15f3892a   Alex Savenko   tab
39
40
41
              $items = $this->models->getItems()->getItemsByIds( $this->lang_id, $items_ids );
  
              foreach($items as &$i) {
816f861a   Alex Savenko   search
42
  
15f3892a   Alex Savenko   tab
43
44
                  $i['cover'] = !empty( $i['group_cover'] ) ? $this->storage->getPhotoUrl( $i['group_cover'], 'avatar', '200x' ) : '/images/packet.jpg';
                  $i['alias'] = $this->seoUrl->setUrl($this->url->get([ 'for' => 'item', 'subtype' => $i['catalog_alias'], 'group_alias' => $i['group_alias'], 'item_id' => $i['id'] ]));
816f861a   Alex Savenko   search
45
46
  
                  if ($discount['discount'] > 0 && $discount['discount'] <= 100 && in_array($i['id'], $discount['group_ids'])) {
15f3892a   Alex Savenko   tab
47
  
816f861a   Alex Savenko   search
48
                      $i['discounted_price'] = round($i['price2']*(1-$discount['discount']/100), 1);
15f3892a   Alex Savenko   tab
49
  
816f861a   Alex Savenko   search
50
                  }
15f3892a   Alex Savenko   tab
51
  
dce46e80   Alex Savenko   first push project
52
              }
15f3892a   Alex Savenko   tab
53
  
dce46e80   Alex Savenko   first push project
54
55
56
          }
  
          die( json_encode( $items ) );
15f3892a   Alex Savenko   tab
57
  
dce46e80   Alex Savenko   first push project
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
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
      }
  
  
      public function addItemsForCompareAction( )
      {
          header('Content-Type: application/json; charset=utf8');
  
          if( $this->request->isAjax() && $this->request->isPost() )
          {
              $item_properties    = $this->request->getPost( 'item_id', 'string', '' );
              $item_properties    = explode( '-', $item_properties );
              $check              = $this->request->getPost( 'check', 'int', '' );
  
              $type_id            = $item_properties['0'];
              $subtype_id         = $item_properties['1'];
              $item_id            = $item_properties['2'];
  
              $compare            = $this->session->get('compare', []);
  
              if( !isset($compare[$type_id][$subtype_id]) || ( isset($compare[$type_id][$subtype_id]) && !in_array( $item_id, $compare[$type_id][$subtype_id] ) ) )
              {
                  $compare[$type_id][$subtype_id][] = $item_id;
              }
  
              elseif( isset($compare[$type_id][$subtype_id]) && in_array( $item_id, $compare[$type_id][$subtype_id] ) )
              {
                  foreach( $compare[$type_id][$subtype_id] as $k => $v )
                  {
                      if( $v == $item_id )
                      {
  
                          unset( $compare[$type_id][$subtype_id][$k] );
  
                          if( empty( $compare[$type_id][$subtype_id] ) )
                          {
                              unset($compare[$type_id][$subtype_id]);
                          }
                          if( empty( $compare[$type_id] ) )
                          {
                              unset($compare[$type_id]);
                          }
  
                      }
                  }
              }
  
              $count = 0;
              $compare_ = [];
  
              if( !empty( $compare ) )
              {
  
                  foreach( $compare as $key => $comp )
                  {
                      $type_ids[] = $key;
  
                      foreach( $comp as $k => $c )
                      {
                          $subtype_ids[] = $k;
  
                          $count += count($c);
                      }
                  }
                  $catalogs = $this->common->getTypeSubtype2( $this->lang_id );
  
                  foreach( $compare as $key => $comp )
                  {
                      foreach( $comp as $k => $c )
                      {
                          $compare_[$key][$k] =
                              [
                                  'title' => $catalogs[$k]['title'],
                                  'count' => count($c),
                                  'items' => $c,
                                  'url'       => $this->url->get([ 'for' => 'compare_items',  'subtype' => $catalogs[$k]['alias'], 'compare_ids' => join('-', $c) ]),
  								'url_del'   => $this->url->get([ 'for' => 'compare_items_del',  'subtype' => $catalogs[$k]['alias'], 'compare_ids' => join('-', $c) ])
  
  							];
                      }
                  }
              }
  
              $this->session->set( 'compare', $compare );
          }
  
          die(json_encode($compare_));
      }
  
      public function toggleCabinetAction() {
          $this->view->disable();
          if( $this->request->isAjax() ) {
              if(!$this->session->has('isToggled')) {
                  $this->session->set('isToggled', true);
              } else {
                  if($this->session->get('isToggled')) {
                      $this->session->set('isToggled', false);
                  } else {
                      $this->session->set('isToggled', true);
                  }
              }
  
          }
          echo json_encode($this->session->get('isToggled'));
      }
  
      public function getActionDiscountAction($action_id) {
          if($this->request->isAjax()) {
              $action_discount = $this->models->getActions()->getActionDiscountByActionId($action_id);
          }
          $this->view->setVar('action_discount', $action_discount);
          $this->view->setRenderLevel(View::LEVEL_ACTION_VIEW);
      }
  
      public function setActionDiscountAction() {
          $this->view->disable();
          if($this->request->isAjax()) {
              $action_id = $this->request->getPost('action_id', 'string', '' );
              $firm_total = $this->request->getPost('firm_total', 'string', '');
  
              $this->session->set('action_id', $action_id);
              $this->session->set('firm_total', $firm_total);
          }
      }
  
      public function getItemGroupAction() {
          $this->view->disable();
          $group_id = $this->request->getPost( 'group_id', 'int', '' );
          $item_id = $this->request->getPost('item_id', 'int', '');
          if(isset($item_id) && !empty($item_id)) {
              $item = $this->models->getItems()->getOneItem($this->lang_id, $item_id);
          } else {
              $item = $this->models->getItems()->getSizesByGroupId($this->lang_id, $group_id);
          }
          echo json_encode($item[0]);
      }
  
      public function applyPromoCodeAction() {
          try {
  
              $this->view->disable();
              $in_cart = $this->session->get('in_cart', []);
              $cart = $this->common->getCartItems($in_cart, $this->lang_id);
  
              $promo_code = $this->request->getPost( 'promo_code', 'string', '' );
  
              $promo_code	= $this->models->getPromoCodes()->getOneDataByCode($promo_code);
              $user =   isset($this->models->getCustomers()->getOneData($this->session->get('id'))['0']) ? $this->models->getCustomers()->getOneData($this->session->get('id'))['0'] : '';
  
  
  
              if(!empty($promo_code)) {
                  if($promo_code[0]['start_date'] && (strtotime($promo_code[0]['start_date']) > strtotime(date("Y-m-d H:i:s")))){
                      throw new \Exception($this->t->_('error_promo_code_not_start'));
                  }
  
                  if($promo_code[0]['end_date'] && (strtotime($promo_code[0]['end_date']) < strtotime(date("Y-m-d H:i:s")))){
                      throw new \Exception($this->t->_('error_promo_code_is_ended'));
                  }
  
                  if($promo_code[0]['single_use']){
                      if( !empty($user)){
                          if(!empty($user['email'])){
                              $data['code'] = $promo_code[0]['code'];
                              $data['email'] = $user['email'];
                              $promo_code_check	= $this->models->getPromoToUser()->getOneDataByCode($data);
                              if(!empty($promo_code_check)){
                                  throw new \Exception($this->t->_('error_promo_code_already_used'));
                              }
                          } else {
                              throw new \Exception($this->t->_('error_promo_code_empty_email'));
                          }
                      } else {
                          throw new \Exception($this->t->_('error_promo_code_empty_user'));
                      }
  
  
  
                  }
  
  
                  if($this->common->applyPromoCode($promo_code[0], $cart['items'])) {
                      $this->session->set('promo_code', $promo_code[0]);
                      $this->common->countOrderSum($cart);
                      echo json_encode(['cart' => $cart,
                      'successMessage' => $this->t->_('success_promo_code'),
                          'status'=>'success'
                      ]);
                      return;
                  } else {
                      throw new \Exception($this->t->_('error_promo_code'));
                  }
              } else {
                  throw new \Exception($this->t->_('error_promo_code_is_empty'));
              }
  
          } catch (\Exception $e) {
  
              echo json_encode(['status'=>'error',
                  'errorMessage' => $e->getMessage()
              ]);
              return null;
          }
  
  
      }
  }