1: | <?php
2: | namespace Opencart\Admin\Controller\Extension;
3: | /**
4: | * Class Analytics
5: | *
6: | * @package Opencart\Admin\Controller\Extension
7: | */
8: | class Analytics 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/analytics');
25: |
26: | // Promotion
27: | $data['promotion'] = $this->load->controller('marketplace/promotion');
28: |
29: | $available = [];
30: |
31: | $results = glob(DIR_EXTENSION . '*/admin/controller/analytics/*.php');
32: |
33: | foreach ($results as $result) {
34: | $available[] = basename($result, '.php');
35: | }
36: |
37: | $installed = [];
38: |
39: | $this->load->model('setting/extension');
40: |
41: | $extensions = $this->model_setting_extension->getExtensionsByType('analytics');
42: |
43: | foreach ($extensions as $extension) {
44: | if (in_array($extension['code'], $available)) {
45: | $installed[] = $extension['code'];
46: | } else {
47: | // Uninstall any missing extensions
48: | $this->model_setting_extension->uninstall('analytics', $extension['code']);
49: | }
50: | }
51: |
52: | $this->load->model('setting/store');
53: | $this->load->model('setting/setting');
54: |
55: | $stores = $this->model_setting_store->getStores();
56: |
57: | $data['extensions'] = [];
58: |
59: | $this->load->model('setting/extension');
60: |
61: | if ($results) {
62: | foreach ($results as $result) {
63: | $path = substr($result, strlen(DIR_EXTENSION));
64: |
65: | $extension = substr($path, 0, strpos($path, '/'));
66: |
67: | $code = basename($result, '.php');
68: |
69: | $this->load->language('extension/' . $extension . '/analytics/' . $code, $code);
70: |
71: | $store_data = [];
72: |
73: | $store_data[] = [
74: | 'name' => $this->config->get('config_name'),
75: | 'edit' => $this->url->link('extension/' . $extension . '/analytics/' . $code, 'user_token=' . $this->session->data['user_token'] . '&store_id=0'),
76: | 'status' => $this->config->get('analytics_' . $code . '_status') ? $this->language->get('text_enabled') : $this->language->get('text_disabled')
77: | ];
78: |
79: | foreach ($stores as $store) {
80: | $store_data[] = [
81: | 'name' => $store['name'],
82: | 'edit' => $this->url->link('extension/' . $extension . '/analytics/' . $code, 'user_token=' . $this->session->data['user_token'] . '&store_id=' . $store['store_id']),
83: | 'status' => $this->model_setting_setting->getValue('analytics_' . $code . '_status', $store['store_id']) ? $this->language->get('text_enabled') : $this->language->get('text_disabled')
84: | ];
85: | }
86: |
87: | $data['extensions'][] = [
88: | 'name' => $this->language->get($code . '_heading_title'),
89: | 'install' => $this->url->link('extension/analytics.install', 'user_token=' . $this->session->data['user_token'] . '&extension=' . $extension . '&code=' . $code),
90: | 'uninstall' => $this->url->link('extension/analytics.uninstall', 'user_token=' . $this->session->data['user_token'] . '&extension=' . $extension . '&code=' . $code),
91: | 'installed' => in_array($code, $installed),
92: | 'store' => $store_data
93: | ];
94: | }
95: | }
96: |
97: | return $this->load->view('extension/analytics', $data);
98: | }
99: |
100: | /**
101: | * Install
102: | *
103: | * @return void
104: | */
105: | public function install(): void {
106: | $this->load->language('extension/analytics');
107: |
108: | $json = [];
109: |
110: | if (isset($this->request->get['extension'])) {
111: | $extension = basename($this->request->get['extension']);
112: | } else {
113: | $extension = '';
114: | }
115: |
116: | if (isset($this->request->get['code'])) {
117: | $code = basename($this->request->get['code']);
118: | } else {
119: | $code = '';
120: | }
121: |
122: | if (!$this->user->hasPermission('modify', 'extension/analytics')) {
123: | $json['error'] = $this->language->get('error_permission');
124: | }
125: |
126: | if (!is_file(DIR_EXTENSION . $extension . '/admin/controller/analytics/' . $code . '.php')) {
127: | $json['error'] = $this->language->get('error_extension');
128: | }
129: |
130: | if (!$json) {
131: | $this->load->model('setting/extension');
132: |
133: | $this->model_setting_extension->install('analytics', $extension, $code);
134: |
135: | $this->load->model('user/user_group');
136: |
137: | $this->model_user_user_group->addPermission($this->user->getGroupId(), 'access', 'extension/' . $extension . '/analytics/' . $code);
138: | $this->model_user_user_group->addPermission($this->user->getGroupId(), 'modify', 'extension/' . $extension . '/analytics/' . $code);
139: |
140: | $namespace = str_replace(['_', '/'], ['', '\\'], ucwords($extension, '_/'));
141: |
142: | // Register controllers, models and system extension folders
143: | $this->autoloader->register('Opencart\Admin\Controller\Extension\\' . $namespace, DIR_EXTENSION . $extension . '/admin/controller/');
144: | $this->autoloader->register('Opencart\Admin\Model\Extension\\' . $namespace, DIR_EXTENSION . $extension . '/admin/model/');
145: | $this->autoloader->register('Opencart\System\Extension\\' . $namespace, DIR_EXTENSION . $extension . '/system/');
146: |
147: | // Template directory
148: | $this->template->addPath('extension/' . $extension, DIR_EXTENSION . $extension . '/admin/view/template/');
149: |
150: | // Language directory
151: | $this->language->addPath('extension/' . $extension, DIR_EXTENSION . $extension . '/admin/language/');
152: |
153: | // Config directory
154: | $this->config->addPath('extension/' . $extension, DIR_EXTENSION . $extension . '/system/config/');
155: |
156: | // Call install method if it exists
157: | $this->load->controller('extension/' . $extension . '/analytics/' . $code . '.install');
158: |
159: | $json['success'] = $this->language->get('text_success');
160: | }
161: |
162: | $this->response->addHeader('Content-Type: application/json');
163: | $this->response->setOutput(json_encode($json));
164: | }
165: |
166: | /**
167: | * Uninstall
168: | *
169: | * @return void
170: | */
171: | public function uninstall(): void {
172: | $this->load->language('extension/analytics');
173: |
174: | $json = [];
175: |
176: | if (!$this->user->hasPermission('modify', 'extension/analytics')) {
177: | $json['error'] = $this->language->get('error_permission');
178: | }
179: |
180: | if (!$json) {
181: | $this->load->model('setting/extension');
182: |
183: | $this->model_setting_extension->uninstall('analytics', $this->request->get['code']);
184: |
185: | // Call uninstall method if it exists
186: | $this->load->controller('extension/' . $this->request->get['extension'] . '/analytics/' . $this->request->get['code'] . '.uninstall');
187: |
188: | $json['success'] = $this->language->get('text_success');
189: | }
190: |
191: | $this->response->addHeader('Content-Type: application/json');
192: | $this->response->setOutput(json_encode($json));
193: | }
194: | }
195: |

OpenCart API API documentation generated by ApiGen dev-master