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