1: <?php
2: namespace Opencart\Admin\Controller\Design;
3: /**
4: * Class Translation
5: *
6: * @package Opencart\Admin\Controller\Design
7: */
8: class Translation extends \Opencart\System\Engine\Controller {
9: /**
10: * Index
11: *
12: * @return void
13: */
14: public function index(): void {
15: $this->load->language('design/translation');
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('design/translation', 'user_token=' . $this->session->data['user_token'] . $url)
43: ];
44:
45: $data['add'] = $this->url->link('design/translation.form', 'user_token=' . $this->session->data['user_token'] . $url);
46: $data['delete'] = $this->url->link('design/translation.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('design/translation', $data));
57: }
58:
59: /**
60: * List
61: *
62: * @return void
63: */
64: public function list(): void {
65: $this->load->language('design/translation');
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 = 'store';
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('design/translation.list', 'user_token=' . $this->session->data['user_token'] . $url);
109:
110: $this->load->model('localisation/language');
111:
112: $data['translations'] = [];
113:
114: $filter_data = [
115: 'sort' => $sort,
116: 'order' => $order,
117: 'start' => ($page - 1) * $this->config->get('config_pagination_admin'),
118: 'limit' => $this->config->get('config_pagination_admin')
119: ];
120:
121: $this->load->model('design/translation');
122:
123: $results = $this->model_design_translation->getTranslations($filter_data);
124:
125: foreach ($results as $result) {
126: $language_info = $this->model_localisation_language->getLanguage($result['language_id']);
127:
128: if ($language_info) {
129: $code = $language_info['code'];
130: $image = $language_info['image'];
131: } else {
132: $code = '';
133: $image = '';
134: }
135:
136: $data['translations'][] = [
137: 'translation_id' => $result['translation_id'],
138: 'store' => ($result['store_id'] ? $result['store'] : $this->language->get('text_default')),
139: 'route' => $result['route'],
140: 'image' => $image,
141: 'language' => $code,
142: 'key' => $result['key'],
143: 'value' => $result['value'],
144: 'edit' => $this->url->link('design/translation.form', 'user_token=' . $this->session->data['user_token'] . '&translation_id=' . $result['translation_id'])
145: ];
146: }
147:
148: $url = '';
149:
150: if ($order == 'ASC') {
151: $url .= '&order=DESC';
152: } else {
153: $url .= '&order=ASC';
154: }
155:
156: $data['sort_store'] = $this->url->link('design/translation.list', 'user_token=' . $this->session->data['user_token'] . '&sort=store' . $url);
157: $data['sort_language'] = $this->url->link('design/translation.list', 'user_token=' . $this->session->data['user_token'] . '&sort=language' . $url);
158: $data['sort_route'] = $this->url->link('design/translation.list', 'user_token=' . $this->session->data['user_token'] . '&sort=route' . $url);
159: $data['sort_key'] = $this->url->link('design/translation.list', 'user_token=' . $this->session->data['user_token'] . '&sort=key' . $url);
160: $data['sort_value'] = $this->url->link('design/translation.list', 'user_token=' . $this->session->data['user_token'] . '&sort=value' . $url);
161:
162: $translation_total = $this->model_design_translation->getTotalTranslations();
163:
164: $data['pagination'] = $this->load->controller('common/pagination', [
165: 'total' => $translation_total,
166: 'page' => $page,
167: 'limit' => $this->config->get('config_pagination_admin'),
168: 'url' => $this->url->link('design/translation.list', 'user_token=' . $this->session->data['user_token'] . $url . '&page={page}')
169: ]);
170:
171: $data['results'] = sprintf($this->language->get('text_pagination'), ($translation_total) ? (($page - 1) * $this->config->get('config_pagination_admin')) + 1 : 0, ((($page - 1) * $this->config->get('config_pagination_admin')) > ($translation_total - $this->config->get('config_pagination_admin'))) ? $translation_total : ((($page - 1) * $this->config->get('config_pagination_admin')) + $this->config->get('config_pagination_admin')), $translation_total, ceil($translation_total / $this->config->get('config_pagination_admin')));
172:
173: $data['sort'] = $sort;
174: $data['order'] = $order;
175:
176: return $this->load->view('design/translation_list', $data);
177: }
178:
179: /**
180: * Form
181: *
182: * @return void
183: */
184: public function form(): void {
185: $this->load->language('design/translation');
186:
187: $this->document->setTitle($this->language->get('heading_title'));
188:
189: $data['text_form'] = !isset($this->request->get['translation_id']) ? $this->language->get('text_add') : $this->language->get('text_edit');
190:
191: $url = '';
192:
193: if (isset($this->request->get['sort'])) {
194: $url .= '&sort=' . $this->request->get['sort'];
195: }
196:
197: if (isset($this->request->get['order'])) {
198: $url .= '&order=' . $this->request->get['order'];
199: }
200:
201: if (isset($this->request->get['page'])) {
202: $url .= '&page=' . $this->request->get['page'];
203: }
204:
205: $data['breadcrumbs'] = [];
206:
207: $data['breadcrumbs'][] = [
208: 'text' => $this->language->get('text_home'),
209: 'href' => $this->url->link('common/dashboard', 'user_token=' . $this->session->data['user_token'])
210: ];
211:
212: $data['breadcrumbs'][] = [
213: 'text' => $this->language->get('heading_title'),
214: 'href' => $this->url->link('design/translation', 'user_token=' . $this->session->data['user_token'] . $url)
215: ];
216:
217: $data['save'] = $this->url->link('design/translation.save', 'user_token=' . $this->session->data['user_token']);
218: $data['back'] = $this->url->link('design/translation', 'user_token=' . $this->session->data['user_token'] . $url);
219:
220: if (isset($this->request->get['translation_id'])) {
221: $this->load->model('design/translation');
222:
223: $translation_info = $this->model_design_translation->getTranslation($this->request->get['translation_id']);
224: }
225:
226: if (isset($this->request->get['translation_id'])) {
227: $data['translation_id'] = (int)$this->request->get['translation_id'];
228: } else {
229: $data['translation_id'] = 0;
230: }
231:
232: $this->load->model('setting/store');
233:
234: $data['stores'] = $this->model_setting_store->getStores();
235:
236: if (!empty($translation_info)) {
237: $data['store_id'] = $translation_info['store_id'];
238: } else {
239: $data['store_id'] = '';
240: }
241:
242: $this->load->model('localisation/language');
243:
244: $data['languages'] = $this->model_localisation_language->getLanguages();
245:
246: if (!empty($translation_info)) {
247: $data['language_id'] = $translation_info['language_id'];
248: } else {
249: $data['language_id'] = '';
250: }
251:
252: if (!empty($translation_info)) {
253: $data['route'] = $translation_info['route'];
254: } else {
255: $data['route'] = '';
256: }
257:
258: if (!empty($translation_info)) {
259: $data['key'] = $translation_info['key'];
260: } else {
261: $data['key'] = '';
262: }
263:
264: if (!empty($translation_info)) {
265: $data['value'] = $translation_info['value'];
266: } else {
267: $data['value'] = '';
268: }
269:
270: $data['user_token'] = $this->session->data['user_token'];
271:
272: $data['header'] = $this->load->controller('common/header');
273: $data['column_left'] = $this->load->controller('common/column_left');
274: $data['footer'] = $this->load->controller('common/footer');
275:
276: $this->response->setOutput($this->load->view('design/translation_form', $data));
277: }
278:
279: /**
280: * Save
281: *
282: * @return void
283: */
284: public function save(): void {
285: $this->load->language('design/translation');
286:
287: $json = [];
288:
289: if (!$this->user->hasPermission('modify', 'design/translation')) {
290: $json['error']['warning'] = $this->language->get('error_permission');
291: }
292:
293: if ((oc_strlen($this->request->post['key']) < 3) || (oc_strlen($this->request->post['key']) > 64)) {
294: $json['error']['key'] = $this->language->get('error_key');
295: }
296:
297: if (!$json) {
298: $this->load->model('design/translation');
299:
300: if (!$this->request->post['translation_id']) {
301: $this->model_design_translation->addTranslation($this->request->post);
302: } else {
303: $this->model_design_translation->editTranslation($this->request->post['translation_id'], $this->request->post);
304: }
305:
306: $json['success'] = $this->language->get('text_success');
307: }
308:
309: $this->response->addHeader('Content-Type: application/json');
310: $this->response->setOutput(json_encode($json));
311: }
312:
313: /**
314: * Delete
315: *
316: * @return void
317: */
318: public function delete(): void {
319: $this->load->language('design/translation');
320:
321: $json = [];
322:
323: if (isset($this->request->post['selected'])) {
324: $selected = $this->request->post['selected'];
325: } else {
326: $selected = [];
327: }
328:
329: if (!$this->user->hasPermission('modify', 'design/translation')) {
330: $json['error'] = $this->language->get('error_permission');
331: }
332:
333: if (!$json) {
334: $this->load->model('design/translation');
335:
336: foreach ($selected as $translation_id) {
337: $this->model_design_translation->deleteTranslation($translation_id);
338: }
339:
340: $json['success'] = $this->language->get('text_success');
341: }
342:
343: $this->response->addHeader('Content-Type: application/json');
344: $this->response->setOutput(json_encode($json));
345: }
346:
347: /**
348: * Path
349: *
350: * @return void
351: */
352: public function path(): void {
353: $this->load->language('design/translation');
354:
355: $json = [];
356:
357: if (isset($this->request->get['language_id'])) {
358: $language_id = (int)$this->request->get['language_id'];
359: } else {
360: $language_id = 0;
361: }
362:
363: $this->load->model('localisation/language');
364:
365: $language_info = $this->model_localisation_language->getLanguage($language_id);
366:
367: if (!empty($language_info)) {
368: $path = glob(DIR_CATALOG . 'language/' . $language_info['code'] . '/*');
369:
370: while (count($path) != 0) {
371: $next = array_shift($path);
372:
373: foreach ((array)glob($next . '/*') as $file) {
374: if (is_dir($file)) {
375: $path[] = $file;
376: }
377:
378: if (substr($file, -4) == '.php') {
379: $json[] = substr(substr($file, strlen(DIR_CATALOG . 'language/' . $language_info['code'] . '/')), 0, -4);
380: }
381: }
382: }
383:
384: $path = glob(DIR_EXTENSION . '*/catalog/language/' . $language_info['code'] . '/*');
385:
386: while (count($path) != 0) {
387: $next = array_shift($path);
388:
389: foreach ((array)glob($next . '/*') as $file) {
390: if (is_dir($file)) {
391: $path[] = $file;
392: }
393:
394: if (substr($file, -4) == '.php') {
395: $new_path = substr($file, strlen(DIR_EXTENSION));
396:
397: $code = substr($new_path, 0, strpos($new_path, '/'));
398:
399: $length = strlen(DIR_EXTENSION . $code . '/catalog/language/' . $language_info['code'] . '/');
400:
401: $route = substr(substr($file, $length), 0, -4);
402:
403: $json[] = 'extension/' . $code . '/' . $route;
404: }
405: }
406: }
407: }
408:
409: $this->response->addHeader('Content-Type: application/json');
410: $this->response->setOutput(json_encode($json));
411: }
412:
413: /**
414: * Translation
415: *
416: * @return void
417: */
418: public function translation(): void {
419: $this->load->language('design/translation');
420:
421: $json = [];
422:
423: if (isset($this->request->get['store_id'])) {
424: $store_id = (int)$this->request->get['store_id'];
425: } else {
426: $store_id = 0;
427: }
428:
429: if (isset($this->request->get['language_id'])) {
430: $language_id = (int)$this->request->get['language_id'];
431: } else {
432: $language_id = 0;
433: }
434:
435: if (isset($this->request->get['path'])) {
436: $route = $this->request->get['path'];
437: } else {
438: $route = '';
439: }
440:
441: $this->load->model('localisation/language');
442:
443: $language_info = $this->model_localisation_language->getLanguage($language_id);
444:
445: $part = explode('/', $route);
446:
447: if ($part[0] != 'extension') {
448: $directory = DIR_CATALOG . 'language/';
449: } else {
450: $directory = DIR_EXTENSION . $part[1] . '/catalog/language/';
451:
452: array_shift($part);
453: // Don't remove. Required for extension route.
454: array_shift($part);
455:
456: $route = implode('/', $part);
457: }
458:
459: if ($language_info && is_file($directory . $language_info['code'] . '/' . $route . '.php') && substr(str_replace('\\', '/', realpath($directory . $language_info['code'] . '/' . $route . '.php')), 0, strlen($directory)) == str_replace('\\', '/', $directory)) {
460: $_ = [];
461:
462: include($directory . $language_info['code'] . '/' . $route . '.php');
463:
464: foreach ($_ as $key => $value) {
465: $json[] = [
466: 'key' => $key,
467: 'value' => $value
468: ];
469: }
470: }
471:
472: $this->response->addHeader('Content-Type: application/json');
473: $this->response->setOutput(json_encode($json));
474: }
475: }
476: