Blame view

src/app/backend/controllers/CustomersController.php 9.16 KB
dce46e80   Alex Savenko   first push project
1
2
3
4
5
6
7
8
9
10
11
12
13
14
  <?php

  

  namespace controllers;

  

  ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

  

  class CustomersController extends \Phalcon\Mvc\Controller

  {

      function indexAction()

      {

          if( !$this->session->get('isAdminAuth') )

          {

              return $this->response->redirect([ 'for' => 'admin_login' ]);

          }

a3f95ff8   Alex Savenko   users search backend
15
16
17
18
  

          $params     = $this->dispatcher->getParams();

          $page       = !empty( $params['page']  ) ? $params['page'] : 1;

  

1abdd6e1   Alex Savenko   search phone
19
          $search_name = $this->request->getPost('search_name', 'string', NULL);

37109e66   Alex Savenko   customers search ...
20
          $search_phone = $this->request->getPost('search_phone', 'string', NULL);

7a04d7d3   Alex Savenko   submit button check
21
          $search_submit = $this->request->getPost('search_submit', 'string', NULL);

4fd9f4d2   Alex Savenko   customers search ...
22
23
          $this->session->set('search_name', $search_name);

          $this->session->set('search_phone', $search_phone);

090d3476   Alex Savenko   submit button check
24
          if (!empty($search_name) && (!empty($search_phone) || $search_phone == '0')) {

cdff229a   Alex Savenko   customers search ...
25
26
27
28
              $data       = $this->models->getCustomers()->getDataByNameAndPhone($search_name, $search_phone);

              $total = 1;

          }

          elseif (!empty($search_name)) {

1abdd6e1   Alex Savenko   search phone
29
30
31
              $data       = $this->models->getCustomers()->getDataByName($search_name);

              $total = 1;

          }

090d3476   Alex Savenko   submit button check
32
          elseif(!empty($search_phone) || $search_phone == '0') {

1abdd6e1   Alex Savenko   search phone
33
              $data       = $this->models->getCustomers()->getDataByPhone($search_phone);

6aa349e8   Alex Savenko   fix total
34
              $total = 1;

a3f95ff8   Alex Savenko   users search backend
35
          }

7a04d7d3   Alex Savenko   submit button check
36
          elseif (isset($search_submit)) {

2a999174   Alex Savenko   customers search ...
37
              $this->flash->notice("Параметры поиска не указаны");

a3f95ff8   Alex Savenko   users search backend
38
39
40
              $data       = $this->models->getCustomers()->getAllData($page);

              $total      = $this->models->getCustomers()->countData();

          }

e56e710d   Alex Savenko   submit button check
41
42
43
44
          else {

              $data       = $this->models->getCustomers()->getAllData($page);

              $total      = $this->models->getCustomers()->countData();

          }

dce46e80   Alex Savenko   first push project
45
46
47
48
49
50
51
52
53
54
55
56
57
  

          if( $total['0']['total'] > \config::get( 'limits/items') )

          {

              $paginate = $this->common->paginate(

                  [

                      'page'              => $page,

                      'items_per_page'    => \config::get( 'limits/admin_orders', 5),

                      'total_items'       => $total[0]['total'],

                      'url_for'           => [ 'for' => 'customers_index_paged', 'page' => $page ],

                      'index_page'       => 'customers_index'

                  ], true

              );

          }

a3f95ff8   Alex Savenko   users search backend
58
  

dce46e80   Alex Savenko   first push project
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
          $this->view->setVars([

              'info' => $data,

              'paginate' => !empty($paginate['output']) ? $paginate['output'] : '' ,

          ]);

      }

  

      function deleteAction($id)

      {

          if( !$this->session->get('isAdminAuth') )

          {

              return $this->response->redirect([ 'for' => 'admin_login' ]);

          }

          $this->models->getCustomers()->deleteData($id);

          return $this->response->redirect([ 'for' => 'customers_index' ]);

      }

  

      function updateAction($id)

      {

  

          if( !$this->session->get('isAdminAuth') )

          {

              return $this->response->redirect([ 'for' => 'admin_login' ]);

          }

  

          if( $this->request->isPost() )

          {

  

              $data['name'] = $this->request->getPost('name', 'string', NULL );

              $data['email'] = $this->request->getPost('email', 'string', NULL );

              $data['password'] = $this->request->getPost('password', 'string', NULL );

              $data['birth_date'] = $this->request->getPost('birth_date', 'string', NULL );

              $data['phone'] = $this->request->getPost('phone', 'string', NULL );

              $data['city'] = $this->request->getPost('city', 'string', NULL );

              $data['address'] = $this->request->getPost('address', 'string', NULL );

              $data['delivery'] = $this->request->getPost('delivery', 'int', NULL );

              $data['pay'] = $this->request->getPost('pay', 'int', NULL );

              $data['subscribed'] = $this->request->getPost('subscribed', 'int', NULL );

              $data['comments'] = $this->request->getPost('comments', 'string', NULL );

              $data['status'] = $this->request->getPost('status', 'int', NULL );

              $data['users_group_id'] = $this->request->getPost('users_group', 'string', NULL );

              $data['special_users_id'] = $this->request->getPost('special_users', 'string', NULL );

  

              $data['pay']        = empty( $data['pay'] ) ? 0 : 1;

              $data['subscribed'] = empty( $data['subscribed'] ) ? 0 : 1;

              $data['status']     = empty( $data['status'] ) ? 0 : 1;

  

              if( !empty( $data['password'] ) )

              {

                  $data['password'] = $this->common->hashPasswd($data['password']);

              }

  

af91d322   Alex Savenko   special_users_id ...
110
              $current_customer = $this->models->getCustomers()->getCustomerByEmail($data['email']);

dce46e80   Alex Savenko   first push project
111
  

af91d322   Alex Savenko   special_users_id ...
112
              if( !empty( $data ) && isset($current_customer[0]))

dce46e80   Alex Savenko   first push project
113
114
115
116
117
118
119
120
121
122
123
124
125
              {

                  if( $this->models->getCustomers()->UpdateData( $data, $id ) )

                  {

                      $this->flash->success( 'Сохранение прошло успешно' );

                      return $this->response->redirect([ 'for' => 'customers_index' ]);

                  }

                  else

                  {

                      $this->flash->error( 'Произошла ошибка во время обновления. Повторите попытку позже' );

                  }

              }

              else

              {

e14ff6a3   Alex Savenko   customer update
126
                  $this->flash->error('Пользователь с таким эмейлом не найден');

dce46e80   Alex Savenko   first push project
127
128
129
130
              }

          }

  

          $users_group = $this->models->getUsersGroup()->getAllData(1);

4c508d5f   Alex Savenko   user update
131
          $special_users = $this->models->getSpecialUsers()->getAllData(1, 0);

dce46e80   Alex Savenko   first push project
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
          $data = $this->models->getCustomers()->getOneData($id);

          $this->view->pick( 'customers/addEdit' );

  

          $this->view->setVars([

              'users_group'=> !empty($users_group) ? $users_group : array(),

              'special_users' => !empty($special_users) ? $special_users : array(),

              'page' => $data

          ]);

  

      }

  

      function addAction()

      {

          if( !$this->session->get('isAdminAuth') )

          {

              return $this->response->redirect([ 'for' => 'admin_login' ]);

          }

  

          if( $this->request->isPost() )

          {

  

              $data['name']       = $this->request->getPost('name', 'string', NULL );

              $data['email']      = $this->request->getPost('email', 'string', NULL );

              $data['password']   = $this->request->getPost('password', 'string', NULL );

              $data['birth_date'] = $this->request->getPost('birth_date', 'string', NULL );

              $data['phone']      = $this->request->getPost('phone', 'string', NULL );

              $data['city']       = $this->request->getPost('city', 'string', NULL );

              $data['address']    = $this->request->getPost('address', 'string', NULL );

              $data['delivery']   = $this->request->getPost('delivery', 'int', NULL );

              $data['pay']        = $this->request->getPost('pay', 'int', NULL );

              $data['subscribed'] = $this->request->getPost('subscribed', 'int', NULL );

              $data['comments']   = $this->request->getPost('comments', 'string', NULL );

              $data['status']     = $this->request->getPost('status', 'int', NULL );

              $data['users_group_id'] = $this->request->getPost('users_group', 'string', NULL );

              $data['special_users_id'] = $this->request->getPost('special_users', 'string', NULL );

  

  

              $data['pay']        = empty( $data['pay'] ) ? 0 : 1;

              $data['subscribed'] = empty( $data['subscribed'] ) ? 0 : 1;

              $data['status']     = empty( $data['status'] ) ? 0 : 1;

  

  

              if( !empty( $data['password'] ) )

              {

                  $data['password'] = $this->common->hashPasswd($data['password']);

  

              }

  

              if( !empty( $data ) && !isset($this->models->getCustomers()->getCustomerByEmail($data['email'])['0']) )

              {

                  if( $this->models->getCustomers()->addData( $data ) )

                  {

                      $this->flash->success( 'Сохранение прошло успешно' );

                      return $this->response->redirect([ 'for' => 'customers_index' ]);

                  }

                  else

                  {

                      $this->flash->error( 'Произошла ошибка во время добавления. Повторите попытку позже' );

                  }

              }

              else

              {

                  $this->flash->error('Пользователь с таким эмейлом уже существует');

              }

          }

          $users_group = $this->models->getUsersGroup()->getAllData(1);

          $special_users = $this->models->getSpecialUsers()->getAllData(1);

          $this->view->pick( 'customers/addEdit' );

  

          $this->view->setVars([

              'users_group'=> !empty($users_group) ? $users_group : array(),

              'special_users' => !empty($special_users) ? $special_users : array()

          ]);

  

      }

a3f95ff8   Alex Savenko   users search backend
207
  

dce46e80   Alex Savenko   first push project
208
  }