ControllerBlogPost::getForm()   F
last analyzed

Complexity

Conditions 53
Paths > 20000

Size

Total Lines 244
Code Lines 153

Duplication

Lines 42
Ratio 17.21 %

Importance

Changes 0
Metric Value
cc 53
eloc 153
nc 429496.7295
nop 0
dl 42
loc 244
rs 2
c 0
b 0
f 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
 * @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
class ControllerBlogPost extends Controller
11
{
12
    private $error = array();
13
14 View Code Duplication
    public function index()
15
    {
16
        $this->load->language('blog/post');
17
18
        $this->document->setTitle($this->language->get('heading_title'));
19
20
        $this->load->model('blog/post');
21
22
        $this->getList();
23
    }
24
25 View Code Duplication
    public function add()
26
    {
27
        $this->load->language('blog/post');
28
29
        $this->document->setTitle($this->language->get('heading_title'));
30
31
        $this->load->model('blog/post');
32
33
        if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) {
34
            $post_id = $this->model_blog_post->addPost($this->request->post);
35
36
            $this->session->data['success'] = $this->language->get('text_success');
37
38
            $url = '';
39
40
            if (isset($this->request->get['filter_name'])) {
41
                $url .= '&filter_name=' . urlencode(html_entity_decode($this->request->get['filter_name'], ENT_QUOTES, 'UTF-8'));
42
            }
43
44
            if (isset($this->request->get['filter_author'])) {
45
                $url .= '&filter_author=' . urlencode(html_entity_decode($this->request->get['filter_author'], ENT_QUOTES, 'UTF-8'));
46
            }
47
48
            if (isset($this->request->get['filter_category'])) {
49
                $url .= '&filter_category=' . $this->request->get['filter_category'];
50
            }
51
52
            if (isset($this->request->get['filter_status'])) {
53
                $url .= '&filter_status=' . $this->request->get['filter_status'];
54
            }
55
56
            if (isset($this->request->get['page'])) {
57
                $url .= '&page=' . $this->request->get['page'];
58
            }
59
60
            if (isset($this->request->get['sort'])) {
61
                $url .= '&sort=' . $this->request->get['sort'];
62
            }
63
64
            if (isset($this->request->get['order'])) {
65
                $url .= '&order=' . $this->request->get['order'];
66
            }
67
68
            if (isset($this->request->post['button']) and $this->request->post['button'] == 'save') {
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as and instead of && is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
69
                $this->response->redirect($this->url->link('blog/post/edit', 'post_id=' . $post_id . '&token=' . $this->session->data['token'] . $url, 'SSL'));
70
            }
71
72
            if (isset($this->request->post['button']) and $this->request->post['button'] == 'new') {
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as and instead of && is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
73
                $this->response->redirect($this->url->link('blog/post/add', 'token=' . $this->session->data['token'] . $url, 'SSL'));
74
            }
75
76
            $this->response->redirect($this->url->link('blog/post', 'token=' . $this->session->data['token'] . $url, 'SSL'));
77
        }
78
79
        $this->getForm();
80
    }
81
82 View Code Duplication
    public function edit()
83
    {
84
        $this->load->language('blog/post');
85
86
        $this->document->setTitle($this->language->get('heading_title'));
87
88
        $this->load->model('blog/post');
89
90
        if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) {
91
            $this->model_blog_post->editPost($this->request->get['post_id'], $this->request->post);
92
93
            $this->session->data['success'] = $this->language->get('text_success');
94
95
            $url = '';
96
97
            if (isset($this->request->get['filter_name'])) {
98
                $url .= '&filter_name=' . urlencode(html_entity_decode($this->request->get['filter_name'], ENT_QUOTES, 'UTF-8'));
99
            }
100
101
            if (isset($this->request->get['filter_author'])) {
102
                $url .= '&filter_author=' . urlencode(html_entity_decode($this->request->get['filter_author'], ENT_QUOTES, 'UTF-8'));
103
            }
104
105
            if (isset($this->request->get['filter_category'])) {
106
                $url .= '&filter_category=' . $this->request->get['filter_category'];
107
            }
108
109
            if (isset($this->request->get['filter_status'])) {
110
                $url .= '&filter_status=' . $this->request->get['filter_status'];
111
            }
112
113
            if (isset($this->request->get['page'])) {
114
                $url .= '&page=' . $this->request->get['page'];
115
            }
116
117
            if (isset($this->request->get['sort'])) {
118
                $url .= '&sort=' . $this->request->get['sort'];
119
            }
120
121
            if (isset($this->request->get['order'])) {
122
                $url .= '&order=' . $this->request->get['order'];
123
            }
124
125
            if (isset($this->request->post['button']) and $this->request->post['button'] == 'save') {
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as and instead of && is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
126
                $this->response->redirect($this->url->link('blog/post/edit', 'post_id=' . $this->request->get['post_id'] . '&token=' . $this->session->data['token'] . $url, 'SSL'));
127
            }
128
129
            if (isset($this->request->post['button']) and $this->request->post['button'] == 'new') {
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as and instead of && is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
130
                $this->response->redirect($this->url->link('blog/post/add', 'token=' . $this->session->data['token'] . $url, 'SSL'));
131
            }
132
133
            $this->response->redirect($this->url->link('blog/post', 'token=' . $this->session->data['token'] . $url, 'SSL'));
134
        }
135
136
        $this->getForm();
137
    }
138
139 View Code Duplication
    public function delete()
140
    {
141
        $this->load->language('blog/post');
142
143
        $this->document->setTitle($this->language->get('heading_title'));
144
145
        $this->load->model('blog/post');
146
147
        if (isset($this->request->post['selected']) && $this->validateDelete()) {
148
            foreach ($this->request->post['selected'] as $post_id) {
149
                $this->model_blog_post->deletePost($post_id);
150
            }
151
152
            $this->session->data['success'] = $this->language->get('text_success');
153
154
            $url = '';
155
156
            if (isset($this->request->get['filter_name'])) {
157
                $url .= '&filter_name=' . urlencode(html_entity_decode($this->request->get['filter_name'], ENT_QUOTES, 'UTF-8'));
158
            }
159
160
            if (isset($this->request->get['filter_author'])) {
161
                $url .= '&filter_author=' . urlencode(html_entity_decode($this->request->get['filter_author'], ENT_QUOTES, 'UTF-8'));
162
            }
163
164
            if (isset($this->request->get['filter_category'])) {
165
                $url .= '&filter_category=' . $this->request->get['filter_category'];
166
            }
167
168
            if (isset($this->request->get['filter_status'])) {
169
                $url .= '&filter_status=' . $this->request->get['filter_status'];
170
            }
171
172
            if (isset($this->request->get['page'])) {
173
                $url .= '&page=' . $this->request->get['page'];
174
            }
175
176
            if (isset($this->request->get['sort'])) {
177
                $url .= '&sort=' . $this->request->get['sort'];
178
            }
179
180
            if (isset($this->request->get['order'])) {
181
                $url .= '&order=' . $this->request->get['order'];
182
            }
183
184
            $this->response->redirect($this->url->link('blog/post', 'token=' . $this->session->data['token'] . $url, 'SSL'));
185
        }
186
187
        $this->getList();
188
    }
189
190 View Code Duplication
    public function copy()
191
    {
192
        $this->load->language('blog/post');
193
194
        $this->document->setTitle($this->language->get('heading_title'));
195
196
        $this->load->model('blog/post');
197
198
        if (isset($this->request->post['selected']) && $this->validateCopy()) {
199
            foreach ($this->request->post['selected'] as $post_id) {
200
                $this->model_blog_post->copyProduct($post_id);
201
            }
202
203
            $this->session->data['success'] = $this->language->get('text_success');
204
205
            $url = '';
206
207
            if (isset($this->request->get['filter_name'])) {
208
                $url .= '&filter_name=' . urlencode(html_entity_decode($this->request->get['filter_name'], ENT_QUOTES, 'UTF-8'));
209
            }
210
211
            if (isset($this->request->get['filter_author'])) {
212
                $url .= '&filter_author=' . urlencode(html_entity_decode($this->request->get['filter_author'], ENT_QUOTES, 'UTF-8'));
213
            }
214
215
            if (isset($this->request->get['filter_category'])) {
216
                $url .= '&filter_category=' . $this->request->get['filter_category'];
217
            }
218
219
            if (isset($this->request->get['filter_status'])) {
220
                $url .= '&filter_status=' . $this->request->get['filter_status'];
221
            }
222
223
            if (isset($this->request->get['page'])) {
224
                $url .= '&page=' . $this->request->get['page'];
225
            }
226
227
            if (isset($this->request->get['sort'])) {
228
                $url .= '&sort=' . $this->request->get['sort'];
229
            }
230
231
            if (isset($this->request->get['order'])) {
232
                $url .= '&order=' . $this->request->get['order'];
233
            }
234
235
            $this->response->redirect($this->url->link('blog/post', 'token=' . $this->session->data['token'] . $url, 'SSL'));
236
        }
237
238
        $this->getList();
239
    }
240
241
    protected function getList()
242
    {
243
        if (isset($this->request->get['filter_name'])) {
244
            $filter_name = $this->request->get['filter_name'];
245
        } else {
246
            $filter_name = null;
247
        }
248
249
        if (isset($this->request->get['filter_author'])) {
250
            $filter_author = $this->request->get['filter_author'];
251
        } else {
252
            $filter_author = null;
253
        }
254
255
        if (isset($this->request->get['filter_category'])) {
256
            $filter_category = $this->request->get['filter_category'];
257
        } else {
258
            $filter_category = null;
259
        }
260
261
        if (isset($this->request->get['filter_status'])) {
262
            $filter_status = $this->request->get['filter_status'];
263
        } else {
264
            $filter_status = null;
265
        }
266
267
        if (isset($this->request->get['page'])) {
268
            $page = $this->request->get['page'];
269
        } else {
270
            $page = 1;
271
        }
272
273
        if (isset($this->request->get['sort'])) {
274
            $sort = $this->request->get['sort'];
275
        } else {
276
            $sort = 'p.date_added';
277
        }
278
279
        if (isset($this->request->get['order'])) {
280
            $order = $this->request->get['order'];
281
        } else {
282
            $order = 'DESC';
283
        }
284
285
        $url = '';
286
287
        if (isset($this->request->get['filter_name'])) {
288
            $url .= '&filter_name=' . urlencode(html_entity_decode($this->request->get['filter_name'], ENT_QUOTES, 'UTF-8'));
289
        }
290
291
        if (isset($this->request->get['filter_author'])) {
292
            $url .= '&filter_author=' . urlencode(html_entity_decode($this->request->get['filter_author'], ENT_QUOTES, 'UTF-8'));
293
        }
294
295
        if (isset($this->request->get['filter_category'])) {
296
            $url .= '&filter_category=' . $this->request->get['filter_category'];
297
        }
298
299
        if (isset($this->request->get['filter_status'])) {
300
            $url .= '&filter_status=' . $this->request->get['filter_status'];
301
        }
302
303
        if (isset($this->request->get['page'])) {
304
            $url .= '&page=' . $this->request->get['page'];
305
        }
306
307
        if (isset($this->request->get['sort'])) {
308
            $url .= '&sort=' . $this->request->get['sort'];
309
        }
310
311
        if (isset($this->request->get['order'])) {
312
            $url .= '&order=' . $this->request->get['order'];
313
        }
314
315
        #Get All Language Text
316
        $data = $this->language->all();
317
318
        $data['add']    = $this->url->link('blog/post/add', 'token=' . $this->session->data['token'], 'SSL');
319
        $data['delete'] = $this->url->link('blog/post/delete', 'token=' . $this->session->data['token'], 'SSL');
320
321
        $data['posts'] = array();
322
323
        $filter_data = array(
324
            'filter_name'     => $filter_name,
325
            'filter_author'   => $filter_author,
326
            'filter_category' => $filter_category,
327
            'filter_status'   => $filter_status,
328
            'sort'            => $sort,
329
            'order'           => $order,
330
            'start'           => ($page - 1) * $this->config->get('config_limit_admin'),
331
            'limit'           => $this->config->get('config_limit_admin')
332
        );
333
334
        $this->load->model('tool/image');
335
336
        $post_total = $this->model_blog_post->getTotalPosts();
337
338
        $results = $this->model_blog_post->getPosts($filter_data);
339
340
        $this->load->model('blog/category');
341
342
        $data['categories'] = $this->model_blog_category->getCategories(0);
343
344
        foreach ($results as $result) {
345
            $category = $this->model_blog_post->getPostCategories($result['post_id']);
346
347 View Code Duplication
            if (is_file(DIR_IMAGE . $result['image'])) {
348
                $image = $this->model_tool_image->resize($result['image'], 40, 40);
349
            } else {
350
                $image = $this->model_tool_image->resize('no_image.png', 40, 40);
351
            }
352
353
            $data['posts'][] = array(
354
                'post_id'       => $result['post_id'],
355
                'image'         => $image,
356
                'name'          => $result['name'],
357
                'category'      => $category,
358
                'comment_total' => $this->model_blog_post->getTotalCommentsByPostId($result['post_id']),
359
                'viewed'        => $result['viewed'],
360
                'date_added'    => $result['date_added'],
361
                'status'        => ($result['status']) ? $this->language->get('text_enabled') : $this->language->get('text_disabled'),
362
                'sort_order'    => $result['sort_order'],
363
                'edit'          => $this->url->link('blog/post/edit', 'token=' . $this->session->data['token'] . '&post_id=' . $result['post_id'] . $url, 'SSL')
364
            );
365
        }
366
367
        $data['token'] = $this->session->data['token'];
368
369
        if (isset($this->error['warning'])) {
370
            $data['error_warning'] = $this->error['warning'];
371
        } else {
372
            $data['error_warning'] = '';
373
        }
374
375 View Code Duplication
        if (isset($this->session->data['success'])) {
376
            $data['success'] = $this->session->data['success'];
377
378
            unset($this->session->data['success']);
379
        } else {
380
            $data['success'] = '';
381
        }
382
383
        if (isset($this->request->post['selected'])) {
384
            $data['selected'] = (array) $this->request->post['selected'];
385
        } else {
386
            $data['selected'] = array();
387
        }
388
389
        $url = '';
390
391
        if (isset($this->request->get['filter_name'])) {
392
            $url .= '&filter_name=' . urlencode(html_entity_decode($this->request->get['filter_name'], ENT_QUOTES, 'UTF-8'));
393
        }
394
395
        if (isset($this->request->get['filter_author'])) {
396
            $url .= '&filter_author=' . urlencode(html_entity_decode($this->request->get['filter_author'], ENT_QUOTES, 'UTF-8'));
397
        }
398
399
        if (isset($this->request->get['filter_category'])) {
400
            $url .= '&filter_category=' . $this->request->get['filter_category'];
401
        }
402
403
        if (isset($this->request->get['filter_status'])) {
404
            $url .= '&filter_status=' . $this->request->get['filter_status'];
405
        }
406
407
        if ($order == 'ASC') {
408
            $url .= '&order=DESC';
409
        } else {
410
            $url .= '&order=ASC';
411
        }
412
413
        if (isset($this->request->get['page'])) {
414
            $url .= '&page=' . $this->request->get['page'];
415
        }
416
417
        $data['sort_name']       = $this->url->link('blog/post', 'token=' . $this->session->data['token'] . '&sort=pd.name' . $url, 'SSL');
418
        $data['sort_category']   = $this->url->link('blog/post', 'token=' . $this->session->data['token'] . '&sort=cd.name' . $url, 'SSL');
419
        $data['sort_status']     = $this->url->link('blog/post', 'token=' . $this->session->data['token'] . '&sort=p.status' . $url, 'SSL');
420
        $data['sort_sort_order'] = $this->url->link('blog/post', 'token=' . $this->session->data['token'] . '&sort=p.sort_order' . $url, 'SSL');
421
        $data['sort_date_added'] = $this->url->link('blog/post', 'token=' . $this->session->data['token'] . '&sort=p.date_added' . $url, 'SSL');
422
423
        $url = '';
424
425
        if (isset($this->request->get['filter_name'])) {
426
            $url .= '&filter_name=' . urlencode(html_entity_decode($this->request->get['filter_name'], ENT_QUOTES, 'UTF-8'));
427
        }
428
429
        if (isset($this->request->get['filter_author'])) {
430
            $url .= '&filter_author=' . urlencode(html_entity_decode($this->request->get['filter_author'], ENT_QUOTES, 'UTF-8'));
431
        }
432
433
        if (isset($this->request->get['filter_category'])) {
434
            $url .= '&filter_category=' . $this->request->get['filter_category'];
435
        }
436
437
        if (isset($this->request->get['filter_status'])) {
438
            $url .= '&filter_status=' . $this->request->get['filter_status'];
439
        }
440
441
        if (isset($this->request->get['sort'])) {
442
            $url .= '&sort=' . $this->request->get['sort'];
443
        }
444
445
        if (isset($this->request->get['order'])) {
446
            $url .= '&order=' . $this->request->get['order'];
447
        }
448
449
        $pagination        = new Pagination();
450
        $pagination->total = $post_total;
451
        $pagination->page  = $page;
0 ignored issues
show
Documentation Bug introduced by
It seems like $page can also be of type double. However, the property $page is declared as type integer. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
452
        $pagination->limit = $this->config->get('config_limit_admin');
453
        $pagination->url   = $this->url->link('blog/post', 'token=' . $this->session->data['token'] . $url . '&page={page}', 'SSL');
454
455
        $data['pagination'] = $pagination->render();
456
457
        $data['results'] = sprintf($this->language->get('text_pagination'), ($post_total) ? (($page - 1) * $this->config->get('config_limit_admin')) + 1 : 0, ((($page - 1) * $this->config->get('config_limit_admin')) > ($post_total - $this->config->get('config_limit_admin'))) ? $post_total : ((($page - 1) * $this->config->get('config_limit_admin')) + $this->config->get('config_limit_admin')), $post_total, ceil($post_total / $this->config->get('config_limit_admin')));
458
459
        $data['filter_name']     = $filter_name;
460
        $data['filter_author']   = $filter_author;
461
        $data['filter_category'] = $filter_category;
462
        $data['filter_status']   = $filter_status;
463
464
        $data['sort']  = $sort;
465
        $data['order'] = $order;
466
467
        $data['sortable'] = (isset($this->request->get['sortable']) && $this->request->get['sortable'] == 'active') ? true : false;
468
469
        $data['header']      = $this->load->controller('common/header');
470
        $data['column_left'] = $this->load->controller('common/column_left');
471
        $data['footer']      = $this->load->controller('common/footer');
472
473
        $this->response->setOutput($this->load->view('blog/post_list.tpl', $data));
474
    }
475
476
    protected function getForm()
477
    {
478
        #Get All Language Text
479
        $data = $this->language->all();
480
481
        $data['text_form'] = !isset($this->request->get['post_id']) ? $this->language->get('text_add') : $this->language->get('text_edit');
482
483
        $data['token'] = $this->session->data['token'];
484
485
        if (isset($this->error['warning'])) {
486
            $data['error_warning'] = $this->error['warning'];
487
        } else {
488
            $data['error_warning'] = '';
489
        }
490
491
        if (isset($this->error['name'])) {
492
            $data['error_name'] = $this->error['name'];
493
        } else {
494
            $data['error_name'] = '';
495
        }
496
497 View Code Duplication
        if (isset($this->error['seo_url'])) {
498
            $data['error_seo_url'] = $this->error['seo_url'];
499
        } else {
500
            $data['error_seo_url'] = array();
501
        }
502
503 View Code Duplication
        if (isset($this->session->data['success'])) {
504
            $data['success'] = $this->session->data['success'];
505
506
            unset($this->session->data['success']);
507
        } else {
508
            $data['success'] = '';
509
        }
510
511
        $url = '';
512
513
        if (isset($this->request->get['filter_name'])) {
514
            $url .= '&filter_name=' . urlencode(html_entity_decode($this->request->get['filter_name'], ENT_QUOTES, 'UTF-8'));
515
        }
516
517
        if (isset($this->request->get['filter_author'])) {
518
            $url .= '&filter_author=' . urlencode(html_entity_decode($this->request->get['filter_author'], ENT_QUOTES, 'UTF-8'));
519
        }
520
521
        if (isset($this->request->get['filter_category'])) {
522
            $url .= '&filter_category=' . $this->request->get['filter_category'];
523
        }
524
525
        if (isset($this->request->get['filter_status'])) {
526
            $url .= '&filter_status=' . $this->request->get['filter_status'];
527
        }
528
529
        if (isset($this->request->get['page'])) {
530
            $url .= '&page=' . $this->request->get['page'];
531
        }
532
533
        if (isset($this->request->get['sort'])) {
534
            $url .= '&sort=' . $this->request->get['sort'];
535
        }
536
537
        if (isset($this->request->get['order'])) {
538
            $url .= '&order=' . $this->request->get['order'];
539
        }
540
541
        if (!isset($this->request->get['post_id'])) {
542
            $data['action'] = $this->url->link('blog/post/add', 'token=' . $this->session->data['token'] . $url, 'SSL');
543
        } else {
544
            $data['action'] = $this->url->link('blog/post/edit', 'token=' . $this->session->data['token'] . '&post_id=' . $this->request->get['post_id'] . $url, 'SSL');
545
        }
546
547
        $data['cancel'] = $this->url->link('blog/post', 'token=' . $this->session->data['token'] . $url, 'SSL');
548
549 View Code Duplication
        if (isset($this->request->get['post_id']) && ($this->request->server['REQUEST_METHOD'] != 'POST')) {
550
            $post_info = $this->model_blog_post->getPost($this->request->get['post_id']);
551
        }
552
553
        $this->load->model('localisation/language');
554
555
        $data['languages'] = $this->model_localisation_language->getLanguages();
556
557 View Code Duplication
        if (isset($this->request->post['post_description'])) {
558
            $data['post_description'] = $this->request->post['post_description'];
559
        } elseif (isset($this->request->get['post_id'])) {
560
            $data['post_description'] = $this->model_blog_post->getPostDescriptions($this->request->get['post_id']);
561
        } else {
562
            $data['post_description'] = array();
563
        }
564
565
        if (isset($this->request->post['status'])) {
566
            $data['status'] = $this->request->post['status'];
567
        } elseif (isset($post_info)) {
568
            $data['status'] = $post_info['status'];
569
        } else {
570
            $data['status'] = 1;
571
        }
572
573
        $this->load->model('user/user');
574
575
        $user_info = $this->model_user_user->getUser($this->user->getId());
576
577
        if ($user_info) {
578
            $data['author'] = $user_info['firstname'];
579
        }
580
581
        if (isset($this->request->post['author'])) {
582
            $data['author'] = $this->request->post['author'];
583
        } elseif (isset($post_info)) {
584
            $data['author'] = $post_info['author'];
585
        } else {
586
            $data['author'] = $user_info['firstname'];
587
        }
588
589
        if (isset($this->request->post['allow_comment'])) {
590
            $data['allow_comment'] = $this->request->post['allow_comment'];
591
        } elseif (isset($post_info)) {
592
            $data['allow_comment'] = $post_info['allow_comment'];
593
        } else {
594
            $data['allow_comment'] = 1;
595
        }
596
597
        if (isset($this->request->post['featured'])) {
598
            $data['featured'] = $this->request->post['featured'];
599
        } elseif (isset($post_info)) {
600
            $data['featured'] = $post_info['featured'];
601
        } else {
602
            $data['featured'] = 0;
603
        }
604
605 View Code Duplication
        if (isset($this->request->post['date_available'])) {
606
            $data['date_available'] = $this->request->post['date_available'];
607
        } elseif (!empty($post_info)) {
608
            $data['date_available'] = ($post_info['date_available'] != '0000-00-00') ? $post_info['date_available'] : '';
609
        } else {
610
            $data['date_available'] = date('Y-m-d');
611
        }
612
613
        $this->load->model('setting/store');
614
615
        $data['stores'] = $this->model_setting_store->getStores();
616
617
        if (isset($this->request->post['post_store'])) {
618
            $data['post_store'] = $this->request->post['post_store'];
619
        } elseif (isset($post_info)) {
620
            $data['post_store'] = $this->model_blog_post->getPostStores($this->request->get['post_id']);
621
        } else {
622
            $data['post_store'] = array(0);
623
        }
624
625
        if (isset($this->request->post['seo_url'])) {
626
            $data['seo_url'] = $this->request->post['seo_url'];
627
        } elseif (!empty($post_info)) {
628
            $data['seo_url'] = $post_info['seo_url'];
629
        } else {
630
            $data['seo_url'] = array();
631
        }
632
633
        if (isset($this->request->post['image'])) {
634
            $data['image'] = $this->request->post['image'];
635
        } elseif (isset($post_info)) {
636
            $data['image'] = $post_info['image'];
637
        } else {
638
            $data['image'] = '';
639
        }
640
641
        $this->load->model('tool/image');
642
643
        $data['placeholder'] = $this->model_tool_image->resize('no_image.png', 100, 100);
644
645
        if (isset($this->request->post['image']) && is_file(DIR_IMAGE . $this->request->post['image'])) {
646
            $data['thumb'] = $this->model_tool_image->resize($this->request->post['image'], 100, 100);
647
        } elseif (!empty($post_info) && is_file(DIR_IMAGE . $post_info['image'])) {
648
            $data['thumb'] = $this->model_tool_image->resize($post_info['image'], 100, 100);
649
        } else {
650
            $data['thumb'] = $this->model_tool_image->resize('no_image.png', 100, 100);
651
        }
652
653
        // Categories
654
        $this->load->model('blog/category');
655
656
        if (isset($this->request->post['post_category'])) {
657
            $categories = $this->request->post['post_category'];
658
        } elseif (isset($this->request->get['post_id'])) {
659
            $categories = $this->model_blog_post->getPostCategories($this->request->get['post_id']);
660
        } else {
661
            $categories = array();
662
        }
663
664
        $data['post_categories'] = array();
665
666 View Code Duplication
        foreach ($categories as $category_id) {
667
            $category_info = $this->model_blog_category->getCategory($category_id);
668
669
            if ($category_info) {
670
                $data['post_categories'][] = array(
671
                    'category_id' => $category_info['category_id'],
672
                    'name' => ($category_info['path']) ? $category_info['path'] . ' &gt; ' . $category_info['name'] : $category_info['name']
673
                );
674
            }
675
        }
676
677
        if (isset($this->request->post['sort_order'])) {
678
            $data['sort_order'] = $this->request->post['sort_order'];
679
        } elseif (isset($post_info)) {
680
            $data['sort_order'] = $post_info['sort_order'];
681
        } else {
682
            $data['sort_order'] = '';
683
        }
684
685
        if (isset($this->request->post['post_layout'])) {
686
            $data['post_layout'] = $this->request->post['post_layout'];
687
        } elseif (isset($post_info)) {
688
            $data['post_layout'] = $this->model_blog_post->getPostLayouts($this->request->get['post_id']);
689
        } else {
690
            $data['post_layout'] = array();
691
        }
692
693
        $data['post_id'] = isset($this->request->get['post_id']) ? $this->request->get['post_id'] : 0;
694
695
        // Preview link
696 View Code Duplication
        foreach ($data['languages'] as $language) {
697
            $data['preview'][$language['language_id']] = $this->getSeoLink($data['post_id'], $language['code']);
698
        }
699
700
        // Show All
701
        foreach ($data['languages'] as $language) {
702
            $data['show_all'][$language['language_id']] = array(
703
                'category' => $this->url->link('blog/post/viewAll', 'type=category&post_id=' . $data['post_id'] . '&language_id=' . $language['language_id'] . '&token=' . $this->session->data['token'], 'SSL'),
704
                'tag'      => $this->url->link('blog/post/viewAll', 'type=tag&post_id=' . $data['post_id'] . '&language_id=' . $language['language_id'] . '&token=' . $this->session->data['token'], 'SSL')
705
            );
706
        }
707
708
        $data['language_code'] = isset($this->session->data['admin_language']) ? $this->session->data['admin_language'] : $this->config->get('config_admin_language');
709
710
        $this->load->model('appearance/layout');
711
712
        $data['layouts'] = $this->model_appearance_layout->getLayouts();
713
714
        $data['header']      = $this->load->controller('common/header');
715
        $data['column_left'] = $this->load->controller('common/column_left');
716
        $data['footer']      = $this->load->controller('common/footer');
717
718
        $this->response->setOutput($this->load->view('blog/post_form.tpl', $data));
719
    }
720
721 View Code Duplication
    protected function validateForm()
722
    {
723
        if (!$this->user->hasPermission('modify', 'blog/post')) {
724
            $this->error['warning'] = $this->language->get('error_permission');
725
        }
726
727
        foreach ($this->request->post['post_description'] as $language_id => $value) {
728
            if ((utf8_strlen($value['name']) < 3) || (utf8_strlen($value['name']) > 255)) {
729
                $this->error['name'][$language_id] = $this->language->get('error_name');
730
            }
731
        }
732
733
        $this->load->model('catalog/url_alias');
734
735
        foreach ($this->request->post['seo_url'] as $language_id => $value) {
736
            $url_alias_info = $this->model_catalog_url_alias->getUrlAlias($value, $language_id);
737
738
            if ($url_alias_info && isset($this->request->get['post_id']) && $url_alias_info['query'] != 'blog_post_id=' . $this->request->get['post_id']) {
739
                $this->error['seo_url'][$language_id] = sprintf($this->language->get('error_seo_url'));
740
            }
741
742
            if ($url_alias_info && !isset($this->request->get['post_id'])) {
743
                $this->error['seo_url'][$language_id] = sprintf($this->language->get('error_seo_url'));
744
            }
745
        }
746
747
        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...
748
            $this->error['warning'] = $this->language->get('error_warning');
749
        }
750
751
        return !$this->error;
752
    }
753
754
    protected function validateDelete()
755
    {
756
        if (!$this->user->hasPermission('modify', 'blog/post')) {
757
            $this->error['warning'] = $this->language->get('error_permission');
758
        }
759
760
        return !$this->error;
761
    }
762
763
    protected function validateCopy()
764
    {
765
        if (!$this->user->hasPermission('modify', 'blog/post')) {
766
            $this->error['warning'] = $this->language->get('error_permission');
767
        }
768
769
        return !$this->error;
770
    }
771
772
    public function autocomplete()
773
    {
774
        $json = array();
775
776
        if (isset($this->request->get['filter_name'])) {
777
            $this->load->model('blog/post');
778
779
            $results = $this->model_blog_post->getPosts(0);
780
781
            foreach ($results as $result) {
782
                $json[] = array(
783
                    'post_id' => $result['post_id'],
784
                    'name'    => strip_tags(html_entity_decode($result['name'], ENT_QUOTES, 'UTF-8'))
785
                );
786
            }
787
        }
788
789
        $sort_order = array();
790
791
        foreach ($json as $key => $value) {
792
            $sort_order[$key] = $value['name'];
793
        }
794
795
        array_multisort($sort_order, SORT_ASC, $json);
796
797
        $this->response->addHeader('Content-Type: application/json');
798
        $this->response->setOutput(json_encode($json));
799
    }
800
801 View Code Duplication
    public function inline()
802
    {
803
        $json = array();
804
805
        if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateInline()) {
806
            $this->load->model('blog/post');
807
808
            if (isset($this->request->post['seo_url'])) {
809
                $this->load->model('catalog/url_alias');
810
811
                $this->model_catalog_url_alias->addAlias('blog_post', $this->request->get['post_id'], $this->request->post['seo_url'], $this->request->post['language_id']);
812
813
                $json['language_id'] = $this->request->post['language_id'];
814
            } else {
815
                foreach ($this->request->post as $key => $value) {
816
                    $this->model_blog_post->updatePost($this->request->get['post_id'], $key, $value);
817
                }
818
            }
819
        }
820
821
        $this->response->addHeader('Content-Type: application/json');
822
        $this->response->setOutput(json_encode($json));
823
    }
824
825 View Code Duplication
    protected function validateInline()
826
    {
827
        if (!$this->user->hasPermission('modify', 'blog/post')) {
828
            $this->error['warning'] = $this->language->get('error_permission');
829
        }
830
831
        if (!isset($this->request->post['image']) && !isset($this->request->post['name']) && !isset($this->request->post['price']) && !isset($this->request->post['special']) && !isset($this->request->post['quantity']) && !isset($this->request->post['status']) && !isset($this->request->post['seo_url'])) {
832
            $this->error['warning'] = $this->language->get('error_inline_field');
833
        }
834
835
        return !$this->error;
836
    }
837
838 View Code Duplication
    public function getSeoLink($post_id, $language_code)
839
    {
840
        $old_session_code = isset($this->session->data['language']) ? $this->session->data['language'] : '';
841
        $old_config_code  = $this->config->get('config_language');
842
843
        $this->session->data['language'] = $language_code;
844
        $this->config->set('config_language', $language_code);
845
846
        $url = $this->config->get('config_url');
847
848
        if (empty($url)) {
849
            $url = HTTP_SERVER;
850
851
            $admin_folder = str_replace(DIR_ROOT, '', DIR_ADMIN);
852
853
            $url = str_replace($admin_folder, '', $url);
854
        }
855
856
        $route = new Route($this->registry);
857
858
        $url .= ltrim($route->rewrite('index.php?route=blog/post&post_id=' . $post_id), '/');
859
860
        if (!empty($old_session_code)) {
861
            $this->session->data['language'] = $old_session_code;
862
        }
863
864
        $this->config->set('config_language', $old_config_code);
865
866
        return $url;
867
    }
868
869
    public function viewAll()
870
    {
871
        $this->load->language('blog/psot');
872
        $this->load->model('blog/post');
873
874
        $data['text_applicable'] = $this->language->get('text_applicable');
875
        $data['text_applied']    = $this->language->get('text_applied');
876
877
        $type = $this->request->get['type'];
878
879
        $data['type'] = $type;
880
881
        switch ($type) {
882 View Code Duplication
            case 'category':
883
                $data['text_all'] = $this->language->get('entry_all_category');
884
885
                $this->load->model('blog/category');
886
887
                $categories = $this->model_blog_post->getPostCategories($this->request->get['post_id']);
888
889
                foreach ($categories as $category_id) {
890
                    $category_info = $this->model_blog_category->getCategory($category_id);
891
892
                    if ($category_info) {
893
                        $data['applied'][] = $category_info['category_id'];
894
                    }
895
                }
896
897
                $data['all'] = $this->model_blog_category->getCategories();
898
                break;
899 View Code Duplication
            case 'tag':
900
                $data['text_all'] = $this->language->get('entry_all_tags');
901
902
                $tags = $this->model_blog_post->getPostTags($this->request->get['post_id']);
903
904
                $data['applied'] = (!empty($tags)) ? $tags : array();
905
906
                $data['all'] = $this->model_blog_post->getTags();
907
                break;
908
        }
909
910
        $this->load->model('localisation/language');
911
912
        $data['languages'] = $this->model_localisation_language->getLanguages();
913
914
        $this->response->setOutput($this->load->view('common/show_all.tpl', $data));
915
    }
916
}
917