1: | <?php
2: | namespace Opencart\Admin\Controller\Extension;
3: | /**
4: | * Class Module
5: | *
6: | * @package Opencart\Admin\Controller\Extension
7: | */
8: | class Module 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: | $this->load->language('extension/module');
25: |
26: | $data['text_layout'] = sprintf($this->language->get('text_layout'), $this->url->link('design/layout', 'user_token=' . $this->session->data['user_token']));
27: |
28: | $available = [];
29: |
30: | $results = glob(DIR_EXTENSION . '*/admin/controller/module/*.php');
31: |
32: | foreach ($results as $result) {
33: | $available[] = basename($result, '.php');
34: | }
35: |
36: | $installed = [];
37: |
38: | $this->load->model('setting/extension');
39: |
40: | $extensions = $this->model_setting_extension->getExtensionsByType('module');
41: |
42: | foreach ($extensions as $extension) {
43: | if (in_array($extension['code'], $available)) {
44: | $installed[] = $extension['code'];
45: | } else {
46: | $this->model_setting_extension->uninstall('module', $extension['code']);
47: | }
48: | }
49: |
50: | $data['extensions'] = [];
51: |
52: | if ($results) {
53: | $this->load->model('setting/module');
54: |
55: | foreach ($results as $result) {
56: | $path = substr($result, strlen(DIR_EXTENSION));
57: |
58: | $extension = substr($path, 0, strpos($path, '/'));
59: |
60: | $code = basename($result, '.php');
61: |
62: | $this->load->language('extension/' . $extension . '/module/' . $code, $code);
63: |
64: | $module_data = [];
65: |
66: | $modules = $this->model_setting_module->getModulesByCode($extension . '.' . $code);
67: |
68: | foreach ($modules as $module) {
69: | if ($module['setting']) {
70: | $setting_info = json_decode($module['setting'], true);
71: | } else {
72: | $setting_info = [];
73: | }
74: |
75: | $module_data[] = [
76: | 'name' => $module['name'],
77: | 'status' => (bool)$setting_info['status'] ? $this->language->get('text_enabled') : $this->language->get('text_disabled'),
78: | 'edit' => $this->url->link('extension/' . $extension . '/module/' . $code, 'user_token=' . $this->session->data['user_token'] . '&module_id=' . $module['module_id']),
79: | 'delete' => $this->url->link('extension/module.delete', 'user_token=' . $this->session->data['user_token'] . '&module_id=' . $module['module_id'])
80: | ];
81: | }
82: |
83: | if ($module_data) {
84: | $status = '';
85: | } else {
86: | $status = $this->config->get('module_' . $code . '_status') ? $this->language->get('text_enabled') : $this->language->get('text_disabled');
87: | }
88: |
89: | $data['extensions'][] = [
90: | 'name' => $this->language->get($code . '_heading_title'),
91: | 'status' => $status,
92: | 'module' => $module_data,
93: | 'install' => $this->url->link('extension/module.install', 'user_token=' . $this->session->data['user_token'] . '&extension=' . $extension . '&code=' . $code),
94: | 'uninstall' => $this->url->link('extension/module.uninstall', 'user_token=' . $this->session->data['user_token'] . '&extension=' . $extension . '&code=' . $code),
95: | 'installed' => in_array($code, $installed),
96: | 'edit' => $this->url->link('extension/' . $extension . '/module/' . $code, 'user_token=' . $this->session->data['user_token'])
97: | ];
98: | }
99: | }
100: |
101: | $sort_order = [];
102: |
103: | foreach ($data['extensions'] as $key => $value) {
104: | $sort_order[$key] = $value['name'];
105: | }
106: |
107: | array_multisort($sort_order, SORT_ASC, $data['extensions']);
108: |
109: | $data['promotion'] = $this->load->controller('marketplace/promotion');
110: |
111: | return $this->load->view('extension/module', $data);
112: | }
113: |
114: | /**
115: | * Install
116: | *
117: | * @return void
118: | */
119: | public function install(): void {
120: | $this->load->language('extension/module');
121: |
122: | $json = [];
123: |
124: | if (isset($this->request->get['extension'])) {
125: | $extension = basename($this->request->get['extension']);
126: | } else {
127: | $extension = '';
128: | }
129: |
130: | if (isset($this->request->get['code'])) {
131: | $code = basename($this->request->get['code']);
132: | } else {
133: | $code = '';
134: | }
135: |
136: | if (!$this->user->hasPermission('modify', 'extension/module')) {
137: | $json['error'] = $this->language->get('error_permission');
138: | }
139: |
140: | if (!is_file(DIR_EXTENSION . $extension . '/admin/controller/module/' . $code . '.php')) {
141: | $json['error'] = $this->language->get('error_extension');
142: | }
143: |
144: | if (!$json) {
145: | $this->load->model('setting/extension');
146: |
147: | $this->model_setting_extension->install('module', $extension, $code);
148: |
149: | $this->load->model('user/user_group');
150: |
151: | $this->model_user_user_group->addPermission($this->user->getGroupId(), 'access', 'extension/' . $extension . '/module/' . $code);
152: | $this->model_user_user_group->addPermission($this->user->getGroupId(), 'modify', 'extension/' . $extension . '/module/' . $code);
153: |
154: | $namespace = str_replace(['_', '/'], ['', '\\'], ucwords($extension, '_/'));
155: |
156: | // Register controllers, models and system extension folders
157: | $this->autoloader->register('Opencart\Admin\Controller\Extension\\' . $namespace, DIR_EXTENSION . $extension . '/admin/controller/');
158: | $this->autoloader->register('Opencart\Admin\Model\Extension\\' . $namespace, DIR_EXTENSION . $extension . '/admin/model/');
159: | $this->autoloader->register('Opencart\System\Extension\\' . $namespace, DIR_EXTENSION . $extension . '/system/');
160: |
161: | // Template directory
162: | $this->template->addPath('extension/' . $extension, DIR_EXTENSION . $extension . '/admin/view/template/');
163: |
164: | // Language directory
165: | $this->language->addPath('extension/' . $extension, DIR_EXTENSION . $extension . '/admin/language/');
166: |
167: | // Config directory
168: | $this->config->addPath('extension/' . $extension, DIR_EXTENSION . $extension . '/system/config/');
169: |
170: | // Call install method if it exists
171: | $this->load->controller('extension/' . $extension . '/module/' . $code . '.install');
172: |
173: | $json['success'] = $this->language->get('text_success');
174: | }
175: |
176: | $this->response->addHeader('Content-Type: application/json');
177: | $this->response->setOutput(json_encode($json));
178: | }
179: |
180: | /**
181: | * Uninstall
182: | *
183: | * @return void
184: | */
185: | public function uninstall(): void {
186: | $this->load->language('extension/module');
187: |
188: | $json = [];
189: |
190: | if (!$this->user->hasPermission('modify', 'extension/module')) {
191: | $json['error'] = $this->language->get('error_permission');
192: | }
193: |
194: | if (!$json) {
195: | $this->load->model('setting/extension');
196: |
197: | $this->model_setting_extension->uninstall('module', $this->request->get['code']);
198: |
199: | $this->load->model('setting/module');
200: |
201: | $this->model_setting_module->deleteModulesByCode($this->request->get['extension'] . '.' . $this->request->get['code']);
202: |
203: | // Call uninstall method if it exists
204: | $this->load->controller('extension/' . $this->request->get['extension'] . '/module/' . $this->request->get['code'] . '.uninstall');
205: |
206: | $json['success'] = $this->language->get('text_success');
207: | }
208: |
209: | $this->response->addHeader('Content-Type: application/json');
210: | $this->response->setOutput(json_encode($json));
211: | }
212: |
213: | /**
214: | * Add
215: | *
216: | * @return void
217: | */
218: | public function add(): void {
219: | $this->load->language('extension/module');
220: |
221: | $json = [];
222: |
223: | if (!$this->user->hasPermission('modify', 'extension/module')) {
224: | $json['error'] = $this->language->get('error_permission');
225: | }
226: |
227: | if (!$json) {
228: | $this->load->language('extension/' . $this->request->get['extension'] . '/module/' . $this->request->get['code'], 'extension');
229: |
230: | $this->load->model('setting/module');
231: |
232: | $this->model_setting_module->addModule($this->request->get['extension'] . '.' . $this->request->get['code'], ['name' => $this->language->get('extension_heading_title')]);
233: |
234: | $json['success'] = $this->language->get('text_success');
235: | }
236: |
237: | $this->response->addHeader('Content-Type: application/json');
238: | $this->response->setOutput(json_encode($json));
239: | }
240: |
241: | /**
242: | * Delete
243: | *
244: | * @return void
245: | */
246: | public function delete(): void {
247: | $this->load->language('extension/module');
248: |
249: | $json = [];
250: |
251: | if (isset($this->request->get['module_id'])) {
252: | $module_id = $this->request->get['module_id'];
253: | } else {
254: | $module_id = 0;
255: | }
256: |
257: | if (!$this->user->hasPermission('modify', 'extension/module')) {
258: | $json['error'] = $this->language->get('error_permission');
259: | }
260: |
261: | if (!$json) {
262: | $this->load->model('setting/module');
263: |
264: | $this->model_setting_module->deleteModule($module_id);
265: |
266: | $json['success'] = $this->language->get('text_success');
267: | }
268: |
269: | $this->response->addHeader('Content-Type: application/json');
270: | $this->response->setOutput(json_encode($json));
271: | }
272: | }
273: |

OpenCart API API documentation generated by ApiGen dev-master