Blame view

src/app/backend/controllers/DiscountController.php 7.52 KB
9586c455   Alex Savenko   base
1
2
3
4
5
6
7
8
9
10
11
12
  <?php
  /**
   * Created by PhpStorm.
   * User: Alex Savenko
   * Date: 20.12.2016
   * Time: 13:35
   */
  
  namespace controllers;
  
  use Phalcon\Mvc\Controller;
  
c4094a95   Alex Savenko   add/edit
13
14
15
16
17
  /**
   * Class DiscountController
   * @package controllers
   * @property \models models
   */
9586c455   Alex Savenko   base
18
19
20
  class DiscountController extends Controller
  {
  
c4094a95   Alex Savenko   add/edit
21
22
23
24
      /**
       * Displays all discounts with pagination
       * @return \Phalcon\Http\ResponseInterface
       */
9586c455   Alex Savenko   base
25
26
27
28
29
30
31
32
33
34
      public function indexAction() {
  
          if( !$this->session->get('isAdminAuth') ) {
  
              return $this->response->redirect([ 'for' => 'admin_login' ]);
  
          }
  
          $params         = $this->dispatcher->getParams();
          $page           = !empty( $params['page']  ) ? $params['page'] : 1;
9586c455   Alex Savenko   base
35
36
37
          $data           = $this->models->getDiscount()->getAllData();
          $total          = $this->models->getDiscount()->countData();
  
5d41bb08   Alex Savenko   add/edit
38
39
          if( $total['0']['total'] > \config::get( 'limits/items') ) {
  
9586c455   Alex Savenko   base
40
41
42
43
44
45
46
47
48
              $paginate = $this->common->paginate(
                  [
                      'page'              => $page,
                      'items_per_page'    => \config::get( 'limits/admin_orders', 5),
                      'total_items'       => $total[0]['total'],
                      'url_for'           => [ 'for' => 'discount_index_paged', 'page' => $page ],
                      'index_page'        => 'discount_index'
                  ], true
              );
5d41bb08   Alex Savenko   add/edit
49
  
9586c455   Alex Savenko   base
50
51
52
53
54
55
56
57
          }
          $this->view->setVars([
              'info' => $data,
              'paginate' => !empty($paginate['output']) ? $paginate['output'] : '' ,
          ]);
  
      }
  
c4094a95   Alex Savenko   add/edit
58
59
60
61
62
63
64
      /**
       * Add discount form
       * @return \Phalcon\Http\ResponseInterface
       */
      public function addAction() {
  
          $titlecmp = function ($a, $b) {
5d41bb08   Alex Savenko   add/edit
65
  
c4094a95   Alex Savenko   add/edit
66
              return strcasecmp($a['title'], $b['title']);
5d41bb08   Alex Savenko   add/edit
67
  
c4094a95   Alex Savenko   add/edit
68
          };
5d41bb08   Alex Savenko   add/edit
69
  
c4094a95   Alex Savenko   add/edit
70
          $lang_id = 1; // ua language
5d41bb08   Alex Savenko   add/edit
71
72
          if( !$this->session->get('isAdminAuth') ) {
  
c4094a95   Alex Savenko   add/edit
73
              return $this->response->redirect([ 'for' => 'admin_login' ]);
5d41bb08   Alex Savenko   add/edit
74
  
c4094a95   Alex Savenko   add/edit
75
76
          }
  
5d41bb08   Alex Savenko   add/edit
77
          if( $this->request->isPost() ) {
c4094a95   Alex Savenko   add/edit
78
79
80
81
82
83
  
              $data['name']           = $this->request->getPost('name', 'string', NULL );
              $data['description']    = $this->request->getPost('description');
              $data['start_date']     = $this->request->getPost('start_date');
              $data['end_date']       = $this->request->getPost('end_date');
              $data['discount']       = $this->request->getPost('discount', 'string', NULL );
e6a3cbef   Alex Savenko   products pick
84
              $data['status']         = 1;
c4094a95   Alex Savenko   add/edit
85
  
fa7778bd   Alex Savenko   status
86
  
9ac2df32   Alex Savenko   products pick
87
              //$data['catalog_ids']    = $this->request->getPost('catalog', 'string', NULL );
fa7778bd   Alex Savenko   status
88
              $data['group_ids']      = $this->request->getPost('items', 'string', NULL );
9ac2df32   Alex Savenko   products pick
89
              //$data['all_items']      = $this->request->getPost('all_items', 'int', NULL);
fa7778bd   Alex Savenko   status
90
  
78a40784   Alex Savenko   products pick
91
              if ($data['discount'] > 100) {
5d41bb08   Alex Savenko   add/edit
92
  
78a40784   Alex Savenko   products pick
93
                  $this->flash->error( 'Размер скидки не может привышать 100' );
5d41bb08   Alex Savenko   add/edit
94
  
c4094a95   Alex Savenko   add/edit
95
              }
5d41bb08   Alex Savenko   add/edit
96
97
              else {
  
78a40784   Alex Savenko   products pick
98
99
100
101
102
103
104
105
106
107
108
                  if(!empty($data['group_ids']) && $this->models->getDiscount()->addData( $data )) {
  
                      $this->flash->success( 'Сохранение прошло успешно' );
                      return $this->response->redirect([ 'for' => 'discount_index' ]);
  
                  }
                  else {
  
                      $this->flash->error( 'Выберите товары на которые распространяется скидка' );
  
                  }
5d41bb08   Alex Savenko   add/edit
109
  
c4094a95   Alex Savenko   add/edit
110
              }
78a40784   Alex Savenko   products pick
111
  
c4094a95   Alex Savenko   add/edit
112
113
114
115
116
117
118
119
120
121
122
          }
  
          $catalog_temp = $this->common->getTypeSubtype1(NULL, $lang_id)['catalog'];
          usort($catalog_temp, $titlecmp);
  
  
          $this->view->setVar('catalog_temp', $catalog_temp);
          $this->view->pick( 'discount/addEdit' );
  
      }
  
5d41bb08   Alex Savenko   add/edit
123
124
125
126
127
128
129
130
131
132
133
134
      /**
       * Delete discount by $id
       * @param $id
       * @return \Phalcon\Http\ResponseInterface
       */
      public function deleteAction($id) {
  
          if( !$this->session->get('isAdminAuth') ) {
  
              return $this->response->redirect([ 'for' => 'admin_login' ]);
  
          }
31e089d6   Alex Savenko   status
135
  
5d41bb08   Alex Savenko   add/edit
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
          $this->models->getDiscount()->deleteData($id);
  
          return $this->response->redirect([ 'for' => 'discount_index' ]);
  
      }
  
      /**
       * Update discount form
       * @param $id
       * @return \Phalcon\Http\ResponseInterface
       */
      public function updateAction($id) {
  
          $titlecmp = function ($a, $b) {
  
              return strcasecmp($a['title'], $b['title']);
  
          };
  
          $lang_id = 1; // ua language
  
          if( !$this->session->get('isAdminAuth') ) {
  
              return $this->response->redirect([ 'for' => 'admin_login' ]);
  
          }
  
          $data = $this->models->getDiscount()->getOneData($id);
  
          if( $this->request->isPost() ) {
  
              $data[0]['name']        = $this->request->getPost('name', 'string', NULL );
2545a440   Alex Savenko   prices
168
              $data[0]['discount']    = $this->request->getPost('discount', 'int', 0 );
5d41bb08   Alex Savenko   add/edit
169
170
171
172
              $data[0]['start_date']  = $this->request->getPost('start_date');
              $data[0]['end_date']    = $this->request->getPost('end_date');
              $data[0]['description'] = $this->request->getPost('description');
  
9f5f9ad2   Alex Savenko   products pick
173
174
175
              $data[0]['catalog_ids']    = $this->request->getPost('catalog', 'string', NULL );
              $data[0]['group_ids']      = $this->request->getPost('items', 'string', NULL );
              $data[0]['all_items']      = $this->request->getPost('all_items', 'int', NULL);
78a40784   Alex Savenko   products pick
176
  
28a7aba8   Alex Savenko   prices
177
              if ($data[0]['discount'] > 100) {
5d41bb08   Alex Savenko   add/edit
178
  
28a7aba8   Alex Savenko   prices
179
                  $this->flash->error( 'Размер скидки не может привышать 100' );
5d41bb08   Alex Savenko   add/edit
180
181
182
183
  
              }
              else {
  
28a7aba8   Alex Savenko   prices
184
185
186
187
188
189
190
191
192
193
194
                  if($this->models->getDiscount()->updateData( $data[0], $id ) ) {
  
                      $this->flash->success( 'Сохранение прошло успешно' );
                      return $this->response->redirect([ 'for' => 'discount_index' ]);
  
                  }
                  else {
  
                      $this->flash->error( 'Update error.' );
  
                  }
5d41bb08   Alex Savenko   add/edit
195
196
197
  
              }
  
28a7aba8   Alex Savenko   prices
198
  
5d41bb08   Alex Savenko   add/edit
199
200
          }
  
c7a9028a   Alex Savenko   products pick
201
202
203
          $data[0]['group_ids'] = $this->common->parseArray($data[0]['group_ids']);
          $data[0]['catalog_ids'] = $this->common->parseArray($data[0]['catalog_ids']);
  
26d1e4f6   Alex Savenko   add/edit
204
205
206
207
          $catalog_temp = $this->common->getTypeSubtype1(NULL, $lang_id)['catalog'];
          usort($catalog_temp, $titlecmp);
  
          foreach($catalog_temp as &$group) {
0fd83787   Alex Savenko   add/edit
208
  
26d1e4f6   Alex Savenko   add/edit
209
              usort($group['sub'], $titlecmp);
0fd83787   Alex Savenko   add/edit
210
  
26d1e4f6   Alex Savenko   add/edit
211
212
213
          }
  
          if(!empty($data[0]['group_ids'][0])) {
0fd83787   Alex Savenko   add/edit
214
  
26d1e4f6   Alex Savenko   add/edit
215
216
              $groups = $this->models->getItems()->getItemsByIds($lang_id, $data[0]['group_ids']);
              usort($groups, $titlecmp);
0fd83787   Alex Savenko   add/edit
217
  
26d1e4f6   Alex Savenko   add/edit
218
219
220
          }
  
          foreach($data as $k => $i) {
0fd83787   Alex Savenko   add/edit
221
  
26d1e4f6   Alex Savenko   add/edit
222
223
              if(isset($i['image']) && !empty($i['image']))
                  $data[$k]['image'] = $this->storage->getPhotoURL($i['image'], 'promo_codes', 'original');
0fd83787   Alex Savenko   add/edit
224
  
26d1e4f6   Alex Savenko   add/edit
225
          }
39420bfc   Alex Savenko   add/edit
226
  
26d1e4f6   Alex Savenko   add/edit
227
  
5d41bb08   Alex Savenko   add/edit
228
229
230
          $this->view->pick( 'discount/addEdit' );
  
          $this->view->setVars([
26d1e4f6   Alex Savenko   add/edit
231
              'page' => $data,
39420bfc   Alex Savenko   add/edit
232
233
              'catalog_temp' => $catalog_temp,
              'groups' => !empty($groups) ? $groups : null
5d41bb08   Alex Savenko   add/edit
234
235
236
          ]);
  
      }
31e089d6   Alex Savenko   status
237
  
ec194b70   Alex Savenko   status
238
239
240
241
242
      /**
       * Switch status indicator
       * @param $id
       * @return \Phalcon\Http\ResponseInterface
       */
31e089d6   Alex Savenko   status
243
244
245
246
247
248
249
250
251
252
      public function switchAction($id) {
  
          if( !$this->session->get('isAdminAuth') ) {
  
              return $this->response->redirect([ 'for' => 'admin_login' ]);
  
          }
  
          $status = $this->models->getDiscount()->getStatus($id);
  
31e089d6   Alex Savenko   status
253
254
          if ($status == 1) {
  
be1609ed   Alex Savenko   status
255
              $this->models->getDiscount()->updateStatus('0', $id);
31e089d6   Alex Savenko   status
256
257
258
259
  
          }
          elseif ($status == 0) {
  
be1609ed   Alex Savenko   status
260
              $this->models->getDiscount()->updateStatus('1', $id);
31e089d6   Alex Savenko   status
261
262
  
          }
c97a48c7   Alex Savenko   status
263
264
          else {
  
e992e0be   Alex Savenko   status
265
              $this->flash->error(var_dump($status));
c97a48c7   Alex Savenko   status
266
267
  
          }
31e089d6   Alex Savenko   status
268
269
270
271
  
          return $this->response->redirect([ 'for' => 'discount_index' ]);
  
      }
9586c455   Alex Savenko   base
272
  }