1: | <?php
2: | namespace Opencart\Admin\Controller\Extension;
3: | /**
4: | * Class Other
5: | *
6: | * @package Opencart\Admin\Controller\Extension
7: | */
8: | class Other extends \Opencart\System\Engine\Controller {
9: | /**
10: | * Index
11: | *
12: | * @return void
13: | */
14: | public function index(): void {
15: | $this->response->setOutput($this->getList());
16: | }
17: |
18: | /**
19: | * Get List
20: | *
21: | * @return string
22: | */
23: | public function getList(): string {
24: | // Had top load again because the method is called directly.
25: | $this->load->language('extension/other');
26: |
27: | $available = [];
28: |
29: | $results = glob(DIR_EXTENSION . '*/admin/controller/other/*.php');
30: |
31: | foreach ($results as $result) {
32: | $available[] = basename($result, '.php');
33: | }
34: |
35: | $installed = [];
36: |
37: | $this->load->model('setting/extension');
38: |
39: | $extensions = $this->model_setting_extension->getExtensionsByType('other');
40: |
41: | foreach ($extensions as $extension) {
42: | if (in_array($extension['code'], $available)) {
43: | $installed[] = $extension['code'];
44: | } else {
45: | $this->model_setting_extension->uninstall('other', $extension['code']);
46: | }
47: | }
48: |
49: | $data['extensions'] = [];
50: |
51: | if ($results) {
52: | foreach ($results as $result) {
53: | $path = substr($result, strlen(DIR_EXTENSION));
54: |
55: | $extension = substr($path, 0, strpos($path, '/'));
56: |
57: | $code = basename($result, '.php');
58: |
59: | $this->load->language('extension/' . $extension . '/other/' . $code, $code);
60: |
61: | $data['extensions'][] = [
62: | 'name' => $this->language->get($code . '_heading_title'),
63: | 'status' => $this->config->get('other_' . $code . '_status') ? $this->language->get('text_enabled') : $this->language->get('text_disabled'),
64: | 'install' => $this->url->link('extension/other.install', 'user_token=' . $this->session->data['user_token'] . '&extension=' . $extension . '&code=' . $code),
65: | 'uninstall' => $this->url->link('extension/other.uninstall', 'user_token=' . $this->session->data['user_token'] . '&extension=' . $extension . '&code=' . $code),
66: | 'installed' => in_array($code, $installed),
67: | 'edit' => $this->url->link('extension/' . $extension . '/other/' . $code, 'user_token=' . $this->session->data['user_token'])
68: | ];
69: | }
70: | }
71: |
72: | $data['promotion'] = $this->load->controller('marketplace/promotion');
73: |
74: | return $this->load->view('extension/other', $data);
75: | }
76: |
77: | /**
78: | * Install
79: | *
80: | * @return void
81: | */
82: | public function install(): void {
83: | $this->load->language('extension/other');
84: |
85: | $json = [];
86: |
87: | if (isset($this->request->get['extension'])) {
88: | $extension = basename($this->request->get['extension']);
89: | } else {
90: | $extension = '';
91: | }
92: |
93: | if (isset($this->request->get['code'])) {
94: | $code = basename($this->request->get['code']);
95: | } else {
96: | $code = '';
97: | }
98: |
99: | if (!$this->user->hasPermission('modify', 'extension/other')) {
100: | $json['error'] = $this->language->get('error_permission');
101: | }
102: |
103: | if (!is_file(DIR_EXTENSION . $extension . '/admin/controller/other/' . $code . '.php')) {
104: | $json['error'] = $this->language->get('error_extension');
105: | }
106: |
107: | if (!$json) {
108: | $this->load->model('setting/extension');
109: |
110: | $this->model_setting_extension->install('other', $extension, $code);
111: |
112: | $this->load->model('user/user_group');
113: |
114: | $this->model_user_user_group->addPermission($this->user->getGroupId(), 'access', 'extension/' . $extension . '/other/' . $code);
115: | $this->model_user_user_group->addPermission($this->user->getGroupId(), 'modify', 'extension/' . $extension . '/other/' . $code);
116: |
117: | $namespace = str_replace(['_', '/'], ['', '\\'], ucwords($extension, '_/'));
118: |
119: | // Register controllers, models and system extension folders
120: | $this->autoloader->register('Opencart\Admin\Controller\Extension\\' . $namespace, DIR_EXTENSION . $extension . '/admin/controller/');
121: | $this->autoloader->register('Opencart\Admin\Model\Extension\\' . $namespace, DIR_EXTENSION . $extension . '/admin/model/');
122: | $this->autoloader->register('Opencart\System\Extension\\' . $namespace, DIR_EXTENSION . $extension . '/system/');
123: |
124: | // Template directory
125: | $this->template->addPath('extension/' . $extension, DIR_EXTENSION . $extension . '/admin/view/template/');
126: |
127: | // Language directory
128: | $this->language->addPath('extension/' . $extension, DIR_EXTENSION . $extension . '/admin/language/');
129: |
130: | // Config directory
131: | $this->config->addPath('extension/' . $extension, DIR_EXTENSION . $extension . '/system/config/');
132: |
133: | // Call install method if it exists
134: | $this->load->controller('extension/' . $extension . '/other/' . $code . '.install');
135: |
136: | $json['success'] = $this->language->get('text_success');
137: | }
138: |
139: | $this->response->addHeader('Content-Type: application/json');
140: | $this->response->setOutput(json_encode($json));
141: | }
142: |
143: | /**
144: | * Uninstall
145: | *
146: | * @return void
147: | */
148: | public function uninstall(): void {
149: | $this->load->language('extension/other');
150: |
151: | $json = [];
152: |
153: | if (!$this->user->hasPermission('modify', 'extension/other')) {
154: | $json['error'] = $this->language->get('error_permission');
155: | }
156: |
157: | if (!$json) {
158: | $this->load->model('setting/extension');
159: |
160: | $this->model_setting_extension->uninstall('other', $this->request->get['code']);
161: |
162: | // Call uninstall method if it exists
163: | $this->load->controller('extension/' . $this->request->get['extension'] . '/other/' . $this->request->get['code'] . '.uninstall');
164: |
165: | $json['success'] = $this->language->get('text_success');
166: | }
167: |
168: | $this->response->addHeader('Content-Type: application/json');
169: | $this->response->setOutput(json_encode($json));
170: | }
171: | }
172: |

OpenCart API API documentation generated by ApiGen dev-master