Issues (2407)

controller/localisation/currency.php (4 issues)

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 ControllerLocalisationCurrency 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
Expected 2 blank lines before function; 1 found
Loading history...
28
    {
29
        $this->load->language('localisation/currency');
30
31
        $this->document->setTitle($this->language->get('heading_title'));
32
33
        $this->load->model('localisation/currency');
34
35
        $this->getList();
36
    }
37
38
    public function add()
39
    {
40
        $this->load->language('localisation/currency');
41
42
        $this->document->setTitle($this->language->get('heading_title'));
43
44
        $this->load->model('localisation/currency');
45
46
        if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) {
47
            $this->model_localisation_currency->addCurrency($this->request->post);
48
49
            $this->session->data['success'] = $this->language->get('text_success');
50
51
            $url = '';
52
53
            if (isset($this->request->get['sort'])) {
54
                $url .= '&sort=' . $this->request->get['sort'];
55
            }
56
57
            if (isset($this->request->get['order'])) {
58
                $url .= '&order=' . $this->request->get['order'];
59
            }
60
61
            if (isset($this->request->get['page'])) {
62
                $url .= '&page=' . $this->request->get['page'];
63
            }
64
65
            $this->response->redirect($this->url->link('localisation/currency', 'token=' . $this->session->data['token'] . $url, true));
66
        }
67
68
        $this->getForm();
69
    }
70
71
    public function edit()
72
    {
73
        $this->load->language('localisation/currency');
74
75
        $this->document->setTitle($this->language->get('heading_title'));
76
77
        $this->load->model('localisation/currency');
78
79
        if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) {
80
            $this->model_localisation_currency->editCurrency($this->request->get['currency_id'], $this->request->post);
81
82
            $this->session->data['success'] = $this->language->get('text_success');
83
84
            $url = '';
85
86
            if (isset($this->request->get['sort'])) {
87
                $url .= '&sort=' . $this->request->get['sort'];
88
            }
89
90
            if (isset($this->request->get['order'])) {
91
                $url .= '&order=' . $this->request->get['order'];
92
            }
93
94
            if (isset($this->request->get['page'])) {
95
                $url .= '&page=' . $this->request->get['page'];
96
            }
97
98
            $this->response->redirect($this->url->link('localisation/currency', 'token=' . $this->session->data['token'] . $url, true));
99
        }
100
101
        $this->getForm();
102
    }
103
104
    public function delete()
105
    {
106
        $this->load->language('localisation/currency');
107
108
        $this->document->setTitle($this->language->get('heading_title'));
109
110
        $this->load->model('localisation/currency');
111
112
        if (isset($this->request->post['selected']) && $this->validateDelete()) {
113
            foreach ($this->request->post['selected'] as $currency_id) {
114
                $this->model_localisation_currency->deleteCurrency($currency_id);
115
            }
116
117
            $this->session->data['success'] = $this->language->get('text_success');
118
119
            $url = '';
120
121
            if (isset($this->request->get['sort'])) {
122
                $url .= '&sort=' . $this->request->get['sort'];
123
            }
124
125
            if (isset($this->request->get['order'])) {
126
                $url .= '&order=' . $this->request->get['order'];
127
            }
128
129
            if (isset($this->request->get['page'])) {
130
                $url .= '&page=' . $this->request->get['page'];
131
            }
132
133
            $this->response->redirect($this->url->link('localisation/currency', 'token=' . $this->session->data['token'] . $url, true));
134
        }
135
136
        $this->getList();
137
    }
138
139
    public function refresh()
140
    {
141
        $this->load->language('localisation/currency');
142
143
        $this->document->setTitle($this->language->get('heading_title'));
144
145
        $this->load->model('localisation/currency');
146
147
        if ($this->validateRefresh()) {
148
            $this->model_localisation_currency->refresh(true);
149
150
            $this->session->data['success'] = $this->language->get('text_success');
151
152
            $url = '';
153
154
            if (isset($this->request->get['sort'])) {
155
                $url .= '&sort=' . $this->request->get['sort'];
156
            }
157
158
            if (isset($this->request->get['order'])) {
159
                $url .= '&order=' . $this->request->get['order'];
160
            }
161
162
            if (isset($this->request->get['page'])) {
163
                $url .= '&page=' . $this->request->get['page'];
164
            }
165
166
            $this->response->redirect($this->url->link('localisation/currency', 'token=' . $this->session->data['token'] . $url, true));
167
        }
168
169
        $this->getList();
170
    }
171
172
    protected function getList()
173
    {
174
        if (isset($this->request->get['sort'])) {
175
            $sort = $this->request->get['sort'];
176
        } else {
177
            $sort = 'title';
178
        }
179
180
        if (isset($this->request->get['order'])) {
181
            $order = $this->request->get['order'];
182
        } else {
183
            $order = 'ASC';
184
        }
185
186
        if (isset($this->request->get['page'])) {
187
            $page = $this->request->get['page'];
188
        } else {
189
            $page = 1;
190
        }
191
192
        $url = '';
193
194
        if (isset($this->request->get['sort'])) {
195
            $url .= '&sort=' . $this->request->get['sort'];
196
        }
197
198
        if (isset($this->request->get['order'])) {
199
            $url .= '&order=' . $this->request->get['order'];
200
        }
201
202
        if (isset($this->request->get['page'])) {
203
            $url .= '&page=' . $this->request->get['page'];
204
        }
205
206
        $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...
207
208
        $data['breadcrumbs'][] = array(
209
            'text' => $this->language->get('text_home'),
210
            'href' => $this->url->link('common/dashboard', 'token=' . $this->session->data['token'], true)
211
        );
212
213
        $data['breadcrumbs'][] = array(
214
            'text' => $this->language->get('heading_title'),
215
            'href' => $this->url->link('localisation/currency', 'token=' . $this->session->data['token'] . $url, true)
216
        );
217
218
        $data['add'] = $this->url->link('localisation/currency/add', 'token=' . $this->session->data['token'] . $url, true);
219
        $data['delete'] = $this->url->link('localisation/currency/delete', 'token=' . $this->session->data['token'] . $url, true);
220
        $data['refresh'] = $this->url->link('localisation/currency/refresh', 'token=' . $this->session->data['token'] . $url, true);
221
222
        $data['currencies'] = array();
223
224
        $filter_data = array(
225
            'sort'  => $sort,
226
            'order' => $order,
227
            'start' => ($page - 1) * $this->config->get('config_limit_admin'),
228
            'limit' => $this->config->get('config_limit_admin')
229
        );
230
231
        $currency_total = $this->model_localisation_currency->getTotalCurrencies();
232
233
        $results = $this->model_localisation_currency->getCurrencies($filter_data);
234
235
        foreach ($results as $result) {
236
            $data['currencies'][] = array(
237
                'currency_id'   => $result['currency_id'],
238
                'title'         => $result['title'] . (($result['code'] == $this->config->get('config_currency')) ? $this->language->get('text_default') : null),
239
                'code'          => $result['code'],
240
                'value'         => $result['value'],
241
                'date_modified' => date($this->language->get('date_format_short'), strtotime($result['date_modified'])),
242
                'edit'          => $this->url->link('localisation/currency/edit', 'token=' . $this->session->data['token'] . '&currency_id=' . $result['currency_id'] . $url, true)
243
            );
244
        }
245
246
        $data['heading_title'] = $this->language->get('heading_title');
247
248
        $data['text_list'] = $this->language->get('text_list');
249
        $data['text_no_results'] = $this->language->get('text_no_results');
250
        $data['text_confirm'] = $this->language->get('text_confirm');
251
252
        $data['column_title'] = $this->language->get('column_title');
253
        $data['column_code'] = $this->language->get('column_code');
254
        $data['column_value'] = $this->language->get('column_value');
255
        $data['column_date_modified'] = $this->language->get('column_date_modified');
256
        $data['column_action'] = $this->language->get('column_action');
257
258
        $data['button_add'] = $this->language->get('button_add');
259
        $data['button_edit'] = $this->language->get('button_edit');
260
        $data['button_delete'] = $this->language->get('button_delete');
261
        $data['button_currency'] = $this->language->get('button_currency');
262
263
        if (isset($this->error['warning'])) {
264
            $data['error_warning'] = $this->error['warning'];
265
        } else {
266
            $data['error_warning'] = '';
267
        }
268
269
        if (isset($this->session->data['success'])) {
270
            $data['success'] = $this->session->data['success'];
271
272
            unset($this->session->data['success']);
273
        } else {
274
            $data['success'] = '';
275
        }
276
277
        if (isset($this->request->post['selected'])) {
278
            $data['selected'] = (array)$this->request->post['selected'];
279
        } else {
280
            $data['selected'] = array();
281
        }
282
283
        $url = '';
284
285
        if ($order == 'ASC') {
286
            $url .= '&order=DESC';
287
        } else {
288
            $url .= '&order=ASC';
289
        }
290
291
        if (isset($this->request->get['page'])) {
292
            $url .= '&page=' . $this->request->get['page'];
293
        }
294
295
        $data['sort_title'] = $this->url->link('localisation/currency', 'token=' . $this->session->data['token'] . '&sort=title' . $url, true);
296
        $data['sort_code'] = $this->url->link('localisation/currency', 'token=' . $this->session->data['token'] . '&sort=code' . $url, true);
297
        $data['sort_value'] = $this->url->link('localisation/currency', 'token=' . $this->session->data['token'] . '&sort=value' . $url, true);
298
        $data['sort_date_modified'] = $this->url->link('localisation/currency', 'token=' . $this->session->data['token'] . '&sort=date_modified' . $url, true);
299
300
        $url = '';
301
302
        if (isset($this->request->get['sort'])) {
303
            $url .= '&sort=' . $this->request->get['sort'];
304
        }
305
306
        if (isset($this->request->get['order'])) {
307
            $url .= '&order=' . $this->request->get['order'];
308
        }
309
310
        $pagination = new \Divine\Engine\Library\Pagination();
311
        $pagination->total = $currency_total;
312
        $pagination->page = $page;
313
        $pagination->limit = $this->config->get('config_limit_admin');
314
        $pagination->url = $this->url->link('localisation/currency', 'token=' . $this->session->data['token'] . $url . '&page={page}', true);
315
316
        $data['pagination'] = $pagination->render();
317
318
        $data['results'] = sprintf($this->language->get('text_pagination'), ($currency_total) ? (($page - 1) * $this->config->get('config_limit_admin')) + 1 : 0, ((($page - 1) * $this->config->get('config_limit_admin')) > ($currency_total - $this->config->get('config_limit_admin'))) ? $currency_total : ((($page - 1) * $this->config->get('config_limit_admin')) + $this->config->get('config_limit_admin')), $currency_total, ceil($currency_total / $this->config->get('config_limit_admin')));
319
320
        $data['sort'] = $sort;
321
        $data['order'] = $order;
322
323
        $data['header'] = $this->load->controller('common/header');
324
        $data['column'] = $this->load->controller('common/column_left');
325
        $data['footer'] = $this->load->controller('common/footer');
326
327
        $this->response->setOutput($this->load->view('localisation/currency_list', $data));
328
    }
329
330
    protected function getForm()
331
    {
332
        $data['heading_title'] = $this->language->get('heading_title');
0 ignored issues
show
Comprehensibility Best Practice introduced by
$data was never initialized. Although not strictly required by PHP, it is generally a good practice to add $data = array(); before regardless.
Loading history...
333
334
        $data['text_form'] = !isset($this->request->get['currency_id']) ? $this->language->get('text_add') : $this->language->get('text_edit');
335
        $data['text_enabled'] = $this->language->get('text_enabled');
336
        $data['text_disabled'] = $this->language->get('text_disabled');
337
        $data['text_iso'] = $this->language->get('text_iso');
338
        
339
        $data['entry_title'] = $this->language->get('entry_title');
340
        $data['entry_code'] = $this->language->get('entry_code');
341
        $data['entry_value'] = $this->language->get('entry_value');
342
        $data['entry_symbol_left'] = $this->language->get('entry_symbol_left');
343
        $data['entry_symbol_right'] = $this->language->get('entry_symbol_right');
344
        $data['entry_decimal_place'] = $this->language->get('entry_decimal_place');
345
        $data['entry_status'] = $this->language->get('entry_status');
346
        
347
        $data['help_code'] = $this->language->get('help_code');
348
        $data['help_value'] = $this->language->get('help_value');
349
350
        $data['button_save'] = $this->language->get('button_save');
351
        $data['button_cancel'] = $this->language->get('button_cancel');
352
353
        $data['tab_general'] = $this->language->get('tab_general');
354
355
        if (isset($this->error['warning'])) {
356
            $data['error_warning'] = $this->error['warning'];
357
        } else {
358
            $data['error_warning'] = '';
359
        }
360
361
        if (isset($this->error['title'])) {
362
            $data['error_title'] = $this->error['title'];
363
        } else {
364
            $data['error_title'] = '';
365
        }
366
367
        if (isset($this->error['code'])) {
368
            $data['error_code'] = $this->error['code'];
369
        } else {
370
            $data['error_code'] = '';
371
        }
372
373
        $url = '';
374
375
        if (isset($this->request->get['sort'])) {
376
            $url .= '&sort=' . $this->request->get['sort'];
377
        }
378
379
        if (isset($this->request->get['order'])) {
380
            $url .= '&order=' . $this->request->get['order'];
381
        }
382
383
        if (isset($this->request->get['page'])) {
384
            $url .= '&page=' . $this->request->get['page'];
385
        }
386
387
        $data['breadcrumbs'] = array();
388
389
        $data['breadcrumbs'][] = array(
390
            'text' => $this->language->get('text_home'),
391
            'href' => $this->url->link('common/dashboard', 'token=' . $this->session->data['token'], true)
392
        );
393
394
        $data['breadcrumbs'][] = array(
395
            'text' => $this->language->get('heading_title'),
396
            'href' => $this->url->link('localisation/currency', 'token=' . $this->session->data['token'] . $url, true)
397
        );
398
399
        if (!isset($this->request->get['currency_id'])) {
400
            $data['action'] = $this->url->link('localisation/currency/add', 'token=' . $this->session->data['token'] . $url, true);
401
        } else {
402
            $data['action'] = $this->url->link('localisation/currency/edit', 'token=' . $this->session->data['token'] . '&currency_id=' . $this->request->get['currency_id'] . $url, true);
403
        }
404
405
        $data['cancel'] = $this->url->link('localisation/currency', 'token=' . $this->session->data['token'] . $url, true);
406
407
        if (isset($this->request->get['currency_id']) && ($this->request->server['REQUEST_METHOD'] != 'POST')) {
408
            $currency_info = $this->model_localisation_currency->getCurrency($this->request->get['currency_id']);
409
        }
410
411
        if (isset($this->request->post['title'])) {
412
            $data['title'] = $this->request->post['title'];
413
        } elseif (!empty($currency_info)) {
414
            $data['title'] = $currency_info['title'];
415
        } else {
416
            $data['title'] = '';
417
        }
418
419
        if (isset($this->request->post['code'])) {
420
            $data['code'] = $this->request->post['code'];
421
        } elseif (!empty($currency_info)) {
422
            $data['code'] = $currency_info['code'];
423
        } else {
424
            $data['code'] = '';
425
        }
426
427
        if (isset($this->request->post['symbol_left'])) {
428
            $data['symbol_left'] = $this->request->post['symbol_left'];
429
        } elseif (!empty($currency_info)) {
430
            $data['symbol_left'] = $currency_info['symbol_left'];
431
        } else {
432
            $data['symbol_left'] = '';
433
        }
434
435
        if (isset($this->request->post['symbol_right'])) {
436
            $data['symbol_right'] = $this->request->post['symbol_right'];
437
        } elseif (!empty($currency_info)) {
438
            $data['symbol_right'] = $currency_info['symbol_right'];
439
        } else {
440
            $data['symbol_right'] = '';
441
        }
442
443
        if (isset($this->request->post['decimal_place'])) {
444
            $data['decimal_place'] = $this->request->post['decimal_place'];
445
        } elseif (!empty($currency_info)) {
446
            $data['decimal_place'] = $currency_info['decimal_place'];
447
        } else {
448
            $data['decimal_place'] = '';
449
        }
450
451
        if (isset($this->request->post['value'])) {
452
            $data['value'] = $this->request->post['value'];
453
        } elseif (!empty($currency_info)) {
454
            $data['value'] = $currency_info['value'];
455
        } else {
456
            $data['value'] = '';
457
        }
458
459
        if (isset($this->request->post['status'])) {
460
            $data['status'] = $this->request->post['status'];
461
        } elseif (!empty($currency_info)) {
462
            $data['status'] = $currency_info['status'];
463
        } else {
464
            $data['status'] = '';
465
        }
466
467
        $data['header'] = $this->load->controller('common/header');
468
        $data['column'] = $this->load->controller('common/column_left');
469
        $data['footer'] = $this->load->controller('common/footer');
470
471
        $this->response->setOutput($this->load->view('localisation/currency_form', $data));
472
    }
473
474
    protected function validateForm()
475
    {
476
        if (!$this->user->hasPermission('modify', 'localisation/currency')) {
477
            $this->error['warning'] = $this->language->get('error_permission');
478
        }
479
480
        if ((\voku\helper\UTF8::strlen($this->request->post['title']) < 3) || (\voku\helper\UTF8::strlen($this->request->post['title']) > 32)) {
481
            $this->error['title'] = $this->language->get('error_title');
482
        }
483
484
        if (\voku\helper\UTF8::strlen($this->request->post['code']) != 3) {
485
            $this->error['code'] = $this->language->get('error_code');
486
        }
487
488
        return !$this->error;
489
    }
490
491
    protected function validateDelete()
492
    {
493
        if (!$this->user->hasPermission('modify', 'localisation/currency')) {
494
            $this->error['warning'] = $this->language->get('error_permission');
495
        }
496
497
        $this->load->model('sale/order');
498
499
        foreach ($this->request->post['selected'] as $currency_id) {
500
            $currency_info = $this->model_localisation_currency->getCurrency($currency_id);
501
502
            if ($currency_info) {
503
                if ($this->config->get('config_currency') == $currency_info['code']) {
504
                    $this->error['warning'] = $this->language->get('error_default');
505
                }
506
            }
507
508
            $order_total = $this->model_sale_order->getTotalOrdersByCurrencyId($currency_id);
509
510
            if ($order_total) {
511
                $this->error['warning'] = sprintf($this->language->get('error_order'), $order_total);
512
            }
513
        }
514
515
        return !$this->error;
516
    }
517
518
    protected function validateRefresh()
519
    {
520
        if (!$this->user->hasPermission('modify', 'localisation/currency')) {
521
            $this->error['warning'] = $this->language->get('error_permission');
522
        }
523
524
        return !$this->error;
525
    }
526
}
527