ControllerToolSeoManager::getList()   F
last analyzed

Complexity

Conditions 31
Paths > 20000

Size

Total Lines 282
Code Lines 182

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 31
eloc 182
c 0
b 0
f 0
nc 805306368
nop 0
dl 0
loc 282
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 ControllerToolSeoManager extends \Divine\Engine\Core\Controller
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
24
{
25
    private $error = array();
26
27
    public function index()
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines before function; 1 found
Loading history...
28
    {
29
        $this->load->model('setting/setting');
30
            
31
        $this->load->language('tool/seomanager');
32
                     
33
    
34
        if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) {
35
            $this->model_setting_setting->editSetting('seomanager', $this->request->post);
36
                
37
            $this->session->data['success'] = $this->language->get('text_success');
38
39
            $this->response->redirect($this->url->link('tool/seomanager', 'token=' . $this->session->data['token'], true));
40
        }
41
            
42
        $this->getList();
43
    }
44
45
    public function update()
46
    {
47
        $this->load->language('tool/seomanager');
48
            
49
        $this->document->setTitle($this->language->get('heading_title'));
50
            
51
        $this->load->model('tool/seomanager');
52
53
        $url = '';
54
55
        if (isset($this->request->get['sort'])) {
56
            $url .= '&sort=' . $this->request->get['sort'];
57
        }
58
59
        if (isset($this->request->get['order'])) {
60
            $url .= '&order=' . $this->request->get['order'];
61
        }
62
63
        if (isset($this->request->get['page'])) {
64
            $url .= '&page=' . $this->request->get['page'];
65
        }
66
67
        if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) {
68
            $this->model_tool_seomanager->updateUrlAlias($this->request->post);
69
            $this->session->data['success'] = $this->language->get('text_success');
70
        }
71
            
72
        $this->response->redirect($this->url->link('tool/seomanager', 'token=' . $this->session->data['token'] . $url, true));
73
    }
74
        
75
    public function clear()
76
    {
77
        $this->load->language('tool/seomanager');
78
                
79
        $url = '';
80
81
        if (isset($this->request->get['sort'])) {
82
            $url .= '&sort=' . $this->request->get['sort'];
83
        }
84
85
        if (isset($this->request->get['order'])) {
86
            $url .= '&order=' . $this->request->get['order'];
87
        }
88
89
        if (isset($this->request->get['page'])) {
90
            $url .= '&page=' . $this->request->get['page'];
91
        }
92
        $this->cache->delete('url_formatter');
93
94
        $this->session->data['success'] = $this->language->get('text_success_clear');
95
        $this->response->redirect($this->url->link('tool/seomanager', 'token=' . $this->session->data['token'] . $url, true));
96
    }
97
98
    public function delete()
99
    {
100
        $this->load->language('tool/seomanager');
101
        $this->load->model('tool/seomanager');
102
        $url = '';
103
104
        if (isset($this->request->get['sort'])) {
105
            $url .= '&sort=' . $this->request->get['sort'];
106
        }
107
108
        if (isset($this->request->get['order'])) {
109
            $url .= '&order=' . $this->request->get['order'];
110
        }
111
112
        if (isset($this->request->get['page'])) {
113
            $url .= '&page=' . $this->request->get['page'];
114
        }
115
116
        if (isset($this->request->post['selected']) && $this->validateDelete()) {
117
            foreach ($this->request->post['selected'] as $url_alias_id) {
118
                $this->model_tool_seomanager->deleteUrlAlias($url_alias_id);
119
            }
120
            $this->session->data['success'] = $this->language->get('text_success');
121
        }
122
123
        $this->response->redirect($this->url->link('tool/seomanager', 'token=' . $this->session->data['token'] . $url, true));
124
    }
125
126
    private function getList()
127
    {
128
        $this->load->model('tool/seomanager');
129
            
130
        if (isset($this->request->get['sort'])) {
131
            $sort = $this->request->get['sort'];
132
        } else {
133
            $sort = 'ua.query';
134
        }
135
136
        if (isset($this->request->get['order'])) {
137
            $order = $this->request->get['order'];
138
        } else {
139
            $order = 'ASC';
140
        }
141
142
        if (isset($this->request->get['page'])) {
143
            $page = $this->request->get['page'];
144
        } else {
145
            $page = 1;
146
        }
147
148
        $url = '';
149
150
        if (isset($this->request->get['sort'])) {
151
            $url .= '&sort=' . $this->request->get['sort'];
152
        }
153
154
        if (isset($this->request->get['order'])) {
155
            $url .= '&order=' . $this->request->get['order'];
156
        }
157
158
        if (isset($this->request->get['page'])) {
159
            $url .= '&page=' . $this->request->get['page'];
160
        }
161
                
162
        $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...
163
164
        $data['breadcrumbs'][] = array(
165
            'text' => $this->language->get('text_home'),
166
            'href' => $this->url->link('common/dashboard', 'token=' . $this->session->data['token'], true)
167
        );
168
169
        $data['breadcrumbs'][] = array(
170
            'text' => $this->language->get('heading_title'),
171
            'href' => $this->url->link('tool/seomanager', 'token=' . $this->session->data['token'] . $url, true)
172
        );
173
                
174
        //dop
175
        $data['action'] = $this->url->link('tool/seomanager', 'token=' . $this->session->data['token'], true);
176
177
        $data['cancel'] = $this->url->link('tool/seomanager', 'token=' . $this->session->data['token'], true);
178
                
179
        $data['token'] = $this->session->data['token'];
180
                
181
        $data['entry_name'] = $this->language->get('entry_name');
182
        $data['entry_html_h1'] = $this->language->get('entry_html_h1');
183
        $data['entry_meta_title'] = $this->language->get('entry_meta_title');
184
        $data['entry_meta_description'] = $this->language->get('entry_meta_description');
185
                
186
        if (isset($this->request->post['seomanager_html_h1_special'])) {
187
            $data['seomanager_html_h1_special'] = $this->request->post['seomanager_html_h1_special'];
188
        } else {
189
            $data['seomanager_html_h1_special'] = $this->config->get('seomanager_html_h1_special');
190
        }
191
                
192
        if (isset($this->request->post['seomanager_meta_title_special'])) {
193
            $data['seomanager_meta_title_special'] = $this->request->post['seomanager_meta_title_special'];
194
        } else {
195
            $data['seomanager_meta_title_special'] = $this->config->get('seomanager_meta_title_special');
196
        }
197
198
        if (isset($this->request->post['seomanager_meta_description_special'])) {
199
            $data['seomanager_meta_description_special'] = $this->request->post['seomanager_meta_description_special'];
200
        } else {
201
            $data['seomanager_meta_description_special'] = $this->config->get('seomanager_meta_description_special');
202
        }
203
204
        if (isset($this->request->post['seomanager_description_special'])) {
205
            $data['seomanager_description_special'] = $this->request->post['seomanager_description_special'];
206
        } else {
207
            $data['seomanager_description_special'] = $this->config->get('seomanager_description_special');
208
        }
209
                
210
        if (isset($this->request->post['seomanager_html_h1_bestseller'])) {
211
            $data['seomanager_html_h1_bestseller'] = $this->request->post['seomanager_html_h1_bestseller'];
212
        } else {
213
            $data['seomanager_html_h1_bestseller'] = $this->config->get('seomanager_html_h1_bestseller');
214
        }
215
                
216
        if (isset($this->request->post['seomanager_meta_title_bestseller'])) {
217
            $data['seomanager_meta_title_bestseller'] = $this->request->post['seomanager_meta_title_bestseller'];
218
        } else {
219
            $data['seomanager_meta_title_bestseller'] = $this->config->get('seomanager_meta_title_bestseller');
220
        }
221
222
        if (isset($this->request->post['seomanager_meta_description_bestseller'])) {
223
            $data['seomanager_meta_description_bestseller'] = $this->request->post['seomanager_meta_description_bestseller'];
224
        } else {
225
            $data['seomanager_meta_description_bestseller'] = $this->config->get('seomanager_meta_description_bestseller');
226
        }
227
                
228
        if (isset($this->request->post['seomanager_description_bestseller'])) {
229
            $data['seomanager_description_bestseller'] = $this->request->post['seomanager_description_bestseller'];
230
        } else {
231
            $data['seomanager_description_bestseller'] = $this->config->get('seomanager_description_bestseller');
232
        }
233
                
234
        if (isset($this->request->post['seomanager_html_h1_latest'])) {
235
            $data['seomanager_html_h1_latest'] = $this->request->post['seomanager_html_h1_latest'];
236
        } else {
237
            $data['seomanager_html_h1_latest'] = $this->config->get('seomanager_html_h1_latest');
238
        }
239
                
240
        if (isset($this->request->post['seomanager_meta_title_latest'])) {
241
            $data['seomanager_meta_title_latest'] = $this->request->post['seomanager_meta_title_latest'];
242
        } else {
243
            $data['seomanager_meta_title_latest'] = $this->config->get('seomanager_meta_title_latest');
244
        }
245
246
        if (isset($this->request->post['seomanager_meta_description_latest'])) {
247
            $data['seomanager_meta_description_latest'] = $this->request->post['seomanager_meta_description_latest'];
248
        } else {
249
            $data['seomanager_meta_description_latest'] = $this->config->get('seomanager_meta_description_latest');
250
        }
251
                
252
        if (isset($this->request->post['seomanager_description_latest'])) {
253
            $data['seomanager_description_latest'] = $this->request->post['seomanager_description_latest'];
254
        } else {
255
            $data['seomanager_description_latest'] = $this->config->get('seomanager_description_latest');
256
        }
257
                
258
        if (isset($this->request->post['seomanager_html_h1_mostviewed'])) {
259
            $data['seomanager_html_h1_mostviewed'] = $this->request->post['seomanager_html_h1_mostviewed'];
260
        } else {
261
            $data['seomanager_html_h1_mostviewed'] = $this->config->get('seomanager_html_h1_mostviewed');
262
        }
263
                
264
        if (isset($this->request->post['seomanager_meta_title_mostviewed'])) {
265
            $data['seomanager_meta_title_mostviewed'] = $this->request->post['seomanager_meta_title_mostviewed'];
266
        } else {
267
            $data['seomanager_meta_title_mostviewed'] = $this->config->get('seomanager_meta_title_mostviewed');
268
        }
269
270
        if (isset($this->request->post['seomanager_meta_description_mostviewed'])) {
271
            $data['seomanager_meta_description_mostviewed'] = $this->request->post['seomanager_meta_description_mostviewed'];
272
        } else {
273
            $data['seomanager_meta_description_mostviewed'] = $this->config->get('seomanager_meta_description_mostviewed');
274
        }
275
                
276
        if (isset($this->request->post['seomanager_description_mostviewed'])) {
277
            $data['seomanager_description_mostviewed'] = $this->request->post['seomanager_description_mostviewed'];
278
        } else {
279
            $data['seomanager_description_mostviewed'] = $this->config->get('seomanager_description_mostviewed');
280
        }
281
                
282
        //dop
283
284
        $data['insert'] = $this->url->link('tool/seomanager/insert', 'token=' . $this->session->data['token'] . $url, true);
285
        $data['delete'] = $this->url->link('tool/seomanager/delete', 'token=' . $this->session->data['token'] . $url, true);
286
        $data['save'] = $this->url->link('tool/seomanager/update', 'token=' . $this->session->data['token'] . $url, true);
287
        $data['clear'] = $this->url->link('tool/seomanager/clear', 'token=' . $this->session->data['token'] . $url, true);
288
289
        $data['url_aliases'] = array();
290
291
        $filterdata = array(
292
            'sort' => $sort,
293
            'order' => $order,
294
            'start' => ($page - 1) * $this->config->get('config_limit_admin'),
295
            'limit' => $this->config->get('config_limit_admin')
296
        );
297
298
        $url_alias_total = $this->model_tool_seomanager->getTotalUrlAalias();
299
300
        $results = $this->model_tool_seomanager->getUrlAaliases($filterdata);
301
302
        foreach ($results as $result) {
303
            $data['url_aliases'][] = array(
304
                'url_alias_id' => $result['url_alias_id'],
305
                'query' => $result['query'],
306
                'keyword' => $result['keyword'],
307
                'selected' => isset($this->request->post['selected']) && in_array($result['url_alias_id'], $this->request->post['selected']),
308
                'action_text' => $this->language->get('text_edit')
309
            );
310
        }
311
312
        $data['heading_title'] = $this->language->get('heading_title');
313
314
        $data['text_no_results'] = $this->language->get('text_no_results');
315
316
        $data['column_query'] = $this->language->get('column_query');
317
        $data['column_keyword'] = $this->language->get('column_keyword');
318
        $data['column_action'] = $this->language->get('column_action');
319
320
        $data['button_insert'] = $this->language->get('button_insert');
321
        $data['button_delete'] = $this->language->get('button_delete');
322
        $data['button_save'] = $this->language->get('button_save');
323
        $data['button_cancel'] = $this->language->get('button_cancel');
324
        $data['button_clear_cache'] = $this->language->get('button_clear_cache');
325
        $data['button_edit'] = $this->language->get('button_edit');
326
                
327
        $data['entry_name'] = $this->language->get('entry_name');
328
        $data['entry_html_h1'] = $this->language->get('entry_html_h1');
329
        $data['entry_meta_title'] = $this->language->get('entry_meta_title');
330
        $data['entry_meta_description'] = $this->language->get('entry_meta_description');
331
        $data['entry_description'] = $this->language->get('entry_description');
332
                
333
        $data['tab_seourl'] = $this->language->get('tab_seourl');
334
        $data['tab_meta'] = $this->language->get('tab_meta');
335
        $data['tab_special'] = $this->language->get('tab_special');
336
        $data['tab_bestseller'] = $this->language->get('tab_bestseller');
337
        $data['tab_latest'] = $this->language->get('tab_latest');
338
        $data['tab_mostviewed'] = $this->language->get('tab_mostviewed');
339
340
        if (isset($this->error['warning'])) {
341
            $data['error_warning'] = $this->error['warning'];
342
        } else {
343
            $data['error_warning'] = '';
344
        }
345
346
        if (isset($this->session->data['success'])) {
347
            $data['success'] = $this->session->data['success'];
348
349
            unset($this->session->data['success']);
350
        } else {
351
            $data['success'] = '';
352
        }
353
354
        $url = '';
355
356
        if ($order == 'ASC') {
357
            $url .= '&order=DESC';
358
        } else {
359
            $url .= '&order=ASC';
360
        }
361
362
        if (isset($this->request->get['page'])) {
363
            $url .= '&page=' . $this->request->get['page'];
364
        }
365
                
366
        $data['breadcrumbs'] = array();
367
368
        $data['breadcrumbs'][] = array(
369
            'text' => $this->language->get('text_home'),
370
            'href' => $this->url->link('common/dashboard', 'token=' . $this->session->data['token'], true)
371
        );
372
373
        $data['breadcrumbs'][] = array(
374
            'text' => $this->language->get('heading_title'),
375
            'href' => $this->url->link('tool/seomanager', 'token=' . $this->session->data['token'] . $url, true)
376
        );
377
378
        $data['sort_query'] = $this->url->link('tool/seomanager', 'token=' . $this->session->data['token'] . '&sort=ua.query' . $url, true);
379
        $data['sort_keyword'] = $this->url->link('tool/seomanager', 'token=' . $this->session->data['token'] . '&sort=ua.keyword' . $url, true);
380
381
        $url = '';
382
383
        if (isset($this->request->get['sort'])) {
384
            $url .= '&sort=' . $this->request->get['sort'];
385
        }
386
387
        if (isset($this->request->get['order'])) {
388
            $url .= '&order=' . $this->request->get['order'];
389
        }
390
391
        $pagination = new \Divine\Engine\Library\Pagination();
392
        $pagination->total = $url_alias_total;
393
        $pagination->page = $page;
394
        $pagination->limit = $this->config->get('config_limit_admin');
395
        $pagination->text = $this->language->get('text_pagination');
0 ignored issues
show
Bug introduced by
The property text does not seem to exist on Divine\Engine\Library\Pagination.
Loading history...
396
        $pagination->url = $this->url->link('tool/seomanager', 'token=' . $this->session->data['token'] . $url . '&page={page}', true);
397
398
        $data['pagination'] = $pagination->render();
399
400
        $data['sort'] = $sort;
401
        $data['order'] = $order;
402
403
        $data['header'] = $this->load->controller('common/header');
404
        $data['column'] = $this->load->controller('common/column_left');
405
        $data['footer'] = $this->load->controller('common/footer');
406
                                
407
        $this->response->setOutput($this->load->view('tool/seomanager', $data));
408
    }
409
410
    private function validateForm()
411
    {
412
        if (!$this->user->hasPermission('modify', 'tool/seomanager')) {
413
            $this->error['warning'] = $this->language->get('error_permission');
414
        }
415
        if (!$this->error) {
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...
416
            return true;
417
        } else {
418
            return false;
419
        }
420
    }
421
422
    private function validateDelete()
423
    {
424
        if (!$this->user->hasPermission('modify', 'tool/seomanager')) {
425
            $this->error['warning'] = $this->language->get('error_permission');
426
        }
427
        if (!$this->error) {
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...
428
            return true;
429
        } else {
430
            return false;
431
        }
432
    }
433
434
    public function install()
435
    {
436
        $this->load->model('tool/seomanager');
437
        $this->model_tool_seomanager->install();
438
    }
439
440
    public function uninstall()
441
    {
442
        $this->load->model('tool/seomanager');
443
        $this->model_tool_seomanager->uninstall();
444
    }
445
}
446