1: <?php
2: namespace Opencart\Admin\Controller\Localisation;
3: /**
4: * Class Language
5: *
6: * @package Opencart\Admin\Controller\Localisation
7: */
8: class Language extends \Opencart\System\Engine\Controller {
9: /**
10: * Index
11: *
12: * @return void
13: */
14: public function index(): void {
15: $this->load->language('localisation/language');
16:
17: $this->document->setTitle($this->language->get('heading_title'));
18:
19: $url = '';
20:
21: if (isset($this->request->get['sort'])) {
22: $url .= '&sort=' . $this->request->get['sort'];
23: }
24:
25: if (isset($this->request->get['order'])) {
26: $url .= '&order=' . $this->request->get['order'];
27: }
28:
29: if (isset($this->request->get['page'])) {
30: $url .= '&page=' . $this->request->get['page'];
31: }
32:
33: $data['breadcrumbs'] = [];
34:
35: $data['breadcrumbs'][] = [
36: 'text' => $this->language->get('text_home'),
37: 'href' => $this->url->link('common/dashboard', 'user_token=' . $this->session->data['user_token'])
38: ];
39:
40: $data['breadcrumbs'][] = [
41: 'text' => $this->language->get('heading_title'),
42: 'href' => $this->url->link('localisation/language', 'user_token=' . $this->session->data['user_token'] . $url)
43: ];
44:
45: $data['add'] = $this->url->link('localisation/language.form', 'user_token=' . $this->session->data['user_token'] . $url);
46: $data['delete'] = $this->url->link('localisation/language.delete', 'user_token=' . $this->session->data['user_token']);
47:
48: $data['list'] = $this->getList();
49:
50: $data['user_token'] = $this->session->data['user_token'];
51:
52: $data['header'] = $this->load->controller('common/header');
53: $data['column_left'] = $this->load->controller('common/column_left');
54: $data['footer'] = $this->load->controller('common/footer');
55:
56: $this->response->setOutput($this->load->view('localisation/language', $data));
57: }
58:
59: /**
60: * List
61: *
62: * @return void
63: */
64: public function list(): void {
65: $this->load->language('localisation/language');
66:
67: $this->response->setOutput($this->getList());
68: }
69:
70: /**
71: * Get List
72: *
73: * @return string
74: */
75: protected function getList(): string {
76: if (isset($this->request->get['sort'])) {
77: $sort = (string)$this->request->get['sort'];
78: } else {
79: $sort = 'name';
80: }
81:
82: if (isset($this->request->get['order'])) {
83: $order = (string)$this->request->get['order'];
84: } else {
85: $order = 'ASC';
86: }
87:
88: if (isset($this->request->get['page'])) {
89: $page = (int)$this->request->get['page'];
90: } else {
91: $page = 1;
92: }
93:
94: $url = '';
95:
96: if (isset($this->request->get['sort'])) {
97: $url .= '&sort=' . $this->request->get['sort'];
98: }
99:
100: if (isset($this->request->get['order'])) {
101: $url .= '&order=' . $this->request->get['order'];
102: }
103:
104: if (isset($this->request->get['page'])) {
105: $url .= '&page=' . $this->request->get['page'];
106: }
107:
108: $data['action'] = $this->url->link('localisation/language.list', 'user_token=' . $this->session->data['user_token'] . $url);
109:
110: $data['languages'] = [];
111:
112: $filter_data = [
113: 'sort' => $sort,
114: 'order' => $order,
115: 'start' => ($page - 1) * $this->config->get('config_pagination_admin'),
116: 'limit' => $this->config->get('config_pagination_admin')
117: ];
118:
119: $this->load->model('localisation/language');
120:
121: $results = $this->model_localisation_language->getLanguages($filter_data);
122:
123: foreach ($results as $result) {
124: $data['languages'][] = [
125: 'language_id' => $result['language_id'],
126: 'name' => $result['name'] . (($result['code'] == $this->config->get('config_language')) ? $this->language->get('text_default') : ''),
127: 'code' => $result['code'],
128: 'status' => $result['status'],
129: 'sort_order' => $result['sort_order'],
130: 'edit' => $this->url->link('localisation/language.form', 'user_token=' . $this->session->data['user_token'] . '&language_id=' . $result['language_id'] . $url)
131: ];
132: }
133:
134: $url = '';
135:
136: if ($order == 'ASC') {
137: $url .= '&order=DESC';
138: } else {
139: $url .= '&order=ASC';
140: }
141:
142: $data['sort_name'] = $this->url->link('localisation/language.list', 'user_token=' . $this->session->data['user_token'] . '&sort=name' . $url);
143: $data['sort_code'] = $this->url->link('localisation/language.list', 'user_token=' . $this->session->data['user_token'] . '&sort=code' . $url);
144: $data['sort_sort_order'] = $this->url->link('localisation/language.list', 'user_token=' . $this->session->data['user_token'] . '&sort=sort_order' . $url);
145:
146: $url = '';
147:
148: if (isset($this->request->get['sort'])) {
149: $url .= '&sort=' . $this->request->get['sort'];
150: }
151:
152: if (isset($this->request->get['order'])) {
153: $url .= '&order=' . $this->request->get['order'];
154: }
155:
156: $language_total = $this->model_localisation_language->getTotalLanguages();
157:
158: $data['pagination'] = $this->load->controller('common/pagination', [
159: 'total' => $language_total,
160: 'page' => $page,
161: 'limit' => $this->config->get('config_pagination_admin'),
162: 'url' => $this->url->link('localisation/language.list', 'user_token=' . $this->session->data['user_token'] . $url . '&page={page}')
163: ]);
164:
165: $data['results'] = sprintf($this->language->get('text_pagination'), ($language_total) ? (($page - 1) * $this->config->get('config_pagination_admin')) + 1 : 0, ((($page - 1) * $this->config->get('config_pagination_admin')) > ($language_total - $this->config->get('config_pagination_admin'))) ? $language_total : ((($page - 1) * $this->config->get('config_pagination_admin')) + $this->config->get('config_pagination_admin')), $language_total, ceil($language_total / $this->config->get('config_pagination_admin')));
166:
167: $data['sort'] = $sort;
168: $data['order'] = $order;
169:
170: return $this->load->view('localisation/language_list', $data);
171: }
172:
173: /**
174: * Form
175: *
176: * @return void
177: */
178: public function form(): void {
179: $this->load->language('localisation/language');
180:
181: $this->document->setTitle($this->language->get('heading_title'));
182:
183: $data['text_form'] = !isset($this->request->get['language_id']) ? $this->language->get('text_add') : $this->language->get('text_edit');
184:
185: $url = '';
186:
187: if (isset($this->request->get['sort'])) {
188: $url .= '&sort=' . $this->request->get['sort'];
189: }
190:
191: if (isset($this->request->get['order'])) {
192: $url .= '&order=' . $this->request->get['order'];
193: }
194:
195: if (isset($this->request->get['page'])) {
196: $url .= '&page=' . $this->request->get['page'];
197: }
198:
199: $data['breadcrumbs'] = [];
200:
201: $data['breadcrumbs'][] = [
202: 'text' => $this->language->get('text_home'),
203: 'href' => $this->url->link('common/dashboard', 'user_token=' . $this->session->data['user_token'])
204: ];
205:
206: $data['breadcrumbs'][] = [
207: 'text' => $this->language->get('heading_title'),
208: 'href' => $this->url->link('localisation/language', 'user_token=' . $this->session->data['user_token'] . $url)
209: ];
210:
211: $data['save'] = $this->url->link('localisation/language.save', 'user_token=' . $this->session->data['user_token']);
212: $data['back'] = $this->url->link('localisation/language', 'user_token=' . $this->session->data['user_token'] . $url);
213:
214: if (isset($this->request->get['language_id'])) {
215: $this->load->model('localisation/language');
216:
217: $language_info = $this->model_localisation_language->getLanguage($this->request->get['language_id']);
218: }
219:
220: if (isset($this->request->get['language_id'])) {
221: $data['language_id'] = (int)$this->request->get['language_id'];
222: } else {
223: $data['language_id'] = 0;
224: }
225:
226: if (!empty($language_info)) {
227: $data['name'] = $language_info['name'];
228: } else {
229: $data['name'] = '';
230: }
231:
232: if (!empty($language_info)) {
233: $data['code'] = $language_info['code'];
234: } else {
235: $data['code'] = '';
236: }
237:
238: if (!empty($language_info)) {
239: $data['locale'] = $language_info['locale'];
240: } else {
241: $data['locale'] = '';
242: }
243:
244: if (!empty($language_info)) {
245: $data['extension'] = $language_info['extension'];
246: } else {
247: $data['extension'] = '';
248: }
249:
250: if (!empty($language_info)) {
251: $data['sort_order'] = $language_info['sort_order'];
252: } else {
253: $data['sort_order'] = 1;
254: }
255:
256: if (!empty($language_info)) {
257: $data['status'] = $language_info['status'];
258: } else {
259: $data['status'] = 1;
260: }
261:
262: $data['header'] = $this->load->controller('common/header');
263: $data['column_left'] = $this->load->controller('common/column_left');
264: $data['footer'] = $this->load->controller('common/footer');
265:
266: $this->response->setOutput($this->load->view('localisation/language_form', $data));
267: }
268:
269: /**
270: * Save
271: *
272: * @return void
273: */
274: public function save(): void {
275: $this->load->language('localisation/language');
276:
277: $json = [];
278:
279: if (!$this->user->hasPermission('modify', 'localisation/language')) {
280: $json['error']['warning'] = $this->language->get('error_permission');
281: }
282:
283: if (!oc_validate_length($this->request->post['name'], 1, 32)) {
284: $json['error']['name'] = $this->language->get('error_name');
285: }
286:
287: if ((oc_strlen($this->request->post['code']) < 2) || (oc_strlen($this->request->post['code']) > 5)) {
288: $json['error']['code'] = $this->language->get('error_code');
289: }
290:
291: if ((oc_strlen($this->request->post['locale']) < 2) || (oc_strlen($this->request->post['locale']) > 255)) {
292: $json['error']['locale'] = $this->language->get('error_locale');
293: }
294:
295: $language_info = $this->model_localisation_language->getLanguageByCode($this->request->post['code']);
296:
297: if (!$this->request->post['language_id']) {
298: if ($language_info) {
299: $json['error']['warning'] = $this->language->get('error_exists');
300: }
301: } else {
302: if ($language_info && ($this->request->post['language_id'] != $language_info['language_id'])) {
303: $json['error']['warning'] = $this->language->get('error_exists');
304: }
305: }
306:
307: if (!$json) {
308: $this->load->model('localisation/language');
309:
310: if (!$this->request->post['language_id']) {
311: $json['language_id'] = $this->model_localisation_language->addLanguage($this->request->post);
312: } else {
313: $this->model_localisation_language->editLanguage($this->request->post['language_id'], $this->request->post);
314: }
315:
316: $json['success'] = $this->language->get('text_success');
317: }
318:
319: $this->response->addHeader('Content-Type: application/json');
320: $this->response->setOutput(json_encode($json));
321: }
322:
323: /**
324: * Delete
325: *
326: * @return void
327: */
328: public function delete(): void {
329: $this->load->language('localisation/language');
330:
331: $json = [];
332:
333: if (isset($this->request->post['selected'])) {
334: $selected = $this->request->post['selected'];
335: } else {
336: $selected = [];
337: }
338:
339: if (!$this->user->hasPermission('modify', 'localisation/language')) {
340: $json['error'] = $this->language->get('error_permission');
341: }
342:
343: $this->load->model('setting/store');
344: $this->load->model('sale/order');
345:
346: foreach ($selected as $language_id) {
347: $language_info = $this->model_localisation_language->getLanguage($language_id);
348:
349: if ($language_info) {
350: if ($this->config->get('config_language') == $language_info['code']) {
351: $json['error'] = $this->language->get('error_default');
352: }
353:
354: if ($this->config->get('config_language_admin') == $language_info['code']) {
355: $json['error'] = $this->language->get('error_admin');
356: }
357:
358: $store_total = $this->model_setting_store->getTotalStoresByLanguage($language_info['code']);
359:
360: if ($store_total) {
361: $json['error'] = sprintf($this->language->get('error_store'), $store_total);
362: }
363: }
364:
365: $order_total = $this->model_sale_order->getTotalOrdersByLanguageId($language_id);
366:
367: if ($order_total) {
368: $json['error'] = sprintf($this->language->get('error_order'), $order_total);
369: }
370: }
371:
372: if (!$json) {
373: $this->load->model('localisation/language');
374:
375: foreach ($selected as $language_id) {
376: $this->model_localisation_language->deleteLanguage($language_id);
377: }
378:
379: $json['success'] = $this->language->get('text_success');
380: }
381:
382: $this->response->addHeader('Content-Type: application/json');
383: $this->response->setOutput(json_encode($json));
384: }
385: }
386: