1: | <?php
|
2: | namespace Opencart\Catalog\Controller\Product;
|
3: | |
4: | |
5: | |
6: | |
7: |
|
8: | class Review extends \Opencart\System\Engine\Controller {
|
9: | |
10: | |
11: |
|
12: | public function index(): string {
|
13: | $this->load->language('product/review');
|
14: |
|
15: | if (isset($this->request->get['product_id'])) {
|
16: | $data['product_id'] = (int)$this->request->get['product_id'];
|
17: | } else {
|
18: | $data['product_id'] = 0;
|
19: | }
|
20: |
|
21: | $data['text_login'] = sprintf($this->language->get('text_login'), $this->url->link('account/login', 'language=' . $this->config->get('config_language')), $this->url->link('account/register', 'language=' . $this->config->get('config_language')));
|
22: |
|
23: | $data['list'] = $this->getList();
|
24: |
|
25: | if ($this->customer->isLogged() || $this->config->get('config_review_guest')) {
|
26: | $data['review_guest'] = true;
|
27: | } else {
|
28: | $data['review_guest'] = false;
|
29: | }
|
30: |
|
31: | if ($this->customer->isLogged()) {
|
32: | $data['customer'] = $this->customer->getFirstName() . ' ' . $this->customer->getLastName();
|
33: | } else {
|
34: | $data['customer'] = '';
|
35: | }
|
36: |
|
37: |
|
38: | $data['review_token'] = $this->session->data['review_token'] = oc_token(32);
|
39: |
|
40: |
|
41: | $this->load->model('setting/extension');
|
42: |
|
43: | $extension_info = $this->model_setting_extension->getExtensionByCode('captcha', $this->config->get('config_captcha'));
|
44: |
|
45: | if ($extension_info && $this->config->get('captcha_' . $this->config->get('config_captcha') . '_status') && in_array('review', (array)$this->config->get('config_captcha_page'))) {
|
46: | $data['captcha'] = $this->load->controller('extension/' . $extension_info['extension'] . '/captcha/' . $extension_info['code']);
|
47: | } else {
|
48: | $data['captcha'] = '';
|
49: | }
|
50: |
|
51: | $data['language'] = $this->config->get('config_language');
|
52: |
|
53: | return $this->load->view('product/review', $data);
|
54: | }
|
55: |
|
56: | |
57: | |
58: | |
59: | |
60: |
|
61: | public function write(): void {
|
62: | $this->load->language('product/review');
|
63: |
|
64: | $json = [];
|
65: |
|
66: | if (isset($this->request->get['product_id'])) {
|
67: | $product_id = (int)$this->request->get['product_id'];
|
68: | } else {
|
69: | $product_id = 0;
|
70: | }
|
71: |
|
72: | if (!isset($this->request->get['review_token']) || !isset($this->session->data['review_token']) || $this->request->get['review_token'] != $this->session->data['review_token']) {
|
73: | $json['error']['warning'] = $this->language->get('error_token');
|
74: | }
|
75: |
|
76: | $keys = [
|
77: | 'author',
|
78: | 'text',
|
79: | 'rating'
|
80: | ];
|
81: |
|
82: | foreach ($keys as $key) {
|
83: | if (!isset($this->request->post[$key])) {
|
84: | $this->request->post[$key] = '';
|
85: | }
|
86: | }
|
87: |
|
88: | if (!$this->config->get('config_review_status')) {
|
89: | $json['error']['warning'] = $this->language->get('error_status');
|
90: | }
|
91: |
|
92: | $this->load->model('catalog/product');
|
93: |
|
94: | $product_info = $this->model_catalog_product->getProduct($product_id);
|
95: |
|
96: | if (!$product_info) {
|
97: | $json['error']['warning'] = $this->language->get('error_product');
|
98: | }
|
99: |
|
100: | if ((oc_strlen($this->request->post['author']) < 3) || (oc_strlen($this->request->post['author']) > 25)) {
|
101: | $json['error']['author'] = $this->language->get('error_author');
|
102: | }
|
103: |
|
104: | if ((oc_strlen($this->request->post['text']) < 25) || (oc_strlen($this->request->post['text']) > 1000)) {
|
105: | $json['error']['text'] = $this->language->get('error_text');
|
106: | }
|
107: |
|
108: | if ($this->request->post['rating'] < 1 || $this->request->post['rating'] > 5) {
|
109: | $json['error']['rating'] = $this->language->get('error_rating');
|
110: | }
|
111: |
|
112: | if (!$this->customer->isLogged() && !$this->config->get('config_review_guest')) {
|
113: | $json['error']['warning'] = $this->language->get('error_login');
|
114: | }
|
115: |
|
116: | if ($this->customer->isLogged() && $this->config->get('config_review_purchased')) {
|
117: | $this->load->model('account/order');
|
118: |
|
119: | if (!$this->model_account_order->getTotalOrdersByProductId($product_id)) {
|
120: | $json['error']['purchased'] = $this->language->get('error_purchased');
|
121: | }
|
122: | }
|
123: |
|
124: |
|
125: | $this->load->model('setting/extension');
|
126: |
|
127: | $extension_info = $this->model_setting_extension->getExtensionByCode('captcha', $this->config->get('config_captcha'));
|
128: |
|
129: | if ($extension_info && $this->config->get('captcha_' . $this->config->get('config_captcha') . '_status') && in_array('review', (array)$this->config->get('config_captcha_page'))) {
|
130: | $captcha = $this->load->controller('extension/' . $extension_info['extension'] . '/captcha/' . $extension_info['code'] . '.validate');
|
131: |
|
132: | if ($captcha) {
|
133: | $json['error']['captcha'] = $captcha;
|
134: | }
|
135: | }
|
136: |
|
137: | if (!$json) {
|
138: | $this->load->model('catalog/review');
|
139: |
|
140: | $this->model_catalog_review->addReview($product_id, $this->request->post);
|
141: |
|
142: | $json['success'] = $this->language->get('text_success');
|
143: | }
|
144: |
|
145: | $this->response->addHeader('Content-Type: application/json');
|
146: | $this->response->setOutput(json_encode($json));
|
147: | }
|
148: |
|
149: | |
150: | |
151: | |
152: | |
153: |
|
154: | public function list(): void {
|
155: | $this->load->language('product/review');
|
156: |
|
157: | $this->response->setOutput($this->getList());
|
158: | }
|
159: |
|
160: | |
161: | |
162: | |
163: | |
164: |
|
165: | public function getList(): string {
|
166: | if (isset($this->request->get['product_id'])) {
|
167: | $product_id = (int)$this->request->get['product_id'];
|
168: | } else {
|
169: | $product_id = 0;
|
170: | }
|
171: |
|
172: | if (isset($this->request->get['page'])) {
|
173: | $page = (int)$this->request->get['page'];
|
174: | } else {
|
175: | $page = 1;
|
176: | }
|
177: |
|
178: | $data['reviews'] = [];
|
179: |
|
180: | $this->load->model('catalog/review');
|
181: |
|
182: | $results = $this->model_catalog_review->getReviewsByProductId($product_id, ($page - 1) * 5, 5);
|
183: |
|
184: | foreach ($results as $result) {
|
185: | $data['reviews'][] = [
|
186: | 'author' => $result['author'],
|
187: | 'text' => nl2br($result['text']),
|
188: | 'rating' => (int)$result['rating'],
|
189: | 'date_added' => date($this->language->get('date_format_short'), strtotime($result['date_added']))
|
190: | ];
|
191: | }
|
192: |
|
193: | $review_total = $this->model_catalog_review->getTotalReviewsByProductId($product_id);
|
194: |
|
195: | $data['pagination'] = $this->load->controller('common/pagination', [
|
196: | 'total' => $review_total,
|
197: | 'page' => $page,
|
198: | 'limit' => 5,
|
199: | 'url' => $this->url->link('product/review.list', 'language=' . $this->config->get('config_language') . '&product_id=' . $product_id . '&page={page}')
|
200: | ]);
|
201: |
|
202: | $data['results'] = sprintf($this->language->get('text_pagination'), ($review_total) ? (($page - 1) * 5) + 1 : 0, ((($page - 1) * 5) > ($review_total - 5)) ? $review_total : ((($page - 1) * 5) + 5), $review_total, ceil($review_total / 5));
|
203: |
|
204: | return $this->load->view('product/review_list', $data);
|
205: | }
|
206: | }
|
207: | |