1: <?php
2: namespace Opencart\Admin\Controller\Extension\Opencart\Module;
3: /**
4: * Class Best Seller
5: *
6: * @package Opencart\Admin\Controller\Extension\Opencart\Module
7: */
8: class BestSeller extends \Opencart\System\Engine\Controller {
9: /**
10: * Index
11: *
12: * @return void
13: */
14: public function index(): void {
15: $this->load->language('extension/opencart/module/bestseller');
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=module')
29: ];
30:
31: if (!isset($this->request->get['module_id'])) {
32: $data['breadcrumbs'][] = [
33: 'text' => $this->language->get('heading_title'),
34: 'href' => $this->url->link('extension/opencart/module/bestseller', 'user_token=' . $this->session->data['user_token'])
35: ];
36: } else {
37: $data['breadcrumbs'][] = [
38: 'text' => $this->language->get('heading_title'),
39: 'href' => $this->url->link('extension/opencart/module/bestseller', 'user_token=' . $this->session->data['user_token'] . '&module_id=' . $this->request->get['module_id'])
40: ];
41: }
42:
43: if (!isset($this->request->get['module_id'])) {
44: $data['save'] = $this->url->link('extension/opencart/module/bestseller.save', 'user_token=' . $this->session->data['user_token']);
45: } else {
46: $data['save'] = $this->url->link('extension/opencart/module/bestseller.save', 'user_token=' . $this->session->data['user_token'] . '&module_id=' . $this->request->get['module_id']);
47: }
48:
49: $data['back'] = $this->url->link('marketplace/extension', 'user_token=' . $this->session->data['user_token'] . '&type=module');
50:
51: if (isset($this->request->get['module_id'])) {
52: $this->load->model('setting/module');
53:
54: $module_info = $this->model_setting_module->getModule($this->request->get['module_id']);
55: }
56:
57: if (isset($module_info['name'])) {
58: $data['name'] = $module_info['name'];
59: } else {
60: $data['name'] = '';
61: }
62:
63: if (isset($module_info['axis'])) {
64: $data['axis'] = $module_info['axis'];
65: } else {
66: $data['axis'] = '';
67: }
68:
69: if (isset($module_info['limit'])) {
70: $data['limit'] = $module_info['limit'];
71: } else {
72: $data['limit'] = 5;
73: }
74:
75: if (isset($module_info['width'])) {
76: $data['width'] = $module_info['width'];
77: } else {
78: $data['width'] = 200;
79: }
80:
81: if (isset($module_info['height'])) {
82: $data['height'] = $module_info['height'];
83: } else {
84: $data['height'] = 200;
85: }
86:
87: if (isset($module_info['status'])) {
88: $data['status'] = $module_info['status'];
89: } else {
90: $data['status'] = '';
91: }
92:
93: if (isset($this->request->get['module_id'])) {
94: $data['module_id'] = (int)$this->request->get['module_id'];
95: } else {
96: $data['module_id'] = 0;
97: }
98:
99: $data['report'] = $this->getReport();
100:
101: $data['user_token'] = $this->session->data['user_token'];
102:
103: $data['header'] = $this->load->controller('common/header');
104: $data['column_left'] = $this->load->controller('common/column_left');
105: $data['footer'] = $this->load->controller('common/footer');
106:
107: $this->response->setOutput($this->load->view('extension/opencart/module/bestseller', $data));
108: }
109:
110: /**
111: * Save
112: *
113: * @return void
114: */
115: public function save(): void {
116: $this->load->language('extension/opencart/module/bestseller');
117:
118: $json = [];
119:
120: if (!$this->user->hasPermission('modify', 'extension/opencart/module/bestseller')) {
121: $json['error']['warning'] = $this->language->get('error_permission');
122: }
123:
124: if ((oc_strlen($this->request->post['name']) < 3) || (oc_strlen($this->request->post['name']) > 64)) {
125: $json['error']['name'] = $this->language->get('error_name');
126: }
127:
128: if (!$this->request->post['width']) {
129: $json['error']['width'] = $this->language->get('error_width');
130: }
131:
132: if (!$this->request->post['height']) {
133: $json['error']['height'] = $this->language->get('error_height');
134: }
135:
136: if (!$json) {
137: $this->load->model('setting/module');
138:
139: if (!$this->request->post['module_id']) {
140: $json['module_id'] = $this->model_setting_module->addModule('opencart.bestseller', $this->request->post);
141: } else {
142: $this->model_setting_module->editModule($this->request->post['module_id'], $this->request->post);
143: }
144:
145: $this->cache->delete('product');
146:
147: $json['success'] = $this->language->get('text_success');
148: }
149:
150: $this->response->addHeader('Content-Type: application/json');
151: $this->response->setOutput(json_encode($json));
152: }
153:
154: /**
155: * Install
156: *
157: * @return void
158: */
159: public function install(): void {
160: if ($this->user->hasPermission('modify', 'extension/opencart/module/bestseller')) {
161: $this->load->model('extension/opencart/module/bestseller');
162:
163: $this->model_extension_opencart_module_bestseller->install();
164: }
165: }
166:
167: /**
168: * Uninstall
169: *
170: * @return void
171: */
172: public function uninstall(): void {
173: if ($this->user->hasPermission('modify', 'extension/opencart/module/bestseller')) {
174: $this->load->model('extension/opencart/module/bestseller');
175:
176: $this->model_extension_opencart_module_bestseller->uninstall();
177: }
178: }
179:
180: /**
181: * Report
182: *
183: * @return void
184: */
185: public function report(): void {
186: $this->load->language('extension/opencart/module/bestseller');
187:
188: $this->response->setOutput($this->getReport());
189: }
190:
191: /**
192: * Get Report
193: *
194: * @return string
195: */
196: public function getReport(): string {
197: if (isset($this->request->get['page']) && $this->request->get['route'] == 'extension/opencart/module/bestseller.report') {
198: $page = (int)$this->request->get['page'];
199: } else {
200: $page = 1;
201: }
202:
203: $limit = 10;
204:
205: $data['reports'] = [];
206:
207: $this->load->model('extension/opencart/module/bestseller');
208: $this->load->model('catalog/product');
209:
210: $results = $this->model_extension_opencart_module_bestseller->getReports(($page - 1) * $limit, $limit);
211:
212: foreach ($results as $result) {
213: $product_info = $this->model_catalog_product->getProduct($result['product_id']);
214:
215: if ($product_info) {
216: $product = $product_info['name'];
217: } else {
218: $product = '';
219: }
220:
221: $data['reports'][] = [
222: 'product' => $product,
223: 'total' => $result['total'],
224: 'edit' => $this->url->link('catalog/product.edit', 'user_token=' . $this->session->data['user_token'] . '&product_id=' . $result['product_id'])
225: ];
226: }
227:
228: $report_total = $this->model_extension_opencart_module_bestseller->getTotalReports();
229:
230: $data['pagination'] = $this->load->controller('common/pagination', [
231: 'total' => $report_total,
232: 'page' => $page,
233: 'limit' => $limit,
234: 'url' => $this->url->link('extension/opencart/module/bestseller.report', 'user_token=' . $this->session->data['user_token'] . '&page={page}')
235: ]);
236:
237: $data['results'] = sprintf($this->language->get('text_pagination'), ($report_total) ? (($page - 1) * $limit) + 1 : 0, ((($page - 1) * $limit) > ($report_total - $limit)) ? $report_total : ((($page - 1) * $limit) + $limit), $report_total, ceil($report_total / $limit));
238:
239: return $this->load->view('extension/opencart/module/bestseller_report', $data);
240: }
241:
242: /**
243: * Sync
244: *
245: * @return void
246: */
247: public function sync(): void {
248: $this->load->language('extension/opencart/module/bestseller');
249:
250: $json = [];
251:
252: if (isset($this->request->get['page'])) {
253: $page = (int)$this->request->get['page'];
254: } else {
255: $page = 1;
256: }
257:
258: if (!$this->user->hasPermission('modify', 'extension/opencart/module/bestseller')) {
259: $json['error'] = $this->language->get('error_permission');
260: }
261:
262: if (!$json) {
263: $this->load->model('extension/opencart/module/bestseller');
264: $this->load->model('catalog/product');
265: $this->load->model('sale/order');
266:
267: $total = $this->model_catalog_product->getTotalProducts();
268: $limit = 10;
269:
270: $start = ($page - 1) * $limit;
271: $end = $start > ($total - $limit) ? $total : ($start + $limit);
272:
273: $product_data = [
274: 'start' => ($page - 1) * $limit,
275: 'limit' => $limit
276: ];
277:
278: $results = $this->model_catalog_product->getProducts($product_data);
279:
280: foreach ($results as $result) {
281: $product_total = $this->model_sale_order->getTotalProductsByProductId($result['product_id']);
282:
283: if ($product_total) {
284: $this->model_extension_opencart_module_bestseller->editTotal($result['product_id'], $product_total);
285: } else {
286: $this->model_extension_opencart_module_bestseller->delete($result['product_id']);
287: }
288: }
289:
290: if ($end < $total) {
291: $json['text'] = sprintf($this->language->get('text_next'), $end, $total);
292:
293: $json['next'] = $this->url->link('extension/opencart/module/bestseller.sync', 'user_token=' . $this->session->data['user_token'] . '&page=' . ($page + 1), true);
294: } else {
295: $json['success'] = sprintf($this->language->get('text_next'), $end, $total);
296:
297: $json['next'] = '';
298: }
299: }
300:
301: $this->response->addHeader('Content-Type: application/json');
302: $this->response->setOutput(json_encode($json));
303: }
304: }
305: