1: <?php
2: namespace Opencart\Admin\Controller\Extension\Opencart\Report;
3: /**
4: * Class Product Viewed
5: *
6: * @package Opencart\Admin\Controller\Extension\Opencart\Report
7: */
8: class ProductViewed extends \Opencart\System\Engine\Controller {
9: /**
10: * Index
11: *
12: * @return void
13: */
14: public function index(): void {
15: $this->load->language('extension/opencart/report/product_viewed');
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=report')
29: ];
30:
31: $data['breadcrumbs'][] = [
32: 'text' => $this->language->get('heading_title'),
33: 'href' => $this->url->link('extension/opencart/report/product_viewed', 'user_token=' . $this->session->data['user_token'])
34: ];
35:
36: $data['save'] = $this->url->link('extension/opencart/report/product_viewed.save', 'user_token=' . $this->session->data['user_token']);
37: $data['back'] = $this->url->link('marketplace/extension', 'user_token=' . $this->session->data['user_token'] . '&type=report');
38:
39: $data['report_product_viewed_status'] = $this->config->get('report_product_viewed_status');
40: $data['report_product_viewed_sort_order'] = $this->config->get('report_product_viewed_sort_order');
41:
42: $data['header'] = $this->load->controller('common/header');
43: $data['column_left'] = $this->load->controller('common/column_left');
44: $data['footer'] = $this->load->controller('common/footer');
45:
46: $this->response->setOutput($this->load->view('extension/opencart/report/product_viewed_form', $data));
47: }
48:
49: /**
50: * Save
51: *
52: * @return void
53: */
54: public function save(): void {
55: $this->load->language('extension/opencart/report/product_viewed');
56:
57: $json = [];
58:
59: if (!$this->user->hasPermission('modify', 'extension/opencart/report/product_viewed')) {
60: $json['error'] = $this->language->get('error_permission');
61: }
62:
63: if (!$json) {
64: $this->load->model('setting/setting');
65:
66: $this->model_setting_setting->editSetting('report_product_viewed', $this->request->post);
67:
68: $json['success'] = $this->language->get('text_success');
69: }
70:
71: $this->response->addHeader('Content-Type: application/json');
72: $this->response->setOutput(json_encode($json));
73: }
74:
75: /**
76: * Install
77: *
78: * @return void
79: */
80: public function install(): void {
81: if ($this->user->hasPermission('modify', 'extension/report')) {
82: $this->load->model('extension/opencart/report/product_viewed');
83:
84: $this->model_extension_opencart_report_product_viewed->install();
85: }
86: }
87:
88: /**
89: * Uninstall
90: *
91: * @return void
92: */
93: public function uninstall(): void {
94: if ($this->user->hasPermission('modify', 'extension/report')) {
95: $this->load->model('extension/opencart/report/product_viewed');
96:
97: $this->model_extension_opencart_report_product_viewed->uninstall();
98: }
99: }
100:
101: /**
102: * Report
103: *
104: * @return void
105: */
106: public function report(): void {
107: $this->load->language('extension/opencart/report/product_viewed');
108:
109: $data['list'] = $this->getReport();
110:
111: $data['user_token'] = $this->session->data['user_token'];
112:
113: $this->response->setOutput($this->load->view('extension/opencart/report/product_viewed', $data));
114: }
115:
116: /**
117: * List
118: *
119: * @return void
120: */
121: public function list(): void {
122: $this->load->language('extension/opencart/report/product_viewed');
123:
124: $this->response->setOutput($this->getReport());
125: }
126:
127: /**
128: * Get Report
129: *
130: * @return string
131: */
132: public function getReport(): string {
133: if (isset($this->request->get['page'])) {
134: $page = (int)$this->request->get['page'];
135: } else {
136: $page = 1;
137: }
138:
139: $data['products'] = [];
140:
141: $this->load->model('extension/opencart/report/product_viewed');
142: $this->load->model('catalog/product');
143:
144: $total = $this->model_extension_opencart_report_product_viewed->getTotal();
145:
146: $viewed_total = $this->model_extension_opencart_report_product_viewed->getTotalViewed();
147:
148: $results = $this->model_extension_opencart_report_product_viewed->getViewed(($page - 1) * $this->config->get('config_pagination'), $this->config->get('config_pagination'));
149:
150: foreach ($results as $result) {
151: $product_info = $this->model_catalog_product->getProduct($result['product_id']);
152:
153: if ($product_info) {
154: if ($result['viewed']) {
155: $percent = round(($result['viewed'] / $total) * 100, 2);
156: } else {
157: $percent = 0;
158: }
159:
160: $data['products'][] = [
161: 'name' => $product_info['name'],
162: 'model' => $product_info['model'],
163: 'viewed' => $result['viewed'],
164: 'percent' => $percent . '%'
165: ];
166: }
167: }
168:
169: $url = '';
170:
171: if (isset($this->request->get['page'])) {
172: $url .= '&page=' . $this->request->get['page'];
173: }
174:
175: $data['pagination'] = $this->load->controller('common/pagination', [
176: 'total' => $viewed_total,
177: 'page' => $page,
178: 'limit' => $this->config->get('config_pagination'),
179: 'url' => $this->url->link('extension/opencart/report/product_viewed.list', 'user_token=' . $this->session->data['user_token'] . '&code=product_viewed&page={page}')
180: ]);
181:
182: $data['results'] = sprintf($this->language->get('text_pagination'), ($viewed_total) ? (($page - 1) * $this->config->get('config_pagination')) + 1 : 0, ((($page - 1) * $this->config->get('config_pagination')) > ($viewed_total - $this->config->get('config_pagination'))) ? $viewed_total : ((($page - 1) * $this->config->get('config_pagination')) + $this->config->get('config_pagination')), $viewed_total, ceil($viewed_total / $this->config->get('config_pagination')));
183:
184: return $this->load->view('extension/opencart/report/product_viewed_list', $data);
185: }
186:
187: /**
188: * Generate
189: *
190: * @return void
191: */
192: public function generate(): void {
193: $this->load->language('extension/opencart/report/product_viewed');
194:
195: $json = [];
196:
197: if (isset($this->request->get['page'])) {
198: $page = (int)$this->request->get['page'];
199: } else {
200: $page = 1;
201: }
202:
203: $limit = 10;
204:
205: if (!$this->user->hasPermission('modify', 'extension/opencart/report/product_viewed')) {
206: $json['error'] = $this->language->get('error_permission');
207: }
208:
209: if (!$json) {
210: $this->load->model('extension/opencart/report/product_viewed');
211:
212: if ($page == 1) {
213: $this->model_extension_opencart_report_product_viewed->clear();
214: }
215:
216: $filter_data = [
217: 'start' => ($page - 1) * $limit,
218: 'limit' => $limit
219: ];
220:
221: $this->load->model('catalog/product');
222:
223: $product_total = $this->model_catalog_product->getTotalProducts();
224:
225: $products = $this->model_catalog_product->getProducts($filter_data);
226:
227: foreach ($products as $product) {
228: $this->model_extension_opencart_report_product_viewed->addReport($product['product_id'], $this->model_catalog_product->getTotalReports($product['product_id']));
229: }
230:
231: if (($page * $limit) <= $product_total) {
232: $json['text'] = sprintf($this->language->get('text_progress'), ($page - 1) * $limit, $product_total);
233:
234: $json['next'] = $this->url->link('extension/opencart/report/product_viewed.generate', 'user_token=' . $this->session->data['user_token'] . '&page=' . ($page + 1), true);
235: } else {
236: $json['success'] = $this->language->get('text_success');
237: }
238: }
239:
240: $this->response->addHeader('Content-Type: application/json');
241: $this->response->setOutput(json_encode($json));
242: }
243: }
244: