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