Completed
Push — development ( ef9e73...b2c3e4 )
by Andrij
20:27
created

Navigation_Widgets::fetch_tpl()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 3
b 0
f 0
nc 1
nop 2
dl 0
loc 6
rs 9.4285
1
<?php
2
3
use Propel\Runtime\ActiveQuery\Criteria;
4
5
if (!defined('BASEPATH')) {
6
    exit('No direct script access allowed');
7
}
8
9
/**
10
 * Image CMS
11
 *
12
 * Navigation widgets
13
 * @property Lib_category lib_category
14
 */
15
class Navigation_Widgets extends MY_Controller
16
{
0 ignored issues
show
introduced by
Opening brace of a class must be on the same line as the definition
Loading history...
17
18
    public function __construct() {
19
        parent::__construct();
20
        $lang = new MY_Lang();
21
        $lang->load('navigation');
22
    }
23
24
    /**
25
     * @return string
26
     */
27
    private function pathGallery() {
28
        if ($this->core->langs[$this->uri->segment(1)]) {
29
            $data_type = $this->uri->segment(1) !== $this->defaultLocale() ? $this->uri->segment(2) : $this->uri->segment(1);
30
        } else {
31
            $data_type = $this->uri->segment(1);
32
        }
33
        return $data_type;
34
    }
35
36
    /**
37
     * @param array $widget
38
     * @return string
39
     * @throws Exception
0 ignored issues
show
introduced by
Comment missing or not on the next line for @throws tag in function comment
Loading history...
40
     */
41
    public function widget_navigation($widget = []) {
42
        $this->load->module('core');
43
44
        $segmentGallery = $this->pathGallery();
45
        if ($this->core->core_data['data_type'] === '404' || !$this->core->core_data['data_type'] || $segmentGallery === 'gallery') {
46
            $data_type = $segmentGallery;
47
        } else {
48
            $data_type = $this->core->core_data['data_type'];
49
        }
50
51
        switch ($data_type) {
52
            case 'category':
53
                $cur_category = $this->core->cat_content;
54
                $path_categories = $this->lib_category->get_category(array_keys($cur_category['path']));
55
56
                $tpl_data = ['navi_cats' => $path_categories];
57
58
                return $this->template->fetch('widgets/' . $widget['name'], $tpl_data);
59
60
            case 'page':
61
                $cur_category = $this->core->cat_content;
62
                $path_categories = $this->lib_category->get_category(array_keys($cur_category['path']));
63
64
                // Insert Page data
65
                $path_categories[] = [
66
                                      'path_url' => $this->core->page_content['cat_url'] . $this->core->page_content['url'],
67
                                      'name'     => $this->core->page_content['title'],
68
                                     ];
69
70
                $tpl_data = ['navi_cats' => $path_categories];
71
72
                return $this->template->fetch('widgets/' . $widget['name'], $tpl_data);
73
74
            case 'brand':
75
                if ($this->core->core_data['id'] != null) {
76
                    $brand = SBrandsQuery::create()->joinWithI18n(MY_Controller::getCurrentLocale())->findOneById($this->core->core_data['id']);
77
78
                    $navi_cats[] = [
0 ignored issues
show
Coding Style Comprehensibility introduced by
$navi_cats was never initialized. Although not strictly required by PHP, it is generally a good practice to add $navi_cats = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
79
                                    'path_url' => 'shop/brand/',
80
                                    'name'     => lang('Brands', 'navigation'),
81
                                   ];
82
                    $navi_cats[] = [
83
                                    'path_url' => 'shop/brand/' . $brand->getUrl(),
84
                                    'name'     => $brand->getName(),
85
                                   ];
86
87
                    $tpl_data = ['navi_cats' => $navi_cats];
88
                    return $this->template->fetch('widgets/' . $widget['name'], $tpl_data);
89
                } else {
90
91
                    if ($data_type == 'brand') {
92
                        return $this->make(lang('Brands', 'navigation'), 'shop/brand/', $widget);
93
                    }
94
                }
95
                break;
0 ignored issues
show
Coding Style introduced by
Case breaking statements must be followed by a single blank line
Loading history...
96
            case 'compare';
97
                return $this->make(lang('Compare', 'navigation'), 'shop/compare/', $widget);
0 ignored issues
show
Coding Style introduced by
Case breaking statements must be followed by a single blank line
Loading history...
98
            case 'order';
99
                return $this->make(lang('Order details', 'navigation'), 'shop/order/', $widget);
0 ignored issues
show
Coding Style introduced by
Case breaking statements must be followed by a single blank line
Loading history...
100
            case 'wish_list':
101
                return $this->make(lang('Wish list', 'navigation'), 'shop/wish_list/', $widget);
102
103
            case 'profile':
104
                return $this->make(lang('Profile', 'navigation'), 'shop/profile/', $widget);
105
106
            case 'search':
107
                return $this->make(lang('Search', 'navigation'), 'shop/search/', $widget);
108
109
            case 'callbacks':
110
                return $this->make(lang('Callbacks', 'navigation'), 'callbacks', $widget);
111
112
            case 'shop':
113
                return $this->make(lang('Compare', 'navigation'), 'shop/compare', $widget);
114
115
            case 'wishlist':
116
                return $this->make(lang('Wishlist', 'navigation'), 'wishlist', $widget);
117
118
            case 'cart':
119
                return $this->make(lang('Cart', 'navigation'), 'shop/cart/', $widget);
120
121
            case 'feedback':
122
                return $this->make(lang('Feedback', 'navigation'), 'feedback', $widget);
123
124
            case 'action_type':
125
                return $this->make(lang('Action type', 'navigation'), 'shop/action_type/show', $widget);
126
127
            case 'shop_category':
128
                if ($this->core->core_data['id'] !== null && $this->core->core_data > 0) {
129
130
                    $category = SCategoryQuery::create()->findOneById($this->core->core_data['id']);
131
                    $categories = $category->buildCategoryPath(Criteria::ASC, MY_Controller::getCurrentLocale());
132
                    $paths = [];
133
134 View Code Duplication
                    foreach ($categories as $category) {
135
                        $paths[] = [
136
                                    'path_url' => 'shop/category/' . $category->getFullPath(),
137
                                    'name'     => $category->getName(),
138
                                   ];
139
                    }
140
141
                    $tpl_data = ['navi_cats' => $paths];
142
                    return $this->template->fetch('widgets/' . $widget['name'], $tpl_data);
143
                } else {
144
                    throw new Exception('Category not found');
145
                }
146
147
                break;
0 ignored issues
show
Unused Code introduced by
break; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
Coding Style introduced by
Case breaking statements must be followed by a single blank line
Loading history...
148
            case 'product':
149
                if ($this->core->core_data['id'] != null && $this->core->core_data['id'] > 0) {
150
                    //get product model
151
                    $product = SProductsQuery::create()
152
                            ->joinWithI18n(MY_Controller::getCurrentLocale())
153
                            ->findOneById($this->core->core_data['id']);
154
155
                    if ($product) {
156
157
                        if ($product->getCategoryId() == null && $product->getCategoryId() == 0) {
158
                            throw new Exception('Category not found');
159
                        }
160
161
                        $category = SCategoryQuery::create()->findOneById($product->getCategoryId());
162
                        $categories = $category->buildCategoryPath(Criteria::ASC, MY_Controller::getCurrentLocale());
163
164 View Code Duplication
                        foreach ($categories as $category) {
165
                            $path[] = [
0 ignored issues
show
Coding Style Comprehensibility introduced by
$path was never initialized. Although not strictly required by PHP, it is generally a good practice to add $path = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
166
                                       'path_url' => 'shop/category/' . $category->getFullPath(),
167
                                       'name'     => $category->getName(),
168
                                      ];
169
                        }
170
171
                        $path[] = [
172
                                   'path_url' => '',
173
                                   'name'     => $product->getName(),
174
                                  ];
175
176
                        $tpl_data = ['navi_cats' => $path];
177
                        return $this->template->fetch('widgets/' . $widget['name'], $tpl_data);
178
                    } else {
179
                        throw new Exception('Product not found');
180
                    }
181
                }
182
                break;
0 ignored issues
show
Coding Style introduced by
Case breaking statements must be followed by a single blank line
Loading history...
183
            case 'gallery':
184
                $uri_segments = $this->uri->segment_array();
185
                $template_vars = $this->template->get_vars();
186
187
                $path = [];
188
                foreach ($uri_segments as $segment) {
189
                    switch ($segment) {
190
                        case 'gallery':
191
                            $path[] = [
192
                                       'path_url' => $segment,
193
                                       'name'     => lang('Gallery', 'navigation'),
194
                                      ];
195
                            break;
0 ignored issues
show
Coding Style introduced by
Case breaking statements must be followed by a single blank line
Loading history...
196
                        case 'category':
197
                            $path[] = [
198
                                       'path_url' => 'gallery/category/' . $template_vars['current_category']['id'],
199
                                       'name'     => $template_vars['current_category']['name'],
200
                                      ];
201
                            break;
0 ignored issues
show
Coding Style introduced by
Case breaking statements must be followed by a single blank line
Loading history...
202
                        case 'album':
203
                            $path[] = [
204
                                       'path_url' => 'gallery/category/' . $template_vars['current_category']['id'],
205
                                       'name'     => $template_vars['current_category']['name'],
206
                                      ];
207
208
                            $path[] = [
209
                                       'path_url' => "gallery/$segment/" . $template_vars['album']['id'],
210
                                       'name'     => $template_vars['album']['name'],
211
                                      ];
212
                            break;
0 ignored issues
show
Coding Style introduced by
Case breaking statements must be followed by a single blank line
Loading history...
213
                        case 'albums':
214
                            $path[] = [
215
                                       'path_url' => "gallery/$segment/",
216
                                       'name'     => lang('All albums', 'navigation'),
217
                                      ];
218
                            break;
219
                    }
220
                }
221
222
                $tpl_data = ['navi_cats' => $path];
223
                return $this->template->fetch('widgets/' . $widget['name'], $tpl_data);
224
225
            case 'auth':
226
                $uri_segments = $this->uri->segment_array();
227
228
                $path = [];
229
                foreach ($uri_segments as $segment) {
230
                    switch ($segment) {
231
                        case 'auth':
232
                            $path[] = [
233
                                       'path_url' => $segment,
234
                                       'name'     => lang('Login', 'navigation'),
235
                                      ];
236
                            break;
0 ignored issues
show
Coding Style introduced by
Case breaking statements must be followed by a single blank line
Loading history...
237
                        case 'register':
238
                            $path = [];
239
                            $path[] = [
240
                                       'path_url' => "auth/$segment",
241
                                       'name'     => lang('Registration', 'navigation'),
242
                                      ];
243
                            break;
0 ignored issues
show
Coding Style introduced by
Case breaking statements must be followed by a single blank line
Loading history...
244
                        case 'activate':
245
                            $path = [];
246
                            $path[] = [
247
                                       'path_url' => "auth/$segment",
248
                                       'name'     => lang('Activation', 'navigation'),
249
                                      ];
250
                            break;
0 ignored issues
show
Coding Style introduced by
Case breaking statements must be followed by a single blank line
Loading history...
251
                        case 'forgot_password':
252
                            $path = [];
253
                            $path[] = [
254
                                       'path_url' => "auth/$segment",
255
                                       'name'     => lang('Remind password', 'navigation'),
256
                                      ];
257
                            break;
0 ignored issues
show
Coding Style introduced by
Case breaking statements must be followed by a single blank line
Loading history...
258
                        case 'reset_password':
259
                            $path = [];
260
                            $path[] = [
261
                                       'path_url' => "auth/$segment",
262
                                       'name'     => lang('Reset password', 'navigation'),
263
                                      ];
264
                            break;
0 ignored issues
show
Coding Style introduced by
Case breaking statements must be followed by a single blank line
Loading history...
265
                        case 'change_password':
266
                            $path = [];
267
                            $path[] = [
268
                                       'path_url' => "auth/$segment",
269
                                       'name'     => lang('Change password', 'navigation'),
270
                                      ];
271
                            break;
272
                    }
273
                }
274
275
                $tpl_data = ['navi_cats' => $path];
276
                return $this->template->fetch('widgets/' . $widget['name'], $tpl_data);
277
        }
278
    }
279
280
    /**
281
     *
282
     * @param string $name
283
     * @param string $path_url
284
     * @param array $widget
285
     * @return string
286
     */
287
    public function make($name, $path_url, $widget) {
288
        $navi_cats[] = [
0 ignored issues
show
Coding Style Comprehensibility introduced by
$navi_cats was never initialized. Although not strictly required by PHP, it is generally a good practice to add $navi_cats = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
289
                        'path_url' => $path_url,
290
                        'name'     => $name,
291
                       ];
292
        $tpl_data = ['navi_cats' => $navi_cats];
293
        return $this->template->fetch('widgets/' . $widget['name'], $tpl_data);
294
    }
295
296
}
297
298
/* End of file widgets.php */