1: | <?php
2: | namespace Opencart\Admin\Controller\Extension;
3: | /**
4: | * Class Feed
5: | *
6: | * @package Opencart\Admin\Controller\Extension
7: | */
8: | class Feed extends \Opencart\System\Engine\Controller {
9: | /**
10: | * @var array<string, string>
11: | */
12: | private array $error = [];
13: |
14: | /**
15: | * Index
16: | *
17: | * @return void
18: | */
19: | public function index(): void {
20: | $this->response->setOutput($this->getList());
21: | }
22: |
23: | /**
24: | * Get List
25: | *
26: | * @return string
27: | */
28: | public function getList(): string {
29: | $this->load->language('extension/feed');
30: |
31: | $available = [];
32: |
33: | $results = glob(DIR_EXTENSION . '*/admin/controller/feed/*.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('feed');
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('feed', $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 . '/feed/' . $code, $code);
64: |
65: | $data['extensions'][] = [
66: | 'name' => $this->language->get($code . '_heading_title'),
67: | 'status' => $this->config->get('feed_' . $code . '_status') ? $this->language->get('text_enabled') : $this->language->get('text_disabled'),
68: | 'install' => $this->url->link('extension/feed.install', 'user_token=' . $this->session->data['user_token'] . '&extension=' . $extension . '&code=' . $code),
69: | 'uninstall' => $this->url->link('extension/feed.uninstall', 'user_token=' . $this->session->data['user_token'] . '&extension=' . $extension . '&code=' . $code),
70: | 'installed' => in_array($code, $installed),
71: | 'edit' => $this->url->link('extension/' . $extension . '/feed/' . $code, 'user_token=' . $this->session->data['user_token'])
72: | ];
73: | }
74: | }
75: |
76: | $data['promotion'] = $this->load->controller('marketplace/promotion');
77: |
78: | return $this->load->view('extension/feed', $data);
79: | }
80: |
81: | /**
82: | * Validate
83: | *
84: | * @return bool
85: | */
86: | protected function validate(): bool {
87: | if (!$this->user->hasPermission('modify', 'extension/feed')) {
88: | $this->error['warning'] = $this->language->get('error_permission');
89: | }
90: |
91: | return !$this->error;
92: | }
93: |
94: | /**
95: | * Install
96: | *
97: | * @return void
98: | */
99: | public function install(): void {
100: | $this->load->language('extension/feed');
101: |
102: | $json = [];
103: |
104: | if (isset($this->request->get['extension'])) {
105: | $extension = basename($this->request->get['extension']);
106: | } else {
107: | $extension = '';
108: | }
109: |
110: | if (isset($this->request->get['code'])) {
111: | $code = basename($this->request->get['code']);
112: | } else {
113: | $code = '';
114: | }
115: |
116: | if (!$this->user->hasPermission('modify', 'extension/feed')) {
117: | $json['error'] = $this->language->get('error_permission');
118: | }
119: |
120: | if (!is_file(DIR_EXTENSION . $extension . '/admin/controller/feed/' . $code . '.php')) {
121: | $json['error'] = $this->language->get('error_extension');
122: | }
123: |
124: | if (!$json) {
125: | $this->load->model('setting/extension');
126: |
127: | $this->model_setting_extension->install('feed', $extension, $code);
128: |
129: | $this->load->model('user/user_group');
130: |
131: | $this->model_user_user_group->addPermission($this->user->getGroupId(), 'access', 'extension/' . $extension . '/feed/' . $code);
132: | $this->model_user_user_group->addPermission($this->user->getGroupId(), 'modify', 'extension/' . $extension . '/feed/' . $code);
133: |
134: | $namespace = str_replace(['_', '/'], ['', '\\'], ucwords($extension, '_/'));
135: |
136: | // Register controllers, models and system extension folders
137: | $this->autoloader->register('Opencart\Admin\Controller\Extension\\' . $namespace, DIR_EXTENSION . $extension . '/admin/controller/');
138: | $this->autoloader->register('Opencart\Admin\Model\Extension\\' . $namespace, DIR_EXTENSION . $extension . '/admin/model/');
139: | $this->autoloader->register('Opencart\System\Extension\\' . $namespace, DIR_EXTENSION . $extension . '/system/');
140: |
141: | // Template directory
142: | $this->template->addPath('extension/' . $extension, DIR_EXTENSION . $extension . '/admin/view/template/');
143: |
144: | // Language directory
145: | $this->language->addPath('extension/' . $extension, DIR_EXTENSION . $extension . '/admin/language/');
146: |
147: | // Config directory
148: | $this->config->addPath('extension/' . $extension, DIR_EXTENSION . $extension . '/system/config/');
149: |
150: | // Call install method if it exists
151: | $this->load->controller('extension/' . $extension . '/feed/' . $code . '.install');
152: |
153: | $json['success'] = $this->language->get('text_success');
154: | }
155: |
156: | $this->response->addHeader('Content-Type: application/json');
157: | $this->response->setOutput(json_encode($json));
158: | }
159: |
160: | /**
161: | * Uninstall
162: | *
163: | * @return void
164: | */
165: | public function uninstall(): void {
166: | $this->load->language('extension/feed');
167: |
168: | $json = [];
169: |
170: | if (!$this->user->hasPermission('modify', 'extension/feed')) {
171: | $json['error'] = $this->language->get('error_permission');
172: | }
173: |
174: | if (!$json) {
175: | $this->load->model('setting/extension');
176: |
177: | $this->model_setting_extension->uninstall('feed', $this->request->get['code']);
178: |
179: | // Call uninstall method if it exists
180: | $this->load->controller('extension/' . $this->request->get['extension'] . '/feed/' . $this->request->get['code'] . '.uninstall');
181: |
182: | $json['success'] = $this->language->get('text_success');
183: | }
184: |
185: | $this->response->addHeader('Content-Type: application/json');
186: | $this->response->setOutput(json_encode($json));
187: | }
188: | }
189: |

OpenCart API API documentation generated by ApiGen dev-master