ControllerCatalogCategory   F
last analyzed

Complexity

Total Complexity 135

Size/Duplication

Total Lines 769
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 466
c 0
b 0
f 0
dl 0
loc 769
rs 2
wmc 135

15 Methods

Rating   Name   Duplication   Size   Complexity  
A disable() 0 23 6
F validateForm() 0 51 21
A getAllCategories() 0 16 4
A repair() 0 31 5
F getForm() 0 298 51
A enable() 0 23 6
B delete() 0 33 7
A index() 0 9 1
A add() 0 35 6
A edit() 0 35 6
A validateDelete() 0 7 2
B getCategories() 0 47 7
A validateRepair() 0 7 2
A autocomplete() 0 35 4
B getList() 0 85 7

How to fix   Complexity   

Complex Class

Complex classes like ControllerCatalogCategory often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use ControllerCatalogCategory, and based on these observations, apply Extract Interface, too.

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 ControllerCatalogCategory 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
    private $category_id = 0;
27
    private $path = array();
28
29
    public function index()
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines before function; 1 found
Loading history...
30
    {
31
        $this->load->language('catalog/category');
32
33
        $this->document->setTitle($this->language->get('heading_title'));
34
35
        $this->load->model('catalog/category');
36
37
        $this->getList();
38
    }
39
40
    public function add()
41
    {
42
        $this->document->addStyle('/public_html/assets/css/administration/general/suneditor.css');
43
        $this->document->addStyle('/public_html/assets/css/administration/page/application/product.css');
44
        $this->document->addScript('/public_html/assets/js/administration/general/suneditor.js');
45
46
        $this->load->language('catalog/category');
47
48
        $this->document->setTitle($this->language->get('heading_title'));
49
50
        $this->load->model('catalog/category');
51
52
        if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) {
53
            $this->model_catalog_category->addCategory($this->request->post);
54
55
            $this->session->data['success'] = $this->language->get('text_success');
56
57
            $url = '';
58
59
            if (isset($this->request->get['sort'])) {
60
                $url .= '&sort=' . $this->request->get['sort'];
61
            }
62
63
            if (isset($this->request->get['order'])) {
64
                $url .= '&order=' . $this->request->get['order'];
65
            }
66
67
            if (isset($this->request->get['page'])) {
68
                $url .= '&page=' . $this->request->get['page'];
69
            }
70
71
            $this->response->redirect($this->url->link('catalog/category', 'token=' . $this->session->data['token'] . $url, true));
72
        }
73
74
        $this->getForm();
75
    }
76
77
    public function edit()
78
    {
79
        $this->document->addStyle('/public_html/assets/css/administration/general/suneditor.css');
80
        $this->document->addStyle('/public_html/assets/css/administration/page/application/product.css');
81
        $this->document->addScript('/public_html/assets/js/administration/general/suneditor.js');
82
83
        $this->load->language('catalog/category');
84
85
        $this->document->setTitle($this->language->get('heading_title'));
86
87
        $this->load->model('catalog/category');
88
89
        if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) {
90
            $this->model_catalog_category->editCategory($this->request->get['category_id'], $this->request->post);
91
92
            $this->session->data['success'] = $this->language->get('text_success');
93
94
            $url = '';
95
96
            if (isset($this->request->get['sort'])) {
97
                $url .= '&sort=' . $this->request->get['sort'];
98
            }
99
100
            if (isset($this->request->get['order'])) {
101
                $url .= '&order=' . $this->request->get['order'];
102
            }
103
104
            if (isset($this->request->get['page'])) {
105
                $url .= '&page=' . $this->request->get['page'];
106
            }
107
108
            $this->response->redirect($this->url->link('catalog/category', 'token=' . $this->session->data['token'] . $url, true));
109
        }
110
111
        $this->getForm();
112
    }
113
114
    public function delete()
115
    {
116
        $this->load->language('catalog/category');
117
118
        $this->document->setTitle($this->language->get('heading_title'));
119
120
        $this->load->model('catalog/category');
121
122
        if (isset($this->request->post['selected']) && $this->validateDelete()) {
123
            foreach ($this->request->post['selected'] as $category_id) {
124
                $this->model_catalog_category->deleteCategory($category_id);
125
            }
126
127
            $this->session->data['success'] = $this->language->get('text_success');
128
129
            $url = '';
130
131
            if (isset($this->request->get['sort'])) {
132
                $url .= '&sort=' . $this->request->get['sort'];
133
            }
134
135
            if (isset($this->request->get['order'])) {
136
                $url .= '&order=' . $this->request->get['order'];
137
            }
138
139
            if (isset($this->request->get['page'])) {
140
                $url .= '&page=' . $this->request->get['page'];
141
            }
142
143
            $this->response->redirect($this->url->link('catalog/category', 'token=' . $this->session->data['token'] . $url, true));
144
        }
145
146
        $this->getList();
147
    }
148
149
    public function repair()
150
    {
151
        $this->load->language('catalog/category');
152
153
        $this->document->setTitle($this->language->get('heading_title'));
154
155
        $this->load->model('catalog/category');
156
157
        if ($this->validateRepair()) {
158
            $this->model_catalog_category->repairCategories();
159
160
            $this->session->data['success'] = $this->language->get('text_success');
161
162
            $url = '';
163
164
            if (isset($this->request->get['sort'])) {
165
                $url .= '&sort=' . $this->request->get['sort'];
166
            }
167
168
            if (isset($this->request->get['order'])) {
169
                $url .= '&order=' . $this->request->get['order'];
170
            }
171
172
            if (isset($this->request->get['page'])) {
173
                $url .= '&page=' . $this->request->get['page'];
174
            }
175
176
            $this->response->redirect($this->url->link('catalog/category', 'token=' . $this->session->data['token'] . $url, true));
177
        }
178
179
        $this->getList();
180
    }
181
182
    protected function getList()
183
    {
184
        $url = '';
185
186
        $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...
187
188
        $data['breadcrumbs'][] = array(
189
            'text' => $this->language->get('text_home'),
190
            'href' => $this->url->link('common/dashboard', 'token=' . $this->session->data['token'], true)
191
        );
192
193
        $data['breadcrumbs'][] = array(
194
            'text' => $this->language->get('heading_title'),
195
            'href' => $this->url->link('catalog/category', 'token=' . $this->session->data['token'] . $url, true)
196
        );
197
198
        $data['add'] = $this->url->link('catalog/category/add', 'token=' . $this->session->data['token'] . $url, true);
199
        $data['delete'] = $this->url->link('catalog/category/delete', 'token=' . $this->session->data['token'] . $url, true);
200
        $data['repair'] = $this->url->link('catalog/category/repair', 'token=' . $this->session->data['token'] . $url, true);
201
        $data['enabled'] = $this->url->link('catalog/category/enable', 'token=' . $this->session->data['token'] . $url, true);
202
        $data['disabled'] = $this->url->link('catalog/category/disable', 'token=' . $this->session->data['token'] . $url, true);
203
204
        if (isset($this->request->get['path'])) {
205
            if ($this->request->get['path'] != '') {
206
                $this->path = explode('_', $this->request->get['path']);
207
                $this->category_id = end($this->path);
208
                $this->session->data['path'] = $this->request->get['path'];
209
            } else {
210
                unset($this->session->data['path']);
211
            }
212
        } elseif (isset($this->session->data['path'])) {
213
            $this->path = explode('_', $this->session->data['path']);
214
            $this->category_id = end($this->path);
215
        }
216
217
        $data['categories'] = $this->getCategories(0);
218
219
        $data['heading_title'] = $this->language->get('heading_title');
220
221
        $data['text_list'] = $this->language->get('text_list');
222
        $data['text_no_results'] = $this->language->get('text_no_results');
223
        $data['text_confirm'] = $this->language->get('text_confirm');
224
225
        $data['column_name'] = $this->language->get('column_name');
226
        $data['column_sort_order'] = $this->language->get('column_sort_order');
227
        $data['column_noindex'] = $this->language->get('column_noindex');
228
        $data['column_action'] = $this->language->get('column_action');
229
230
        $data['button_add'] = $this->language->get('button_add');
231
        $data['button_edit'] = $this->language->get('button_edit');
232
        $data['button_shop'] = $this->language->get('button_shop');
233
        $data['button_delete'] = $this->language->get('button_delete');
234
        $data['button_rebuild'] = $this->language->get('button_rebuild');
235
        $data['button_enable'] = $this->language->get('button_enable');
236
        $data['button_disable'] = $this->language->get('button_disable');
237
238
        if (isset($this->error['warning'])) {
239
            $data['error_warning'] = $this->error['warning'];
240
        } else {
241
            $data['error_warning'] = '';
242
        }
243
244
        if (isset($this->session->data['success'])) {
245
            $data['success'] = $this->session->data['success'];
246
247
            unset($this->session->data['success']);
248
        } else {
249
            $data['success'] = '';
250
        }
251
252
        if (isset($this->request->post['selected'])) {
253
            $data['selected'] = (array)$this->request->post['selected'];
254
        } else {
255
            $data['selected'] = array();
256
        }
257
258
        $url = '';
0 ignored issues
show
Unused Code introduced by
The assignment to $url is dead and can be removed.
Loading history...
259
        $category_total = $this->model_catalog_category->getTotalCategories();
260
261
        $data['results'] = $this->language->get('text_category_total') . ($category_total);
262
        $data['header'] = $this->load->controller('common/header');
263
        $data['column'] = $this->load->controller('common/column_left');
264
        $data['footer'] = $this->load->controller('common/footer');
265
266
        $this->response->setOutput($this->load->view('catalog/category_list', $data));
267
    }
268
269
    protected function getForm()
270
    {
271
        $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...
272
273
        $data['text_form'] = !isset($this->request->get['category_id']) ? $this->language->get('text_add') : $this->language->get('text_edit');
274
        $data['text_none'] = $this->language->get('text_none');
275
        $data['text_default'] = $this->language->get('text_default');
276
        $data['text_enabled'] = $this->language->get('text_enabled');
277
        $data['text_disabled'] = $this->language->get('text_disabled');
278
279
        $data['entry_name'] = $this->language->get('entry_name');
280
        $data['entry_description'] = $this->language->get('entry_description');
281
        $data['entry_description_bottom'] = $this->language->get('entry_description_bottom');
282
        $data['entry_meta_title'] = $this->language->get('entry_meta_title');
283
        $data['entry_meta_h1'] = $this->language->get('entry_meta_h1');
284
        $data['entry_meta_description'] = $this->language->get('entry_meta_description');
285
        $data['entry_keyword'] = $this->language->get('entry_keyword');
286
        $data['entry_parent'] = $this->language->get('entry_parent');
287
        $data['entry_filter'] = $this->language->get('entry_filter');
288
        $data['entry_image'] = $this->language->get('entry_image');
289
        $data['entry_top'] = $this->language->get('entry_top');
290
        $data['entry_column'] = $this->language->get('entry_column');
291
        $data['entry_sort_order'] = $this->language->get('entry_sort_order');
292
        $data['entry_status'] = $this->language->get('entry_status');
293
        $data['entry_noindex'] = $this->language->get('entry_noindex');
294
        $data['entry_layout'] = $this->language->get('entry_layout');
295
        $data['entry_related_wb'] = $this->language->get('entry_related_wb');
296
        $data['entry_related_article'] = $this->language->get('entry_related_article');
297
298
        $data['help_filter'] = $this->language->get('help_filter');
299
        $data['help_keyword'] = $this->language->get('help_keyword');
300
        $data['help_top'] = $this->language->get('help_top');
301
        $data['help_column'] = $this->language->get('help_column');
302
        $data['help_noindex'] = $this->language->get('help_noindex');
303
304
        $data['button_save'] = $this->language->get('button_save');
305
        $data['button_cancel'] = $this->language->get('button_cancel');
306
307
        $data['tab_general'] = $this->language->get('tab_general');
308
        $data['tab_data'] = $this->language->get('tab_data');
309
        $data['tab_related'] = $this->language->get('tab_related');
310
        $data['tab_design'] = $this->language->get('tab_design');
311
312
        if (isset($this->error['warning'])) {
313
            $data['error_warning'] = $this->error['warning'];
314
        } else {
315
            $data['error_warning'] = '';
316
        }
317
318
        if (isset($this->error['name'])) {
319
            $data['error_name'] = $this->error['name'];
320
        } else {
321
            $data['error_name'] = array();
322
        }
323
324
        if (isset($this->error['meta_title'])) {
325
            $data['error_meta_title'] = $this->error['meta_title'];
326
        } else {
327
            $data['error_meta_title'] = array();
328
        }
329
330
        if (isset($this->error['meta_h1'])) {
331
            $data['error_meta_h1'] = $this->error['meta_h1'];
332
        } else {
333
            $data['error_meta_h1'] = array();
334
        }
335
336
        if (isset($this->error['keyword'])) {
337
            $data['error_keyword'] = $this->error['keyword'];
338
        } else {
339
            $data['error_keyword'] = '';
340
        }
341
342
        if (isset($this->error['parent'])) {
343
            $data['error_parent'] = $this->error['parent'];
344
        } else {
345
            $data['error_parent'] = '';
346
        }
347
348
        $url = '';
349
350
        if (isset($this->request->get['sort'])) {
351
            $url .= '&sort=' . $this->request->get['sort'];
352
        }
353
354
        if (isset($this->request->get['order'])) {
355
            $url .= '&order=' . $this->request->get['order'];
356
        }
357
358
        if (isset($this->request->get['page'])) {
359
            $url .= '&page=' . $this->request->get['page'];
360
        }
361
362
        $data['breadcrumbs'] = array();
363
364
        $data['breadcrumbs'][] = array(
365
            'text' => $this->language->get('text_home'),
366
            'href' => $this->url->link('common/dashboard', 'token=' . $this->session->data['token'], true)
367
        );
368
369
        $data['breadcrumbs'][] = array(
370
            'text' => $this->language->get('heading_title'),
371
            'href' => $this->url->link('catalog/category', 'token=' . $this->session->data['token'] . $url, true)
372
        );
373
374
        if (!isset($this->request->get['category_id'])) {
375
            $data['action'] = $this->url->link('catalog/category/add', 'token=' . $this->session->data['token'] . $url, true);
376
        } else {
377
            $data['action'] = $this->url->link('catalog/category/edit', 'token=' . $this->session->data['token'] . '&category_id=' . $this->request->get['category_id'] . $url, true);
378
        }
379
380
        $data['cancel'] = $this->url->link('catalog/category', 'token=' . $this->session->data['token'] . $url, true);
381
382
        if (isset($this->request->get['category_id']) && ($this->request->server['REQUEST_METHOD'] != 'POST')) {
383
            $category_info = $this->model_catalog_category->getCategory($this->request->get['category_id']);
384
        }
385
386
        $data['token'] = $this->session->data['token'];
387
388
        $this->load->model('localisation/language');
389
390
        $data['languages'] = $this->model_localisation_language->getLanguages();
391
392
        if (isset($this->request->post['category_description'])) {
393
            $data['category_description'] = $this->request->post['category_description'];
394
        } elseif (isset($this->request->get['category_id'])) {
395
            $data['category_description'] = $this->model_catalog_category->getCategoryDescriptions($this->request->get['category_id']);
396
        } else {
397
            $data['category_description'] = array();
398
        }
399
400
        $language_id = $this->config->get('config_language_id');
401
        if (isset($data['category_description'][$language_id]['name'])) {
402
            $data['heading_title'] = $data['category_description'][$language_id]['name'];
403
        }
404
405
        if (isset($this->request->post['path'])) {
406
            $data['path'] = $this->request->post['path'];
407
        } elseif (!empty($category_info)) {
408
            $data['path'] = $category_info['path'];
409
        } else {
410
            $data['path'] = '';
411
        }
412
413
        if (isset($this->request->post['parent_id'])) {
414
            $data['parent_id'] = $this->request->post['parent_id'];
415
        } elseif (!empty($category_info)) {
416
            $data['parent_id'] = $category_info['parent_id'];
417
        } else {
418
            $data['parent_id'] = 0;
419
        }
420
421
        $this->load->model('catalog/filter');
422
423
        if (isset($this->request->post['category_filter'])) {
424
            $filters = $this->request->post['category_filter'];
425
        } elseif (isset($this->request->get['category_id'])) {
426
            $filters = $this->model_catalog_category->getCategoryFilters($this->request->get['category_id']);
427
        } else {
428
            $filters = array();
429
        }
430
431
        $data['category_filters'] = array();
432
433
        foreach ($filters as $filter_id) {
434
            $filter_info = $this->model_catalog_filter->getFilter($filter_id);
435
436
            if ($filter_info) {
437
                $data['category_filters'][] = array(
438
                    'filter_id' => $filter_info['filter_id'],
439
                    'name'      => $filter_info['group'] . ' &gt; ' . $filter_info['name']
440
                );
441
            }
442
        }
443
444
        if (isset($this->request->post['keyword'])) {
445
            $data['keyword'] = $this->request->post['keyword'];
446
        } elseif (!empty($category_info)) {
447
            $data['keyword'] = $category_info['keyword'];
448
        } else {
449
            $data['keyword'] = '';
450
        }
451
452
        if (isset($this->request->post['image'])) {
453
            $data['image'] = $this->request->post['image'];
454
        } elseif (!empty($category_info)) {
455
            $data['image'] = $category_info['image'];
456
        } else {
457
            $data['image'] = '';
458
        }
459
460
        
461
462
        if (isset($this->request->post['image']) && is_file($_SERVER['DOCUMENT_ROOT'] . '/public_html/assets/images/' . $this->request->post['image'])) {
463
            $data['thumb'] = '/public_html/assets/images/' . $this->request->post['image'];
464
        } elseif (!empty($category_info) && is_file($_SERVER['DOCUMENT_ROOT'] . '/public_html/assets/images/' . $category_info['image'])) {
465
            $data['thumb'] = '/public_html/assets/images/' . $category_info['image'];
466
        } else {
467
            $data['thumb'] = '/public_html/assets/images/no_image.png';
468
        }
469
470
        $data['placeholder'] = '/public_html/assets/images/no_image.png';
471
472
        if (isset($this->request->post['top'])) {
473
            $data['top'] = $this->request->post['top'];
474
        } elseif (!empty($category_info)) {
475
            $data['top'] = $category_info['top'];
476
        } else {
477
            $data['top'] = 0;
478
        }
479
480
        if (isset($this->request->post['sort_order'])) {
481
            $data['sort_order'] = $this->request->post['sort_order'];
482
        } elseif (!empty($category_info)) {
483
            $data['sort_order'] = $category_info['sort_order'];
484
        } else {
485
            $data['sort_order'] = 0;
486
        }
487
488
        if (isset($this->request->post['product_related'])) {
489
            $products = $this->request->post['product_related'];
490
        } elseif (isset($category_info)) {
491
            $products = $this->model_catalog_category->getProductRelated($this->request->get['category_id']);
492
        } else {
493
            $products = array();
494
        }
495
496
        $data['product_related'] = array();
497
498
        $this->load->model('catalog/product');
499
500
        foreach ($products as $product_id) {
501
            $related_info = $this->model_catalog_product->getProduct($product_id);
502
503
            if ($related_info) {
504
                $data['product_related'][] = array(
505
                    'product_id' => $related_info['product_id'],
506
                    'name'       => $related_info['name']
507
                );
508
            }
509
        }
510
511
        if (isset($this->request->post['article_related'])) {
512
            $articles = $this->request->post['article_related'];
513
        } elseif (isset($category_info)) {
514
            $articles = $this->model_catalog_category->getArticleRelated($this->request->get['category_id']);
515
        } else {
516
            $articles = array();
517
        }
518
519
        $data['article_related'] = array();
520
521
        $this->load->model('blog/article');
522
523
        foreach ($articles as $article_id) {
524
            $related_info = $this->model_blog_article->getArticle($article_id);
525
526
            if ($related_info) {
527
                $data['article_related'][] = array(
528
                    'article_id' => $related_info['article_id'],
529
                    'name'       => $related_info['name']
530
                );
531
            }
532
        }
533
534
        if (isset($this->request->post['status'])) {
535
            $data['status'] = $this->request->post['status'];
536
        } elseif (!empty($category_info)) {
537
            $data['status'] = $category_info['status'];
538
        } else {
539
            $data['status'] = true;
540
        }
541
542
        if (isset($this->request->post['noindex'])) {
543
            $data['noindex'] = $this->request->post['noindex'];
544
        } elseif (!empty($category_info)) {
545
            $data['noindex'] = $category_info['noindex'];
546
        } else {
547
            $data['noindex'] = 1;
548
        }
549
550
        if (isset($this->request->post['category_layout'])) {
551
            $data['category_layout'] = $this->request->post['category_layout'];
552
        } elseif (isset($this->request->get['category_id'])) {
553
            $data['category_layout'] = $this->model_catalog_category->getCategoryLayouts($this->request->get['category_id']);
554
        } else {
555
            $data['category_layout'] = array();
556
        }
557
558
        $this->load->model('design/layout');
559
560
        $data['layouts'] = $this->model_design_layout->getLayouts();
561
562
        $data['header'] = $this->load->controller('common/header');
563
        $data['column'] = $this->load->controller('common/column_left');
564
        $data['footer'] = $this->load->controller('common/footer');
565
566
        $this->response->setOutput($this->load->view('catalog/category_form', $data));
567
    }
568
569
    protected function validateForm()
570
    {
571
        if (!$this->user->hasPermission('modify', 'catalog/category')) {
572
            $this->error['warning'] = $this->language->get('error_permission');
573
        }
574
575
        foreach ($this->request->post['category_description'] as $language_id => $value) {
576
            if ((\voku\helper\UTF8::strlen($value['name']) < 2) || (\voku\helper\UTF8::strlen($value['name']) > 255)) {
577
                $this->error['name'][$language_id] = $this->language->get('error_name');
578
            }
579
580
            if ((\voku\helper\UTF8::strlen($value['meta_title']) < 0) || (\voku\helper\UTF8::strlen($value['meta_title']) > 255)) {
581
                $this->error['meta_title'][$language_id] = $this->language->get('error_meta_title');
582
            }
583
584
            if ((\voku\helper\UTF8::strlen($value['meta_h1']) < 0) || (\voku\helper\UTF8::strlen($value['meta_h1']) > 255)) {
585
                $this->error['meta_h1'][$language_id] = $this->language->get('error_meta_h1');
586
            }
587
        }
588
589
        if (isset($this->request->get['category_id']) && $this->request->post['parent_id']) {
590
            $results = $this->model_catalog_category->getCategoryPath($this->request->post['parent_id']);
591
592
            foreach ($results as $result) {
593
                if ($result['path_id'] == $this->request->get['category_id']) {
594
                    $this->error['parent'] = $this->language->get('error_parent');
595
596
                    break;
597
                }
598
            }
599
        }
600
601
        if (\voku\helper\UTF8::strlen($this->request->post['keyword']) > 0) {
602
            $this->load->model('catalog/url_alias');
603
604
            $url_alias_info = $this->model_catalog_url_alias->getUrlAlias($this->request->post['keyword']);
605
606
            if ($url_alias_info && isset($this->request->get['category_id']) && $url_alias_info['query'] != 'category_id=' . $this->request->get['category_id']) {
607
                $this->error['keyword'] = sprintf($this->language->get('error_keyword'));
608
            }
609
610
            if ($url_alias_info && !isset($this->request->get['category_id'])) {
611
                $this->error['keyword'] = sprintf($this->language->get('error_keyword'));
612
            }
613
        }
614
615
        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...
616
            $this->error['warning'] = $this->language->get('error_warning');
617
        }
618
619
        return !$this->error;
620
    }
621
622
    public function enable()
623
    {
624
        $this->load->language('catalog/category');
625
        $this->document->setTitle($this->language->get('heading_title'));
626
        $this->load->model('catalog/category');
627
        if (isset($this->request->post['selected'])) {
628
            foreach ($this->request->post['selected'] as $category_id) {
629
                $this->model_catalog_category->editCategoryStatus($category_id, 1);
630
            }
631
            $this->session->data['success'] = $this->language->get('text_success');
632
            $url = '';
633
            if (isset($this->request->get['page'])) {
634
                $url .= '&page=' . $this->request->get['page'];
635
            }
636
            if (isset($this->request->get['sort'])) {
637
                $url .= '&sort=' . $this->request->get['sort'];
638
            }
639
            if (isset($this->request->get['order'])) {
640
                $url .= '&order=' . $this->request->get['order'];
641
            }
642
            $this->response->redirect($this->url->link('catalog/category', 'token=' . $this->session->data['token'] . $url, true));
643
        }
644
        $this->getList();
645
    }
646
647
    public function disable()
648
    {
649
        $this->load->language('catalog/category');
650
        $this->document->setTitle($this->language->get('heading_title'));
651
        $this->load->model('catalog/category');
652
        if (isset($this->request->post['selected'])) {
653
            foreach ($this->request->post['selected'] as $category_id) {
654
                $this->model_catalog_category->editCategoryStatus($category_id, 0);
655
            }
656
            $this->session->data['success'] = $this->language->get('text_success');
657
            $url = '';
658
            if (isset($this->request->get['page'])) {
659
                $url .= '&page=' . $this->request->get['page'];
660
            }
661
            if (isset($this->request->get['sort'])) {
662
                $url .= '&sort=' . $this->request->get['sort'];
663
            }
664
            if (isset($this->request->get['order'])) {
665
                $url .= '&order=' . $this->request->get['order'];
666
            }
667
            $this->response->redirect($this->url->link('catalog/category', 'token=' . $this->session->data['token'] . $url, true));
668
        }
669
        $this->getList();
670
    }
671
672
673
    protected function validateDelete()
674
    {
675
        if (!$this->user->hasPermission('modify', 'catalog/category')) {
676
            $this->error['warning'] = $this->language->get('error_permission');
677
        }
678
679
        return !$this->error;
680
    }
681
682
    protected function validateRepair()
683
    {
684
        if (!$this->user->hasPermission('modify', 'catalog/category')) {
685
            $this->error['warning'] = $this->language->get('error_permission');
686
        }
687
688
        return !$this->error;
689
    }
690
691
    public function autocomplete()
692
    {
693
        $json = array();
694
695
        if (isset($this->request->get['filter_name'])) {
696
            $this->load->model('catalog/category');
697
698
            $filter_data = array(
699
                'filter_name' => $this->request->get['filter_name'],
700
                'sort'        => 'name',
701
                'order'       => 'ASC',
702
                'start'       => 0,
703
                'limit'       => 5
704
            );
705
706
            $results = $this->model_catalog_category->getCategories($filter_data);
707
708
            foreach ($results as $result) {
709
                $json[] = array(
710
                    'category_id' => $result['category_id'],
711
                    'name'        => strip_tags(html_entity_decode($result['name'], ENT_QUOTES, 'UTF-8'))
712
                );
713
            }
714
        }
715
716
        $sort_order = array();
717
718
        foreach ($json as $key => $value) {
719
            $sort_order[$key] = $value['name'];
720
        }
721
722
        array_multisort($sort_order, SORT_ASC, $json);
723
724
        $this->response->addHeader('Content-Type: application/json');
725
        $this->response->setOutput(json_encode($json));
726
    }
727
728
    private function getCategories($parent_id, $parent_path = '', $indent = '')
729
    {
730
        $category_id = array_shift($this->path);
731
        $output = array();
732
        static $href_category = null;
733
        static $href_action = null;
734
        if ($href_category === null) {
735
            $href_category = $this->url->link('catalog/category', 'token=' . $this->session->data['token'] . '&path=', true);
736
            $href_action = $this->url->link('catalog/category/update', 'token=' . $this->session->data['token'] . '&category_id=', true);
737
        }
738
        $results = $this->model_catalog_category->getCategoriesByParentId($parent_id);
739
        foreach ($results as $result) {
740
            $path = $parent_path . $result['category_id'];
741
            $href = ($result['children']) ? $href_category . $path : '';
742
            $name = $result['name'];
743
            if ($category_id == $result['category_id']) {
744
                $name = '<b>' . $name . '</b>';
745
                $data['breadcrumbs'][] = array(
746
                    'text'      => $result['name'],
747
                    'href'      => $href,
748
                    'separator' => ' :: '
749
                );
750
                $href = '';
751
            }
752
            $selected = isset($this->request->post['selected']) && in_array($result['category_id'], $this->request->post['selected']);
753
            $action = array();
754
            $action[] = array(
755
                'text' => $this->language->get('text_edit'),
756
                'href' => $href_action . $result['category_id']
757
            );
758
            $output[$result['category_id']] = array(
759
                'category_id' => $result['category_id'],
760
                'name'        => $name,
761
                'sort_order'  => $result['sort_order'],
762
                'noindex'        => $result['noindex'],
763
                'edit'        => $this->url->link('catalog/category/edit', 'token=' . $this->session->data['token'] . '&category_id=' . $result['category_id'], true),
764
                'selected'    => $selected,
765
                'action'      => $action,
766
                'href'        => $href,
767
                'href_shop'   => '/index.php?route=product/category&path=' . ($result['category_id']),
768
                'indent'      => $indent
769
            );
770
            if ($category_id == $result['category_id']) {
771
                $output += $this->getCategories($result['category_id'], $path . '_', $indent . str_repeat('&nbsp;', 8));
772
            }
773
        }
774
        return $output;
775
    }
776
    private function getAllCategories($categories, $parent_id = 0, $parent_name = '')
777
    {
778
        $output = array();
779
        if (array_key_exists($parent_id, $categories)) {
780
            if ($parent_name != '') {
781
                $parent_name .= $this->language->get('text_separator');
782
            }
783
            foreach ($categories[$parent_id] as $category) {
784
                $output[$category['category_id']] = array(
785
                    'category_id' => $category['category_id'],
786
                    'name'        => $parent_name . $category['name']
787
                );
788
                $output += $this->getAllCategories($categories, $category['category_id'], $parent_name . $category['name']);
789
            }
790
        }
791
        return $output;
792
    }
793
}
794