1: | <?php
|
2: | namespace Opencart\Admin\Controller\Extension\Opencart\Dashboard;
|
3: | |
4: | |
5: | |
6: | |
7: |
|
8: | class Chart extends \Opencart\System\Engine\Controller {
|
9: | |
10: | |
11: | |
12: | |
13: |
|
14: | public function index(): void {
|
15: | $this->load->language('extension/opencart/dashboard/chart');
|
16: |
|
17: | $this->document->setTitle($this->language->get('heading_title'));
|
18: |
|
19: | $data['breadcrumbs'] = [];
|
20: |
|
21: | $data['breadcrumbs'][] = [
|
22: | 'text' => $this->language->get('text_home'),
|
23: | 'href' => $this->url->link('common/dashboard', 'user_token=' . $this->session->data['user_token'])
|
24: | ];
|
25: |
|
26: | $data['breadcrumbs'][] = [
|
27: | 'text' => $this->language->get('text_extension'),
|
28: | 'href' => $this->url->link('marketplace/extension', 'user_token=' . $this->session->data['user_token'] . '&type=dashboard')
|
29: | ];
|
30: |
|
31: | $data['breadcrumbs'][] = [
|
32: | 'text' => $this->language->get('heading_title'),
|
33: | 'href' => $this->url->link('extension/opencart/dashboard/chart', 'user_token=' . $this->session->data['user_token'])
|
34: | ];
|
35: |
|
36: | $data['save'] = $this->url->link('extension/opencart/dashboard/chart.save', 'user_token=' . $this->session->data['user_token']);
|
37: | $data['back'] = $this->url->link('marketplace/extension', 'user_token=' . $this->session->data['user_token'] . '&type=dashboard');
|
38: |
|
39: | $data['dashboard_chart_width'] = $this->config->get('dashboard_chart_width');
|
40: |
|
41: | $data['columns'] = [];
|
42: |
|
43: | for ($i = 3; $i <= 12; $i++) {
|
44: | $data['columns'][] = $i;
|
45: | }
|
46: |
|
47: | $data['dashboard_chart_status'] = $this->config->get('dashboard_chart_status');
|
48: | $data['dashboard_chart_sort_order'] = $this->config->get('dashboard_chart_sort_order');
|
49: |
|
50: | $data['header'] = $this->load->controller('common/header');
|
51: | $data['column_left'] = $this->load->controller('common/column_left');
|
52: | $data['footer'] = $this->load->controller('common/footer');
|
53: |
|
54: | $this->response->setOutput($this->load->view('extension/opencart/dashboard/chart_form', $data));
|
55: | }
|
56: |
|
57: | |
58: | |
59: | |
60: | |
61: |
|
62: | public function save(): void {
|
63: | $this->load->language('extension/opencart/dashboard/chart');
|
64: |
|
65: | $json = [];
|
66: |
|
67: | if (!$this->user->hasPermission('modify', 'extension/opencart/dashboard/chart')) {
|
68: | $json['error'] = $this->language->get('error_permission');
|
69: | }
|
70: |
|
71: | if (!$json) {
|
72: | $this->load->model('setting/setting');
|
73: |
|
74: | $this->model_setting_setting->editSetting('dashboard_chart', $this->request->post);
|
75: |
|
76: | $json['success'] = $this->language->get('text_success');
|
77: | }
|
78: |
|
79: | $this->response->addHeader('Content-Type: application/json');
|
80: | $this->response->setOutput(json_encode($json));
|
81: | }
|
82: |
|
83: | |
84: | |
85: | |
86: | |
87: |
|
88: | public function dashboard(): string {
|
89: | $this->load->language('extension/opencart/dashboard/chart');
|
90: |
|
91: | $data['user_token'] = $this->session->data['user_token'];
|
92: |
|
93: | return $this->load->view('extension/opencart/dashboard/chart_info', $data);
|
94: | }
|
95: |
|
96: | |
97: | |
98: | |
99: | |
100: |
|
101: | public function chart(): void {
|
102: | $this->load->language('extension/opencart/dashboard/chart');
|
103: |
|
104: | $json = [];
|
105: |
|
106: | $this->load->model('extension/opencart/report/customer');
|
107: | $this->load->model('extension/opencart/report/sale');
|
108: |
|
109: | $json['order'] = [];
|
110: | $json['customer'] = [];
|
111: | $json['xaxis'] = [];
|
112: |
|
113: | $json['order']['label'] = $this->language->get('text_order');
|
114: | $json['customer']['label'] = $this->language->get('text_customer');
|
115: | $json['order']['data'] = [];
|
116: | $json['customer']['data'] = [];
|
117: |
|
118: | if (isset($this->request->get['range'])) {
|
119: | $range = $this->request->get['range'];
|
120: | } else {
|
121: | $range = 'day';
|
122: | }
|
123: |
|
124: | switch ($range) {
|
125: | default:
|
126: | case 'day':
|
127: | $results = $this->model_extension_opencart_report_sale->getTotalOrdersByDay();
|
128: |
|
129: | foreach ($results as $key => $value) {
|
130: | $json['order']['data'][] = [$key, $value['total']];
|
131: | }
|
132: |
|
133: | $results = $this->model_extension_opencart_report_customer->getTotalCustomersByDay();
|
134: |
|
135: | foreach ($results as $key => $value) {
|
136: | $json['customer']['data'][] = [$key, $value['total']];
|
137: | }
|
138: |
|
139: | for ($i = 0; $i < 24; $i++) {
|
140: | $json['xaxis'][] = [$i, $i];
|
141: | }
|
142: | break;
|
143: | case 'week':
|
144: | $results = $this->model_extension_opencart_report_sale->getTotalOrdersByWeek();
|
145: |
|
146: | foreach ($results as $key => $value) {
|
147: | $json['order']['data'][] = [$key, $value['total']];
|
148: | }
|
149: |
|
150: | $results = $this->model_extension_opencart_report_customer->getTotalCustomersByWeek();
|
151: |
|
152: | foreach ($results as $key => $value) {
|
153: | $json['customer']['data'][] = [$key, $value['total']];
|
154: | }
|
155: |
|
156: | $date_start = strtotime('-' . date('w') . ' days');
|
157: |
|
158: | for ($i = 0; $i < 7; $i++) {
|
159: | $date = date('Y-m-d', $date_start + ($i * 86400));
|
160: |
|
161: | $json['xaxis'][] = [date('w', strtotime($date)), date('D', strtotime($date))];
|
162: | }
|
163: | break;
|
164: | case 'month':
|
165: | $results = $this->model_extension_opencart_report_sale->getTotalOrdersByMonth();
|
166: |
|
167: | foreach ($results as $key => $value) {
|
168: | $json['order']['data'][] = [$key, $value['total']];
|
169: | }
|
170: |
|
171: | $results = $this->model_extension_opencart_report_customer->getTotalCustomersByMonth();
|
172: |
|
173: | foreach ($results as $key => $value) {
|
174: | $json['customer']['data'][] = [$key, $value['total']];
|
175: | }
|
176: |
|
177: | for ($i = 1; $i <= date('t'); $i++) {
|
178: | $date = date('Y') . '-' . date('m') . '-' . $i;
|
179: |
|
180: | $json['xaxis'][] = [date('j', strtotime($date)), date('d', strtotime($date))];
|
181: | }
|
182: | break;
|
183: | case 'year':
|
184: | $results = $this->model_extension_opencart_report_sale->getTotalOrdersByYear();
|
185: |
|
186: | foreach ($results as $key => $value) {
|
187: | $json['order']['data'][] = [$key, $value['total']];
|
188: | }
|
189: |
|
190: | $results = $this->model_extension_opencart_report_customer->getTotalCustomersByYear();
|
191: |
|
192: | foreach ($results as $key => $value) {
|
193: | $json['customer']['data'][] = [$key, $value['total']];
|
194: | }
|
195: |
|
196: | for ($i = 1; $i <= 12; $i++) {
|
197: | $json['xaxis'][] = [$i, date('M', mktime(0, 0, 0, $i, 1))];
|
198: | }
|
199: | break;
|
200: | }
|
201: |
|
202: | $this->response->addHeader('Content-Type: application/json');
|
203: | $this->response->setOutput(json_encode($json));
|
204: | }
|
205: | }
|
206: | |