Issues (2407)

administration/controller/customer/customer.php (10 issues)

1
<?php
2
3
/* 	Divine CMS - Open source CMS for widespread use.
4
    Copyright (c) 2019 Mykola Burakov ([email protected])
5
6
    See SOURCE.txt for other and additional information.
7
8
    This file is part of Divine CMS.
9
10
    This program is free software: you can redistribute it and/or modify
11
    it under the terms of the GNU General Public License as published by
12
    the Free Software Foundation, either version 3 of the License, or
13
    (at your option) any later version.
14
15
    This program is distributed in the hope that it will be useful,
16
    but WITHOUT ANY WARRANTY; without even the implied warranty of
17
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
    GNU General Public License for more details.
19
20
    You should have received a copy of the GNU General Public License
21
    along with this program. If not, see <http://www.gnu.org/licenses/>. */
22
23
class ControllerCustomerCustomer extends \Divine\Engine\Core\Controller
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
24
{
25
    private $error = array();
26
27
    public function index()
0 ignored issues
show
Expected 2 blank lines before function; 1 found
Loading history...
28
    {
29
        $this->load->language('customer/customer');
30
31
        $this->document->setTitle($this->language->get('heading_title'));
32
33
        $this->load->model('customer/customer');
34
35
        $this->getList();
36
    }
37
38
    public function add()
39
    {
40
        $this->load->language('customer/customer');
41
42
        $this->document->setTitle($this->language->get('heading_title'));
43
44
        $this->load->model('customer/customer');
45
46
        if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) {
47
            $this->model_customer_customer->addCustomer($this->request->post);
48
49
            $this->session->data['success'] = $this->language->get('text_success');
50
51
            $url = '';
52
53
            if (isset($this->request->get['filter_name'])) {
54
                $url .= '&filter_name=' . urlencode(html_entity_decode($this->request->get['filter_name'], ENT_QUOTES, 'UTF-8'));
55
            }
56
57
            if (isset($this->request->get['filter_email'])) {
58
                $url .= '&filter_email=' . urlencode(html_entity_decode($this->request->get['filter_email'], ENT_QUOTES, 'UTF-8'));
59
            }
60
61
            if (isset($this->request->get['filter_customer_group_id'])) {
62
                $url .= '&filter_customer_group_id=' . $this->request->get['filter_customer_group_id'];
63
            }
64
65
            if (isset($this->request->get['filter_status'])) {
66
                $url .= '&filter_status=' . $this->request->get['filter_status'];
67
            }
68
69
            if (isset($this->request->get['filter_approved'])) {
70
                $url .= '&filter_approved=' . $this->request->get['filter_approved'];
71
            }
72
73
            if (isset($this->request->get['filter_ip'])) {
74
                $url .= '&filter_ip=' . $this->request->get['filter_ip'];
75
            }
76
77
            if (isset($this->request->get['filter_date_added'])) {
78
                $url .= '&filter_date_added=' . $this->request->get['filter_date_added'];
79
            }
80
81
            if (isset($this->request->get['sort'])) {
82
                $url .= '&sort=' . $this->request->get['sort'];
83
            }
84
85
            if (isset($this->request->get['order'])) {
86
                $url .= '&order=' . $this->request->get['order'];
87
            }
88
89
            if (isset($this->request->get['page'])) {
90
                $url .= '&page=' . $this->request->get['page'];
91
            }
92
93
            $this->response->redirect($this->url->link('customer/customer', 'token=' . $this->session->data['token'] . $url, true));
94
        }
95
96
        $this->getForm();
97
    }
98
99
    public function edit()
100
    {
101
        $this->load->language('customer/customer');
102
103
        $this->document->setTitle($this->language->get('heading_title'));
104
105
        $this->load->model('customer/customer');
106
107
        if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) {
108
            $this->model_customer_customer->editCustomer($this->request->get['customer_id'], $this->request->post);
109
110
            $this->session->data['success'] = $this->language->get('text_success');
111
112
            $url = '';
113
114
            if (isset($this->request->get['filter_name'])) {
115
                $url .= '&filter_name=' . urlencode(html_entity_decode($this->request->get['filter_name'], ENT_QUOTES, 'UTF-8'));
116
            }
117
118
            if (isset($this->request->get['filter_email'])) {
119
                $url .= '&filter_email=' . urlencode(html_entity_decode($this->request->get['filter_email'], ENT_QUOTES, 'UTF-8'));
120
            }
121
122
            if (isset($this->request->get['filter_customer_group_id'])) {
123
                $url .= '&filter_customer_group_id=' . $this->request->get['filter_customer_group_id'];
124
            }
125
126
            if (isset($this->request->get['filter_status'])) {
127
                $url .= '&filter_status=' . $this->request->get['filter_status'];
128
            }
129
130
            if (isset($this->request->get['filter_approved'])) {
131
                $url .= '&filter_approved=' . $this->request->get['filter_approved'];
132
            }
133
134
            if (isset($this->request->get['filter_ip'])) {
135
                $url .= '&filter_ip=' . $this->request->get['filter_ip'];
136
            }
137
138
            if (isset($this->request->get['filter_date_added'])) {
139
                $url .= '&filter_date_added=' . $this->request->get['filter_date_added'];
140
            }
141
142
            if (isset($this->request->get['sort'])) {
143
                $url .= '&sort=' . $this->request->get['sort'];
144
            }
145
146
            if (isset($this->request->get['order'])) {
147
                $url .= '&order=' . $this->request->get['order'];
148
            }
149
150
            if (isset($this->request->get['page'])) {
151
                $url .= '&page=' . $this->request->get['page'];
152
            }
153
154
            $this->response->redirect($this->url->link('customer/customer', 'token=' . $this->session->data['token'] . $url, true));
155
        }
156
157
        $this->getForm();
158
    }
159
160
    public function delete()
161
    {
162
        $this->load->language('customer/customer');
163
164
        $this->document->setTitle($this->language->get('heading_title'));
165
166
        $this->load->model('customer/customer');
167
168
        if (isset($this->request->post['selected']) && $this->validateDelete()) {
169
            foreach ($this->request->post['selected'] as $customer_id) {
170
                $this->model_customer_customer->deleteCustomer($customer_id);
171
            }
172
173
            $this->session->data['success'] = $this->language->get('text_success');
174
175
            $url = '';
176
177
            if (isset($this->request->get['filter_name'])) {
178
                $url .= '&filter_name=' . urlencode(html_entity_decode($this->request->get['filter_name'], ENT_QUOTES, 'UTF-8'));
179
            }
180
181
            if (isset($this->request->get['filter_email'])) {
182
                $url .= '&filter_email=' . urlencode(html_entity_decode($this->request->get['filter_email'], ENT_QUOTES, 'UTF-8'));
183
            }
184
185
            if (isset($this->request->get['filter_customer_group_id'])) {
186
                $url .= '&filter_customer_group_id=' . $this->request->get['filter_customer_group_id'];
187
            }
188
189
            if (isset($this->request->get['filter_status'])) {
190
                $url .= '&filter_status=' . $this->request->get['filter_status'];
191
            }
192
193
            if (isset($this->request->get['filter_approved'])) {
194
                $url .= '&filter_approved=' . $this->request->get['filter_approved'];
195
            }
196
197
            if (isset($this->request->get['filter_ip'])) {
198
                $url .= '&filter_ip=' . $this->request->get['filter_ip'];
199
            }
200
201
            if (isset($this->request->get['filter_date_added'])) {
202
                $url .= '&filter_date_added=' . $this->request->get['filter_date_added'];
203
            }
204
205
            if (isset($this->request->get['sort'])) {
206
                $url .= '&sort=' . $this->request->get['sort'];
207
            }
208
209
            if (isset($this->request->get['order'])) {
210
                $url .= '&order=' . $this->request->get['order'];
211
            }
212
213
            if (isset($this->request->get['page'])) {
214
                $url .= '&page=' . $this->request->get['page'];
215
            }
216
217
            $this->response->redirect($this->url->link('customer/customer', 'token=' . $this->session->data['token'] . $url, true));
218
        }
219
220
        $this->getList();
221
    }
222
223
    public function approve()
224
    {
225
        $this->load->language('customer/customer');
226
227
        $this->document->setTitle($this->language->get('heading_title'));
228
229
        $this->load->model('customer/customer');
230
231
        $customers = array();
232
233
        if (isset($this->request->post['selected'])) {
234
            $customers = $this->request->post['selected'];
235
        } elseif (isset($this->request->get['customer_id'])) {
236
            $customers[] = $this->request->get['customer_id'];
237
        }
238
239
        if ($customers && $this->validateApprove()) {
240
            $this->model_customer_customer->approve($this->request->get['customer_id']);
241
242
            $this->session->data['success'] = $this->language->get('text_success');
243
244
            $url = '';
245
246
            if (isset($this->request->get['filter_name'])) {
247
                $url .= '&filter_name=' . urlencode(html_entity_decode($this->request->get['filter_name'], ENT_QUOTES, 'UTF-8'));
248
            }
249
250
            if (isset($this->request->get['filter_email'])) {
251
                $url .= '&filter_email=' . urlencode(html_entity_decode($this->request->get['filter_email'], ENT_QUOTES, 'UTF-8'));
252
            }
253
254
            if (isset($this->request->get['filter_customer_group_id'])) {
255
                $url .= '&filter_customer_group_id=' . $this->request->get['filter_customer_group_id'];
256
            }
257
258
            if (isset($this->request->get['filter_status'])) {
259
                $url .= '&filter_status=' . $this->request->get['filter_status'];
260
            }
261
262
            if (isset($this->request->get['filter_approved'])) {
263
                $url .= '&filter_approved=' . $this->request->get['filter_approved'];
264
            }
265
266
            if (isset($this->request->get['filter_ip'])) {
267
                $url .= '&filter_ip=' . $this->request->get['filter_ip'];
268
            }
269
270
            if (isset($this->request->get['filter_date_added'])) {
271
                $url .= '&filter_date_added=' . $this->request->get['filter_date_added'];
272
            }
273
274
            if (isset($this->request->get['sort'])) {
275
                $url .= '&sort=' . $this->request->get['sort'];
276
            }
277
278
            if (isset($this->request->get['order'])) {
279
                $url .= '&order=' . $this->request->get['order'];
280
            }
281
282
            if (isset($this->request->get['page'])) {
283
                $url .= '&page=' . $this->request->get['page'];
284
            }
285
286
            $this->response->redirect($this->url->link('customer/customer', 'token=' . $this->session->data['token'] . $url, true));
287
        }
288
289
        $this->getList();
290
    }
291
292
    public function unlock()
293
    {
294
        $this->load->language('customer/customer');
295
296
        $this->document->setTitle($this->language->get('heading_title'));
297
298
        $this->load->model('customer/customer');
299
300
        if (isset($this->request->get['email']) && $this->validateUnlock()) {
301
            $this->model_customer_customer->deleteLoginAttempts($this->request->get['email']);
302
303
            $this->session->data['success'] = $this->language->get('text_success');
304
305
            $url = '';
306
307
            if (isset($this->request->get['filter_name'])) {
308
                $url .= '&filter_name=' . urlencode(html_entity_decode($this->request->get['filter_name'], ENT_QUOTES, 'UTF-8'));
309
            }
310
311
            if (isset($this->request->get['filter_email'])) {
312
                $url .= '&filter_email=' . urlencode(html_entity_decode($this->request->get['filter_email'], ENT_QUOTES, 'UTF-8'));
313
            }
314
315
            if (isset($this->request->get['filter_customer_group_id'])) {
316
                $url .= '&filter_customer_group_id=' . $this->request->get['filter_customer_group_id'];
317
            }
318
319
            if (isset($this->request->get['filter_status'])) {
320
                $url .= '&filter_status=' . $this->request->get['filter_status'];
321
            }
322
323
            if (isset($this->request->get['filter_approved'])) {
324
                $url .= '&filter_approved=' . $this->request->get['filter_approved'];
325
            }
326
327
            if (isset($this->request->get['filter_ip'])) {
328
                $url .= '&filter_ip=' . $this->request->get['filter_ip'];
329
            }
330
331
            if (isset($this->request->get['filter_date_added'])) {
332
                $url .= '&filter_date_added=' . $this->request->get['filter_date_added'];
333
            }
334
335
            if (isset($this->request->get['sort'])) {
336
                $url .= '&sort=' . $this->request->get['sort'];
337
            }
338
339
            if (isset($this->request->get['order'])) {
340
                $url .= '&order=' . $this->request->get['order'];
341
            }
342
343
            if (isset($this->request->get['page'])) {
344
                $url .= '&page=' . $this->request->get['page'];
345
            }
346
347
            $this->response->redirect($this->url->link('customer/customer', 'token=' . $this->session->data['token'] . $url, true));
348
        }
349
350
        $this->getList();
351
    }
352
353
    protected function getList()
354
    {
355
        if (isset($this->request->get['filter_name'])) {
356
            $filter_name = $this->request->get['filter_name'];
357
        } else {
358
            $filter_name = null;
359
        }
360
361
        if (isset($this->request->get['filter_email'])) {
362
            $filter_email = $this->request->get['filter_email'];
363
        } else {
364
            $filter_email = null;
365
        }
366
367
        if (isset($this->request->get['filter_customer_group_id'])) {
368
            $filter_customer_group_id = $this->request->get['filter_customer_group_id'];
369
        } else {
370
            $filter_customer_group_id = null;
371
        }
372
373
        if (isset($this->request->get['filter_status'])) {
374
            $filter_status = $this->request->get['filter_status'];
375
        } else {
376
            $filter_status = null;
377
        }
378
379
        if (isset($this->request->get['filter_approved'])) {
380
            $filter_approved = $this->request->get['filter_approved'];
381
        } else {
382
            $filter_approved = null;
383
        }
384
385
        if (isset($this->request->get['filter_ip'])) {
386
            $filter_ip = $this->request->get['filter_ip'];
387
        } else {
388
            $filter_ip = null;
389
        }
390
391
        if (isset($this->request->get['filter_date_added'])) {
392
            $filter_date_added = $this->request->get['filter_date_added'];
393
        } else {
394
            $filter_date_added = null;
395
        }
396
397
        if (isset($this->request->get['sort'])) {
398
            $sort = $this->request->get['sort'];
399
        } else {
400
            $sort = 'name';
401
        }
402
403
        if (isset($this->request->get['order'])) {
404
            $order = $this->request->get['order'];
405
        } else {
406
            $order = 'ASC';
407
        }
408
409
        if (isset($this->request->get['page'])) {
410
            $page = $this->request->get['page'];
411
        } else {
412
            $page = 1;
413
        }
414
415
        $url = '';
416
417
        if (isset($this->request->get['filter_name'])) {
418
            $url .= '&filter_name=' . urlencode(html_entity_decode($this->request->get['filter_name'], ENT_QUOTES, 'UTF-8'));
419
        }
420
421
        if (isset($this->request->get['filter_email'])) {
422
            $url .= '&filter_email=' . urlencode(html_entity_decode($this->request->get['filter_email'], ENT_QUOTES, 'UTF-8'));
423
        }
424
425
        if (isset($this->request->get['filter_customer_group_id'])) {
426
            $url .= '&filter_customer_group_id=' . $this->request->get['filter_customer_group_id'];
427
        }
428
429
        if (isset($this->request->get['filter_status'])) {
430
            $url .= '&filter_status=' . $this->request->get['filter_status'];
431
        }
432
433
        if (isset($this->request->get['filter_approved'])) {
434
            $url .= '&filter_approved=' . $this->request->get['filter_approved'];
435
        }
436
437
        if (isset($this->request->get['filter_ip'])) {
438
            $url .= '&filter_ip=' . $this->request->get['filter_ip'];
439
        }
440
441
        if (isset($this->request->get['filter_date_added'])) {
442
            $url .= '&filter_date_added=' . $this->request->get['filter_date_added'];
443
        }
444
445
        if (isset($this->request->get['sort'])) {
446
            $url .= '&sort=' . $this->request->get['sort'];
447
        }
448
449
        if (isset($this->request->get['order'])) {
450
            $url .= '&order=' . $this->request->get['order'];
451
        }
452
453
        if (isset($this->request->get['page'])) {
454
            $url .= '&page=' . $this->request->get['page'];
455
        }
456
457
        $data['breadcrumbs'] = array();
0 ignored issues
show
Comprehensibility Best Practice introduced by
$data was never initialized. Although not strictly required by PHP, it is generally a good practice to add $data = array(); before regardless.
Loading history...
458
459
        $data['breadcrumbs'][] = array(
460
            'text' => $this->language->get('text_home'),
461
            'href' => $this->url->link('common/dashboard', 'token=' . $this->session->data['token'], true)
462
        );
463
464
        $data['breadcrumbs'][] = array(
465
            'text' => $this->language->get('heading_title'),
466
            'href' => $this->url->link('customer/customer', 'token=' . $this->session->data['token'] . $url, true)
467
        );
468
469
        $data['add'] = $this->url->link('customer/customer/add', 'token=' . $this->session->data['token'] . $url, true);
470
        $data['delete'] = $this->url->link('customer/customer/delete', 'token=' . $this->session->data['token'] . $url, true);
471
472
        $data['customers'] = array();
473
474
        $filter_data = array(
475
            'filter_name'              => $filter_name,
476
            'filter_email'             => $filter_email,
477
            'filter_customer_group_id' => $filter_customer_group_id,
478
            'filter_status'            => $filter_status,
479
            'filter_approved'          => $filter_approved,
480
            'filter_date_added'        => $filter_date_added,
481
            'filter_ip'                => $filter_ip,
482
            'sort'                     => $sort,
483
            'order'                    => $order,
484
            'start'                    => ($page - 1) * $this->config->get('config_limit_admin'),
485
            'limit'                    => $this->config->get('config_limit_admin')
486
        );
487
488
        $customer_total = $this->model_customer_customer->getTotalCustomers($filter_data);
489
490
        $results = $this->model_customer_customer->getCustomers($filter_data);
491
492
        foreach ($results as $result) {
493
            if (!$result['approved']) {
494
                $approve = $this->url->link('customer/customer/approve', 'token=' . $this->session->data['token'] . '&customer_id=' . $result['customer_id'] . $url, true);
495
            } else {
496
                $approve = '';
497
            }
498
499
            $login_info = $this->model_customer_customer->getTotalLoginAttempts($result['email']);
500
501
            if ($login_info && $login_info['total'] >= $this->config->get('config_login_attempts')) {
502
                $unlock = $this->url->link('customer/customer/unlock', 'token=' . $this->session->data['token'] . '&email=' . $result['email'] . $url, true);
503
            } else {
504
                $unlock = '';
505
            }
506
507
            $data['customers'][] = array(
508
                'customer_id'    => $result['customer_id'],
509
                'name'           => $result['name'],
510
                'email'          => $result['email'],
511
                'customer_group' => $result['customer_group'],
512
                'status'         => ($result['status'] ? $this->language->get('text_enabled') : $this->language->get('text_disabled')),
513
                'ip'             => $result['ip'],
514
                'date_added'     => date($this->language->get('date_format_short'), strtotime($result['date_added'])),
515
                'approve'        => $approve,
516
                'unlock'         => $unlock,
517
                'edit'           => $this->url->link('customer/customer/edit', 'token=' . $this->session->data['token'] . '&customer_id=' . $result['customer_id'] . $url, true)
518
            );
519
        }
520
521
        $data['heading_title'] = $this->language->get('heading_title');
522
523
        $data['text_list'] = $this->language->get('text_list');
524
        $data['text_enabled'] = $this->language->get('text_enabled');
525
        $data['text_disabled'] = $this->language->get('text_disabled');
526
        $data['text_yes'] = $this->language->get('text_yes');
527
        $data['text_no'] = $this->language->get('text_no');
528
        $data['text_default'] = $this->language->get('text_default');
529
        $data['text_no_results'] = $this->language->get('text_no_results');
530
        $data['text_confirm'] = $this->language->get('text_confirm');
531
532
        $data['column_name'] = $this->language->get('column_name');
533
        $data['column_email'] = $this->language->get('column_email');
534
        $data['column_customer_group'] = $this->language->get('column_customer_group');
535
        $data['column_status'] = $this->language->get('column_status');
536
        $data['column_approved'] = $this->language->get('column_approved');
537
        $data['column_ip'] = $this->language->get('column_ip');
538
        $data['column_date_added'] = $this->language->get('column_date_added');
539
        $data['column_action'] = $this->language->get('column_action');
540
541
        $data['entry_name'] = $this->language->get('entry_name');
542
        $data['entry_email'] = $this->language->get('entry_email');
543
        $data['entry_customer_group'] = $this->language->get('entry_customer_group');
544
        $data['entry_status'] = $this->language->get('entry_status');
545
        $data['entry_approved'] = $this->language->get('entry_approved');
546
        $data['entry_ip'] = $this->language->get('entry_ip');
547
        $data['entry_date_added'] = $this->language->get('entry_date_added');
548
549
        $data['button_approve'] = $this->language->get('button_approve');
550
        $data['button_add'] = $this->language->get('button_add');
551
        $data['button_edit'] = $this->language->get('button_edit');
552
        $data['button_delete'] = $this->language->get('button_delete');
553
        $data['button_filter'] = $this->language->get('button_filter');
554
        $data['button_login'] = $this->language->get('button_login');
555
        $data['button_unlock'] = $this->language->get('button_unlock');
556
557
        $data['token'] = $this->session->data['token'];
558
559
        if (isset($this->error['warning'])) {
560
            $data['error_warning'] = $this->error['warning'];
561
        } else {
562
            $data['error_warning'] = '';
563
        }
564
565
        if (isset($this->session->data['success'])) {
566
            $data['success'] = $this->session->data['success'];
567
568
            unset($this->session->data['success']);
569
        } else {
570
            $data['success'] = '';
571
        }
572
573
        if (isset($this->request->post['selected'])) {
574
            $data['selected'] = (array)$this->request->post['selected'];
575
        } else {
576
            $data['selected'] = array();
577
        }
578
579
        $url = '';
580
581
        if (isset($this->request->get['filter_name'])) {
582
            $url .= '&filter_name=' . urlencode(html_entity_decode($this->request->get['filter_name'], ENT_QUOTES, 'UTF-8'));
583
        }
584
585
        if (isset($this->request->get['filter_email'])) {
586
            $url .= '&filter_email=' . urlencode(html_entity_decode($this->request->get['filter_email'], ENT_QUOTES, 'UTF-8'));
587
        }
588
589
        if (isset($this->request->get['filter_customer_group_id'])) {
590
            $url .= '&filter_customer_group_id=' . $this->request->get['filter_customer_group_id'];
591
        }
592
593
        if (isset($this->request->get['filter_status'])) {
594
            $url .= '&filter_status=' . $this->request->get['filter_status'];
595
        }
596
597
        if (isset($this->request->get['filter_approved'])) {
598
            $url .= '&filter_approved=' . $this->request->get['filter_approved'];
599
        }
600
601
        if (isset($this->request->get['filter_ip'])) {
602
            $url .= '&filter_ip=' . $this->request->get['filter_ip'];
603
        }
604
605
        if (isset($this->request->get['filter_date_added'])) {
606
            $url .= '&filter_date_added=' . $this->request->get['filter_date_added'];
607
        }
608
609
        if ($order == 'ASC') {
610
            $url .= '&order=DESC';
611
        } else {
612
            $url .= '&order=ASC';
613
        }
614
615
        if (isset($this->request->get['page'])) {
616
            $url .= '&page=' . $this->request->get['page'];
617
        }
618
619
        $data['sort_name'] = $this->url->link('customer/customer', 'token=' . $this->session->data['token'] . '&sort=name' . $url, true);
620
        $data['sort_email'] = $this->url->link('customer/customer', 'token=' . $this->session->data['token'] . '&sort=c.email' . $url, true);
621
        $data['sort_customer_group'] = $this->url->link('customer/customer', 'token=' . $this->session->data['token'] . '&sort=customer_group' . $url, true);
622
        $data['sort_status'] = $this->url->link('customer/customer', 'token=' . $this->session->data['token'] . '&sort=c.status' . $url, true);
623
        $data['sort_ip'] = $this->url->link('customer/customer', 'token=' . $this->session->data['token'] . '&sort=c.ip' . $url, true);
624
        $data['sort_date_added'] = $this->url->link('customer/customer', 'token=' . $this->session->data['token'] . '&sort=c.date_added' . $url, true);
625
626
        $url = '';
627
628
        if (isset($this->request->get['filter_name'])) {
629
            $url .= '&filter_name=' . urlencode(html_entity_decode($this->request->get['filter_name'], ENT_QUOTES, 'UTF-8'));
630
        }
631
632
        if (isset($this->request->get['filter_email'])) {
633
            $url .= '&filter_email=' . urlencode(html_entity_decode($this->request->get['filter_email'], ENT_QUOTES, 'UTF-8'));
634
        }
635
636
        if (isset($this->request->get['filter_customer_group_id'])) {
637
            $url .= '&filter_customer_group_id=' . $this->request->get['filter_customer_group_id'];
638
        }
639
640
        if (isset($this->request->get['filter_status'])) {
641
            $url .= '&filter_status=' . $this->request->get['filter_status'];
642
        }
643
644
        if (isset($this->request->get['filter_approved'])) {
645
            $url .= '&filter_approved=' . $this->request->get['filter_approved'];
646
        }
647
648
        if (isset($this->request->get['filter_ip'])) {
649
            $url .= '&filter_ip=' . $this->request->get['filter_ip'];
650
        }
651
652
        if (isset($this->request->get['filter_date_added'])) {
653
            $url .= '&filter_date_added=' . $this->request->get['filter_date_added'];
654
        }
655
656
        if (isset($this->request->get['sort'])) {
657
            $url .= '&sort=' . $this->request->get['sort'];
658
        }
659
660
        if (isset($this->request->get['order'])) {
661
            $url .= '&order=' . $this->request->get['order'];
662
        }
663
664
        $pagination = new \Divine\Engine\Library\Pagination();
665
        $pagination->total = $customer_total;
666
        $pagination->page = $page;
667
        $pagination->limit = $this->config->get('config_limit_admin');
668
        $pagination->url = $this->url->link('customer/customer', 'token=' . $this->session->data['token'] . $url . '&page={page}', true);
669
670
        $data['pagination'] = $pagination->render();
671
672
        $data['results'] = sprintf($this->language->get('text_pagination'), ($customer_total) ? (($page - 1) * $this->config->get('config_limit_admin')) + 1 : 0, ((($page - 1) * $this->config->get('config_limit_admin')) > ($customer_total - $this->config->get('config_limit_admin'))) ? $customer_total : ((($page - 1) * $this->config->get('config_limit_admin')) + $this->config->get('config_limit_admin')), $customer_total, ceil($customer_total / $this->config->get('config_limit_admin')));
673
674
        $data['filter_name'] = $filter_name;
675
        $data['filter_email'] = $filter_email;
676
        $data['filter_customer_group_id'] = $filter_customer_group_id;
677
        $data['filter_status'] = $filter_status;
678
        $data['filter_approved'] = $filter_approved;
679
        $data['filter_ip'] = $filter_ip;
680
        $data['filter_date_added'] = $filter_date_added;
681
682
        $this->load->model('customer/customer_group');
683
684
        $data['customer_groups'] = $this->model_customer_customer_group->getCustomerGroups();
685
686
        $data['sort'] = $sort;
687
        $data['order'] = $order;
688
689
        $data['header'] = $this->load->controller('common/header');
690
        $data['column'] = $this->load->controller('common/column_left');
691
        $data['footer'] = $this->load->controller('common/footer');
692
693
        $this->response->setOutput($this->load->view('customer/customer_list', $data));
694
    }
695
696
    protected function getForm()
697
    {
698
        $data['heading_title'] = $this->language->get('heading_title');
0 ignored issues
show
Comprehensibility Best Practice introduced by
$data was never initialized. Although not strictly required by PHP, it is generally a good practice to add $data = array(); before regardless.
Loading history...
699
700
        $data['text_form'] = !isset($this->request->get['customer_id']) ? $this->language->get('text_add') : $this->language->get('text_edit');
701
        $data['text_enabled'] = $this->language->get('text_enabled');
702
        $data['text_disabled'] = $this->language->get('text_disabled');
703
        $data['text_yes'] = $this->language->get('text_yes');
704
        $data['text_no'] = $this->language->get('text_no');
705
        $data['text_select'] = $this->language->get('text_select');
706
        $data['text_none'] = $this->language->get('text_none');
707
        $data['text_loading'] = $this->language->get('text_loading');
708
        $data['text_add_ban_ip'] = $this->language->get('text_add_ban_ip');
709
        $data['text_remove_ban_ip'] = $this->language->get('text_remove_ban_ip');
710
711
        $data['entry_customer_group'] = $this->language->get('entry_customer_group');
712
        $data['entry_firstname'] = $this->language->get('entry_firstname');
713
        $data['entry_lastname'] = $this->language->get('entry_lastname');
714
        $data['entry_email'] = $this->language->get('entry_email');
715
        $data['entry_telephone'] = $this->language->get('entry_telephone');
716
        $data['entry_fax'] = $this->language->get('entry_fax');
717
        $data['entry_password'] = $this->language->get('entry_password');
718
        $data['entry_confirm'] = $this->language->get('entry_confirm');
719
        $data['entry_newsletter'] = $this->language->get('entry_newsletter');
720
        $data['entry_status'] = $this->language->get('entry_status');
721
        $data['entry_approved'] = $this->language->get('entry_approved');
722
        $data['entry_safe'] = $this->language->get('entry_safe');
723
        $data['entry_company'] = $this->language->get('entry_company');
724
        $data['entry_address_1'] = $this->language->get('entry_address_1');
725
        $data['entry_address_2'] = $this->language->get('entry_address_2');
726
        $data['entry_city'] = $this->language->get('entry_city');
727
        $data['entry_postcode'] = $this->language->get('entry_postcode');
728
        $data['entry_zone'] = $this->language->get('entry_zone');
729
        $data['entry_country'] = $this->language->get('entry_country');
730
        $data['entry_default'] = $this->language->get('entry_default');
731
        $data['entry_comment'] = $this->language->get('entry_comment');
732
        $data['entry_description'] = $this->language->get('entry_description');
733
        $data['entry_amount'] = $this->language->get('entry_amount');
734
        $data['entry_points'] = $this->language->get('entry_points');
735
736
        $data['help_safe'] = $this->language->get('help_safe');
737
        $data['help_points'] = $this->language->get('help_points');
738
739
        $data['button_save'] = $this->language->get('button_save');
740
        $data['button_cancel'] = $this->language->get('button_cancel');
741
        $data['button_address_add'] = $this->language->get('button_address_add');
742
        $data['button_history_add'] = $this->language->get('button_history_add');
743
        $data['button_transaction_add'] = $this->language->get('button_transaction_add');
744
        $data['button_reward_add'] = $this->language->get('button_reward_add');
745
        $data['button_remove'] = $this->language->get('button_remove');
746
        $data['button_upload'] = $this->language->get('button_upload');
747
748
        $data['tab_general'] = $this->language->get('tab_general');
749
        $data['tab_address'] = $this->language->get('tab_address');
750
        $data['tab_history'] = $this->language->get('tab_history');
751
        $data['tab_transaction'] = $this->language->get('tab_transaction');
752
        $data['tab_reward'] = $this->language->get('tab_reward');
753
        $data['tab_ip'] = $this->language->get('tab_ip');
754
755
        $data['token'] = $this->session->data['token'];
756
757
        if (isset($this->request->get['customer_id'])) {
758
            $data['customer_id'] = $this->request->get['customer_id'];
759
        } else {
760
            $data['customer_id'] = 0;
761
        }
762
763
        if (isset($this->error['warning'])) {
764
            $data['error_warning'] = $this->error['warning'];
765
        } else {
766
            $data['error_warning'] = '';
767
        }
768
769
        if (isset($this->error['firstname'])) {
770
            $data['error_firstname'] = $this->error['firstname'];
771
        } else {
772
            $data['error_firstname'] = '';
773
        }
774
775
        if (isset($this->error['lastname'])) {
776
            $data['error_lastname'] = $this->error['lastname'];
777
        } else {
778
            $data['error_lastname'] = '';
779
        }
780
781
        if (isset($this->error['email'])) {
782
            $data['error_email'] = $this->error['email'];
783
        } else {
784
            $data['error_email'] = '';
785
        }
786
787
        if (isset($this->error['telephone'])) {
788
            $data['error_telephone'] = $this->error['telephone'];
789
        } else {
790
            $data['error_telephone'] = '';
791
        }
792
793
        if (isset($this->error['password'])) {
794
            $data['error_password'] = $this->error['password'];
795
        } else {
796
            $data['error_password'] = '';
797
        }
798
799
        if (isset($this->error['confirm'])) {
800
            $data['error_confirm'] = $this->error['confirm'];
801
        } else {
802
            $data['error_confirm'] = '';
803
        }
804
805
        if (isset($this->error['custom_field'])) {
806
            $data['error_custom_field'] = $this->error['custom_field'];
807
        } else {
808
            $data['error_custom_field'] = array();
809
        }
810
811
        if (isset($this->error['address'])) {
812
            $data['error_address'] = $this->error['address'];
813
        } else {
814
            $data['error_address'] = array();
815
        }
816
817
        $url = '';
818
819
        if (isset($this->request->get['filter_name'])) {
820
            $url .= '&filter_name=' . urlencode(html_entity_decode($this->request->get['filter_name'], ENT_QUOTES, 'UTF-8'));
821
        }
822
823
        if (isset($this->request->get['filter_email'])) {
824
            $url .= '&filter_email=' . urlencode(html_entity_decode($this->request->get['filter_email'], ENT_QUOTES, 'UTF-8'));
825
        }
826
827
        if (isset($this->request->get['filter_customer_group_id'])) {
828
            $url .= '&filter_customer_group_id=' . $this->request->get['filter_customer_group_id'];
829
        }
830
831
        if (isset($this->request->get['filter_status'])) {
832
            $url .= '&filter_status=' . $this->request->get['filter_status'];
833
        }
834
835
        if (isset($this->request->get['filter_approved'])) {
836
            $url .= '&filter_approved=' . $this->request->get['filter_approved'];
837
        }
838
839
        if (isset($this->request->get['filter_ip'])) {
840
            $url .= '&filter_ip=' . $this->request->get['filter_ip'];
841
        }
842
843
        if (isset($this->request->get['filter_date_added'])) {
844
            $url .= '&filter_date_added=' . $this->request->get['filter_date_added'];
845
        }
846
847
        if (isset($this->request->get['sort'])) {
848
            $url .= '&sort=' . $this->request->get['sort'];
849
        }
850
851
        if (isset($this->request->get['order'])) {
852
            $url .= '&order=' . $this->request->get['order'];
853
        }
854
855
        if (isset($this->request->get['page'])) {
856
            $url .= '&page=' . $this->request->get['page'];
857
        }
858
859
        $data['breadcrumbs'] = array();
860
861
        $data['breadcrumbs'][] = array(
862
            'text' => $this->language->get('text_home'),
863
            'href' => $this->url->link('common/dashboard', 'token=' . $this->session->data['token'], true)
864
        );
865
866
        $data['breadcrumbs'][] = array(
867
            'text' => $this->language->get('heading_title'),
868
            'href' => $this->url->link('customer/customer', 'token=' . $this->session->data['token'] . $url, true)
869
        );
870
871
        if (!isset($this->request->get['customer_id'])) {
872
            $data['action'] = $this->url->link('customer/customer/add', 'token=' . $this->session->data['token'] . $url, true);
873
        } else {
874
            $data['action'] = $this->url->link('customer/customer/edit', 'token=' . $this->session->data['token'] . '&customer_id=' . $this->request->get['customer_id'] . $url, true);
875
        }
876
877
        $data['cancel'] = $this->url->link('customer/customer', 'token=' . $this->session->data['token'] . $url, true);
878
879
        if (isset($this->request->get['customer_id']) && ($this->request->server['REQUEST_METHOD'] != 'POST')) {
880
            $customer_info = $this->model_customer_customer->getCustomer($this->request->get['customer_id']);
881
        }
882
883
        $this->load->model('customer/customer_group');
884
885
        $data['customer_groups'] = $this->model_customer_customer_group->getCustomerGroups();
886
887
        if (isset($this->request->post['customer_group_id'])) {
888
            $data['customer_group_id'] = $this->request->post['customer_group_id'];
889
        } elseif (!empty($customer_info)) {
890
            $data['customer_group_id'] = $customer_info['customer_group_id'];
891
        } else {
892
            $data['customer_group_id'] = $this->config->get('config_customer_group_id');
893
        }
894
895
        if (isset($this->request->post['firstname'])) {
896
            $data['firstname'] = $this->request->post['firstname'];
897
        } elseif (!empty($customer_info)) {
898
            $data['firstname'] = $customer_info['firstname'];
899
        } else {
900
            $data['firstname'] = '';
901
        }
902
903
        if (isset($this->request->post['lastname'])) {
904
            $data['lastname'] = $this->request->post['lastname'];
905
        } elseif (!empty($customer_info)) {
906
            $data['lastname'] = $customer_info['lastname'];
907
        } else {
908
            $data['lastname'] = '';
909
        }
910
911
        if (isset($this->request->post['email'])) {
912
            $data['email'] = $this->request->post['email'];
913
        } elseif (!empty($customer_info)) {
914
            $data['email'] = $customer_info['email'];
915
        } else {
916
            $data['email'] = '';
917
        }
918
919
        if (isset($this->request->post['telephone'])) {
920
            $data['telephone'] = $this->request->post['telephone'];
921
        } elseif (!empty($customer_info)) {
922
            $data['telephone'] = $customer_info['telephone'];
923
        } else {
924
            $data['telephone'] = '';
925
        }
926
927
        if (isset($this->request->post['fax'])) {
928
            $data['fax'] = $this->request->post['fax'];
929
        } elseif (!empty($customer_info)) {
930
            $data['fax'] = $customer_info['fax'];
931
        } else {
932
            $data['fax'] = '';
933
        }
934
935
        // Custom Fields
936
        $this->load->model('customer/custom_field');
937
938
        $data['custom_fields'] = array();
939
940
        $filter_data = array(
941
            'sort'  => 'cf.sort_order',
942
            'order' => 'ASC'
943
        );
944
945
        $custom_fields = $this->model_customer_custom_field->getCustomFields($filter_data);
946
947
        foreach ($custom_fields as $custom_field) {
948
            $data['custom_fields'][] = array(
949
                'custom_field_id'    => $custom_field['custom_field_id'],
950
                'custom_field_value' => $this->model_customer_custom_field->getCustomFieldValues($custom_field['custom_field_id']),
951
                'name'               => $custom_field['name'],
952
                'value'              => $custom_field['value'],
953
                'type'               => $custom_field['type'],
954
                'location'           => $custom_field['location'],
955
                'sort_order'         => $custom_field['sort_order']
956
            );
957
        }
958
959
        if (isset($this->request->post['custom_field'])) {
960
            $data['account_custom_field'] = $this->request->post['custom_field'];
961
        } elseif (!empty($customer_info)) {
962
            $data['account_custom_field'] = json_decode($customer_info['custom_field'], true);
963
        } else {
964
            $data['account_custom_field'] = array();
965
        }
966
967
        if (isset($this->request->post['newsletter'])) {
968
            $data['newsletter'] = $this->request->post['newsletter'];
969
        } elseif (!empty($customer_info)) {
970
            $data['newsletter'] = $customer_info['newsletter'];
971
        } else {
972
            $data['newsletter'] = '';
973
        }
974
975
        if (isset($this->request->post['status'])) {
976
            $data['status'] = $this->request->post['status'];
977
        } elseif (!empty($customer_info)) {
978
            $data['status'] = $customer_info['status'];
979
        } else {
980
            $data['status'] = true;
981
        }
982
983
        if (isset($this->request->post['approved'])) {
984
            $data['approved'] = $this->request->post['approved'];
985
        } elseif (!empty($customer_info)) {
986
            $data['approved'] = $customer_info['approved'];
987
        } else {
988
            $data['approved'] = true;
989
        }
990
991
        if (isset($this->request->post['safe'])) {
992
            $data['safe'] = $this->request->post['safe'];
993
        } elseif (!empty($customer_info)) {
994
            $data['safe'] = $customer_info['safe'];
995
        } else {
996
            $data['safe'] = 0;
997
        }
998
999
        if (isset($this->request->post['password'])) {
1000
            $data['password'] = $this->request->post['password'];
1001
        } else {
1002
            $data['password'] = '';
1003
        }
1004
1005
        if (isset($this->request->post['confirm'])) {
1006
            $data['confirm'] = $this->request->post['confirm'];
1007
        } else {
1008
            $data['confirm'] = '';
1009
        }
1010
1011
        $this->load->model('localisation/country');
1012
1013
        $data['countries'] = $this->model_localisation_country->getCountries();
1014
1015
        if (isset($this->request->post['address'])) {
1016
            $data['addresses'] = $this->request->post['address'];
1017
        } elseif (isset($this->request->get['customer_id'])) {
1018
            $data['addresses'] = $this->model_customer_customer->getAddresses($this->request->get['customer_id']);
1019
        } else {
1020
            $data['addresses'] = array();
1021
        }
1022
1023
        if (isset($this->request->post['address_id'])) {
1024
            $data['address_id'] = $this->request->post['address_id'];
1025
        } elseif (!empty($customer_info)) {
1026
            $data['address_id'] = $customer_info['address_id'];
1027
        } else {
1028
            $data['address_id'] = '';
1029
        }
1030
1031
        $data['header'] = $this->load->controller('common/header');
1032
        $data['column'] = $this->load->controller('common/column_left');
1033
        $data['footer'] = $this->load->controller('common/footer');
1034
1035
        $this->response->setOutput($this->load->view('customer/customer_form', $data));
1036
    }
1037
1038
    protected function validateForm()
1039
    {
1040
        if (!$this->user->hasPermission('modify', 'customer/customer')) {
1041
            $this->error['warning'] = $this->language->get('error_permission');
1042
        }
1043
1044
        if ((\voku\helper\UTF8::strlen($this->request->post['firstname']) < 1) || (\voku\helper\UTF8::strlen(trim($this->request->post['firstname'])) > 32)) {
1045
            $this->error['firstname'] = $this->language->get('error_firstname');
1046
        }
1047
1048
        if ((\voku\helper\UTF8::strlen($this->request->post['lastname']) < 1) || (\voku\helper\UTF8::strlen(trim($this->request->post['lastname'])) > 32)) {
1049
            $this->error['lastname'] = $this->language->get('error_lastname');
1050
        }
1051
1052
        if ((\voku\helper\UTF8::strlen($this->request->post['email']) > 96) || !filter_var($this->request->post['email'], FILTER_VALIDATE_EMAIL)) {
1053
            $this->error['email'] = $this->language->get('error_email');
1054
        }
1055
1056
        $customer_info = $this->model_customer_customer->getCustomerByEmail($this->request->post['email']);
1057
1058
        if (!isset($this->request->get['customer_id'])) {
1059
            if ($customer_info) {
1060
                $this->error['warning'] = $this->language->get('error_exists');
1061
            }
1062
        } else {
1063
            if ($customer_info && ($this->request->get['customer_id'] != $customer_info['customer_id'])) {
1064
                $this->error['warning'] = $this->language->get('error_exists');
1065
            }
1066
        }
1067
1068
        if ((\voku\helper\UTF8::strlen($this->request->post['telephone']) < 3) || (\voku\helper\UTF8::strlen($this->request->post['telephone']) > 32)) {
1069
            $this->error['telephone'] = $this->language->get('error_telephone');
1070
        }
1071
1072
        // Custom field validation
1073
        $this->load->model('customer/custom_field');
1074
1075
        $custom_fields = $this->model_customer_custom_field->getCustomFields(array('filter_customer_group_id' => $this->request->post['customer_group_id']));
1076
1077
        foreach ($custom_fields as $custom_field) {
1078
            if (($custom_field['location'] == 'account') && $custom_field['required'] && empty($this->request->post['custom_field'][$custom_field['custom_field_id']])) {
1079
                $this->error['custom_field'][$custom_field['custom_field_id']] = sprintf($this->language->get('error_custom_field'), $custom_field['name']);
1080
            } elseif (($custom_field['location'] == 'account') && ($custom_field['type'] == 'text') && !empty($custom_field['validation']) && !filter_var($this->request->post['custom_field'][$custom_field['custom_field_id']], FILTER_VALIDATE_REGEXP, array('options' => array('regexp' => $custom_field['validation'])))) {
1081
                $this->error['custom_field'][$custom_field['custom_field_id']] = sprintf($this->language->get('error_custom_field'), $custom_field['name']);
1082
            }
1083
        }
1084
1085
        if ($this->request->post['password'] || (!isset($this->request->get['customer_id']))) {
1086
            if ((\voku\helper\UTF8::strlen($this->request->post['password']) < 4) || (\voku\helper\UTF8::strlen($this->request->post['password']) > 20)) {
1087
                $this->error['password'] = $this->language->get('error_password');
1088
            }
1089
1090
            if ($this->request->post['password'] != $this->request->post['confirm']) {
1091
                $this->error['confirm'] = $this->language->get('error_confirm');
1092
            }
1093
        }
1094
1095
        if (isset($this->request->post['address'])) {
1096
            foreach ($this->request->post['address'] as $key => $value) {
1097
                if ((\voku\helper\UTF8::strlen($value['firstname']) < 1) || (\voku\helper\UTF8::strlen($value['firstname']) > 32)) {
1098
                    $this->error['address'][$key]['firstname'] = $this->language->get('error_firstname');
1099
                }
1100
1101
                if ((\voku\helper\UTF8::strlen($value['lastname']) < 1) || (\voku\helper\UTF8::strlen($value['lastname']) > 32)) {
1102
                    $this->error['address'][$key]['lastname'] = $this->language->get('error_lastname');
1103
                }
1104
1105
                if ((\voku\helper\UTF8::strlen($value['address_1']) < 3) || (\voku\helper\UTF8::strlen($value['address_1']) > 128)) {
1106
                    $this->error['address'][$key]['address_1'] = $this->language->get('error_address_1');
1107
                }
1108
1109
                if ((\voku\helper\UTF8::strlen($value['city']) < 2) || (\voku\helper\UTF8::strlen($value['city']) > 128)) {
1110
                    $this->error['address'][$key]['city'] = $this->language->get('error_city');
1111
                }
1112
1113
                $this->load->model('localisation/country');
1114
1115
                $country_info = $this->model_localisation_country->getCountry($value['country_id']);
1116
1117
                if ($country_info && $country_info['postcode_required'] && (\voku\helper\UTF8::strlen($value['postcode']) < 2 || \voku\helper\UTF8::strlen($value['postcode']) > 10)) {
1118
                    $this->error['address'][$key]['postcode'] = $this->language->get('error_postcode');
1119
                }
1120
1121
                if ($value['country_id'] == '') {
1122
                    $this->error['address'][$key]['country'] = $this->language->get('error_country');
1123
                }
1124
1125
                if (!isset($value['zone_id']) || $value['zone_id'] == '') {
1126
                    $this->error['address'][$key]['zone'] = $this->language->get('error_zone');
1127
                }
1128
1129
                foreach ($custom_fields as $custom_field) {
1130
                    if (($custom_field['location'] == 'address') && $custom_field['required'] && empty($value['custom_field'][$custom_field['custom_field_id']])) {
1131
                        $this->error['address'][$key]['custom_field'][$custom_field['custom_field_id']] = sprintf($this->language->get('error_custom_field'), $custom_field['name']);
1132
                    } elseif (($custom_field['location'] == 'address') && ($custom_field['type'] == 'text') && !empty($custom_field['validation']) && !filter_var($value['custom_field'][$custom_field['custom_field_id']], FILTER_VALIDATE_REGEXP, array('options' => array('regexp' => $custom_field['validation'])))) {
1133
                        $this->error['address'][$key]['custom_field'][$custom_field['custom_field_id']] = sprintf($this->language->get('error_custom_field'), $custom_field['name']);
1134
                    }
1135
                }
1136
            }
1137
        }
1138
1139
        if ($this->error && !isset($this->error['warning'])) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->error of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
1140
            $this->error['warning'] = $this->language->get('error_warning');
1141
        }
1142
1143
        return !$this->error;
1144
    }
1145
1146
    protected function validateDelete()
1147
    {
1148
        if (!$this->user->hasPermission('modify', 'customer/customer')) {
1149
            $this->error['warning'] = $this->language->get('error_permission');
1150
        }
1151
1152
        return !$this->error;
1153
    }
1154
1155
    protected function validateApprove()
1156
    {
1157
        if (!$this->user->hasPermission('modify', 'customer/customer')) {
1158
            $this->error['warning'] = $this->language->get('error_permission');
1159
        }
1160
1161
        return !$this->error;
1162
    }
1163
1164
    protected function validateUnlock()
1165
    {
1166
        if (!$this->user->hasPermission('modify', 'customer/customer')) {
1167
            $this->error['warning'] = $this->language->get('error_permission');
1168
        }
1169
1170
        return !$this->error;
1171
    }
1172
1173
    public function login()
1174
    {
1175
        if (isset($this->request->get['customer_id'])) {
1176
            $customer_id = $this->request->get['customer_id'];
1177
        } else {
1178
            $customer_id = 0;
1179
        }
1180
1181
        $this->load->model('customer/customer');
1182
1183
        $customer_info = $this->model_customer_customer->getCustomer($customer_id);
1184
1185
        if ($customer_info) {
1186
            // Create token to login with
1187
            $token = (new \Tokenly\TokenGenerator\TokenGenerator())->generateToken(64, 'SR');
1188
            ;
1189
1190
            $this->model_customer_customer->editToken($customer_id, $token);
1191
1192
            $this->response->redirect('/index.php?route=account/login&token=' . $token);
1193
        } else {
1194
            $this->load->language('error/not_found');
1195
1196
            $this->document->setTitle($this->language->get('heading_title'));
1197
1198
            $data['heading_title'] = $this->language->get('heading_title');
0 ignored issues
show
Comprehensibility Best Practice introduced by
$data was never initialized. Although not strictly required by PHP, it is generally a good practice to add $data = array(); before regardless.
Loading history...
1199
1200
            $data['text_not_found'] = $this->language->get('text_not_found');
1201
1202
            $data['breadcrumbs'] = array();
1203
1204
            $data['breadcrumbs'][] = array(
1205
                'text' => $this->language->get('text_home'),
1206
                'href' => $this->url->link('common/dashboard', 'token=' . $this->session->data['token'], true)
1207
            );
1208
1209
            $data['breadcrumbs'][] = array(
1210
                'text' => $this->language->get('heading_title'),
1211
                'href' => $this->url->link('error/not_found', 'token=' . $this->session->data['token'], true)
1212
            );
1213
1214
            $data['header'] = $this->load->controller('common/header');
1215
            $data['column'] = $this->load->controller('common/column_left');
1216
            $data['footer'] = $this->load->controller('common/footer');
1217
1218
            $this->response->setOutput($this->load->view('error/not_found', $data));
1219
        }
1220
    }
1221
1222
    public function history()
1223
    {
1224
        $this->load->language('customer/customer');
1225
1226
        $this->load->model('customer/customer');
1227
1228
        $data['text_no_results'] = $this->language->get('text_no_results');
0 ignored issues
show
Comprehensibility Best Practice introduced by
$data was never initialized. Although not strictly required by PHP, it is generally a good practice to add $data = array(); before regardless.
Loading history...
1229
1230
        $data['column_date_added'] = $this->language->get('column_date_added');
1231
        $data['column_comment'] = $this->language->get('column_comment');
1232
1233
        if (isset($this->request->get['page'])) {
1234
            $page = $this->request->get['page'];
1235
        } else {
1236
            $page = 1;
1237
        }
1238
1239
        $data['histories'] = array();
1240
1241
        $results = $this->model_customer_customer->getHistories($this->request->get['customer_id'], ($page - 1) * 10, 10);
1242
1243
        foreach ($results as $result) {
1244
            $data['histories'][] = array(
1245
                'comment'    => $result['comment'],
1246
                'date_added' => date($this->language->get('date_format_short'), strtotime($result['date_added']))
1247
            );
1248
        }
1249
1250
        $history_total = $this->model_customer_customer->getTotalHistories($this->request->get['customer_id']);
1251
1252
        $pagination = new \Divine\Engine\Library\Pagination();
1253
        $pagination->total = $history_total;
1254
        $pagination->page = $page;
1255
        $pagination->limit = 10;
1256
        $pagination->url = $this->url->link('customer/customer/history', 'token=' . $this->session->data['token'] . '&customer_id=' . $this->request->get['customer_id'] . '&page={page}', true);
1257
1258
        $data['pagination'] = $pagination->render();
1259
1260
        $data['results'] = sprintf($this->language->get('text_pagination'), ($history_total) ? (($page - 1) * 10) + 1 : 0, ((($page - 1) * 10) > ($history_total - 10)) ? $history_total : ((($page - 1) * 10) + 10), $history_total, ceil($history_total / 10));
1261
1262
        $this->response->setOutput($this->load->view('customer/customer_history', $data));
1263
    }
1264
1265
    public function addHistory()
1266
    {
1267
        $this->load->language('customer/customer');
1268
1269
        $json = array();
1270
1271
        if (!$this->user->hasPermission('modify', 'customer/customer')) {
1272
            $json['error'] = $this->language->get('error_permission');
1273
        } else {
1274
            $this->load->model('customer/customer');
1275
1276
            $this->model_customer_customer->addHistory($this->request->get['customer_id'], $this->request->post['comment']);
1277
1278
            $json['success'] = $this->language->get('text_success');
1279
        }
1280
1281
        $this->response->addHeader('Content-Type: application/json');
1282
        $this->response->setOutput(json_encode($json));
1283
    }
1284
1285
    public function transaction()
1286
    {
1287
        $this->load->language('customer/customer');
1288
1289
        $this->load->model('customer/customer');
1290
1291
        $data['text_no_results'] = $this->language->get('text_no_results');
0 ignored issues
show
Comprehensibility Best Practice introduced by
$data was never initialized. Although not strictly required by PHP, it is generally a good practice to add $data = array(); before regardless.
Loading history...
1292
        $data['text_balance'] = $this->language->get('text_balance');
1293
1294
        $data['column_date_added'] = $this->language->get('column_date_added');
1295
        $data['column_description'] = $this->language->get('column_description');
1296
        $data['column_amount'] = $this->language->get('column_amount');
1297
1298
        if (isset($this->request->get['page'])) {
1299
            $page = $this->request->get['page'];
1300
        } else {
1301
            $page = 1;
1302
        }
1303
1304
        $data['transactions'] = array();
1305
1306
        $results = $this->model_customer_customer->getTransactions($this->request->get['customer_id'], ($page - 1) * 10, 10);
1307
1308
        foreach ($results as $result) {
1309
            $data['transactions'][] = array(
1310
                'amount'      => $this->currency->format($result['amount'], $this->config->get('config_currency')),
1311
                'description' => $result['description'],
1312
                'date_added'  => date($this->language->get('date_format_short'), strtotime($result['date_added']))
1313
            );
1314
        }
1315
1316
        $data['balance'] = $this->currency->format($this->model_customer_customer->getTransactionTotal($this->request->get['customer_id']), $this->config->get('config_currency'));
1317
1318
        $transaction_total = $this->model_customer_customer->getTotalTransactions($this->request->get['customer_id']);
1319
1320
        $pagination = new \Divine\Engine\Library\Pagination();
1321
        $pagination->total = $transaction_total;
1322
        $pagination->page = $page;
1323
        $pagination->limit = 10;
1324
        $pagination->url = $this->url->link('customer/customer/transaction', 'token=' . $this->session->data['token'] . '&customer_id=' . $this->request->get['customer_id'] . '&page={page}', true);
1325
1326
        $data['pagination'] = $pagination->render();
1327
1328
        $data['results'] = sprintf($this->language->get('text_pagination'), ($transaction_total) ? (($page - 1) * 10) + 1 : 0, ((($page - 1) * 10) > ($transaction_total - 10)) ? $transaction_total : ((($page - 1) * 10) + 10), $transaction_total, ceil($transaction_total / 10));
1329
1330
        $this->response->setOutput($this->load->view('customer/customer_transaction', $data));
1331
    }
1332
1333
    public function addTransaction()
1334
    {
1335
        $this->load->language('customer/customer');
1336
1337
        $json = array();
1338
1339
        if (!$this->user->hasPermission('modify', 'customer/customer')) {
1340
            $json['error'] = $this->language->get('error_permission');
1341
        } else {
1342
            $this->load->model('customer/customer');
1343
1344
            $this->model_customer_customer->addTransaction($this->request->get['customer_id'], $this->request->post['description'], $this->request->post['amount']);
1345
1346
            $json['success'] = $this->language->get('text_success');
1347
        }
1348
1349
        $this->response->addHeader('Content-Type: application/json');
1350
        $this->response->setOutput(json_encode($json));
1351
    }
1352
1353
    public function reward()
1354
    {
1355
        $this->load->language('customer/customer');
1356
1357
        $this->load->model('customer/customer');
1358
1359
        $data['text_no_results'] = $this->language->get('text_no_results');
0 ignored issues
show
Comprehensibility Best Practice introduced by
$data was never initialized. Although not strictly required by PHP, it is generally a good practice to add $data = array(); before regardless.
Loading history...
1360
        $data['text_balance'] = $this->language->get('text_balance');
1361
1362
        $data['column_date_added'] = $this->language->get('column_date_added');
1363
        $data['column_description'] = $this->language->get('column_description');
1364
        $data['column_points'] = $this->language->get('column_points');
1365
1366
        if (isset($this->request->get['page'])) {
1367
            $page = $this->request->get['page'];
1368
        } else {
1369
            $page = 1;
1370
        }
1371
1372
        $data['rewards'] = array();
1373
1374
        $results = $this->model_customer_customer->getRewards($this->request->get['customer_id'], ($page - 1) * 10, 10);
1375
1376
        foreach ($results as $result) {
1377
            $data['rewards'][] = array(
1378
                'points'      => $result['points'],
1379
                'description' => $result['description'],
1380
                'date_added'  => date($this->language->get('date_format_short'), strtotime($result['date_added']))
1381
            );
1382
        }
1383
1384
        $data['balance'] = $this->model_customer_customer->getRewardTotal($this->request->get['customer_id']);
1385
1386
        $reward_total = $this->model_customer_customer->getTotalRewards($this->request->get['customer_id']);
1387
1388
        $pagination = new \Divine\Engine\Library\Pagination();
1389
        $pagination->total = $reward_total;
1390
        $pagination->page = $page;
1391
        $pagination->limit = 10;
1392
        $pagination->url = $this->url->link('customer/customer/reward', 'token=' . $this->session->data['token'] . '&customer_id=' . $this->request->get['customer_id'] . '&page={page}', true);
1393
1394
        $data['pagination'] = $pagination->render();
1395
1396
        $data['results'] = sprintf($this->language->get('text_pagination'), ($reward_total) ? (($page - 1) * 10) + 1 : 0, ((($page - 1) * 10) > ($reward_total - 10)) ? $reward_total : ((($page - 1) * 10) + 10), $reward_total, ceil($reward_total / 10));
1397
1398
        $this->response->setOutput($this->load->view('customer/customer_reward', $data));
1399
    }
1400
1401
    public function addReward()
1402
    {
1403
        $this->load->language('customer/customer');
1404
1405
        $json = array();
1406
1407
        if (!$this->user->hasPermission('modify', 'customer/customer')) {
1408
            $json['error'] = $this->language->get('error_permission');
1409
        } else {
1410
            $this->load->model('customer/customer');
1411
1412
            $this->model_customer_customer->addReward($this->request->get['customer_id'], $this->request->post['description'], $this->request->post['points']);
1413
1414
            $json['success'] = $this->language->get('text_success');
1415
        }
1416
1417
        $this->response->addHeader('Content-Type: application/json');
1418
        $this->response->setOutput(json_encode($json));
1419
    }
1420
1421
    public function ip()
1422
    {
1423
        $this->load->language('customer/customer');
1424
1425
        $this->load->model('customer/customer');
1426
1427
        $data['text_no_results'] = $this->language->get('text_no_results');
0 ignored issues
show
Comprehensibility Best Practice introduced by
$data was never initialized. Although not strictly required by PHP, it is generally a good practice to add $data = array(); before regardless.
Loading history...
1428
1429
        $data['column_ip'] = $this->language->get('column_ip');
1430
        $data['column_total'] = $this->language->get('column_total');
1431
        $data['column_date_added'] = $this->language->get('column_date_added');
1432
1433
        if (isset($this->request->get['page'])) {
1434
            $page = $this->request->get['page'];
1435
        } else {
1436
            $page = 1;
1437
        }
1438
1439
        $data['ips'] = array();
1440
1441
        $results = $this->model_customer_customer->getIps($this->request->get['customer_id'], ($page - 1) * 10, 10);
1442
1443
        foreach ($results as $result) {
1444
            $data['ips'][] = array(
1445
                'ip'         => $result['ip'],
1446
                'total'      => $this->model_customer_customer->getTotalCustomersByIp($result['ip']),
1447
                'date_added' => date('d/m/y', strtotime($result['date_added'])),
1448
                'filter_ip'  => $this->url->link('customer/customer', 'token=' . $this->session->data['token'] . '&filter_ip=' . $result['ip'], true)
1449
            );
1450
        }
1451
1452
        $ip_total = $this->model_customer_customer->getTotalIps($this->request->get['customer_id']);
1453
1454
        $pagination = new \Divine\Engine\Library\Pagination();
1455
        $pagination->total = $ip_total;
1456
        $pagination->page = $page;
1457
        $pagination->limit = 10;
1458
        $pagination->url = $this->url->link('customer/customer/ip', 'token=' . $this->session->data['token'] . '&customer_id=' . $this->request->get['customer_id'] . '&page={page}', true);
1459
1460
        $data['pagination'] = $pagination->render();
1461
1462
        $data['results'] = sprintf($this->language->get('text_pagination'), ($ip_total) ? (($page - 1) * 10) + 1 : 0, ((($page - 1) * 10) > ($ip_total - 10)) ? $ip_total : ((($page - 1) * 10) + 10), $ip_total, ceil($ip_total / 10));
1463
1464
        $this->response->setOutput($this->load->view('customer/customer_ip', $data));
1465
    }
1466
1467
    public function autocomplete()
1468
    {
1469
        $json = array();
1470
1471
        if (isset($this->request->get['filter_name']) || isset($this->request->get['filter_email'])) {
1472
            if (isset($this->request->get['filter_name'])) {
1473
                $filter_name = $this->request->get['filter_name'];
1474
            } else {
1475
                $filter_name = '';
1476
            }
1477
1478
            if (isset($this->request->get['filter_email'])) {
1479
                $filter_email = $this->request->get['filter_email'];
1480
            } else {
1481
                $filter_email = '';
1482
            }
1483
1484
            $this->load->model('customer/customer');
1485
1486
            $filter_data = array(
1487
                'filter_name'  => $filter_name,
1488
                'filter_email' => $filter_email,
1489
                'start'        => 0,
1490
                'limit'        => 5
1491
            );
1492
1493
            $results = $this->model_customer_customer->getCustomers($filter_data);
1494
1495
            foreach ($results as $result) {
1496
                $json[] = array(
1497
                    'customer_id'       => $result['customer_id'],
1498
                    'customer_group_id' => $result['customer_group_id'],
1499
                    'name'              => strip_tags(html_entity_decode($result['name'], ENT_QUOTES, 'UTF-8')),
1500
                    'customer_group'    => $result['customer_group'],
1501
                    'firstname'         => $result['firstname'],
1502
                    'lastname'          => $result['lastname'],
1503
                    'email'             => $result['email'],
1504
                    'telephone'         => $result['telephone'],
1505
                    'fax'               => $result['fax'],
1506
                    'custom_field'      => json_decode($result['custom_field'], true),
1507
                    'address'           => $this->model_customer_customer->getAddresses($result['customer_id'])
1508
                );
1509
            }
1510
        }
1511
1512
        $sort_order = array();
1513
1514
        foreach ($json as $key => $value) {
1515
            $sort_order[$key] = $value['name'];
1516
        }
1517
1518
        array_multisort($sort_order, SORT_ASC, $json);
1519
1520
        $this->response->addHeader('Content-Type: application/json');
1521
        $this->response->setOutput(json_encode($json));
1522
    }
1523
1524
    public function customfield()
1525
    {
1526
        $json = array();
1527
1528
        $this->load->model('customer/custom_field');
1529
1530
        // Customer Group
1531
        if (isset($this->request->get['customer_group_id'])) {
1532
            $customer_group_id = $this->request->get['customer_group_id'];
1533
        } else {
1534
            $customer_group_id = $this->config->get('config_customer_group_id');
1535
        }
1536
1537
        $custom_fields = $this->model_customer_custom_field->getCustomFields(array('filter_customer_group_id' => $customer_group_id));
1538
1539
        foreach ($custom_fields as $custom_field) {
1540
            $json[] = array(
1541
                'custom_field_id' => $custom_field['custom_field_id'],
1542
                'required'        => empty($custom_field['required']) || $custom_field['required'] == 0 ? false : true
1543
            );
1544
        }
1545
1546
        $this->response->addHeader('Content-Type: application/json');
1547
        $this->response->setOutput(json_encode($json));
1548
    }
1549
1550
    public function address()
1551
    {
1552
        $json = array();
1553
1554
        if (!empty($this->request->get['address_id'])) {
1555
            $this->load->model('customer/customer');
1556
1557
            $json = $this->model_customer_customer->getAddress($this->request->get['address_id']);
1558
        }
1559
1560
        $this->response->addHeader('Content-Type: application/json');
1561
        $this->response->setOutput(json_encode($json));
1562
    }
1563
}
1564