ControllerCatalogOption::validateDelete()   A
last analyzed

Complexity

Conditions 4
Paths 6

Size

Total Lines 17
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 8
c 0
b 0
f 0
nc 6
nop 0
dl 0
loc 17
rs 10
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 ControllerCatalogOption 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('catalog/option');
30
31
        $this->document->setTitle($this->language->get('heading_title'));
32
33
        $this->load->model('catalog/option');
34
35
        $this->getList();
36
    }
37
38
    public function add()
39
    {
40
        $this->load->language('catalog/option');
41
42
        $this->document->setTitle($this->language->get('heading_title'));
43
44
        $this->load->model('catalog/option');
45
46
        if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) {
47
            $this->model_catalog_option->addOption($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('catalog/option', 'token=' . $this->session->data['token'] . $url, true));
66
        }
67
68
        $this->getForm();
69
    }
70
71
    public function edit()
72
    {
73
        $this->load->language('catalog/option');
74
75
        $this->document->setTitle($this->language->get('heading_title'));
76
77
        $this->load->model('catalog/option');
78
79
        if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) {
80
            $this->model_catalog_option->editOption($this->request->get['option_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('catalog/option', 'token=' . $this->session->data['token'] . $url, true));
99
        }
100
101
        $this->getForm();
102
    }
103
104
    public function delete()
105
    {
106
        $this->load->language('catalog/option');
107
108
        $this->document->setTitle($this->language->get('heading_title'));
109
110
        $this->load->model('catalog/option');
111
112
        if (isset($this->request->post['selected']) && $this->validateDelete()) {
113
            foreach ($this->request->post['selected'] as $option_id) {
114
                $this->model_catalog_option->deleteOption($option_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('catalog/option', '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 = 'od.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('catalog/option', 'token=' . $this->session->data['token'] . $url, true)
183
        );
184
185
        $data['add'] = $this->url->link('catalog/option/add', 'token=' . $this->session->data['token'] . $url, true);
186
        $data['delete'] = $this->url->link('catalog/option/delete', 'token=' . $this->session->data['token'] . $url, true);
187
188
        $data['options'] = 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
        $option_total = $this->model_catalog_option->getTotalOptions();
198
199
        $results = $this->model_catalog_option->getOptions($filter_data);
200
201
        foreach ($results as $result) {
202
            $data['options'][] = array(
203
                'option_id'  => $result['option_id'],
204
                'name'       => $result['name'],
205
                'sort_order' => $result['sort_order'],
206
                'edit'       => $this->url->link('catalog/option/edit', 'token=' . $this->session->data['token'] . '&option_id=' . $result['option_id'] . $url, true)
207
            );
208
        }
209
210
        $data['heading_title'] = $this->language->get('heading_title');
211
212
        $data['text_list'] = $this->language->get('text_list');
213
        $data['text_no_results'] = $this->language->get('text_no_results');
214
        $data['text_confirm'] = $this->language->get('text_confirm');
215
216
        $data['column_name'] = $this->language->get('column_name');
217
        $data['column_sort_order'] = $this->language->get('column_sort_order');
218
        $data['column_action'] = $this->language->get('column_action');
219
220
        $data['button_add'] = $this->language->get('button_add');
221
        $data['button_edit'] = $this->language->get('button_edit');
222
        $data['button_delete'] = $this->language->get('button_delete');
223
224
        if (isset($this->error['warning'])) {
225
            $data['error_warning'] = $this->error['warning'];
226
        } else {
227
            $data['error_warning'] = '';
228
        }
229
230
        if (isset($this->session->data['success'])) {
231
            $data['success'] = $this->session->data['success'];
232
233
            unset($this->session->data['success']);
234
        } else {
235
            $data['success'] = '';
236
        }
237
238
        if (isset($this->request->post['selected'])) {
239
            $data['selected'] = (array)$this->request->post['selected'];
240
        } else {
241
            $data['selected'] = array();
242
        }
243
244
        $url = '';
245
246
        if ($order == 'ASC') {
247
            $url .= '&order=DESC';
248
        } else {
249
            $url .= '&order=ASC';
250
        }
251
252
        if (isset($this->request->get['page'])) {
253
            $url .= '&page=' . $this->request->get['page'];
254
        }
255
256
        $data['sort_name'] = $this->url->link('catalog/option', 'token=' . $this->session->data['token'] . '&sort=od.name' . $url, true);
257
        $data['sort_sort_order'] = $this->url->link('catalog/option', 'token=' . $this->session->data['token'] . '&sort=o.sort_order' . $url, true);
258
259
        $url = '';
260
261
        if (isset($this->request->get['sort'])) {
262
            $url .= '&sort=' . $this->request->get['sort'];
263
        }
264
265
        if (isset($this->request->get['order'])) {
266
            $url .= '&order=' . $this->request->get['order'];
267
        }
268
269
        $pagination = new \Divine\Engine\Library\Pagination();
270
        $pagination->total = $option_total;
271
        $pagination->page = $page;
272
        $pagination->limit = $this->config->get('config_limit_admin');
273
        $pagination->url = $this->url->link('catalog/option', 'token=' . $this->session->data['token'] . $url . '&page={page}', true);
274
275
        $data['pagination'] = $pagination->render();
276
277
        $data['results'] = sprintf($this->language->get('text_pagination'), ($option_total) ? (($page - 1) * $this->config->get('config_limit_admin')) + 1 : 0, ((($page - 1) * $this->config->get('config_limit_admin')) > ($option_total - $this->config->get('config_limit_admin'))) ? $option_total : ((($page - 1) * $this->config->get('config_limit_admin')) + $this->config->get('config_limit_admin')), $option_total, ceil($option_total / $this->config->get('config_limit_admin')));
278
279
        $data['sort'] = $sort;
280
        $data['order'] = $order;
281
282
        $data['header'] = $this->load->controller('common/header');
283
        $data['column'] = $this->load->controller('common/column_left');
284
        $data['footer'] = $this->load->controller('common/footer');
285
286
        $this->response->setOutput($this->load->view('catalog/option_list', $data));
287
    }
288
289
    protected function getForm()
290
    {
291
        $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...
292
293
        $data['text_form'] = !isset($this->request->get['option_id']) ? $this->language->get('text_add') : $this->language->get('text_edit');
294
        $data['text_choose'] = $this->language->get('text_choose');
295
        $data['text_select'] = $this->language->get('text_select');
296
        $data['text_radio'] = $this->language->get('text_radio');
297
        $data['text_checkbox'] = $this->language->get('text_checkbox');
298
299
        $data['entry_name'] = $this->language->get('entry_name');
300
        $data['entry_type'] = $this->language->get('entry_type');
301
        $data['entry_option_value'] = $this->language->get('entry_option_value');
302
        $data['entry_image'] = $this->language->get('entry_image');
303
        $data['entry_sort_order'] = $this->language->get('entry_sort_order');
304
305
        $data['button_save'] = $this->language->get('button_save');
306
        $data['button_cancel'] = $this->language->get('button_cancel');
307
        $data['button_option_value_add'] = $this->language->get('button_option_value_add');
308
        $data['button_remove'] = $this->language->get('button_remove');
309
310
        if (isset($this->error['warning'])) {
311
            $data['error_warning'] = $this->error['warning'];
312
        } else {
313
            $data['error_warning'] = '';
314
        }
315
316
        if (isset($this->error['name'])) {
317
            $data['error_name'] = $this->error['name'];
318
        } else {
319
            $data['error_name'] = array();
320
        }
321
322
        if (isset($this->error['option_value'])) {
323
            $data['error_option_value'] = $this->error['option_value'];
324
        } else {
325
            $data['error_option_value'] = array();
326
        }
327
328
        $url = '';
329
330
        if (isset($this->request->get['sort'])) {
331
            $url .= '&sort=' . $this->request->get['sort'];
332
        }
333
334
        if (isset($this->request->get['order'])) {
335
            $url .= '&order=' . $this->request->get['order'];
336
        }
337
338
        if (isset($this->request->get['page'])) {
339
            $url .= '&page=' . $this->request->get['page'];
340
        }
341
342
        $data['breadcrumbs'] = array();
343
344
        $data['breadcrumbs'][] = array(
345
            'text' => $this->language->get('text_home'),
346
            'href' => $this->url->link('common/dashboard', 'token=' . $this->session->data['token'], true)
347
        );
348
349
        $data['breadcrumbs'][] = array(
350
            'text' => $this->language->get('heading_title'),
351
            'href' => $this->url->link('catalog/option', 'token=' . $this->session->data['token'] . $url, true)
352
        );
353
354
        if (!isset($this->request->get['option_id'])) {
355
            $data['action'] = $this->url->link('catalog/option/add', 'token=' . $this->session->data['token'] . $url, true);
356
        } else {
357
            $data['action'] = $this->url->link('catalog/option/edit', 'token=' . $this->session->data['token'] . '&option_id=' . $this->request->get['option_id'] . $url, true);
358
        }
359
360
        $data['cancel'] = $this->url->link('catalog/option', 'token=' . $this->session->data['token'] . $url, true);
361
362
        if (isset($this->request->get['option_id']) && ($this->request->server['REQUEST_METHOD'] != 'POST')) {
363
            $option_info = $this->model_catalog_option->getOption($this->request->get['option_id']);
364
        }
365
366
        $data['token'] = $this->session->data['token'];
367
368
        $this->load->model('localisation/language');
369
370
        $data['languages'] = $this->model_localisation_language->getLanguages();
371
372
        if (isset($this->request->post['option_description'])) {
373
            $data['option_description'] = $this->request->post['option_description'];
374
        } elseif (isset($this->request->get['option_id'])) {
375
            $data['option_description'] = $this->model_catalog_option->getOptionDescriptions($this->request->get['option_id']);
376
        } else {
377
            $data['option_description'] = array();
378
        }
379
380
        if (isset($this->request->post['type'])) {
381
            $data['type'] = $this->request->post['type'];
382
        } elseif (!empty($option_info)) {
383
            $data['type'] = $option_info['type'];
384
        } else {
385
            $data['type'] = '';
386
        }
387
388
        if (isset($this->request->post['sort_order'])) {
389
            $data['sort_order'] = $this->request->post['sort_order'];
390
        } elseif (!empty($option_info)) {
391
            $data['sort_order'] = $option_info['sort_order'];
392
        } else {
393
            $data['sort_order'] = '';
394
        }
395
396
        if (isset($this->request->post['option_value'])) {
397
            $option_values = $this->request->post['option_value'];
398
        } elseif (isset($this->request->get['option_id'])) {
399
            $option_values = $this->model_catalog_option->getOptionValueDescriptions($this->request->get['option_id']);
400
        } else {
401
            $option_values = array();
402
        }
403
404
        
405
406
        $data['option_values'] = array();
407
408
        foreach ($option_values as $option_value) {
409
            if (is_file($_SERVER['DOCUMENT_ROOT'] . '/public_html/assets/images/' . $option_value['image'])) {
410
                $image = $option_value['image'];
411
                $thumb = $option_value['image'];
412
            } else {
413
                $image = '';
414
                $thumb = 'no_image.png';
415
            }
416
417
            $data['option_values'][] = array(
418
                'option_value_id'          => $option_value['option_value_id'],
419
                'option_value_description' => $option_value['option_value_description'],
420
                'image'                    => $image,
421
                'thumb'                    => '/public_html/assets/images/' . $thumb,
422
                'sort_order'               => $option_value['sort_order']
423
            );
424
        }
425
426
        $data['placeholder'] = '/public_html/assets/images/no_image.png';
427
428
        $data['header'] = $this->load->controller('common/header');
429
        $data['column'] = $this->load->controller('common/column_left');
430
        $data['footer'] = $this->load->controller('common/footer');
431
432
        $this->response->setOutput($this->load->view('catalog/option_form', $data));
433
    }
434
435
    protected function validateForm()
436
    {
437
        if (!$this->user->hasPermission('modify', 'catalog/option')) {
438
            $this->error['warning'] = $this->language->get('error_permission');
439
        }
440
441
        foreach ($this->request->post['option_description'] as $language_id => $value) {
442
            if ((\voku\helper\UTF8::strlen($value['name']) < 1) || (\voku\helper\UTF8::strlen($value['name']) > 128)) {
443
                $this->error['name'][$language_id] = $this->language->get('error_name');
444
            }
445
        }
446
447
        if (($this->request->post['type'] == 'select' || $this->request->post['type'] == 'radio' || $this->request->post['type'] == 'checkbox') && !isset($this->request->post['option_value'])) {
448
            $this->error['warning'] = $this->language->get('error_type');
449
        }
450
451
        if (isset($this->request->post['option_value'])) {
452
            foreach ($this->request->post['option_value'] as $option_value_id => $option_value) {
453
                foreach ($option_value['option_value_description'] as $language_id => $option_value_description) {
454
                    if ((\voku\helper\UTF8::strlen($option_value_description['name']) < 1) || (\voku\helper\UTF8::strlen($option_value_description['name']) > 128)) {
455
                        $this->error['option_value'][$option_value_id][$language_id] = $this->language->get('error_option_value');
456
                    }
457
                }
458
            }
459
        }
460
461
        return !$this->error;
462
    }
463
464
    protected function validateDelete()
465
    {
466
        if (!$this->user->hasPermission('modify', 'catalog/option')) {
467
            $this->error['warning'] = $this->language->get('error_permission');
468
        }
469
470
        $this->load->model('catalog/product');
471
472
        foreach ($this->request->post['selected'] as $option_id) {
473
            $product_total = $this->model_catalog_product->getTotalProductsByOptionId($option_id);
474
475
            if ($product_total) {
476
                $this->error['warning'] = sprintf($this->language->get('error_product'), $product_total);
477
            }
478
        }
479
480
        return !$this->error;
481
    }
482
483
    public function autocomplete()
484
    {
485
        $json = array();
486
487
        if (isset($this->request->get['filter_name'])) {
488
            $this->load->language('catalog/option');
489
490
            $this->load->model('catalog/option');
491
492
            
493
494
            $filter_data = array(
495
                'filter_name' => $this->request->get['filter_name'],
496
                'start'       => 0,
497
                'limit'       => 5
498
            );
499
500
            $options = $this->model_catalog_option->getOptions($filter_data);
501
502
            foreach ($options as $option) {
503
                $option_value_data = array();
504
505
                if ($option['type'] == 'select' || $option['type'] == 'radio' || $option['type'] == 'checkbox' || $option['type'] == 'image') {
506
                    $option_values = $this->model_catalog_option->getOptionValues($option['option_id']);
507
508
                    foreach ($option_values as $option_value) {
509
                        if (is_file($_SERVER['DOCUMENT_ROOT'] . '/public_html/assets/images/' . $option_value['image'])) {
510
                            $image = '/public_html/assets/images/' . $option_value['image'];
511
                        } else {
512
                            $image = '/public_html/assets/images/no_image.png';
513
                        }
514
515
                        $option_value_data[] = array(
516
                            'option_value_id' => $option_value['option_value_id'],
517
                            'name'            => strip_tags(html_entity_decode($option_value['name'], ENT_QUOTES, 'UTF-8')),
518
                            'image'           => $image
519
                        );
520
                    }
521
522
                    $sort_order = array();
523
524
                    foreach ($option_value_data as $key => $value) {
525
                        $sort_order[$key] = $value['name'];
526
                    }
527
528
                    array_multisort($sort_order, SORT_ASC, $option_value_data);
529
                }
530
531
                $type = '';
532
533
                if ($option['type'] == 'select' || $option['type'] == 'radio' || $option['type'] == 'checkbox') {
534
                    $type = $this->language->get('text_choose');
535
                }
536
537
                $json[] = array(
538
                    'option_id'    => $option['option_id'],
539
                    'name'         => strip_tags(html_entity_decode($option['name'], ENT_QUOTES, 'UTF-8')),
540
                    'category'     => $type,
541
                    'type'         => $option['type'],
542
                    'option_value' => $option_value_data
543
                );
544
            }
545
        }
546
547
        $sort_order = array();
548
549
        foreach ($json as $key => $value) {
550
            $sort_order[$key] = $value['name'];
551
        }
552
553
        array_multisort($sort_order, SORT_ASC, $json);
554
555
        $this->response->addHeader('Content-Type: application/json');
556
        $this->response->setOutput(json_encode($json));
557
    }
558
}
559