Completed
Push — master ( c0ee0c...9768c0 )
by
unknown
33:12 queued 18:11
created

ControllerModuleProducts::getFormFields()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 105
Code Lines 73

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 73
nc 4
nop 1
dl 0
loc 105
rs 8.2857
c 0
b 0
f 0

How to fix   Long Method   

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
 * @package     Arastta eCommerce
4
 * @copyright   2015-2017 Arastta Association. All rights reserved.
5
 * @copyright   See CREDITS.txt for credits and other copyright notices.
6
 * @license     GNU GPL version 3; see LICENSE.txt
7
 * @link        https://arastta.org
8
 */
9
10
use Arastta\Component\Form\Form as AForm;
11
12
class ControllerModuleProducts extends Controller
13
{
14
    private $error = array();
15
16
    public function index()
17
    {
18
        $this->load->language('module/products');
19
20
        $this->document->setTitle($this->language->get('heading_title'));
21
22
        $this->load->model('extension/module');
23
24
        if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validate()) {
25
            if (!isset($this->request->get['module_id'])) {
26
                $this->model_extension_module->addModule('products', $this->request->post);
27
            } else {
28
                $this->model_extension_module->editModule($this->request->get['module_id'], $this->request->post);
29
            }
30
31
            $this->session->data['success'] = $this->language->get('text_success');
32
33
            if (isset($this->request->post['button']) && $this->request->post['button'] == 'save') {
34
                $module_id = '';
35
36
                if (isset($this->request->get['module_id'])) {
37
                    $module_id = '&module_id=' . $this->request->get['module_id'];
38
                } elseif ($this->db->getLastId()) {
39
                    $module_id = '&module_id=' . $this->db->getLastId();
40
                }
41
42
                $this->response->redirect($this->url->link('module/products', 'token=' . $this->session->data['token'] . $module_id, 'SSL'));
43
            }
44
45
            if (isset($this->request->post['button']) && $this->request->post['button'] == 'new') {
46
                $this->response->redirect($this->url->link('module/products', 'token=' . $this->session->data['token'], 'SSL'));
47
            }
48
49
            $this->response->redirect($this->url->link('extension/module', 'token=' . $this->session->data['token'], 'SSL'));
50
        }
51
52
        #Get All Language Text
53
        $data = $this->language->all();
54
55
        if (isset($this->error['warning'])) {
56
            $data['error_warning'] = $this->error['warning'];
57
        } else {
58
            $data['error_warning'] = '';
59
        }
60
61
        if (isset($this->error['name'])) {
62
            $data['error_name'] = $this->error['name'];
63
        } else {
64
            $data['error_name'] = '';
65
        }
66
67
        if (!isset($this->request->get['module_id'])) {
68
            $data['action'] = $this->url->link('module/products', 'token=' . $this->session->data['token'], 'SSL');
69
        } else {
70
            $data['action'] = $this->url->link('module/products', 'token=' . $this->session->data['token'] . '&module_id=' . $this->request->get['module_id'], 'SSL');
71
        }
72
73
        $data['cancel'] = $this->url->link('extension/module', 'token=' . $this->session->data['token'], 'SSL');
74
75 View Code Duplication
        if (isset($this->request->get['module_id']) && ($this->request->server['REQUEST_METHOD'] != 'POST')) {
76
            $module_info = $this->model_extension_module->getModule($this->request->get['module_id']);
77
        }
78
79
        $data['token'] = $this->session->data['token'];
80
81
        if (isset($this->request->post['name'])) {
82
            $data['name'] = $this->request->post['name'];
83
        } elseif (!empty($module_info)) {
84
            $data['name'] = $module_info['name'];
85
        } else {
86
            $data['name'] = '';
87
        }
88
89
        if (isset($this->request->post['title'])) {
90
            $data['title'] = $this->request->post['title'];
91
        } elseif (!empty($module_info)) {
92
            $data['title'] = $module_info['title'];
93
        } else {
94
            $data['title'] = '';
95
        }
96
97
        if (isset($this->request->post['show_title'])) {
98
            $data['show_title'] = $this->request->post['show_title'];
99
        } elseif (!empty($module_info)) {
100
            $data['show_title'] = $module_info['show_title'];
101
        } else {
102
            $data['show_title'] = '0';
103
        }
104
105
        if (isset($this->request->post['module_class'])) {
106
            $data['module_class'] = $this->request->post['module_class'];
107
        } elseif (!empty($module_info)) {
108
            $data['module_class'] = $module_info['module_class'];
109
        } else {
110
            $data['module_class'] = '';
111
        }
112
113
        if (isset($this->request->post['module_column'])) {
114
            $data['module_column'] = $this->request->post['module_column'];
115
        } elseif (!empty($module_info)) {
116
            $data['module_column'] = $module_info['module_column'];
117
        } else {
118
            $data['module_column'] = '4';
119
        }
120
121
        if (isset($this->request->post['type'])) {
122
            $data['type'] = $this->request->post['type'];
123
        } elseif (!empty($module_info)) {
124
            $data['type'] = $module_info['type'];
125
        } else {
126
            $data['type'] = '';
127
        }
128
129
        // Categories
130
        $this->load->model('catalog/category');
131
132 View Code Duplication
        if (isset($this->request->post['category'])) {
133
            $categories = $this->request->post['category'];
134
        } elseif (!empty($module_info['category'])) {
135
            $categories = $module_info['category'];
136
        } else {
137
            $categories = array();
138
        }
139
140
        $data['categories'] = array();
141
142
        if (!empty($categories)) {
143 View Code Duplication
            foreach ($categories as $category_id) {
144
                $category_info = $this->model_catalog_category->getCategory($category_id);
145
146
                if ($category_info) {
147
                    $data['categories'][] = array(
148
                        'category_id' => $category_info['category_id'],
149
                        'name'        => ($category_info['path']) ? $category_info['path'] . ' &gt; ' . $category_info['name'] : $category_info['name']
150
                    );
151
                }
152
            }
153
        }
154
155
        // Products
156
        $this->load->model('catalog/product');
157
158
        $data['products'] = array();
159
160 View Code Duplication
        if (!empty($this->request->post['product'])) {
161
            $products = $this->request->post['product'];
162
        } elseif (!empty($module_info['product'])) {
163
            $products = $module_info['product'];
164
        } else {
165
            $products = array();
166
        }
167
168 View Code Duplication
        if (!empty($products)) {
169
            foreach ($products as $product_id) {
170
                $product_info = $this->model_catalog_product->getProduct($product_id);
171
172
                if ($product_info) {
173
                    $data['products'][] = array(
174
                        'product_id' => $product_info['product_id'],
175
                        'name'       => $product_info['name']
176
                    );
177
                }
178
            }
179
        }
180
181
        if (isset($this->request->post['random_product'])) {
182
            $data['random_product'] = $this->request->post['random_product'];
183
        } elseif (!empty($module_info)) {
184
            $data['random_product'] = $module_info['random_product'];
185
        } else {
186
            $data['random_product'] = '0';
187
        }
188
189
        if (isset($this->request->post['limit'])) {
190
            $data['limit'] = $this->request->post['limit'];
191
        } elseif (!empty($module_info)) {
192
            $data['limit'] = $module_info['limit'];
193
        } else {
194
            $data['limit'] = '4';
195
        }
196
197
        if (isset($this->request->post['width'])) {
198
            $data['width'] = $this->request->post['width'];
199
        } elseif (!empty($module_info)) {
200
            $data['width'] = $module_info['width'];
201
        } else {
202
            $data['width'] = '200';
203
        }
204
205
        if (isset($this->request->post['height'])) {
206
            $data['height'] = $this->request->post['height'];
207
        } elseif (!empty($module_info)) {
208
            $data['height'] = $module_info['height'];
209
        } else {
210
            $data['height'] = '200';
211
        }
212
213
        if (isset($this->request->post['status'])) {
214
            $data['status'] = $this->request->post['status'];
215
        } elseif (!empty($module_info)) {
216
            $data['status'] = $module_info['status'];
217
        } else {
218
            $data['status'] = 1;
219
        }
220
221
        if (isset($this->request->post['product_image'])) {
222
            $data['product_image'] = $this->request->post['product_image'];
223
        } elseif (!empty($module_info)) {
224
            $data['product_image'] = $module_info['product_image'];
225
        } else {
226
            $data['product_image'] = '1';
227
        }
228
229
        if (isset($this->request->post['product_name'])) {
230
            $data['product_name'] = $this->request->post['product_name'];
231
        } elseif (!empty($module_info)) {
232
            $data['product_name'] = $module_info['product_name'];
233
        } else {
234
            $data['product_name'] = '1';
235
        }
236
237
        if (isset($this->request->post['product_description'])) {
238
            $data['product_description'] = $this->request->post['product_description'];
239
        } elseif (!empty($module_info)) {
240
            $data['product_description'] = $module_info['product_description'];
241
        } else {
242
            $data['product_description'] = '1';
243
        }
244
245
        if (isset($this->request->post['product_rating'])) {
246
            $data['product_rating'] = $this->request->post['product_rating'];
247
        } elseif (!empty($module_info)) {
248
            $data['product_rating'] = $module_info['product_rating'];
249
        } else {
250
            $data['product_rating'] = '1';
251
        }
252
253
        if (isset($this->request->post['product_price'])) {
254
            $data['product_price'] = $this->request->post['product_price'];
255
        } elseif (!empty($module_info)) {
256
            $data['product_price'] = $module_info['product_price'];
257
        } else {
258
            $data['product_price'] = '1';
259
        }
260
261
        if (isset($this->request->post['add_to_cart'])) {
262
            $data['add_to_cart'] = $this->request->post['add_to_cart'];
263
        } elseif (!empty($module_info)) {
264
            $data['add_to_cart'] = $module_info['add_to_cart'];
265
        } else {
266
            $data['add_to_cart'] = '1';
267
        }
268
269
        if (isset($this->request->post['product_image'])) {
270
            $data['wish_list'] = $this->request->post['wish_list'];
271
        } elseif (!empty($module_info)) {
272
            $data['wish_list'] = $module_info['wish_list'];
273
        } else {
274
            $data['wish_list'] = '1';
275
        }
276
277
        if (isset($this->request->post['product_image'])) {
278
            $data['compare'] = $this->request->post['compare'];
279
        } elseif (!empty($module_info)) {
280
            $data['compare'] = $module_info['compare'];
281
        } else {
282
            $data['compare'] = '1';
283
        }
284
285
        $data['form_fields'] = $this->getFormFields($data);
286
287
        $data['header']      = $this->load->controller('common/header');
288
        $data['column_left'] = $this->load->controller('common/column_left');
289
        $data['footer']      = $this->load->controller('common/footer');
290
291
        $this->response->setOutput($this->load->view('module/products.tpl', $data));
292
    }
293
294
    protected function getFormFields($data)
295
    {
296
        $action = str_replace('amp;', '', $data['action']);
297
298
        $option_text = array('yes' => $this->language->get('text_enabled'), 'no' => $this->language->get('text_disabled'));
299
300
        $form = new AForm('form-cart', $action);
301
302
        $form->addElement(new Arastta\Component\Form\Element\HTML('<legend>' . $this->language->get('text_general') . '</legend>'));
303
304
        $name = array('value' => $data['name'], 'required' => 'required');
305
        $form->addElement(new Arastta\Component\Form\Element\Textbox($this->language->get('entry_name'), 'name', $name));
306
307
        $title = array('value' => $data['title'], 'required' => 'required');
308
        $form->addElement(new Arastta\Component\Form\Element\Textbox($this->language->get('entry_title'), 'title', $title));
309
310
        $show_title = array('value' => $data['show_title'], 'labelclass' => 'radio-inline');
311
        $form->addElement(new Arastta\Component\Form\Element\YesNo($this->language->get('entry_show_title'), 'show_title', $show_title, $option_text));
312
313
        $module_class = array('value' => $data['module_class']);
314
        $form->addElement(new Arastta\Component\Form\Element\Textbox($this->language->get('entry_module_class'), 'module_class', $module_class));
315
316
        $module_column = array('value' => $data['module_column'], 'selected' => $data['module_column'], 'id' => 'input-module-column');
317
        $module_column_option  = array('1' => '1', '2' => '2', '3' => '3', '4' => '4', '6' => '6');
318
        $form->addElement(new Arastta\Component\Form\Element\Select($this->language->get('entry_module_column'), 'module_column', $module_column_option, $module_column, $option_text));
0 ignored issues
show
Unused Code introduced by
The call to Select::__construct() has too many arguments starting with $option_text.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
319
320
        $type        = array('value' => $data['type'], 'selected' => $data['type'], 'id' => 'input-type');
321
        $type_option = array('all' => $this->language->get('text_all'), 'bestsellers' => $this->language->get('text_bestsellers'), 'featured' => $this->language->get('text_featured'), 'latest' => $this->language->get('text_latest'), 'special' => $this->language->get('text_special'));
322
        $form->addElement(new Arastta\Component\Form\Element\Select($this->language->get('entry_type'), 'type', $type_option, $type));
323
324
        $html  = '<div class="form-group">';
325
        $html .= '    <label class="col-sm-2 control-label" for="input-category"><span data-toggle="tooltip" title="' . $this->language->get('help_category') . '">' . $this->language->get('entry_category') . '</span></label>';
326
        $html .= '    <div class="col-sm-10">';
327
        $html .= '        <input type="text" name="category" value="" placeholder="' . $this->language->get('entry_category') . '" id="input-category" class="form-control" />';
328
        $html .= '        <div id="categories" class="well well-sm" style="height: 150px; overflow: auto;">';
329
                            foreach ($data['categories'] as $category) {
330
        $html .= '          <div id="categories' .  $category['category_id'] . '"><i class="fa fa-minus-circle"></i> '. $category['name'];
331
        $html .= '            <input type="hidden" name="category[]" value="' . $category['category_id'] . '" />';
332
        $html .= '          </div>';
333
                            }
334
        $html .= '        </div>';
335
        $html .= '    </div>';
336
        $html .= '</div>';
337
338
        $form->addElement(new Arastta\Component\Form\Element\HTML($html));
339
340
        $html  = '<div class="form-group">';
341
        $html .= '    <label class="col-sm-2 control-label" for="input-product"><span data-toggle="tooltip" title="' . $this->language->get('help_product') . '">' . $this->language->get('entry_product') . '</span></label>';
342
        $html .= '    <div class="col-sm-10">';
343
        $html .= '        <input type="text" name="product" value="" placeholder="' . $this->language->get('entry_product') . '" id="input-product" class="form-control" />';
344
        $html .= '        <div id="featured-product" class="well well-sm" style="height: 150px; overflow: auto;">';
345
                            foreach ($data['products'] as $product) {
346
        $html .= '          <div id="featured-product' .  $product['product_id'] . '"><i class="fa fa-minus-circle"></i> '. $product['name'];
347
        $html .= '            <input type="hidden" name="product[]" value="' . $product['product_id'] . '" />';
348
        $html .= '          </div>';
349
                            }
350
        $html .= '        </div>';
351
        $html .= '    </div>';
352
        $html .= '</div>';
353
354
        $form->addElement(new Arastta\Component\Form\Element\HTML($html));
355
356
        $random_product = array('value' => $data['random_product'], 'labelclass' => 'radio-inline');
357
        $form->addElement(new Arastta\Component\Form\Element\YesNo($this->language->get('entry_random_product'), 'random_product', $random_product, $option_text));
358
359
        $limit = array('value' => $data['limit'], 'required' => 'required');
360
        $form->addElement(new Arastta\Component\Form\Element\Textbox($this->language->get('entry_limit'), 'limit', $limit));
361
362
        $width = array('value' => $data['width'], 'required' => 'required');
363
        $form->addElement(new Arastta\Component\Form\Element\Textbox($this->language->get('entry_width'), 'width', $width));
364
365
        $height = array('value' => $data['height'], 'required' => 'required');
366
        $form->addElement(new Arastta\Component\Form\Element\Textbox($this->language->get('entry_height'), 'height', $height));
367
368
        $status = array('value' => $data['status'], 'labelclass' => 'radio-inline');
369
        $form->addElement(new Arastta\Component\Form\Element\YesNo($this->language->get('entry_status'), 'status', $status, $option_text));
370
371
        $form->addElement(new Arastta\Component\Form\Element\HTML('<legend>' . $this->language->get('text_view') . '</legend>'));
372
373
        $product_image = array('value' => $data['product_image'], 'labelclass' => 'radio-inline');
374
        $form->addElement(new Arastta\Component\Form\Element\YesNo($this->language->get('entry_image'), 'product_image', $product_image, $option_text));
375
376
        $product_name = array('value' => $data['product_name'], 'labelclass' => 'radio-inline');
377
        $form->addElement(new Arastta\Component\Form\Element\YesNo($this->language->get('entry_pname'), 'product_name', $product_name, $option_text));
378
379
        $product_description = array('value' => $data['product_description'], 'labelclass' => 'radio-inline');
380
        $form->addElement(new Arastta\Component\Form\Element\YesNo($this->language->get('entry_pdescription'), 'product_description', $product_description, $option_text));
381
382
        $product_rating = array('value' => $data['product_rating'], 'labelclass' => 'radio-inline');
383
        $form->addElement(new Arastta\Component\Form\Element\YesNo($this->language->get('entry_rating'), 'product_rating', $product_rating, $option_text));
384
385
        $product_price = array('value' => $data['product_price'], 'labelclass' => 'radio-inline');
386
        $form->addElement(new Arastta\Component\Form\Element\YesNo($this->language->get('entry_price'), 'product_price', $product_price, $option_text));
387
388
        $add_to_cart = array('value' => $data['add_to_cart'], 'labelclass' => 'radio-inline');
389
        $form->addElement(new Arastta\Component\Form\Element\YesNo($this->language->get('entry_add_to_cart'), 'add_to_cart', $add_to_cart, $option_text));
390
391
        $wish_list = array('value' => $data['wish_list'], 'labelclass' => 'radio-inline');
392
        $form->addElement(new Arastta\Component\Form\Element\YesNo($this->language->get('entry_wish_list'), 'wish_list', $wish_list, $option_text));
393
394
        $compare = array('value' => $data['compare'], 'labelclass' => 'radio-inline');
395
        $form->addElement(new Arastta\Component\Form\Element\YesNo($this->language->get('entry_compare'), 'compare', $compare, $option_text));
396
397
        return $form->render(true);
398
    }
399
400 View Code Duplication
    protected function validate()
401
    {
402
        if (!$this->user->hasPermission('modify', 'module/products')) {
403
            $this->error['warning'] = $this->language->get('error_permission');
404
        }
405
406
        if ((utf8_strlen($this->request->post['name']) < 3) || (utf8_strlen($this->request->post['name']) > 64)) {
407
            $this->error['name'] = $this->language->get('error_name');
408
        }
409
410
        return !$this->error;
411
    }
412
}
413