Issues (2407)

application/controller/account/address.php (5 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 ControllerAccountAddress 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
        if (!$this->customer->isLogged()) {
30
            $this->session->data['redirect'] = $this->url->link('account/address', '', true);
31
32
            $this->response->redirect($this->url->link('account/login', '', true));
33
        }
34
35
        $this->load->language('account/address');
36
37
        $this->document->setTitle($this->language->get('heading_title'));
38
39
        $this->load->model('account/address');
40
41
        $this->getList();
42
    }
43
44
    public function add()
45
    {
46
        if (!$this->customer->isLogged()) {
47
            $this->session->data['redirect'] = $this->url->link('account/address', '', true);
48
49
            $this->response->redirect($this->url->link('account/login', '', true));
50
        }
51
52
        $this->load->language('account/address');
53
54
        $this->document->setTitle($this->language->get('heading_title'));
55
56
        $this->load->model('account/address');
57
58
        if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) {
59
            $this->model_account_address->addAddress($this->request->post);
60
            
61
            $this->session->data['success'] = $this->language->get('text_add');
62
63
            $this->response->redirect($this->url->link('account/address', '', true));
64
        }
65
66
        $this->getForm();
67
    }
68
69
    public function edit()
70
    {
71
        if (!$this->customer->isLogged()) {
72
            $this->session->data['redirect'] = $this->url->link('account/address', '', true);
73
74
            $this->response->redirect($this->url->link('account/login', '', true));
75
        }
76
77
        $this->load->language('account/address');
78
79
        $this->document->setTitle($this->language->get('heading_title'));
80
81
        $this->load->model('account/address');
82
        
83
        if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) {
84
            $this->model_account_address->editAddress($this->request->get['address_id'], $this->request->post);
85
86
            // Default Shipping Address
87
            if (isset($this->session->data['shipping_address']['address_id']) && ($this->request->get['address_id'] == $this->session->data['shipping_address']['address_id'])) {
88
                $this->session->data['shipping_address'] = $this->model_account_address->getAddress($this->request->get['address_id']);
89
90
                unset($this->session->data['shipping_method']);
91
                unset($this->session->data['shipping_methods']);
92
            }
93
94
            // Default Payment Address
95
            if (isset($this->session->data['payment_address']['address_id']) && ($this->request->get['address_id'] == $this->session->data['payment_address']['address_id'])) {
96
                $this->session->data['payment_address'] = $this->model_account_address->getAddress($this->request->get['address_id']);
97
98
                unset($this->session->data['payment_method']);
99
                unset($this->session->data['payment_methods']);
100
            }
101
102
            $this->session->data['success'] = $this->language->get('text_edit');
103
104
            $this->response->redirect($this->url->link('account/address', '', true));
105
        }
106
107
        $this->getForm();
108
    }
109
110
    public function delete()
111
    {
112
        if (!$this->customer->isLogged()) {
113
            $this->session->data['redirect'] = $this->url->link('account/address', '', true);
114
115
            $this->response->redirect($this->url->link('account/login', '', true));
116
        }
117
118
        $this->load->language('account/address');
119
120
        $this->document->setTitle($this->language->get('heading_title'));
121
122
        $this->load->model('account/address');
123
124
        if (isset($this->request->get['address_id']) && $this->validateDelete()) {
125
            $this->model_account_address->deleteAddress($this->request->get['address_id']);
126
127
            // Default Shipping Address
128
            if (isset($this->session->data['shipping_address']['address_id']) && ($this->request->get['address_id'] == $this->session->data['shipping_address']['address_id'])) {
129
                unset($this->session->data['shipping_address']);
130
                unset($this->session->data['shipping_method']);
131
                unset($this->session->data['shipping_methods']);
132
            }
133
134
            // Default Payment Address
135
            if (isset($this->session->data['payment_address']['address_id']) && ($this->request->get['address_id'] == $this->session->data['payment_address']['address_id'])) {
136
                unset($this->session->data['payment_address']);
137
                unset($this->session->data['payment_method']);
138
                unset($this->session->data['payment_methods']);
139
            }
140
141
            $this->session->data['success'] = $this->language->get('text_delete');
142
143
            $this->response->redirect($this->url->link('account/address', '', true));
144
        }
145
146
        $this->getList();
147
    }
148
149
    protected function getList()
150
    {
151
        $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...
152
            'text' => $this->language->get('text_home'),
153
            'href' => $this->url->link('common/home')
154
        );
155
156
        $data['breadcrumbs'][] = array(
157
            'text' => $this->language->get('text_account'),
158
            'href' => $this->url->link('account/account', '', true)
159
        );
160
161
        $data['breadcrumbs'][] = array(
162
            'text' => $this->language->get('heading_title'),
163
            'href' => $this->url->link('account/address', '', true)
164
        );
165
166
        $data['heading_title'] = $this->language->get('heading_title');
167
168
        $data['text_address_book'] = $this->language->get('text_address_book');
169
        $data['text_empty'] = $this->language->get('text_empty');
170
171
        $data['button_new_address'] = $this->language->get('button_new_address');
172
        $data['button_edit'] = $this->language->get('button_edit');
173
        $data['button_delete'] = $this->language->get('button_delete');
174
        $data['button_back'] = $this->language->get('button_back');
175
176
        if (isset($this->error['warning'])) {
177
            $data['error_warning'] = $this->error['warning'];
178
        } else {
179
            $data['error_warning'] = '';
180
        }
181
182
        if (isset($this->session->data['success'])) {
183
            $data['success'] = $this->session->data['success'];
184
185
            unset($this->session->data['success']);
186
        } else {
187
            $data['success'] = '';
188
        }
189
190
        $data['addresses'] = array();
191
192
        $results = $this->model_account_address->getAddresses();
193
194
        foreach ($results as $result) {
195
            if ($result['address_format']) {
196
                $format = $result['address_format'];
197
            } else {
198
                $format = '{firstname} {lastname}' . "\n" . '{company}' . "\n" . '{address_1}' . "\n" . '{address_2}' . "\n" . '{city} {postcode}' . "\n" . '{zone}' . "\n" . '{country}';
199
            }
200
201
            $find = array(
202
                '{firstname}',
203
                '{lastname}',
204
                '{company}',
205
                '{address_1}',
206
                '{address_2}',
207
                '{city}',
208
                '{postcode}',
209
                '{zone}',
210
                '{zone_code}',
211
                '{country}'
212
            );
213
214
            $replace = array(
215
                'firstname' => $result['firstname'],
216
                'lastname'  => $result['lastname'],
217
                'company'   => $result['company'],
218
                'address_1' => $result['address_1'],
219
                'address_2' => $result['address_2'],
220
                'city'      => $result['city'],
221
                'postcode'  => $result['postcode'],
222
                'zone'      => $result['zone'],
223
                'zone_code' => $result['zone_code'],
224
                'country'   => $result['country']
225
            );
226
227
            $data['addresses'][] = array(
228
                'address_id' => $result['address_id'],
229
                'address'    => str_replace(array("\r\n", "\r", "\n"), '<br />', preg_replace(array("/\s\s+/", "/\r\r+/", "/\n\n+/"), '<br />', trim(str_replace($find, $replace, $format)))),
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal /\s\s+/ does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
230
                'update'     => $this->url->link('account/address/edit', 'address_id=' . $result['address_id'], true),
231
                'delete'     => $this->url->link('account/address/delete', 'address_id=' . $result['address_id'], true)
232
            );
233
        }
234
235
        $data['add'] = $this->url->link('account/address/add', '', true);
236
        $data['back'] = $this->url->link('account/account', '', true);
237
238
        $data['column'] = $this->load->controller('common/column');
239
        $data['content_top'] = $this->load->controller('common/content_top');
240
        $data['content_bottom'] = $this->load->controller('common/content_bottom');
241
        $data['footer'] = $this->load->controller('common/footer');
242
        $data['header'] = $this->load->controller('common/header');
243
244
        $this->response->setOutput($this->load->view('account/address_list', $data));
245
    }
246
247
    protected function getForm()
248
    {
249
        $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...
250
251
        $data['breadcrumbs'][] = array(
252
            'text' => $this->language->get('text_home'),
253
            'href' => $this->url->link('common/home')
254
        );
255
256
        $data['breadcrumbs'][] = array(
257
            'text' => $this->language->get('text_account'),
258
            'href' => $this->url->link('account/account', '', true)
259
        );
260
261
        $data['breadcrumbs'][] = array(
262
            'text' => $this->language->get('heading_title'),
263
            'href' => $this->url->link('account/address', '', true)
264
        );
265
266
        if (!isset($this->request->get['address_id'])) {
267
            $data['breadcrumbs'][] = array(
268
                'text' => $this->language->get('text_edit_address'),
269
                'href' => $this->url->link('account/address/add', '', true)
270
            );
271
        } else {
272
            $data['breadcrumbs'][] = array(
273
                'text' => $this->language->get('text_edit_address'),
274
                'href' => $this->url->link('account/address/edit', 'address_id=' . $this->request->get['address_id'], true)
275
            );
276
        }
277
278
        $data['heading_title'] = $this->language->get('heading_title');
279
280
        $data['text_edit_address'] = $this->language->get('text_edit_address');
281
        $data['text_yes'] = $this->language->get('text_yes');
282
        $data['text_no'] = $this->language->get('text_no');
283
        $data['text_select'] = $this->language->get('text_select');
284
        $data['text_none'] = $this->language->get('text_none');
285
        $data['text_loading'] = $this->language->get('text_loading');
286
287
        $data['entry_firstname'] = $this->language->get('entry_firstname');
288
        $data['entry_lastname'] = $this->language->get('entry_lastname');
289
        $data['entry_company'] = $this->language->get('entry_company');
290
        $data['entry_address_1'] = $this->language->get('entry_address_1');
291
        $data['entry_address_2'] = $this->language->get('entry_address_2');
292
        $data['entry_postcode'] = $this->language->get('entry_postcode');
293
        $data['entry_city'] = $this->language->get('entry_city');
294
        $data['entry_country'] = $this->language->get('entry_country');
295
        $data['entry_zone'] = $this->language->get('entry_zone');
296
        $data['entry_default'] = $this->language->get('entry_default');
297
298
        $data['button_continue'] = $this->language->get('button_continue');
299
        $data['button_back'] = $this->language->get('button_back');
300
        $data['button_upload'] = $this->language->get('button_upload');
301
302
        if (isset($this->error['firstname'])) {
303
            $data['error_firstname'] = $this->error['firstname'];
304
        } else {
305
            $data['error_firstname'] = '';
306
        }
307
308
        if (isset($this->error['lastname'])) {
309
            $data['error_lastname'] = $this->error['lastname'];
310
        } else {
311
            $data['error_lastname'] = '';
312
        }
313
314
        if (isset($this->error['address_1'])) {
315
            $data['error_address_1'] = $this->error['address_1'];
316
        } else {
317
            $data['error_address_1'] = '';
318
        }
319
320
        if (isset($this->error['city'])) {
321
            $data['error_city'] = $this->error['city'];
322
        } else {
323
            $data['error_city'] = '';
324
        }
325
326
        if (isset($this->error['postcode'])) {
327
            $data['error_postcode'] = $this->error['postcode'];
328
        } else {
329
            $data['error_postcode'] = '';
330
        }
331
332
        if (isset($this->error['country'])) {
333
            $data['error_country'] = $this->error['country'];
334
        } else {
335
            $data['error_country'] = '';
336
        }
337
338
        if (isset($this->error['zone'])) {
339
            $data['error_zone'] = $this->error['zone'];
340
        } else {
341
            $data['error_zone'] = '';
342
        }
343
344
        if (isset($this->error['custom_field'])) {
345
            $data['error_custom_field'] = $this->error['custom_field'];
346
        } else {
347
            $data['error_custom_field'] = array();
348
        }
349
        
350
        if (!isset($this->request->get['address_id'])) {
351
            $data['action'] = $this->url->link('account/address/add', '', true);
352
        } else {
353
            $data['action'] = $this->url->link('account/address/edit', 'address_id=' . $this->request->get['address_id'], true);
354
        }
355
356
        if (isset($this->request->get['address_id']) && ($this->request->server['REQUEST_METHOD'] != 'POST')) {
357
            $address_info = $this->model_account_address->getAddress($this->request->get['address_id']);
358
        }
359
360
        if (isset($this->request->post['firstname'])) {
361
            $data['firstname'] = $this->request->post['firstname'];
362
        } elseif (!empty($address_info)) {
363
            $data['firstname'] = $address_info['firstname'];
364
        } else {
365
            $data['firstname'] = '';
366
        }
367
368
        if (isset($this->request->post['lastname'])) {
369
            $data['lastname'] = $this->request->post['lastname'];
370
        } elseif (!empty($address_info)) {
371
            $data['lastname'] = $address_info['lastname'];
372
        } else {
373
            $data['lastname'] = '';
374
        }
375
376
        if (isset($this->request->post['company'])) {
377
            $data['company'] = $this->request->post['company'];
378
        } elseif (!empty($address_info)) {
379
            $data['company'] = $address_info['company'];
380
        } else {
381
            $data['company'] = '';
382
        }
383
384
        if (isset($this->request->post['address_1'])) {
385
            $data['address_1'] = $this->request->post['address_1'];
386
        } elseif (!empty($address_info)) {
387
            $data['address_1'] = $address_info['address_1'];
388
        } else {
389
            $data['address_1'] = '';
390
        }
391
392
        if (isset($this->request->post['address_2'])) {
393
            $data['address_2'] = $this->request->post['address_2'];
394
        } elseif (!empty($address_info)) {
395
            $data['address_2'] = $address_info['address_2'];
396
        } else {
397
            $data['address_2'] = '';
398
        }
399
400
        if (isset($this->request->post['postcode'])) {
401
            $data['postcode'] = $this->request->post['postcode'];
402
        } elseif (!empty($address_info)) {
403
            $data['postcode'] = $address_info['postcode'];
404
        } else {
405
            $data['postcode'] = '';
406
        }
407
408
        if (isset($this->request->post['city'])) {
409
            $data['city'] = $this->request->post['city'];
410
        } elseif (!empty($address_info)) {
411
            $data['city'] = $address_info['city'];
412
        } else {
413
            $data['city'] = '';
414
        }
415
416
        if (isset($this->request->post['country_id'])) {
417
            $data['country_id'] = (int)$this->request->post['country_id'];
418
        } elseif (!empty($address_info)) {
419
            $data['country_id'] = $address_info['country_id'];
420
        } else {
421
            $data['country_id'] = $this->config->get('config_country_id');
422
        }
423
424
        if (isset($this->request->post['zone_id'])) {
425
            $data['zone_id'] = (int)$this->request->post['zone_id'];
426
        } elseif (!empty($address_info)) {
427
            $data['zone_id'] = $address_info['zone_id'];
428
        } else {
429
            $data['zone_id'] = '';
430
        }
431
432
        $this->load->model('localisation/country');
433
434
        $data['countries'] = $this->model_localisation_country->getCountries();
435
436
        // Custom fields
437
        $this->load->model('account/custom_field');
438
439
        $data['custom_fields'] = $this->model_account_custom_field->getCustomFields($this->config->get('config_customer_group_id'));
440
441
        if (isset($this->request->post['custom_field'])) {
442
            $data['address_custom_field'] = $this->request->post['custom_field'];
443
        } elseif (isset($address_info)) {
444
            $data['address_custom_field'] = $address_info['custom_field'];
445
        } else {
446
            $data['address_custom_field'] = array();
447
        }
448
449
        if (isset($this->request->post['default'])) {
450
            $data['default'] = $this->request->post['default'];
451
        } elseif (isset($this->request->get['address_id'])) {
452
            $data['default'] = $this->customer->getAddressId() == $this->request->get['address_id'];
453
        } else {
454
            $data['default'] = false;
455
        }
456
457
        $data['back'] = $this->url->link('account/address', '', true);
458
459
        $data['column'] = $this->load->controller('common/column');
460
        $data['content_top'] = $this->load->controller('common/content_top');
461
        $data['content_bottom'] = $this->load->controller('common/content_bottom');
462
        $data['footer'] = $this->load->controller('common/footer');
463
        $data['header'] = $this->load->controller('common/header');
464
465
466
        $this->response->setOutput($this->load->view('account/address_form', $data));
467
    }
468
469
    protected function validateForm()
470
    {
471
        if ((\voku\helper\UTF8::strlen(trim($this->request->post['firstname'])) < 1) || (\voku\helper\UTF8::strlen(trim($this->request->post['firstname'])) > 32)) {
472
            $this->error['firstname'] = $this->language->get('error_firstname');
473
        }
474
475
        if ((\voku\helper\UTF8::strlen(trim($this->request->post['lastname'])) < 1) || (\voku\helper\UTF8::strlen(trim($this->request->post['lastname'])) > 32)) {
476
            $this->error['lastname'] = $this->language->get('error_lastname');
477
        }
478
479
        if ((\voku\helper\UTF8::strlen(trim($this->request->post['address_1'])) < 3) || (\voku\helper\UTF8::strlen(trim($this->request->post['address_1'])) > 128)) {
480
            $this->error['address_1'] = $this->language->get('error_address_1');
481
        }
482
483
        if ((\voku\helper\UTF8::strlen(trim($this->request->post['city'])) < 2) || (\voku\helper\UTF8::strlen(trim($this->request->post['city'])) > 128)) {
484
            $this->error['city'] = $this->language->get('error_city');
485
        }
486
487
        $this->load->model('localisation/country');
488
489
        $country_info = $this->model_localisation_country->getCountry($this->request->post['country_id']);
490
491
        if ($country_info && $country_info['postcode_required'] && (\voku\helper\UTF8::strlen(trim($this->request->post['postcode'])) < 2 || \voku\helper\UTF8::strlen(trim($this->request->post['postcode'])) > 10)) {
492
            $this->error['postcode'] = $this->language->get('error_postcode');
493
        }
494
495
        if ($this->request->post['country_id'] == '' || !is_numeric($this->request->post['country_id'])) {
496
            $this->error['country'] = $this->language->get('error_country');
497
        }
498
499
        if (!isset($this->request->post['zone_id']) || $this->request->post['zone_id'] == '' || !is_numeric($this->request->post['zone_id'])) {
500
            $this->error['zone'] = $this->language->get('error_zone');
501
        }
502
503
        // Custom field validation
504
        $this->load->model('account/custom_field');
505
506
        $custom_fields = $this->model_account_custom_field->getCustomFields($this->config->get('config_customer_group_id'));
507
508
        foreach ($custom_fields as $custom_field) {
509
            if (($custom_field['location'] == 'address') && $custom_field['required'] && empty($this->request->post['custom_field'][$custom_field['custom_field_id']])) {
510
                $this->error['custom_field'][$custom_field['custom_field_id']] = sprintf($this->language->get('error_custom_field'), $custom_field['name']);
511
            } elseif (($custom_field['location'] == 'address') && ($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'])))) {
512
                $this->error['custom_field'][$custom_field['custom_field_id']] = sprintf($this->language->get('error_custom_field'), $custom_field['name']);
513
            }
514
        }
515
516
        return !$this->error;
517
    }
518
519
    protected function validateDelete()
520
    {
521
        if ($this->model_account_address->getTotalAddresses() == 1) {
522
            $this->error['warning'] = $this->language->get('error_delete');
523
        }
524
525
        if ($this->customer->getAddressId() == $this->request->get['address_id']) {
526
            $this->error['warning'] = $this->language->get('error_default');
527
        }
528
529
        return !$this->error;
530
    }
531
}
532