Issues (2407)

controller/localisation/language.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 ControllerLocalisationLanguage 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/language');
30
31
        $this->document->setTitle($this->language->get('heading_title'));
32
33
        $this->load->model('localisation/language');
34
35
        $this->getList();
36
    }
37
38
    public function add()
39
    {
40
        $this->load->language('localisation/language');
41
42
        $this->document->setTitle($this->language->get('heading_title'));
43
44
        $this->load->model('localisation/language');
45
46
        if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) {
47
            $this->model_localisation_language->addLanguage($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/language', 'token=' . $this->session->data['token'] . $url, true));
66
        }
67
68
        $this->getForm();
69
    }
70
71
    public function edit()
72
    {
73
        $this->load->language('localisation/language');
74
75
        $this->document->setTitle($this->language->get('heading_title'));
76
77
        $this->load->model('localisation/language');
78
79
        if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) {
80
            $this->model_localisation_language->editLanguage($this->request->get['language_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/language', 'token=' . $this->session->data['token'] . $url, true));
99
        }
100
101
        $this->getForm();
102
    }
103
104
    public function delete()
105
    {
106
        $this->load->language('localisation/language');
107
108
        $this->document->setTitle($this->language->get('heading_title'));
109
110
        $this->load->model('localisation/language');
111
112
        if (isset($this->request->post['selected']) && $this->validateDelete()) {
113
            foreach ($this->request->post['selected'] as $language_id) {
114
                $this->model_localisation_language->deleteLanguage($language_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/language', 'token=' . $this->session->data['token'] . $url, true));
134
        }
135
136
        $this->getList();
137
    }
138
139
    protected function getList()
140
    {
141
        if (isset($this->request->get['sort'])) {
142
            $sort = $this->request->get['sort'];
143
        } else {
144
            $sort = 'name';
145
        }
146
147
        if (isset($this->request->get['order'])) {
148
            $order = $this->request->get['order'];
149
        } else {
150
            $order = 'ASC';
151
        }
152
153
        if (isset($this->request->get['page'])) {
154
            $page = $this->request->get['page'];
155
        } else {
156
            $page = 1;
157
        }
158
159
        $url = '';
160
161
        if (isset($this->request->get['sort'])) {
162
            $url .= '&sort=' . $this->request->get['sort'];
163
        }
164
165
        if (isset($this->request->get['order'])) {
166
            $url .= '&order=' . $this->request->get['order'];
167
        }
168
169
        if (isset($this->request->get['page'])) {
170
            $url .= '&page=' . $this->request->get['page'];
171
        }
172
173
        $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...
174
175
        $data['breadcrumbs'][] = array(
176
            'text' => $this->language->get('text_home'),
177
            'href' => $this->url->link('common/dashboard', 'token=' . $this->session->data['token'], true)
178
        );
179
180
        $data['breadcrumbs'][] = array(
181
            'text' => $this->language->get('heading_title'),
182
            'href' => $this->url->link('localisation/language', 'token=' . $this->session->data['token'] . $url, true)
183
        );
184
185
        $data['add'] = $this->url->link('localisation/language/add', 'token=' . $this->session->data['token'] . $url, true);
186
        $data['delete'] = $this->url->link('localisation/language/delete', 'token=' . $this->session->data['token'] . $url, true);
187
188
        $data['languages'] = array();
189
190
        $filter_data = array(
191
            'sort'  => $sort,
192
            'order' => $order,
193
            'start' => ($page - 1) * $this->config->get('config_limit_admin'),
194
            'limit' => $this->config->get('config_limit_admin')
195
        );
196
197
        $language_total = $this->model_localisation_language->getTotalLanguages();
198
199
        $results = $this->model_localisation_language->getLanguages($filter_data);
200
201
        foreach ($results as $result) {
202
            $data['languages'][] = array(
203
                'language_id' => $result['language_id'],
204
                'name'        => $result['name'] . (($result['code'] == $this->config->get('config_language')) ? $this->language->get('text_default') : null),
205
                'code'        => $result['code'],
206
                'sort_order'  => $result['sort_order'],
207
                'edit'        => $this->url->link('localisation/language/edit', 'token=' . $this->session->data['token'] . '&language_id=' . $result['language_id'] . $url, true)
208
            );
209
        }
210
211
        $data['heading_title'] = $this->language->get('heading_title');
212
213
        $data['text_list'] = $this->language->get('text_list');
214
        $data['text_no_results'] = $this->language->get('text_no_results');
215
        $data['text_confirm'] = $this->language->get('text_confirm');
216
217
        $data['column_name'] = $this->language->get('column_name');
218
        $data['column_code'] = $this->language->get('column_code');
219
        $data['column_sort_order'] = $this->language->get('column_sort_order');
220
        $data['column_action'] = $this->language->get('column_action');
221
222
        $data['button_add'] = $this->language->get('button_add');
223
        $data['button_edit'] = $this->language->get('button_edit');
224
        $data['button_delete'] = $this->language->get('button_delete');
225
226
        if (isset($this->error['warning'])) {
227
            $data['error_warning'] = $this->error['warning'];
228
        } else {
229
            $data['error_warning'] = '';
230
        }
231
232
        if (isset($this->session->data['success'])) {
233
            $data['success'] = $this->session->data['success'];
234
235
            unset($this->session->data['success']);
236
        } else {
237
            $data['success'] = '';
238
        }
239
240
        if (isset($this->request->post['selected'])) {
241
            $data['selected'] = (array)$this->request->post['selected'];
242
        } else {
243
            $data['selected'] = array();
244
        }
245
246
        $url = '';
247
248
        if ($order == 'ASC') {
249
            $url .= '&order=DESC';
250
        } else {
251
            $url .= '&order=ASC';
252
        }
253
254
        if (isset($this->request->get['page'])) {
255
            $url .= '&page=' . $this->request->get['page'];
256
        }
257
258
        $data['sort_name'] = $this->url->link('localisation/language', 'token=' . $this->session->data['token'] . '&sort=name' . $url, true);
259
        $data['sort_code'] = $this->url->link('localisation/language', 'token=' . $this->session->data['token'] . '&sort=code' . $url, true);
260
        $data['sort_sort_order'] = $this->url->link('localisation/language', 'token=' . $this->session->data['token'] . '&sort=sort_order' . $url, true);
261
262
        $url = '';
263
264
        if (isset($this->request->get['sort'])) {
265
            $url .= '&sort=' . $this->request->get['sort'];
266
        }
267
268
        if (isset($this->request->get['order'])) {
269
            $url .= '&order=' . $this->request->get['order'];
270
        }
271
272
        $pagination = new \Divine\Engine\Library\Pagination();
273
        $pagination->total = $language_total;
274
        $pagination->page = $page;
275
        $pagination->limit = $this->config->get('config_limit_admin');
276
        $pagination->url = $this->url->link('localisation/language', 'token=' . $this->session->data['token'] . $url . '&page={page}', true);
277
278
        $data['pagination'] = $pagination->render();
279
280
        $data['results'] = sprintf($this->language->get('text_pagination'), ($language_total) ? (($page - 1) * $this->config->get('config_limit_admin')) + 1 : 0, ((($page - 1) * $this->config->get('config_limit_admin')) > ($language_total - $this->config->get('config_limit_admin'))) ? $language_total : ((($page - 1) * $this->config->get('config_limit_admin')) + $this->config->get('config_limit_admin')), $language_total, ceil($language_total / $this->config->get('config_limit_admin')));
281
282
        $data['sort'] = $sort;
283
        $data['order'] = $order;
284
        $data['header'] = $this->load->controller('common/header');
285
        $data['column'] = $this->load->controller('common/column_left');
286
        $data['footer'] = $this->load->controller('common/footer');
287
288
        $this->response->setOutput($this->load->view('localisation/language_list', $data));
289
    }
290
291
    protected function getForm()
292
    {
293
        $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...
294
295
        $data['text_form'] = !isset($this->request->get['language_id']) ? $this->language->get('text_add') : $this->language->get('text_edit');
296
        $data['text_enabled'] = $this->language->get('text_enabled');
297
        $data['text_disabled'] = $this->language->get('text_disabled');
298
299
        $data['entry_name'] = $this->language->get('entry_name');
300
        $data['entry_code'] = $this->language->get('entry_code');
301
        $data['entry_locale'] = $this->language->get('entry_locale');
302
        $data['entry_sort_order'] = $this->language->get('entry_sort_order');
303
        $data['entry_status'] = $this->language->get('entry_status');
304
305
        $data['help_locale'] = $this->language->get('help_locale');
306
        $data['help_status'] = $this->language->get('help_status');
307
308
        $data['button_save'] = $this->language->get('button_save');
309
        $data['button_cancel'] = $this->language->get('button_cancel');
310
311
        if (isset($this->error['warning'])) {
312
            $data['error_warning'] = $this->error['warning'];
313
        } else {
314
            $data['error_warning'] = '';
315
        }
316
317
        if (isset($this->error['name'])) {
318
            $data['error_name'] = $this->error['name'];
319
        } else {
320
            $data['error_name'] = '';
321
        }
322
323
        if (isset($this->error['code'])) {
324
            $data['error_code'] = $this->error['code'];
325
        } else {
326
            $data['error_code'] = '';
327
        }
328
        
329
        if (isset($this->error['locale'])) {
330
            $data['error_locale'] = $this->error['locale'];
331
        } else {
332
            $data['error_locale'] = '';
333
        }
334
        
335
        $url = '';
336
337
        if (isset($this->request->get['sort'])) {
338
            $url .= '&sort=' . $this->request->get['sort'];
339
        }
340
341
        if (isset($this->request->get['order'])) {
342
            $url .= '&order=' . $this->request->get['order'];
343
        }
344
345
        if (isset($this->request->get['page'])) {
346
            $url .= '&page=' . $this->request->get['page'];
347
        }
348
349
        $data['breadcrumbs'] = array();
350
351
        $data['breadcrumbs'][] = array(
352
            'text' => $this->language->get('text_home'),
353
            'href' => $this->url->link('common/dashboard', 'token=' . $this->session->data['token'], true)
354
        );
355
356
        $data['breadcrumbs'][] = array(
357
            'text' => $this->language->get('heading_title'),
358
            'href' => $this->url->link('localisation/language', 'token=' . $this->session->data['token'] . $url, true)
359
        );
360
361
        if (!isset($this->request->get['language_id'])) {
362
            $data['action'] = $this->url->link('localisation/language/add', 'token=' . $this->session->data['token'] . $url, true);
363
        } else {
364
            $data['action'] = $this->url->link('localisation/language/edit', 'token=' . $this->session->data['token'] . '&language_id=' . $this->request->get['language_id'] . $url, true);
365
        }
366
367
        $data['cancel'] = $this->url->link('localisation/language', 'token=' . $this->session->data['token'] . $url, true);
368
369
        if (isset($this->request->get['language_id']) && ($this->request->server['REQUEST_METHOD'] != 'POST')) {
370
            $language_info = $this->model_localisation_language->getLanguage($this->request->get['language_id']);
371
        }
372
373
        if (isset($this->request->post['name'])) {
374
            $data['name'] = $this->request->post['name'];
375
        } elseif (!empty($language_info)) {
376
            $data['name'] = $language_info['name'];
377
        } else {
378
            $data['name'] = '';
379
        }
380
381
        if (isset($this->request->post['code'])) {
382
            $data['code'] = $this->request->post['code'];
383
        } elseif (!empty($language_info)) {
384
            $data['code'] = $language_info['code'];
385
        } else {
386
            $data['code'] = '';
387
        }
388
        
389
        $data['languages'] = array();
390
        
391
        $folders = glob(SR_LANGUAGE . '*', GLOB_ONLYDIR);
392
393
        foreach ($folders as $folder) {
394
            $data['languages'][] = basename($folder);
395
        }
396
397
        if (isset($this->request->post['locale'])) {
398
            $data['locale'] = $this->request->post['locale'];
399
        } elseif (!empty($language_info)) {
400
            $data['locale'] = $language_info['locale'];
401
        } else {
402
            $data['locale'] = '';
403
        }
404
        
405
        if (isset($this->request->post['sort_order'])) {
406
            $data['sort_order'] = $this->request->post['sort_order'];
407
        } elseif (!empty($language_info)) {
408
            $data['sort_order'] = $language_info['sort_order'];
409
        } else {
410
            $data['sort_order'] = 1;
411
        }
412
413
        if (isset($this->request->post['status'])) {
414
            $data['status'] = $this->request->post['status'];
415
        } elseif (!empty($language_info)) {
416
            $data['status'] = $language_info['status'];
417
        } else {
418
            $data['status'] = true;
419
        }
420
421
        $data['header'] = $this->load->controller('common/header');
422
        $data['column'] = $this->load->controller('common/column_left');
423
        $data['footer'] = $this->load->controller('common/footer');
424
425
        $this->response->setOutput($this->load->view('localisation/language_form', $data));
426
    }
427
428
    protected function validateForm()
429
    {
430
        if (!$this->user->hasPermission('modify', 'localisation/language')) {
431
            $this->error['warning'] = $this->language->get('error_permission');
432
        }
433
434
        if ((\voku\helper\UTF8::strlen($this->request->post['name']) < 3) || (\voku\helper\UTF8::strlen($this->request->post['name']) > 32)) {
435
            $this->error['name'] = $this->language->get('error_name');
436
        }
437
438
        if (\voku\helper\UTF8::strlen($this->request->post['code']) < 2) {
439
            $this->error['code'] = $this->language->get('error_code');
440
        }
441
        
442
        if (!$this->request->post['locale']) {
443
            $this->error['locale'] = $this->language->get('error_locale');
444
        }
445
        
446
        $language_info = $this->model_localisation_language->getLanguageByCode($this->request->post['code']);
447
448
        if (!isset($this->request->get['language_id'])) {
449
            if ($language_info) {
450
                $this->error['warning'] = $this->language->get('error_exists');
451
            }
452
        } else {
453
            if ($language_info && ($this->request->get['language_id'] != $language_info['language_id'])) {
454
                $this->error['warning'] = $this->language->get('error_exists');
455
            }
456
        }
457
458
        return !$this->error;
459
    }
460
461
    protected function validateDelete()
462
    {
463
        if (!$this->user->hasPermission('modify', 'localisation/language')) {
464
            $this->error['warning'] = $this->language->get('error_permission');
465
        }
466
467
        $this->load->model('sale/order');
468
469
        foreach ($this->request->post['selected'] as $language_id) {
470
            $language_info = $this->model_localisation_language->getLanguage($language_id);
471
472
            if ($language_info) {
473
                if ($this->config->get('config_language') == $language_info['code']) {
474
                    $this->error['warning'] = $this->language->get('error_default');
475
                }
476
477
                if ($this->config->get('config_admin_language') == $language_info['code']) {
478
                    $this->error['warning'] = $this->language->get('error_admin');
479
                }
480
            }
481
482
            $order_total = $this->model_sale_order->getTotalOrdersByLanguageId($language_id);
483
484
            if ($order_total) {
485
                $this->error['warning'] = sprintf($this->language->get('error_order'), $order_total);
486
            }
487
        }
488
489
        return !$this->error;
490
    }
491
}
492