ControllerCustomerCustomField::getForm()   F
last analyzed

Complexity

Conditions 33
Paths > 20000

Size

Total Lines 208
Code Lines 143

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 33
eloc 143
c 0
b 0
f 0
nc 120932352
nop 0
dl 0
loc 208
rs 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 ControllerCustomerCustomField 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
Coding Style introduced by
Expected 2 blank lines before function; 1 found
Loading history...
28
    {
29
        $this->load->language('customer/custom_field');
30
31
        $this->document->setTitle($this->language->get('heading_title'));
32
33
        $this->load->model('customer/custom_field');
34
35
        $this->getList();
36
    }
37
38
    public function add()
39
    {
40
        $this->load->language('customer/custom_field');
41
42
        $this->document->setTitle($this->language->get('heading_title'));
43
44
        $this->load->model('customer/custom_field');
45
46
        if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) {
47
            $this->model_customer_custom_field->addCustomField($this->request->post);
48
49
            $this->session->data['success'] = $this->language->get('text_success');
50
51
            $url = '';
52
53
            if (isset($this->request->get['sort'])) {
54
                $url .= '&sort=' . $this->request->get['sort'];
55
            }
56
57
            if (isset($this->request->get['order'])) {
58
                $url .= '&order=' . $this->request->get['order'];
59
            }
60
61
            if (isset($this->request->get['page'])) {
62
                $url .= '&page=' . $this->request->get['page'];
63
            }
64
65
            $this->response->redirect($this->url->link('customer/custom_field', 'token=' . $this->session->data['token'] . $url, true));
66
        }
67
68
        $this->getForm();
69
    }
70
71
    public function edit()
72
    {
73
        $this->load->language('customer/custom_field');
74
75
        $this->document->setTitle($this->language->get('heading_title'));
76
77
        $this->load->model('customer/custom_field');
78
79
        if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) {
80
            $this->model_customer_custom_field->editCustomField($this->request->get['custom_field_id'], $this->request->post);
81
82
            $this->session->data['success'] = $this->language->get('text_success');
83
84
            $url = '';
85
86
            if (isset($this->request->get['sort'])) {
87
                $url .= '&sort=' . $this->request->get['sort'];
88
            }
89
90
            if (isset($this->request->get['order'])) {
91
                $url .= '&order=' . $this->request->get['order'];
92
            }
93
94
            if (isset($this->request->get['page'])) {
95
                $url .= '&page=' . $this->request->get['page'];
96
            }
97
98
            $this->response->redirect($this->url->link('customer/custom_field', 'token=' . $this->session->data['token'] . $url, true));
99
        }
100
101
        $this->getForm();
102
    }
103
104
    public function delete()
105
    {
106
        $this->load->language('customer/custom_field');
107
108
        $this->document->setTitle($this->language->get('heading_title'));
109
110
        $this->load->model('customer/custom_field');
111
112
        if (isset($this->request->post['selected']) && $this->validateDelete()) {
113
            foreach ($this->request->post['selected'] as $custom_field_id) {
114
                $this->model_customer_custom_field->deleteCustomField($custom_field_id);
115
            }
116
117
            $this->session->data['success'] = $this->language->get('text_success');
118
119
            $url = '';
120
121
            if (isset($this->request->get['sort'])) {
122
                $url .= '&sort=' . $this->request->get['sort'];
123
            }
124
125
            if (isset($this->request->get['order'])) {
126
                $url .= '&order=' . $this->request->get['order'];
127
            }
128
129
            if (isset($this->request->get['page'])) {
130
                $url .= '&page=' . $this->request->get['page'];
131
            }
132
133
            $this->response->redirect($this->url->link('customer/custom_field', 'token=' . $this->session->data['token'] . $url, true));
134
        }
135
136
        $this->getList();
137
    }
138
139
    protected function getList()
140
    {
141
        if (isset($this->request->get['sort'])) {
142
            $sort = $this->request->get['sort'];
143
        } else {
144
            $sort = 'cfd.name';
145
        }
146
147
        if (isset($this->request->get['order'])) {
148
            $order = $this->request->get['order'];
149
        } else {
150
            $order = 'ASC';
151
        }
152
153
        if (isset($this->request->get['page'])) {
154
            $page = $this->request->get['page'];
155
        } else {
156
            $page = 1;
157
        }
158
159
        $url = '';
160
161
        if (isset($this->request->get['sort'])) {
162
            $url .= '&sort=' . $this->request->get['sort'];
163
        }
164
165
        if (isset($this->request->get['order'])) {
166
            $url .= '&order=' . $this->request->get['order'];
167
        }
168
169
        if (isset($this->request->get['page'])) {
170
            $url .= '&page=' . $this->request->get['page'];
171
        }
172
173
        $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...
174
175
        $data['breadcrumbs'][] = array(
176
            'text' => $this->language->get('text_home'),
177
            'href' => $this->url->link('common/dashboard', 'token=' . $this->session->data['token'], true)
178
        );
179
180
        $data['breadcrumbs'][] = array(
181
            'text' => $this->language->get('heading_title'),
182
            'href' => $this->url->link('customer/custom_field', 'token=' . $this->session->data['token'] . $url, true)
183
        );
184
185
        $data['add'] = $this->url->link('customer/custom_field/add', 'token=' . $this->session->data['token'] . $url, true);
186
        $data['delete'] = $this->url->link('customer/custom_field/delete', 'token=' . $this->session->data['token'] . $url, true);
187
188
        $data['custom_fields'] = array();
189
190
        $filter_data = array(
191
            'sort'  => $sort,
192
            'order' => $order,
193
            'start' => ($page - 1) * $this->config->get('config_limit_admin'),
194
            'limit' => $this->config->get('config_limit_admin')
195
        );
196
197
        $custom_field_total = $this->model_customer_custom_field->getTotalCustomFields();
198
199
        $results = $this->model_customer_custom_field->getCustomFields($filter_data);
200
201
        foreach ($results as $result) {
202
            $type = '';
203
204
            switch ($result['type']) {
205
                case 'select':
206
                    $type = $this->language->get('text_select');
207
                    break;
208
                case 'radio':
209
                    $type = $this->language->get('text_radio');
210
                    break;
211
                case 'checkbox':
212
                    $type = $this->language->get('text_checkbox');
213
                    break;
214
                case 'input':
215
                    $type = $this->language->get('text_input');
216
                    break;
217
                case 'text':
218
                    $type = $this->language->get('text_text');
219
                    break;
220
                case 'textarea':
221
                    $type = $this->language->get('text_textarea');
222
                    break;
223
                case 'file':
224
                    $type = $this->language->get('text_file');
225
                    break;
226
                case 'date':
227
                    $type = $this->language->get('text_date');
228
                    break;
229
                case 'datetime':
230
                    $type = $this->language->get('text_datetime');
231
                    break;
232
                case 'time':
233
                    $type = $this->language->get('text_time');
234
                    break;
235
            }
236
237
            $data['custom_fields'][] = array(
238
                'custom_field_id' => $result['custom_field_id'],
239
                'name'            => $result['name'],
240
                'location'        => $this->language->get('text_' . $result['location']),
241
                'type'            => $type,
242
                'status'          => $result['status'],
243
                'sort_order'      => $result['sort_order'],
244
                'edit'            => $this->url->link('customer/custom_field/edit', 'token=' . $this->session->data['token'] . '&custom_field_id=' . $result['custom_field_id'] . $url, true)
245
            );
246
        }
247
248
        $data['heading_title'] = $this->language->get('heading_title');
249
250
        $data['text_list'] = $this->language->get('text_list');
251
        $data['text_no_results'] = $this->language->get('text_no_results');
252
        $data['text_confirm'] = $this->language->get('text_confirm');
253
254
        $data['column_name'] = $this->language->get('column_name');
255
        $data['column_location'] = $this->language->get('column_location');
256
        $data['column_type'] = $this->language->get('column_type');
257
        $data['column_status'] = $this->language->get('column_status');
258
        $data['column_sort_order'] = $this->language->get('column_sort_order');
259
        $data['column_action'] = $this->language->get('column_action');
260
261
        $data['button_add'] = $this->language->get('button_add');
262
        $data['button_edit'] = $this->language->get('button_edit');
263
        $data['button_delete'] = $this->language->get('button_delete');
264
265
        if (isset($this->error['warning'])) {
266
            $data['error_warning'] = $this->error['warning'];
267
        } else {
268
            $data['error_warning'] = '';
269
        }
270
271
        if (isset($this->session->data['success'])) {
272
            $data['success'] = $this->session->data['success'];
273
274
            unset($this->session->data['success']);
275
        } else {
276
            $data['success'] = '';
277
        }
278
279
        if (isset($this->request->post['selected'])) {
280
            $data['selected'] = (array)$this->request->post['selected'];
281
        } else {
282
            $data['selected'] = array();
283
        }
284
285
        $url = '';
286
287
        if ($order == 'ASC') {
288
            $url .= '&order=DESC';
289
        } else {
290
            $url .= '&order=ASC';
291
        }
292
293
        if (isset($this->request->get['page'])) {
294
            $url .= '&page=' . $this->request->get['page'];
295
        }
296
297
        $data['sort_name'] = $this->url->link('customer/custom_field', 'token=' . $this->session->data['token'] . '&sort=cfd.name' . $url, true);
298
        $data['sort_location'] = $this->url->link('customer/custom_field', 'token=' . $this->session->data['token'] . '&sort=cf.location' . $url, true);
299
        $data['sort_type'] = $this->url->link('customer/custom_field', 'token=' . $this->session->data['token'] . '&sort=cf.type' . $url, true);
300
        $data['sort_status'] = $this->url->link('customer/custom_field', 'token=' . $this->session->data['token'] . '&sort=cf.status' . $url, true);
301
        $data['sort_sort_order'] = $this->url->link('customer/custom_field', 'token=' . $this->session->data['token'] . '&sort=cf.sort_order' . $url, true);
302
303
        $url = '';
304
305
        if (isset($this->request->get['sort'])) {
306
            $url .= '&sort=' . $this->request->get['sort'];
307
        }
308
309
        if (isset($this->request->get['order'])) {
310
            $url .= '&order=' . $this->request->get['order'];
311
        }
312
313
        $pagination = new \Divine\Engine\Library\Pagination();
314
        $pagination->total = $custom_field_total;
315
        $pagination->page = $page;
316
        $pagination->limit = $this->config->get('config_limit_admin');
317
        $pagination->url = $this->url->link('customer/custom_field', 'token=' . $this->session->data['token'] . $url . '&page={page}', true);
318
319
        $data['pagination'] = $pagination->render();
320
321
        $data['results'] = sprintf($this->language->get('text_pagination'), ($custom_field_total) ? (($page - 1) * $this->config->get('config_limit_admin')) + 1 : 0, ((($page - 1) * $this->config->get('config_limit_admin')) > ($custom_field_total - $this->config->get('config_limit_admin'))) ? $custom_field_total : ((($page - 1) * $this->config->get('config_limit_admin')) + $this->config->get('config_limit_admin')), $custom_field_total, ceil($custom_field_total / $this->config->get('config_limit_admin')));
322
323
        $data['sort'] = $sort;
324
        $data['order'] = $order;
325
326
        $data['header'] = $this->load->controller('common/header');
327
        $data['column'] = $this->load->controller('common/column_left');
328
        $data['footer'] = $this->load->controller('common/footer');
329
330
        $this->response->setOutput($this->load->view('customer/custom_field_list', $data));
331
    }
332
333
    protected function getForm()
334
    {
335
        $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...
336
337
        $data['text_form'] = !isset($this->request->get['custom_field_id']) ? $this->language->get('text_add') : $this->language->get('text_edit');
338
        $data['text_choose'] = $this->language->get('text_choose');
339
        $data['text_select'] = $this->language->get('text_select');
340
        $data['text_radio'] = $this->language->get('text_radio');
341
        $data['text_checkbox'] = $this->language->get('text_checkbox');
342
        $data['text_input'] = $this->language->get('text_input');
343
        $data['text_text'] = $this->language->get('text_text');
344
        $data['text_textarea'] = $this->language->get('text_textarea');
345
        $data['text_file'] = $this->language->get('text_file');
346
        $data['text_date'] = $this->language->get('text_date');
347
        $data['text_datetime'] = $this->language->get('text_datetime');
348
        $data['text_time'] = $this->language->get('text_time');
349
        $data['text_account'] = $this->language->get('text_account');
350
        $data['text_address'] = $this->language->get('text_address');
351
        $data['text_enabled'] = $this->language->get('text_enabled');
352
        $data['text_disabled'] = $this->language->get('text_disabled');
353
        $data['text_regex'] = $this->language->get('text_regex');
354
355
        $data['entry_name'] = $this->language->get('entry_name');
356
        $data['entry_location'] = $this->language->get('entry_location');
357
        $data['entry_type'] = $this->language->get('entry_type');
358
        $data['entry_value'] = $this->language->get('entry_value');
359
        $data['entry_validation'] = $this->language->get('entry_validation');
360
        $data['entry_custom_value'] = $this->language->get('entry_custom_value');
361
        $data['entry_customer_group'] = $this->language->get('entry_customer_group');
362
        $data['entry_required'] = $this->language->get('entry_required');
363
        $data['entry_status'] = $this->language->get('entry_status');
364
        $data['entry_sort_order'] = $this->language->get('entry_sort_order');
365
366
        $data['help_regex'] = $this->language->get('help_regex');
367
        $data['help_sort_order'] = $this->language->get('help_sort_order');
368
369
        $data['button_save'] = $this->language->get('button_save');
370
        $data['button_cancel'] = $this->language->get('button_cancel');
371
        $data['button_custom_field_value_add'] = $this->language->get('button_custom_field_value_add');
372
        $data['button_remove'] = $this->language->get('button_remove');
373
374
        if (isset($this->error['warning'])) {
375
            $data['error_warning'] = $this->error['warning'];
376
        } else {
377
            $data['error_warning'] = '';
378
        }
379
380
        if (isset($this->error['name'])) {
381
            $data['error_name'] = $this->error['name'];
382
        } else {
383
            $data['error_name'] = array();
384
        }
385
386
        if (isset($this->error['custom_field_value'])) {
387
            $data['error_custom_field_value'] = $this->error['custom_field_value'];
388
        } else {
389
            $data['error_custom_field_value'] = array();
390
        }
391
392
        $url = '';
393
394
        if (isset($this->request->get['sort'])) {
395
            $url .= '&sort=' . $this->request->get['sort'];
396
        }
397
398
        if (isset($this->request->get['order'])) {
399
            $url .= '&order=' . $this->request->get['order'];
400
        }
401
402
        if (isset($this->request->get['page'])) {
403
            $url .= '&page=' . $this->request->get['page'];
404
        }
405
406
        $data['breadcrumbs'] = array();
407
408
        $data['breadcrumbs'][] = array(
409
            'text' => $this->language->get('text_home'),
410
            'href' => $this->url->link('common/dashboard', 'token=' . $this->session->data['token'], true)
411
        );
412
413
        $data['breadcrumbs'][] = array(
414
            'text' => $this->language->get('heading_title'),
415
            'href' => $this->url->link('customer/custom_field', 'token=' . $this->session->data['token'] . $url, true)
416
        );
417
418
        if (!isset($this->request->get['custom_field_id'])) {
419
            $data['action'] = $this->url->link('customer/custom_field/add', 'token=' . $this->session->data['token'] . $url, true);
420
        } else {
421
            $data['action'] = $this->url->link('customer/custom_field/edit', 'token=' . $this->session->data['token'] . '&custom_field_id=' . $this->request->get['custom_field_id'] . $url, true);
422
        }
423
424
        $data['cancel'] = $this->url->link('customer/custom_field', 'token=' . $this->session->data['token'] . $url, true);
425
426
        if (isset($this->request->get['custom_field_id']) && ($this->request->server['REQUEST_METHOD'] != 'POST')) {
427
            $custom_field_info = $this->model_customer_custom_field->getCustomField($this->request->get['custom_field_id']);
428
        }
429
430
        $data['token'] = $this->session->data['token'];
431
432
        $this->load->model('localisation/language');
433
434
        $data['languages'] = $this->model_localisation_language->getLanguages();
435
436
        if (isset($this->request->post['custom_field_description'])) {
437
            $data['custom_field_description'] = $this->request->post['custom_field_description'];
438
        } elseif (isset($this->request->get['custom_field_id'])) {
439
            $data['custom_field_description'] = $this->model_customer_custom_field->getCustomFieldDescriptions($this->request->get['custom_field_id']);
440
        } else {
441
            $data['custom_field_description'] = array();
442
        }
443
444
        if (isset($this->request->post['location'])) {
445
            $data['location'] = $this->request->post['location'];
446
        } elseif (!empty($custom_field_info)) {
447
            $data['location'] = $custom_field_info['location'];
448
        } else {
449
            $data['location'] = '';
450
        }
451
452
        if (isset($this->request->post['type'])) {
453
            $data['type'] = $this->request->post['type'];
454
        } elseif (!empty($custom_field_info)) {
455
            $data['type'] = $custom_field_info['type'];
456
        } else {
457
            $data['type'] = '';
458
        }
459
460
        if (isset($this->request->post['value'])) {
461
            $data['value'] = $this->request->post['value'];
462
        } elseif (!empty($custom_field_info)) {
463
            $data['value'] = $custom_field_info['value'];
464
        } else {
465
            $data['value'] = '';
466
        }
467
468
        if (isset($this->request->post['validation'])) {
469
            $data['validation'] = $this->request->post['validation'];
470
        } elseif (!empty($custom_field_info)) {
471
            $data['validation'] = $custom_field_info['validation'];
472
        } else {
473
            $data['validation'] = '';
474
        }
475
476
        if (isset($this->request->post['status'])) {
477
            $data['status'] = $this->request->post['status'];
478
        } elseif (!empty($custom_field_info)) {
479
            $data['status'] = $custom_field_info['status'];
480
        } else {
481
            $data['status'] = '';
482
        }
483
484
        if (isset($this->request->post['sort_order'])) {
485
            $data['sort_order'] = $this->request->post['sort_order'];
486
        } elseif (!empty($custom_field_info)) {
487
            $data['sort_order'] = $custom_field_info['sort_order'];
488
        } else {
489
            $data['sort_order'] = '';
490
        }
491
492
        if (isset($this->request->post['custom_field_value'])) {
493
            $custom_field_values = $this->request->post['custom_field_value'];
494
        } elseif (isset($this->request->get['custom_field_id'])) {
495
            $custom_field_values = $this->model_customer_custom_field->getCustomFieldValueDescriptions($this->request->get['custom_field_id']);
496
        } else {
497
            $custom_field_values = array();
498
        }
499
500
        $data['custom_field_values'] = array();
501
502
        foreach ($custom_field_values as $custom_field_value) {
503
            $data['custom_field_values'][] = array(
504
                'custom_field_value_id'          => $custom_field_value['custom_field_value_id'],
505
                'custom_field_value_description' => $custom_field_value['custom_field_value_description'],
506
                'sort_order'                     => $custom_field_value['sort_order']
507
            );
508
        }
509
510
        if (isset($this->request->post['custom_field_customer_group'])) {
511
            $custom_field_customer_groups = $this->request->post['custom_field_customer_group'];
512
        } elseif (isset($this->request->get['custom_field_id'])) {
513
            $custom_field_customer_groups = $this->model_customer_custom_field->getCustomFieldCustomerGroups($this->request->get['custom_field_id']);
514
        } else {
515
            $custom_field_customer_groups = array();
516
        }
517
518
        $data['custom_field_customer_group'] = array();
519
520
        foreach ($custom_field_customer_groups as $custom_field_customer_group) {
521
            $data['custom_field_customer_group'][] = $custom_field_customer_group['customer_group_id'];
522
        }
523
524
        $data['custom_field_required'] = array();
525
526
        foreach ($custom_field_customer_groups as $custom_field_customer_group) {
527
            if ($custom_field_customer_group['required']) {
528
                $data['custom_field_required'][] = $custom_field_customer_group['customer_group_id'];
529
            }
530
        }
531
532
        $this->load->model('customer/customer_group');
533
534
        $data['customer_groups'] = $this->model_customer_customer_group->getCustomerGroups();
535
536
        $data['header'] = $this->load->controller('common/header');
537
        $data['column'] = $this->load->controller('common/column_left');
538
        $data['footer'] = $this->load->controller('common/footer');
539
540
        $this->response->setOutput($this->load->view('customer/custom_field_form', $data));
541
    }
542
543
    protected function validateForm()
544
    {
545
        if (!$this->user->hasPermission('modify', 'customer/custom_field')) {
546
            $this->error['warning'] = $this->language->get('error_permission');
547
        }
548
549
        foreach ($this->request->post['custom_field_description'] as $language_id => $value) {
550
            if ((\voku\helper\UTF8::strlen($value['name']) < 1) || (\voku\helper\UTF8::strlen($value['name']) > 128)) {
551
                $this->error['name'][$language_id] = $this->language->get('error_name');
552
            }
553
        }
554
555
        if (($this->request->post['type'] == 'select' || $this->request->post['type'] == 'radio' || $this->request->post['type'] == 'checkbox')) {
556
            if (!isset($this->request->post['custom_field_value'])) {
557
                $this->error['warning'] = $this->language->get('error_type');
558
            }
559
560
            if (isset($this->request->post['custom_field_value'])) {
561
                foreach ($this->request->post['custom_field_value'] as $custom_field_value_id => $custom_field_value) {
562
                    foreach ($custom_field_value['custom_field_value_description'] as $language_id => $custom_field_value_description) {
563
                        if ((\voku\helper\UTF8::strlen($custom_field_value_description['name']) < 1) || (\voku\helper\UTF8::strlen($custom_field_value_description['name']) > 128)) {
564
                            $this->error['custom_field_value'][$custom_field_value_id][$language_id] = $this->language->get('error_custom_value');
565
                        }
566
                    }
567
                }
568
            }
569
        }
570
571
        return !$this->error;
572
    }
573
574
    protected function validateDelete()
575
    {
576
        if (!$this->user->hasPermission('modify', 'customer/custom_field')) {
577
            $this->error['warning'] = $this->language->get('error_permission');
578
        }
579
580
        return !$this->error;
581
    }
582
}
583