1: <?php
2: namespace Opencart\Admin\Controller\Catalog;
3: /**
4: * Class Review
5: *
6: * @package Opencart\Admin\Controller\Catalog
7: */
8: class Review extends \Opencart\System\Engine\Controller {
9: /**
10: * Index
11: *
12: * @return void
13: */
14: public function index(): void {
15: $this->load->language('catalog/review');
16:
17: $this->document->setTitle($this->language->get('heading_title'));
18:
19: if (isset($this->request->get['filter_product'])) {
20: $filter_product = $this->request->get['filter_product'];
21: } else {
22: $filter_product = '';
23: }
24:
25: if (isset($this->request->get['filter_author'])) {
26: $filter_author = $this->request->get['filter_author'];
27: } else {
28: $filter_author = '';
29: }
30:
31: if (isset($this->request->get['filter_status'])) {
32: $filter_status = $this->request->get['filter_status'];
33: } else {
34: $filter_status = '';
35: }
36:
37: if (isset($this->request->get['filter_date_from'])) {
38: $filter_date_from = $this->request->get['filter_date_from'];
39: } else {
40: $filter_date_from = '';
41: }
42:
43: if (isset($this->request->get['filter_date_to'])) {
44: $filter_date_to = $this->request->get['filter_date_to'];
45: } else {
46: $filter_date_to = '';
47: }
48:
49: $url = '';
50:
51: if (isset($this->request->get['filter_product'])) {
52: $url .= '&filter_product=' . urlencode(html_entity_decode($this->request->get['filter_product'], ENT_QUOTES, 'UTF-8'));
53: }
54:
55: if (isset($this->request->get['filter_author'])) {
56: $url .= '&filter_author=' . urlencode(html_entity_decode($this->request->get['filter_author'], ENT_QUOTES, 'UTF-8'));
57: }
58:
59: if (isset($this->request->get['filter_status'])) {
60: $url .= '&filter_status=' . $this->request->get['filter_status'];
61: }
62:
63: if (isset($this->request->get['filter_date_from'])) {
64: $url .= '&filter_date_from=' . $this->request->get['filter_date_from'];
65: }
66:
67: if (isset($this->request->get['filter_date_to'])) {
68: $url .= '&filter_date_to=' . $this->request->get['filter_date_to'];
69: }
70:
71: if (isset($this->request->get['sort'])) {
72: $url .= '&sort=' . $this->request->get['sort'];
73: }
74:
75: if (isset($this->request->get['order'])) {
76: $url .= '&order=' . $this->request->get['order'];
77: }
78:
79: if (isset($this->request->get['page'])) {
80: $url .= '&page=' . $this->request->get['page'];
81: }
82:
83: $data['breadcrumbs'] = [];
84:
85: $data['breadcrumbs'][] = [
86: 'text' => $this->language->get('text_home'),
87: 'href' => $this->url->link('common/dashboard', 'user_token=' . $this->session->data['user_token'])
88: ];
89:
90: $data['breadcrumbs'][] = [
91: 'text' => $this->language->get('heading_title'),
92: 'href' => $this->url->link('catalog/review', 'user_token=' . $this->session->data['user_token'] . $url)
93: ];
94:
95: $data['add'] = $this->url->link('catalog/review.form', 'user_token=' . $this->session->data['user_token'] . $url);
96: $data['delete'] = $this->url->link('catalog/review.delete', 'user_token=' . $this->session->data['user_token']);
97:
98: $data['list'] = $this->getList();
99:
100: $data['filter_product'] = $filter_product;
101: $data['filter_author'] = $filter_author;
102: $data['filter_status'] = $filter_status;
103: $data['filter_date_from'] = $filter_date_from;
104: $data['filter_date_to'] = $filter_date_to;
105:
106: $data['user_token'] = $this->session->data['user_token'];
107:
108: $data['header'] = $this->load->controller('common/header');
109: $data['column_left'] = $this->load->controller('common/column_left');
110: $data['footer'] = $this->load->controller('common/footer');
111:
112: $this->response->setOutput($this->load->view('catalog/review', $data));
113: }
114:
115: /**
116: * List
117: *
118: * @return void
119: */
120: public function list(): void {
121: $this->load->language('catalog/review');
122:
123: $this->response->setOutput($this->getList());
124: }
125:
126: /**
127: * Get List
128: *
129: * @return string
130: */
131: protected function getList(): string {
132: if (isset($this->request->get['filter_product'])) {
133: $filter_product = $this->request->get['filter_product'];
134: } else {
135: $filter_product = '';
136: }
137:
138: if (isset($this->request->get['filter_author'])) {
139: $filter_author = $this->request->get['filter_author'];
140: } else {
141: $filter_author = '';
142: }
143:
144: if (isset($this->request->get['filter_status'])) {
145: $filter_status = $this->request->get['filter_status'];
146: } else {
147: $filter_status = '';
148: }
149:
150: if (isset($this->request->get['filter_date_from'])) {
151: $filter_date_from = $this->request->get['filter_date_from'];
152: } else {
153: $filter_date_from = '';
154: }
155:
156: if (isset($this->request->get['filter_date_to'])) {
157: $filter_date_to = $this->request->get['filter_date_to'];
158: } else {
159: $filter_date_to = '';
160: }
161:
162: if (isset($this->request->get['order'])) {
163: $order = (string)$this->request->get['order'];
164: } else {
165: $order = 'DESC';
166: }
167:
168: if (isset($this->request->get['sort'])) {
169: $sort = (string)$this->request->get['sort'];
170: } else {
171: $sort = 'r.date_added';
172: }
173:
174: if (isset($this->request->get['page'])) {
175: $page = (int)$this->request->get['page'];
176: } else {
177: $page = 1;
178: }
179:
180: $url = '';
181:
182: if (isset($this->request->get['filter_product'])) {
183: $url .= '&filter_product=' . urlencode(html_entity_decode($this->request->get['filter_product'], ENT_QUOTES, 'UTF-8'));
184: }
185:
186: if (isset($this->request->get['filter_author'])) {
187: $url .= '&filter_author=' . urlencode(html_entity_decode($this->request->get['filter_author'], ENT_QUOTES, 'UTF-8'));
188: }
189:
190: if (isset($this->request->get['filter_status'])) {
191: $url .= '&filter_status=' . $this->request->get['filter_status'];
192: }
193:
194: if (isset($this->request->get['filter_date_from'])) {
195: $url .= '&filter_date_from=' . $this->request->get['filter_date_from'];
196: }
197:
198: if (isset($this->request->get['filter_date_to'])) {
199: $url .= '&filter_date_to=' . $this->request->get['filter_date_to'];
200: }
201:
202: if (isset($this->request->get['sort'])) {
203: $url .= '&sort=' . $this->request->get['sort'];
204: }
205:
206: if (isset($this->request->get['order'])) {
207: $url .= '&order=' . $this->request->get['order'];
208: }
209:
210: if (isset($this->request->get['page'])) {
211: $url .= '&page=' . $this->request->get['page'];
212: }
213:
214: $data['action'] = $this->url->link('catalog/review.list', 'user_token=' . $this->session->data['user_token'] . $url);
215:
216: $data['reviews'] = [];
217:
218: $filter_data = [
219: 'filter_product' => $filter_product,
220: 'filter_author' => $filter_author,
221: 'filter_status' => $filter_status,
222: 'filter_date_from' => $filter_date_from,
223: 'filter_date_to' => $filter_date_to,
224: 'sort' => $sort,
225: 'order' => $order,
226: 'start' => ($page - 1) * $this->config->get('config_pagination_admin'),
227: 'limit' => $this->config->get('config_pagination_admin')
228: ];
229:
230: $this->load->model('catalog/review');
231:
232: $results = $this->model_catalog_review->getReviews($filter_data);
233:
234: foreach ($results as $result) {
235: $data['reviews'][] = [
236: 'review_id' => $result['review_id'],
237: 'name' => $result['name'],
238: 'author' => $result['author'],
239: 'rating' => $result['rating'],
240: 'status' => $result['status'],
241: 'date_added' => date($this->language->get('date_format_short'), strtotime($result['date_added'])),
242: 'edit' => $this->url->link('catalog/review.form', 'user_token=' . $this->session->data['user_token'] . '&review_id=' . $result['review_id'] . $url)
243: ];
244: }
245:
246: $url = '';
247:
248: if (isset($this->request->get['filter_product'])) {
249: $url .= '&filter_product=' . urlencode(html_entity_decode($this->request->get['filter_product'], ENT_QUOTES, 'UTF-8'));
250: }
251:
252: if (isset($this->request->get['filter_author'])) {
253: $url .= '&filter_author=' . urlencode(html_entity_decode($this->request->get['filter_author'], ENT_QUOTES, 'UTF-8'));
254: }
255:
256: if (isset($this->request->get['filter_status'])) {
257: $url .= '&filter_status=' . $this->request->get['filter_status'];
258: }
259:
260: if (isset($this->request->get['filter_date_from'])) {
261: $url .= '&filter_date_from=' . $this->request->get['filter_date_from'];
262: }
263:
264: if (isset($this->request->get['filter_date_to'])) {
265: $url .= '&filter_date_to=' . $this->request->get['filter_date_to'];
266: }
267:
268: if ($order == 'ASC') {
269: $url .= '&order=DESC';
270: } else {
271: $url .= '&order=ASC';
272: }
273:
274: $data['sort_product'] = $this->url->link('catalog/review.list', 'user_token=' . $this->session->data['user_token'] . '&sort=pd.name' . $url);
275: $data['sort_author'] = $this->url->link('catalog/review.list', 'user_token=' . $this->session->data['user_token'] . '&sort=r.author' . $url);
276: $data['sort_rating'] = $this->url->link('catalog/review.list', 'user_token=' . $this->session->data['user_token'] . '&sort=r.rating' . $url);
277: $data['sort_date_added'] = $this->url->link('catalog/review.list', 'user_token=' . $this->session->data['user_token'] . '&sort=r.date_added' . $url);
278:
279: $url = '';
280:
281: if (isset($this->request->get['filter_product'])) {
282: $url .= '&filter_product=' . urlencode(html_entity_decode($this->request->get['filter_product'], ENT_QUOTES, 'UTF-8'));
283: }
284:
285: if (isset($this->request->get['filter_author'])) {
286: $url .= '&filter_author=' . urlencode(html_entity_decode($this->request->get['filter_author'], ENT_QUOTES, 'UTF-8'));
287: }
288:
289: if (isset($this->request->get['filter_status'])) {
290: $url .= '&filter_status=' . $this->request->get['filter_status'];
291: }
292:
293: if (isset($this->request->get['filter_date_from'])) {
294: $url .= '&filter_date_from=' . $this->request->get['filter_date_from'];
295: }
296:
297: if (isset($this->request->get['filter_date_to'])) {
298: $url .= '&filter_date_to=' . $this->request->get['filter_date_to'];
299: }
300:
301: if (isset($this->request->get['sort'])) {
302: $url .= '&sort=' . $this->request->get['sort'];
303: }
304:
305: if (isset($this->request->get['order'])) {
306: $url .= '&order=' . $this->request->get['order'];
307: }
308:
309: $review_total = $this->model_catalog_review->getTotalReviews($filter_data);
310:
311: $data['pagination'] = $this->load->controller('common/pagination', [
312: 'total' => $review_total,
313: 'page' => $page,
314: 'limit' => $this->config->get('config_pagination_admin'),
315: 'url' => $this->url->link('catalog/review.list', 'user_token=' . $this->session->data['user_token'] . $url . '&page={page}')
316: ]);
317:
318: $data['results'] = sprintf($this->language->get('text_pagination'), ($review_total) ? (($page - 1) * $this->config->get('config_pagination_admin')) + 1 : 0, ((($page - 1) * $this->config->get('config_pagination_admin')) > ($review_total - $this->config->get('config_pagination_admin'))) ? $review_total : ((($page - 1) * $this->config->get('config_pagination_admin')) + $this->config->get('config_pagination_admin')), $review_total, ceil($review_total / $this->config->get('config_pagination_admin')));
319:
320: $data['filter_product'] = $filter_product;
321: $data['filter_author'] = $filter_author;
322: $data['filter_status'] = $filter_status;
323: $data['filter_date_from'] = $filter_date_from;
324: $data['filter_date_to'] = $filter_date_to;
325:
326: $data['sort'] = $sort;
327: $data['order'] = $order;
328:
329: return $this->load->view('catalog/review_list', $data);
330: }
331:
332: /**
333: * Form
334: *
335: * @return void
336: */
337: public function form(): void {
338: $this->load->language('catalog/review');
339:
340: $this->document->setTitle($this->language->get('heading_title'));
341:
342: $data['text_form'] = !isset($this->request->get['review_id']) ? $this->language->get('text_add') : $this->language->get('text_edit');
343:
344: $url = '';
345:
346: if (isset($this->request->get['filter_product'])) {
347: $url .= '&filter_product=' . urlencode(html_entity_decode($this->request->get['filter_product'], ENT_QUOTES, 'UTF-8'));
348: }
349:
350: if (isset($this->request->get['filter_author'])) {
351: $url .= '&filter_author=' . urlencode(html_entity_decode($this->request->get['filter_author'], ENT_QUOTES, 'UTF-8'));
352: }
353:
354: if (isset($this->request->get['filter_status'])) {
355: $url .= '&filter_status=' . $this->request->get['filter_status'];
356: }
357:
358: if (isset($this->request->get['filter_date_from'])) {
359: $url .= '&filter_date_from=' . $this->request->get['filter_date_from'];
360: }
361:
362: if (isset($this->request->get['filter_date_to'])) {
363: $url .= '&filter_date_to=' . $this->request->get['filter_date_to'];
364: }
365:
366: if (isset($this->request->get['sort'])) {
367: $url .= '&sort=' . $this->request->get['sort'];
368: }
369:
370: if (isset($this->request->get['order'])) {
371: $url .= '&order=' . $this->request->get['order'];
372: }
373:
374: if (isset($this->request->get['page'])) {
375: $url .= '&page=' . $this->request->get['page'];
376: }
377:
378: $data['breadcrumbs'] = [];
379:
380: $data['breadcrumbs'][] = [
381: 'text' => $this->language->get('text_home'),
382: 'href' => $this->url->link('common/dashboard', 'user_token=' . $this->session->data['user_token'])
383: ];
384:
385: $data['breadcrumbs'][] = [
386: 'text' => $this->language->get('heading_title'),
387: 'href' => $this->url->link('catalog/review', 'user_token=' . $this->session->data['user_token'] . $url)
388: ];
389:
390: $data['save'] = $this->url->link('catalog/review.save', 'user_token=' . $this->session->data['user_token']);
391: $data['back'] = $this->url->link('catalog/review', 'user_token=' . $this->session->data['user_token'] . $url);
392:
393: if (isset($this->request->get['review_id'])) {
394: $this->load->model('catalog/review');
395:
396: $review_info = $this->model_catalog_review->getReview($this->request->get['review_id']);
397: }
398:
399: if (isset($this->request->get['review_id'])) {
400: $data['review_id'] = (int)$this->request->get['review_id'];
401: } else {
402: $data['review_id'] = 0;
403: }
404:
405: if (!empty($review_info)) {
406: $data['product_id'] = $review_info['product_id'];
407: } else {
408: $data['product_id'] = '';
409: }
410:
411: if (!empty($review_info)) {
412: $data['product'] = $review_info['product'];
413: } else {
414: $data['product'] = '';
415: }
416:
417: if (!empty($review_info)) {
418: $data['author'] = $review_info['author'];
419: } else {
420: $data['author'] = '';
421: }
422:
423: if (!empty($review_info)) {
424: $data['text'] = $review_info['text'];
425: } else {
426: $data['text'] = '';
427: }
428:
429: if (!empty($review_info)) {
430: $data['rating'] = $review_info['rating'];
431: } else {
432: $data['rating'] = '';
433: }
434:
435: if (!empty($review_info)) {
436: $data['date_added'] = ($review_info['date_added'] != '0000-00-00 00:00' ? $review_info['date_added'] : date('Y-m-d'));
437: } else {
438: $data['date_added'] = date('Y-m-d');
439: }
440:
441: if (!empty($review_info)) {
442: $data['status'] = $review_info['status'];
443: } else {
444: $data['status'] = '';
445: }
446:
447: $data['user_token'] = $this->session->data['user_token'];
448:
449: $data['header'] = $this->load->controller('common/header');
450: $data['column_left'] = $this->load->controller('common/column_left');
451: $data['footer'] = $this->load->controller('common/footer');
452:
453: $this->response->setOutput($this->load->view('catalog/review_form', $data));
454: }
455:
456: /**
457: * Save
458: *
459: * @return void
460: */
461: public function save(): void {
462: $this->load->language('catalog/review');
463:
464: $json = [];
465:
466: if (!$this->user->hasPermission('modify', 'catalog/review')) {
467: $json['error']['warning'] = $this->language->get('error_permission');
468: }
469:
470: if ((oc_strlen($this->request->post['author']) < 3) || (oc_strlen($this->request->post['author']) > 64)) {
471: $json['error']['author'] = $this->language->get('error_author');
472: }
473:
474: if (!$this->request->post['product_id']) {
475: $json['error']['product'] = $this->language->get('error_product');
476: }
477:
478: if (oc_strlen($this->request->post['text']) < 1) {
479: $json['error']['text'] = $this->language->get('error_text');
480: }
481:
482: if (!isset($this->request->post['rating']) || $this->request->post['rating'] < 0 || $this->request->post['rating'] > 5) {
483: $json['error']['rating'] = $this->language->get('error_rating');
484: }
485:
486: if (isset($json['error']) && !isset($json['error']['warning'])) {
487: $json['error']['warning'] = $this->language->get('error_warning');
488: }
489:
490: if (!$json) {
491: $this->load->model('catalog/review');
492:
493: if (!$this->request->post['review_id']) {
494: $json['review_id'] = $this->model_catalog_review->addReview($this->request->post);
495: } else {
496: $this->model_catalog_review->editReview($this->request->post['review_id'], $this->request->post);
497: }
498:
499: $json['success'] = $this->language->get('text_success');
500: }
501:
502: $this->response->addHeader('Content-Type: application/json');
503: $this->response->setOutput(json_encode($json));
504: }
505:
506: /**
507: * Delete
508: *
509: * @return void
510: */
511: public function delete(): void {
512: $this->load->language('catalog/review');
513:
514: $json = [];
515:
516: if (isset($this->request->post['selected'])) {
517: $selected = $this->request->post['selected'];
518: } else {
519: $selected = [];
520: }
521:
522: if (!$this->user->hasPermission('modify', 'catalog/review')) {
523: $json['error'] = $this->language->get('error_permission');
524: }
525:
526: if (!$json) {
527: $this->load->model('catalog/review');
528:
529: foreach ($selected as $review_id) {
530: $this->model_catalog_review->deleteReview($review_id);
531: }
532:
533: $json['success'] = $this->language->get('text_success');
534: }
535:
536: $this->response->addHeader('Content-Type: application/json');
537: $this->response->setOutput(json_encode($json));
538: }
539:
540: /**
541: * Sync
542: *
543: * @return void
544: */
545: public function sync(): void {
546: $this->load->language('catalog/review');
547:
548: $json = [];
549:
550: if (isset($this->request->get['page'])) {
551: $page = (int)$this->request->get['page'];
552: } else {
553: $page = 1;
554: }
555:
556: if (!$this->user->hasPermission('modify', 'catalog/review')) {
557: $json['error'] = $this->language->get('error_permission');
558: }
559:
560: if (!$json) {
561: $this->load->model('catalog/product');
562: $this->load->model('catalog/review');
563:
564: $limit = 10;
565:
566: $product_data = [
567: 'start' => ($page - 1) * $limit,
568: 'limit' => $limit
569: ];
570:
571: $results = $this->model_catalog_product->getProducts($product_data);
572:
573: foreach ($results as $result) {
574: $this->model_catalog_product->editRating($result['product_id'], $this->model_catalog_review->getRating($result['product_id']));
575: }
576:
577: $product_total = $this->model_catalog_product->getTotalProducts();
578:
579: $start = ($page - 1) * $limit;
580: $end = $start > ($product_total - $limit) ? $product_total : ($start + $limit);
581:
582: if ($end < $product_total) {
583: $json['text'] = sprintf($this->language->get('text_next'), $start, $end, $product_total);
584:
585: $json['next'] = $this->url->link('catalog/review.sync', 'user_token=' . $this->session->data['user_token'] . '&page=' . ($page + 1), true);
586: } else {
587: $json['success'] = $this->language->get('text_success');
588:
589: $json['next'] = '';
590: }
591: }
592:
593: $this->response->addHeader('Content-Type: application/json');
594: $this->response->setOutput(json_encode($json));
595: }
596: }
597: