1: | <?php
2: | namespace Opencart\Catalog\Controller\Cms;
3: | /**
4: | * Class Comment
5: | *
6: | * @package Opencart\Catalog\Controller\Cms
7: | */
8: | class Comment extends \Opencart\System\Engine\Controller {
9: | /**
10: | * @return string
11: | */
12: | public function index(): string {
13: | $this->load->language('cms/comment');
14: |
15: | if (isset($this->request->get['article_id'])) {
16: | $data['article_id'] = (int)$this->request->get['article_id'];
17: | } else {
18: | $data['article_id'] = 0;
19: | }
20: |
21: | if (isset($this->request->get['sort']) && $this->request->get['route'] == 'cms/comment') {
22: | $sort = $this->request->get['sort'];
23: | } else {
24: | $sort = 'date_added';
25: | }
26: |
27: | if (isset($this->request->get['order']) && $this->request->get['route'] == 'cms/comment') {
28: | $order = $this->request->get['order'];
29: | } else {
30: | $order = 'DESC';
31: | }
32: |
33: | if (isset($this->request->get['page'])) {
34: | $page = (int)$this->request->get['page'];
35: | } else {
36: | $page = 1;
37: | }
38: |
39: | $data['logged'] = $this->customer->isLogged();
40: | $data['login'] = $this->url->link('account/login', 'language=' . $this->config->get('config_language') . '&page=' . $page . '&redirect=' . urlencode($this->url->link('cms/blog.info', 'language=' . $this->config->get('config_language') . '&article_id=' . $data['article_id'], true)));
41: |
42: | $this->session->data['comment_token'] = oc_token(32);
43: |
44: | // Create a login token to prevent brute force attacks
45: | $data['comment_add'] = $this->url->link('cms/comment.add', 'language=' . $this->config->get('config_language') . '&article_id=' . $data['article_id'] . '&comment_token=' . $this->session->data['comment_token'], true);
46: | $data['like'] = $this->url->link('cms/comment.rate', 'language=' . $this->config->get('config_language') . '&article_id=' . $data['article_id'] . '&rate=1&comment_token=' . $this->session->data['comment_token'], true);
47: | $data['dislike'] = $this->url->link('cms/comment.rate', 'language=' . $this->config->get('config_language') . '&article_id=' . $data['article_id'] . '&rate=0&comment_token=' . $this->session->data['comment_token'], true);
48: |
49: | $this->load->model('cms/article');
50: |
51: | $data['list'] = $this->controller_cms_comment->getList();
52: |
53: | $data['sorts'] = [];
54: |
55: | $data['sorts'][] = [
56: | 'text' => $this->language->get('text_date_added_asc'),
57: | 'value' => 'date_added-ASC',
58: | 'href' => $this->url->link('cms/comment.list', 'language=' . $this->config->get('config_language') . '&article_id=' . $data['article_id'] . '&sort=date_added&order=ASC')
59: | ];
60: |
61: | $data['sorts'][] = [
62: | 'text' => $this->language->get('text_date_added_desc'),
63: | 'value' => 'date_added-DESC',
64: | 'href' => $this->url->link('cms/comment.list', 'language=' . $this->config->get('config_language') . '&article_id=' . $data['article_id'] . '&sort=date_added&order=DESC')
65: | ];
66: |
67: | $data['sorts'][] = [
68: | 'text' => $this->language->get('text_rating_asc'),
69: | 'value' => 'rating-ASC',
70: | 'href' => $this->url->link('cms/comment.list', 'language=' . $this->config->get('config_language') . '&article_id=' . $data['article_id'] . '&sort=rating&order=ASC')
71: | ];
72: |
73: | $data['sorts'][] = [
74: | 'text' => $this->language->get('text_rating_desc'),
75: | 'value' => 'rating-DESC',
76: | 'href' => $this->url->link('cms/comment.list', 'language=' . $this->config->get('config_language') . '&article_id=' . $data['article_id'] . '&sort=rating&order=DESC')
77: | ];
78: |
79: | $data['sort'] = $sort;
80: | $data['order'] = $order;
81: |
82: | // Captcha
83: | $this->load->model('setting/extension');
84: |
85: | $extension_info = $this->model_setting_extension->getExtensionByCode('captcha', $this->config->get('config_captcha'));
86: |
87: | if ($extension_info && $this->config->get('captcha_' . $this->config->get('config_captcha') . '_status') && in_array('comment', (array)$this->config->get('config_captcha_page'))) {
88: | $data['captcha'] = $this->load->controller('extension/' . $extension_info['extension'] . '/captcha/' . $extension_info['code']);
89: | } else {
90: | $data['captcha'] = '';
91: | }
92: |
93: | $data['language'] = $this->config->get('config_language');
94: |
95: | return $this->load->view('cms/comment', $data);
96: | }
97: |
98: | /**
99: | * List
100: | *
101: | * @return void
102: | */
103: | public function list(): void {
104: | $this->load->language('cms/comment');
105: |
106: | $this->response->setOutput($this->controller_cms_comment->getList());
107: | }
108: |
109: | /**
110: | * Get List
111: | *
112: | * @return string
113: | */
114: | public function getList(): string {
115: | if (isset($this->request->get['article_id'])) {
116: | $article_id = (int)$this->request->get['article_id'];
117: | } else {
118: | $article_id = 0;
119: | }
120: |
121: | if (isset($this->request->get['sort']) && $this->request->get['route'] == 'cms/comment.list') {
122: | $sort = $this->request->get['sort'];
123: | } else {
124: | $sort = 'date_added';
125: | }
126: |
127: | if (isset($this->request->get['order']) && $this->request->get['route'] == 'cms/comment.list') {
128: | $order = $this->request->get['order'];
129: | } else {
130: | $order = 'DESC';
131: | }
132: |
133: | if (isset($this->request->get['page'])) {
134: | $page = (int)$this->request->get['page'];
135: | } else {
136: | $page = 1;
137: | }
138: |
139: | $limit = 5;
140: |
141: | $data['comments'] = [];
142: |
143: | $filter_data = [
144: | 'parent_id' => 0,
145: | 'sort' => $sort,
146: | 'order' => $order,
147: | 'start' => ($page - 1) * $limit,
148: | 'limit' => $limit
149: | ];
150: |
151: | $this->load->model('cms/article');
152: |
153: | $results = $this->model_cms_article->getComments($article_id, $filter_data);
154: |
155: | foreach ($results as $result) {
156: | $data['comments'][] = [
157: | 'article_comment_id' => $result['article_comment_id'],
158: | 'comment' => nl2br($result['comment']),
159: | 'author' => $result['author'],
160: | 'date_added' => date($this->language->get('date_format_short'), strtotime($result['date_added'])),
161: | 'like' => $this->url->link('cms/comment.rate', 'language=' . $this->config->get('config_language') . '&article_id=' . $article_id . '&article_comment_id=' . $result['article_comment_id'] . '&comment_token=' . $this->session->data['comment_token'] . '&rate=1', true),
162: | 'dislike' => $this->url->link('cms/comment.rate', 'language=' . $this->config->get('config_language') . '&article_id=' . $article_id . '&article_comment_id=' . $result['article_comment_id'] . '&comment_token=' . $this->session->data['comment_token'] . '&rate=0', true),
163: | 'reply' => $this->url->link('cms/comment.reply', 'language=' . $this->config->get('config_language') . '&article_id=' . $article_id . '&parent_id=' . $result['article_comment_id'], true),
164: | 'reply_add' => $this->url->link('cms/comment.add', 'language=' . $this->config->get('config_language') . '&article_id=' . $article_id . '&parent_id=' . $result['article_comment_id'] . '&comment_token=' . $this->session->data['comment_token'], true),
165: | 'reply_total' => $this->model_cms_article->getTotalComments($article_id, ['parent_id' => $result['article_comment_id']])
166: | ];
167: | }
168: |
169: | $comment_total = $this->model_cms_article->getTotalComments($article_id, $filter_data);
170: |
171: | $data['pagination'] = $this->load->controller('common/pagination', [
172: | 'total' => $comment_total,
173: | 'page' => $page,
174: | 'limit' => $limit,
175: | 'url' => $this->url->link('cms/comment.list', 'language=' . $this->config->get('config_language') . '&article_id=' . $article_id . '&page={page}')
176: | ]);
177: |
178: | $data['results'] = sprintf($this->language->get('text_pagination'), ($comment_total) ? (($page - 1) * $limit) + 1 : 0, ((($page - 1) * $limit) > ($comment_total - $limit)) ? $comment_total : ((($page - 1) * $limit) + $limit), $comment_total, ceil($comment_total / $limit));
179: |
180: | $data['refresh'] = $this->url->link('cms/comment.list', 'language=' . $this->config->get('config_language') . '&article_id=' . $article_id . '&page=' . $page, true);
181: |
182: | $data['logged'] = $this->customer->isLogged();
183: | $data['login'] = $this->url->link('account/login', 'language=' . $this->config->get('config_language') . '&redirect=' . urlencode($this->url->link('cms/blog.info', 'language=' . $this->config->get('config_language') . '&article_id=' . $article_id, true)));
184: |
185: | return $this->load->view('cms/comment_list', $data);
186: | }
187: |
188: | /**
189: | * Reply
190: | *
191: | * @return void
192: | */
193: | public function reply(): void {
194: | $this->load->language('cms/comment');
195: |
196: | $this->response->setOutput($this->controller_cms_comment->getReplies());
197: | }
198: |
199: | /**
200: | * Get Replies
201: | *
202: | * @return string
203: | */
204: | public function getReplies(): string {
205: | if (isset($this->request->get['article_id'])) {
206: | $article_id = (int)$this->request->get['article_id'];
207: | } else {
208: | $article_id = 0;
209: | }
210: |
211: | if (isset($this->request->get['parent_id'])) {
212: | $parent_id = (int)$this->request->get['parent_id'];
213: | } else {
214: | $parent_id = 0;
215: | }
216: |
217: | if (isset($this->request->get['page'])) {
218: | $page = (int)$this->request->get['page'];
219: | } else {
220: | $page = 1;
221: | }
222: |
223: | $limit = 5;
224: |
225: | $data['replies'] = [];
226: |
227: | $filter_data = [
228: | 'parent_id' => $parent_id,
229: | 'sort' => 'date_added',
230: | 'order' => 'ASC',
231: | 'start' => ($page - 1) * $limit,
232: | 'limit' => $limit
233: | ];
234: |
235: | $this->load->model('cms/article');
236: |
237: | $results = $this->model_cms_article->getComments($article_id, $filter_data);
238: |
239: | foreach ($results as $result) {
240: | $data['replies'][] = [
241: | 'article_comment_id' => $result['article_comment_id'],
242: | 'parent_id' => $result['parent_id'],
243: | 'comment' => nl2br($result['comment']),
244: | 'author' => $result['author'],
245: | 'date_added' => date($this->language->get('date_format_short'), strtotime($result['date_added']))
246: | ];
247: | }
248: |
249: | $reply_total = $this->model_cms_article->getTotalComments($article_id, $filter_data);
250: |
251: | $data['refresh'] = $this->url->link('cms/comment.reply', 'language=' . $this->config->get('config_language') . '&article_id=' . $article_id . '&parent_id=' . $parent_id . '&page=' . $page, true);
252: |
253: | if (($page * $limit) < $reply_total) {
254: | $data['next'] = $this->url->link('cms/comment.reply', 'language=' . $this->config->get('config_language') . '&article_id=' . $article_id . '&parent_id=' . $parent_id . '&page=' . ($page + 1), true);
255: | } else {
256: | $data['next'] = '';
257: | }
258: |
259: | $data['parent_id'] = $parent_id;
260: | $data['page'] = $page;
261: |
262: | return $this->load->view('cms/comment_reply', $data);
263: | }
264: |
265: | /**
266: | * Add
267: | *
268: | * @return void
269: | */
270: | public function add(): void {
271: | $this->load->language('cms/comment');
272: |
273: | $json = [];
274: |
275: | if (isset($this->request->get['article_id'])) {
276: | $article_id = (int)$this->request->get['article_id'];
277: | } else {
278: | $article_id = 0;
279: | }
280: |
281: | if (isset($this->request->get['parent_id'])) {
282: | $parent_id = (int)$this->request->get['parent_id'];
283: | } else {
284: | $parent_id = 0;
285: | }
286: |
287: | $keys = [
288: | 'author',
289: | 'comment'
290: | ];
291: |
292: | foreach ($keys as $key) {
293: | if (!isset($this->request->post[$key])) {
294: | $this->request->post[$key] = '';
295: | }
296: | }
297: |
298: | if (!isset($this->request->get['comment_token']) || !isset($this->session->data['comment_token']) || $this->request->get['comment_token'] != $this->session->data['comment_token']) {
299: | $json['error']['warning'] = $this->language->get('error_token');
300: | }
301: |
302: | if (!$this->customer->isLogged()) {
303: | $json['error']['warning'] = $this->language->get('error_login');
304: | }
305: |
306: | $this->load->model('cms/article');
307: |
308: | $article_info = $this->model_cms_article->getArticle($article_id);
309: |
310: | if (!$article_info) {
311: | $json['error']['warning'] = $this->language->get('error_article');
312: | }
313: |
314: | if ((oc_strlen($this->request->post['author']) < 3) || (oc_strlen($this->request->post['author']) > 25)) {
315: | $json['error']['author'] = $this->language->get('error_author');
316: | }
317: |
318: | if ((oc_strlen($this->request->post['comment']) < 2) || (oc_strlen($this->request->post['comment']) > 1000)) {
319: | $json['error']['comment'] = $this->language->get('error_comment');
320: | }
321: |
322: | if ($this->config->get('config_comment_interval')) {
323: | $filter_data = [
324: | 'customer_id' => $this->customer->getId(),
325: | 'sort' => 'date_added',
326: | 'order' => 'DESC',
327: | 'start' => 0,
328: | 'limit' => 1
329: | ];
330: |
331: | $results = $this->model_cms_article->getComments($article_id, $filter_data);
332: |
333: | foreach ($results as $result) {
334: | if (strtotime('+' . $this->config->get('config_comment_interval') . ' minute', strtotime($result['date_added'])) >= time()) {
335: | $json['error']['warning'] = sprintf($this->language->get('error_interval'), $this->config->get('config_comment_interval'));
336: |
337: | break;
338: | }
339: | }
340: | }
341: |
342: | // Captcha
343: | $this->load->model('setting/extension');
344: |
345: | $extension_info = $this->model_setting_extension->getExtensionByCode('captcha', $this->config->get('config_captcha'));
346: |
347: | if ($extension_info && $this->config->get('captcha_' . $this->config->get('config_captcha') . '_status') && in_array('comment', (array)$this->config->get('config_captcha_page'))) {
348: | $captcha = $this->load->controller('extension/' . $extension_info['extension'] . '/captcha/' . $extension_info['code'] . '.validate');
349: |
350: | if ($captcha) {
351: | $json['error']['captcha'] = $captcha;
352: | }
353: | }
354: |
355: | if (!$this->config->get('config_comment_status')) {
356: | $json['error']['warning'] = $this->language->get('error_status');
357: | }
358: |
359: | if (!$json) {
360: | // Anti-Spam
361: | $this->load->model('cms/antispam');
362: |
363: | $spam = $this->model_cms_antispam->getSpam($this->request->post['comment']);
364: |
365: | // If customer has been approved to make comments without moderation
366: | if ($this->customer->isCommenter()) {
367: | $status = 1;
368: | // If auto approve comments
369: | } elseif ($this->config->get('config_comment_approve') && !$spam) {
370: | $status = 1;
371: | } else {
372: | $status = 0;
373: | }
374: |
375: | $comment_data = $this->request->post + [
376: | 'parent_id' => $parent_id,
377: | 'status' => $status
378: | ];
379: |
380: | $this->model_cms_article->addComment($article_id, $comment_data);
381: |
382: | if ($status) {
383: | $json['success'] = $this->language->get('text_success');
384: | } else {
385: | $json['success'] = $this->language->get('text_queue');
386: | }
387: | }
388: |
389: | $this->response->addHeader('Content-Type: application/json');
390: | $this->response->setOutput(json_encode($json));
391: | }
392: |
393: | /**
394: | * Rating
395: | *
396: | * @return void
397: | */
398: | public function rate(): void {
399: | $this->load->language('cms/comment');
400: |
401: | $json = [];
402: |
403: | if (isset($this->request->get['article_id'])) {
404: | $article_id = (int)$this->request->get['article_id'];
405: | } else {
406: | $article_id = 0;
407: | }
408: |
409: | if (isset($this->request->get['article_comment_id'])) {
410: | $article_comment_id = (int)$this->request->get['article_comment_id'];
411: | } else {
412: | $article_comment_id = 0;
413: | }
414: |
415: | if (isset($this->request->get['rate'])) {
416: | $rating = (bool)$this->request->get['rate'];
417: | } else {
418: | $rating = 0;
419: | }
420: |
421: | if (!isset($this->request->get['comment_token']) || !isset($this->session->data['comment_token']) || $this->request->get['comment_token'] != $this->session->data['comment_token']) {
422: | $json['error'] = $this->language->get('error_token');
423: | }
424: |
425: | if (!$this->customer->isLogged()) {
426: | $json['error'] = $this->language->get('error_login');
427: | }
428: |
429: | $this->load->model('cms/article');
430: |
431: | $article_info = $this->model_cms_article->getArticle($article_id);
432: |
433: | if (!$article_info) {
434: | $json['error'] = $this->language->get('error_article');
435: | }
436: |
437: | // Comment to rate
438: | if ($article_comment_id) {
439: | $article_comment_info = $this->model_cms_article->getComment($article_comment_id);
440: |
441: | if (!$article_comment_info) {
442: | $json['error'] = $this->language->get('error_article_comment');
443: | }
444: | }
445: |
446: | if (!$json) {
447: | // Delete previous rating if there is one
448: | $this->model_cms_article->deleteRating($article_id, $article_comment_id);
449: |
450: | $this->model_cms_article->addRating($article_id, $article_comment_id, $rating);
451: |
452: | $like = 0;
453: | $dislike = 0;
454: |
455: | $results = $this->model_cms_article->getRatings($article_id, $article_comment_id);
456: |
457: | foreach ($results as $result) {
458: | if ($result['rating'] == 1) {
459: | $like = $result['total'];
460: | }
461: |
462: | if ($result['rating'] == 0) {
463: | $dislike = $result['total'];
464: | }
465: | }
466: |
467: | if (!$article_comment_id) {
468: | $this->model_cms_article->editRating($article_id, $like - $dislike);
469: | } else {
470: | $this->model_cms_article->editCommentRating($article_id, $article_comment_id, $like - $dislike);
471: | }
472: |
473: | $json['success'] = $this->language->get('text_rating');
474: | }
475: |
476: | $this->response->addHeader('Content-Type: application/json');
477: | $this->response->setOutput(json_encode($json));
478: | }
479: | }
480: |

OpenCart API API documentation generated by ApiGen dev-master