1: <?php
2: namespace Opencart\Admin\Controller\Common;
3: /**
4: * Class Language
5: *
6: * @package Opencart\Admin\Controller\Common
7: */
8: class Language extends \Opencart\System\Engine\Controller {
9: /**
10: * Index
11: *
12: * @return string
13: */
14: public function index(): string {
15: $data['languages'] = [];
16:
17: $this->load->model('localisation/language');
18:
19: $results = $this->model_localisation_language->getLanguages();
20:
21: foreach ($results as $result) {
22: $data['languages'][] = [
23: 'name' => $result['name'],
24: 'code' => $result['code'],
25: 'image' => $result['image']
26: ];
27: }
28:
29: if (isset($this->request->cookie['language'])) {
30: $data['code'] = $this->request->cookie['language'];
31: } else {
32: $data['code'] = $this->config->get('config_language');
33: }
34:
35: // Redirect
36: $url_data = $this->request->get;
37:
38: if (isset($url_data['route'])) {
39: $route = $url_data['route'];
40: } else {
41: $route = 'common/dashboard';
42: }
43:
44: unset($url_data['route']);
45:
46: $url = '';
47:
48: if ($url_data) {
49: $url .= '&' . urldecode(http_build_query($url_data));
50: }
51:
52: $data['redirect'] = $this->url->link($route, $url);
53:
54: $data['user_token'] = $this->session->data['user_token'];
55:
56: return $this->load->view('common/language', $data);
57: }
58:
59: /**
60: * Save
61: *
62: * @return void
63: */
64: public function save(): void {
65: $this->load->language('common/language');
66:
67: $json = [];
68:
69: if (isset($this->request->post['code'])) {
70: $code = $this->request->post['code'];
71: } else {
72: $code = '';
73: }
74:
75: if (isset($this->request->post['redirect'])) {
76: $redirect = html_entity_decode($this->request->post['redirect'], ENT_QUOTES, 'UTF-8');
77: } else {
78: $redirect = '';
79: }
80:
81: $this->load->model('localisation/language');
82:
83: $language_info = $this->model_localisation_language->getLanguageByCode($code);
84:
85: if (!$language_info) {
86: $json['error'] = $this->language->get('error_language');
87: }
88:
89: if (!$json) {
90: $option = [
91: 'expires' => time() + 60 * 60 * 24 * 365 * 10,
92: 'path' => $this->config->get('session_path'),
93: 'SameSite' => $this->config->get('config_session_samesite')
94: ];
95:
96: setcookie('language', $code, $option);
97:
98: if ($redirect && str_starts_with($redirect, $this->config->get('config_url'))) {
99: $json['redirect'] = $redirect;
100: } else {
101: $json['redirect'] = $this->url->link('common/dashboard', 'user_token=' . $this->session->data['user_token'], true);
102: }
103: }
104:
105: $this->response->addHeader('Content-Type: application/json');
106: $this->response->setOutput(json_encode($json));
107: }
108: }
109: