ControllerProductManufacturer::info()   F
last analyzed

Complexity

Conditions 41
Paths > 20000

Size

Total Lines 352
Code Lines 220

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 41
eloc 220
c 0
b 0
f 0
nc 311428608
nop 0
dl 0
loc 352
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 ControllerProductManufacturer 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
    public function index()
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines before function; 0 found
Loading history...
26
    {
27
        $this->load->language('product/manufacturer');
28
29
        $this->load->model('catalog/manufacturer');
30
31
32
33
        $this->document->setTitle($this->language->get('heading_title'));
34
35
        $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...
36
37
        $data['text_index'] = $this->language->get('text_index');
38
        $data['text_empty'] = $this->language->get('text_empty');
39
40
        $data['button_continue'] = $this->language->get('button_continue');
41
42
        $data['breadcrumbs'] = array();
43
44
        $data['breadcrumbs'][] = array(
45
            'text' => $this->language->get('text_home'),
46
            'href' => $this->url->link('common/home')
47
        );
48
49
        $data['breadcrumbs'][] = array(
50
            'text' => $this->language->get('text_brand'),
51
            'href' => $this->url->link('product/manufacturer')
52
        );
53
54
        $data['categories'] = array();
55
56
        $results = $this->model_catalog_manufacturer->getManufacturers();
57
58
        foreach ($results as $result) {
59
            if (is_numeric(\voku\helper\UTF8::substr($result['name'], 0, 1))) {
60
                $key = '0 - 9';
61
            } else {
62
                $key = \voku\helper\UTF8::substr(\voku\helper\UTF8::strtoupper($result['name']), 0, 1);
63
            }
64
65
            if (!isset($data['categories'][$key])) {
66
                $data['categories'][$key]['name'] = $key;
67
            }
68
69
            $data['categories'][$key]['manufacturer'][] = array(
70
                'name' => $result['name'],
71
                'href' => $this->url->link('product/manufacturer/info', 'manufacturer_id=' . $result['manufacturer_id'])
72
            );
73
        }
74
75
        $data['continue'] = $this->url->link('common/home');
76
77
        $data['column'] = $this->load->controller('common/column');
78
79
        $data['content_top'] = $this->load->controller('common/content_top');
80
        $data['content_bottom'] = $this->load->controller('common/content_bottom');
81
        $data['footer'] = $this->load->controller('common/footer');
82
        $data['header'] = $this->load->controller('common/header');
83
84
        $this->response->setOutput($this->load->view('product/manufacturer_list', $data));
85
    }
86
87
    public function info()
88
    {
89
        $this->load->language('product/manufacturer');
90
91
        $this->load->model('catalog/manufacturer');
92
93
        $this->load->model('catalog/product');
94
95
96
97
        if (isset($this->request->get['manufacturer_id'])) {
98
            $manufacturer_id = (int) $this->request->get['manufacturer_id'];
99
        } else {
100
            $manufacturer_id = 0;
101
        }
102
103
        if (isset($this->request->get['sort'])) {
104
            $sort = $this->request->get['sort'];
105
            $this->document->setRobots('noindex,follow');
106
        } else {
107
            $sort = 'p.sort_order';
108
        }
109
110
        if (isset($this->request->get['order'])) {
111
            $order = $this->request->get['order'];
112
        } else {
113
            $order = 'ASC';
114
        }
115
116
        if (isset($this->request->get['page'])) {
117
            $page = $this->request->get['page'];
118
            $this->document->setRobots('noindex,follow');
119
        } else {
120
            $page = 1;
121
        }
122
123
        if (isset($this->request->get['limit'])) {
124
            $limit = ((int) $this->request->get['limit'] > 100 ? 100 : (int) $this->request->get['limit']);
0 ignored issues
show
Unused Code introduced by
The assignment to $limit is dead and can be removed.
Loading history...
125
            $this->document->setRobots('noindex,follow');
126
        } else {
127
            $limit = (int) $this->config->get('config_limit_store');
128
        }
129
130
        $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...
131
132
        $data['breadcrumbs'][] = array(
133
            'text' => $this->language->get('text_home'),
134
            'href' => $this->url->link('common/home')
135
        );
136
137
        $data['breadcrumbs'][] = array(
138
            'text' => $this->language->get('text_brand'),
139
            'href' => $this->url->link('product/manufacturer')
140
        );
141
142
        $manufacturer_info = $this->model_catalog_manufacturer->getManufacturer($manufacturer_id);
143
144
        if ($manufacturer_info) {
145
            if ($manufacturer_info['meta_title']) {
146
                $this->document->setTitle($manufacturer_info['meta_title']);
147
            } else {
148
                $this->document->setTitle($manufacturer_info['name']);
149
            }
150
151
            if ($manufacturer_info['noindex'] <= 0) {
152
                $this->document->setRobots('noindex,follow');
153
            }
154
155
            if ($manufacturer_info['meta_h1']) {
156
                $data['heading_title'] = $manufacturer_info['meta_h1'];
157
            } else {
158
                $data['heading_title'] = $manufacturer_info['name'];
159
            }
160
161
            $this->document->setDescription($manufacturer_info['meta_description']);
162
163
            $data['description'] = html_entity_decode($manufacturer_info['description'], ENT_QUOTES, 'UTF-8');
164
            $data['description_bottom'] = html_entity_decode($manufacturer_info['description_bottom'], ENT_QUOTES, 'UTF-8');
165
166
            if ($manufacturer_info['image']) {
167
                $data['thumb'] = '/public_html/assets/images/' . $manufacturer_info['image'];
168
            } else {
169
                $data['thumb'] = '/public_html/assets/images/no_image.png';
170
            }
171
172
            $url = '';
173
174
            if (isset($this->request->get['sort'])) {
175
                $url .= '&sort=' . $this->request->get['sort'];
176
            }
177
178
            if (isset($this->request->get['order'])) {
179
                $url .= '&order=' . $this->request->get['order'];
180
            }
181
182
            if (isset($this->request->get['page'])) {
183
                $url .= '&page=' . $this->request->get['page'];
184
            }
185
186
            if (isset($this->request->get['limit'])) {
187
                $url .= '&limit=' . $this->request->get['limit'];
188
            }
189
190
            $data['breadcrumbs'][] = array(
191
                'text' => $manufacturer_info['name'],
192
                'href' => $this->url->link('product/manufacturer/info', 'manufacturer_id=' . $this->request->get['manufacturer_id'] . $url)
193
            );
194
195
            $data['text_empty'] = $this->language->get('text_empty');
196
            $data['text_quantity'] = $this->language->get('text_quantity');
197
            $data['text_manufacturer'] = $this->language->get('text_manufacturer');
198
            $data['text_model'] = $this->language->get('text_model');
199
            $data['text_price'] = $this->language->get('text_price');
200
            $data['text_points'] = $this->language->get('text_points');
201
            $data['text_sort'] = $this->language->get('text_sort');
202
            $data['text_limit'] = $this->language->get('text_limit');
203
            $data['text_go_back'] = $this->language->get('text_go_back');
204
205
            $data['button_buy_it'] = $this->language->get('button_buy_it');
206
            $data['button_continue'] = $this->language->get('button_continue');
207
            $data['button_list'] = $this->language->get('button_list');
208
            $data['button_grid'] = $this->language->get('button_grid');
209
210
            $data['products'] = array();
211
212
            $filter_data = array(
213
                'filter_manufacturer_id' => $manufacturer_id,
214
                'sort'                   => $sort,
215
                'order'                  => $order,
216
                'start'                  => ($page - 1) * $limit,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $limit does not seem to be defined for all execution paths leading up to this point.
Loading history...
217
                'limit'                  => $limit
218
            );
219
220
            $product_total = $this->model_catalog_product->getTotalProducts($filter_data);
221
222
            $results = $this->model_catalog_product->getProducts($filter_data);
223
224
            foreach ($results as $result) {
225
                if ($result['image']) {
226
                    $image = '/public_html/assets/images/' . $result['image'];
227
                } else {
228
                    $image = '/public_html/assets/images/no_image.png';
229
                }
230
231
                if ($this->customer->isLogged() || !$this->config->get('config_customer_price')) {
232
                    $price = $this->currency->format($result['price'], $this->session->data['currency']);
233
                } else {
234
                    $price = false;
235
                }
236
237
                if ((float) $result['special']) {
238
                    $special = $this->currency->format($result['special'], $this->session->data['currency']);
239
                } else {
240
                    $special = false;
241
                }
242
243
                if ($result['description_mini']) {
244
                    $description = \voku\helper\UTF8::substr(strip_tags(html_entity_decode($result['description_mini'], ENT_QUOTES, 'UTF-8')), 0);
245
                } else {
246
                    $description = \voku\helper\UTF8::substr(strip_tags(html_entity_decode($result['description'], ENT_QUOTES, 'UTF-8')), 0, $this->config->get($this->config->get('config_theme') . '_product_description_length')) . '..';
0 ignored issues
show
Bug introduced by
Are you sure voku\helper\UTF8::substr...t_description_length')) of type false|string can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

246
                    $description = /** @scrutinizer ignore-type */ \voku\helper\UTF8::substr(strip_tags(html_entity_decode($result['description'], ENT_QUOTES, 'UTF-8')), 0, $this->config->get($this->config->get('config_theme') . '_product_description_length')) . '..';
Loading history...
247
                }
248
249
                $stickers = $this->getStickers($result['product_id']);
250
251
                $data['products'][] = array(
252
                    'product_id'  => $result['product_id'],
253
                    'thumb'       => $image,
254
                    'name'        => $result['name'],
255
                    'description' => $description,
256
                    'price'       => $price,
257
                    'special'     => $special,
258
                    'sticker'     => $stickers,
259
                    'minimum'     => $result['minimum'] > 0 ? $result['minimum'] : 1,
260
                    'href'        => $this->url->link('product/product', 'manufacturer_id=' . $result['manufacturer_id'] . '&product_id=' . $result['product_id'] . $url)
261
                );
262
            }
263
264
            $url = '';
265
266
            if (isset($this->request->get['limit'])) {
267
                $url .= '&limit=' . $this->request->get['limit'];
268
            }
269
270
            $data['sorts'] = array();
271
272
            $data['sorts'][] = array(
273
                'text'  => $this->language->get('text_default'),
274
                'value' => 'p.sort_order-ASC',
275
                'href'  => $this->url->link('product/manufacturer/info', 'manufacturer_id=' . $this->request->get['manufacturer_id'] . '&sort=p.sort_order&order=ASC' . $url)
276
            );
277
278
            $data['sorts'][] = array(
279
                'text'  => $this->language->get('text_name_asc'),
280
                'value' => 'pd.name-ASC',
281
                'href'  => $this->url->link('product/manufacturer/info', 'manufacturer_id=' . $this->request->get['manufacturer_id'] . '&sort=pd.name&order=ASC' . $url)
282
            );
283
284
            $data['sorts'][] = array(
285
                'text'  => $this->language->get('text_name_desc'),
286
                'value' => 'pd.name-DESC',
287
                'href'  => $this->url->link('product/manufacturer/info', 'manufacturer_id=' . $this->request->get['manufacturer_id'] . '&sort=pd.name&order=DESC' . $url)
288
            );
289
290
            $data['sorts'][] = array(
291
                'text'  => $this->language->get('text_price_asc'),
292
                'value' => 'p.price-ASC',
293
                'href'  => $this->url->link('product/manufacturer/info', 'manufacturer_id=' . $this->request->get['manufacturer_id'] . '&sort=p.price&order=ASC' . $url)
294
            );
295
296
            $data['sorts'][] = array(
297
                'text'  => $this->language->get('text_price_desc'),
298
                'value' => 'p.price-DESC',
299
                'href'  => $this->url->link('product/manufacturer/info', 'manufacturer_id=' . $this->request->get['manufacturer_id'] . '&sort=p.price&order=DESC' . $url)
300
            );
301
302
            $data['sorts'][] = array(
303
                'text'  => $this->language->get('text_model_asc'),
304
                'value' => 'p.model-ASC',
305
                'href'  => $this->url->link('product/manufacturer/info', 'manufacturer_id=' . $this->request->get['manufacturer_id'] . '&sort=p.model&order=ASC' . $url)
306
            );
307
308
            $data['sorts'][] = array(
309
                'text'  => $this->language->get('text_model_desc'),
310
                'value' => 'p.model-DESC',
311
                'href'  => $this->url->link('product/manufacturer/info', 'manufacturer_id=' . $this->request->get['manufacturer_id'] . '&sort=p.model&order=DESC' . $url)
312
            );
313
314
            $url = '';
315
316
            if (isset($this->request->get['sort'])) {
317
                $url .= '&sort=' . $this->request->get['sort'];
318
            }
319
320
            if (isset($this->request->get['order'])) {
321
                $url .= '&order=' . $this->request->get['order'];
322
            }
323
324
            $data['limits'] = array();
325
326
            $limits = array_unique(array($this->config->get('config_limit_store'), 25, 50, 75, 100));
327
328
            sort($limits);
329
330
            foreach ($limits as $value) {
331
                $data['limits'][] = array(
332
                    'text'  => $value,
333
                    'value' => $value,
334
                    'href'  => $this->url->link('product/manufacturer/info', 'manufacturer_id=' . $this->request->get['manufacturer_id'] . $url . '&limit=' . $value)
335
                );
336
            }
337
338
            $url = '';
339
340
            if (isset($this->request->get['sort'])) {
341
                $url .= '&sort=' . $this->request->get['sort'];
342
            }
343
344
            if (isset($this->request->get['order'])) {
345
                $url .= '&order=' . $this->request->get['order'];
346
            }
347
348
            if (isset($this->request->get['limit'])) {
349
                $url .= '&limit=' . $this->request->get['limit'];
350
            }
351
352
            $pagination = new \Divine\Engine\Library\Pagination();
353
            $pagination->total = $product_total;
354
            $pagination->page = $page;
355
            $pagination->limit = $limit;
356
            $pagination->url = $this->url->link('product/manufacturer/info', 'manufacturer_id=' . $this->request->get['manufacturer_id'] .  $url . '&page={page}');
357
358
            $data['pagination'] = $pagination->render();
359
360
            $data['results'] = sprintf($this->language->get('text_pagination'), ($product_total) ? (($page - 1) * $limit) + 1 : 0, ((($page - 1) * $limit) > ($product_total - $limit)) ? $product_total : ((($page - 1) * $limit) + $limit), $product_total, ceil($product_total / $limit));
361
362
            // http://googlewebmastercentral.blogspot.com/2011/09/pagination-with-relnext-and-relprev.html
363
            if ($page == 1) {
364
                $this->document->addLink($this->url->link('product/manufacturer/info', 'manufacturer_id=' . $this->request->get['manufacturer_id'], true), 'canonical');
365
            } elseif ($page == 2) {
366
                $this->document->addLink($this->url->link('product/manufacturer/info', 'manufacturer_id=' . $this->request->get['manufacturer_id'], true), 'prev');
367
            } else {
368
                $this->document->addLink($this->url->link('product/manufacturer/info', 'manufacturer_id=' . $this->request->get['manufacturer_id'] . $url . '&page=' . ($page - 1), true), 'prev');
369
            }
370
371
            if ($limit && ceil($product_total / $limit) > $page) {
372
                $this->document->addLink($this->url->link('product/manufacturer/info', 'manufacturer_id=' . $this->request->get['manufacturer_id'] . $url . '&page=' . ($page + 1), true), 'next');
373
            }
374
375
            $data['sort'] = $sort;
376
            $data['order'] = $order;
377
            $data['limit'] = $limit;
378
379
            $data['continue'] = $this->url->link('common/home');
380
381
            $data['column'] = $this->load->controller('common/column');
382
383
            $data['content_top'] = $this->load->controller('common/content_top');
384
            $data['content_bottom'] = $this->load->controller('common/content_bottom');
385
            $data['footer'] = $this->load->controller('common/footer');
386
            $data['header'] = $this->load->controller('common/header');
387
388
            $this->response->setOutput($this->load->view('product/manufacturer_info', $data));
389
        } else {
390
            $url = '';
391
392
            if (isset($this->request->get['manufacturer_id'])) {
393
                $url .= '&manufacturer_id=' . $this->request->get['manufacturer_id'];
394
            }
395
396
            if (isset($this->request->get['sort'])) {
397
                $url .= '&sort=' . $this->request->get['sort'];
398
            }
399
400
            if (isset($this->request->get['order'])) {
401
                $url .= '&order=' . $this->request->get['order'];
402
            }
403
404
            if (isset($this->request->get['page'])) {
405
                $url .= '&page=' . $this->request->get['page'];
406
            }
407
408
            if (isset($this->request->get['limit'])) {
409
                $url .= '&limit=' . $this->request->get['limit'];
410
            }
411
412
            $data['breadcrumbs'][] = array(
413
                'text' => $this->language->get('text_error'),
414
                'href' => $this->url->link('product/manufacturer/info', $url)
415
            );
416
417
            $this->document->setTitle($this->language->get('text_error'));
418
419
            $data['heading_title'] = $this->language->get('text_error');
420
421
            $data['text_error'] = $this->language->get('text_error');
422
            $data['text_go_back'] = $this->language->get('text_go_back');
423
            $data['text_get_back'] = $this->language->get('text_get_back');
424
425
            $data['button_continue'] = $this->language->get('button_continue');
426
427
            $data['continue'] = $this->url->link('common/home');
428
429
            $this->response->addHeader($this->request->server['SERVER_PROTOCOL'] . ' 404 Not Found');
430
431
            $data['header'] = $this->load->controller('common/header');
432
            $data['footer'] = $this->load->controller('common/footer');
433
            $data['column'] = $this->load->controller('common/column');
434
435
            $data['content_top'] = $this->load->controller('common/content_top');
436
            $data['content_bottom'] = $this->load->controller('common/content_bottom');
437
438
            $this->response->setOutput($this->load->view('error/not_found', $data));
439
        }
440
    }
441
442
    private function getStickers($product_id)
443
    {
444
        $stickers = $this->model_catalog_product->getProductStickerbyProductId($product_id);
445
446
        if (!$stickers) {
447
            return;
448
        }
449
450
        $data['stickers'] = 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...
451
452
        foreach ($stickers as $sticker) {
453
            $data['stickers'][] = array(
454
                'position' => $sticker['position'],
455
                'image' => '/public_html/assets/images/' . $sticker['image']
456
            );
457
        }
458
459
        return $this->load->view('product/stickers', $data);
460
    }
461
}
462