1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
(defined('BASEPATH')) OR exit('No direct script access allowed'); |
4
|
|
|
|
5
|
|
|
class Commentsapi extends Comments |
6
|
|
|
{ |
|
|
|
|
7
|
|
|
|
8
|
|
|
public $validation_errors; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* @var string $comments_locale |
12
|
|
|
*/ |
13
|
|
|
private $comments_locale; |
14
|
|
|
|
15
|
|
|
public function __construct() { |
16
|
|
|
parent::__construct(); |
17
|
|
|
$this->load->module('core'); |
18
|
|
|
$this->module = $this->getModule($this->input->server('HTTP_REFERER')); |
19
|
|
|
$lang = new MY_Lang(); |
20
|
|
|
$lang->load('comments'); |
21
|
|
|
|
22
|
|
|
$this->tpl_name = 'comments_api'; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* New comments realization |
27
|
|
|
* @param string $url |
28
|
|
|
* @return array comments |
29
|
|
|
*/ |
30
|
|
|
public function getComments($url) { |
31
|
|
|
$this->load->model('base'); |
32
|
|
|
$this->_init_settings(); |
33
|
|
|
$this->module = $this->getModule($url); |
34
|
|
|
$item_id = $this->parsUrl($url); |
35
|
|
|
|
36
|
|
|
$comments = $this->base->get($item_id, 0, $this->module, 99999, $this->order_by); |
37
|
|
|
|
38
|
|
|
// Read comments template |
39
|
|
|
// Set page id for comments form |
40
|
|
View Code Duplication |
if ($comments != FALSE) { |
41
|
|
|
$this->cache->store('comments_' . $item_id . $this->module, $comments, $this->cache_ttl, 'comments'); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
$comment_ch = []; |
45
|
|
|
|
46
|
|
View Code Duplication |
if (is_array($comments)) { |
47
|
|
|
$i = 0; |
48
|
|
|
foreach ($comments as $comment) { |
49
|
|
|
if ($comment['parent'] > 0) { |
50
|
|
|
$comment_ch[] = $comment; |
51
|
|
|
unset($comments[$i]); |
52
|
|
|
} |
53
|
|
|
$i++; |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
$data = [ |
58
|
|
|
'comments_arr' => $comments, |
59
|
|
|
'comment_ch' => $comment_ch, |
60
|
|
|
'comment_controller' => $this->comment_controller, |
61
|
|
|
'total_comments' => lang('Total comments: ', 'comments') . count($comments), |
62
|
|
|
'can_comment' => $this->can_comment, |
63
|
|
|
'use_captcha' => $this->use_captcha, |
64
|
|
|
'use_moderation' => $this->use_moderation, |
65
|
|
|
'enable_comments' => $this->enable_comments, |
66
|
|
|
]; |
67
|
|
|
|
68
|
|
View Code Duplication |
if ($this->use_captcha == TRUE) { |
69
|
|
|
$this->dx_auth->captcha(); |
70
|
|
|
$data['cap_image'] = $this->dx_auth->get_captcha_image(); |
71
|
|
|
} |
72
|
|
|
return $data; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param string $url |
77
|
|
|
* @return array |
78
|
|
|
*/ |
79
|
|
|
public function renderAsArray($url) { |
80
|
|
|
$this->load->model('base'); |
81
|
|
|
$this->_init_settings(); |
82
|
|
|
|
83
|
|
|
$this->module = $this->getModule($url); |
84
|
|
|
$item_id = $this->parsUrl($url); |
85
|
|
|
$commentsCount = $this->getTotalCommentsForProducts($item_id); |
86
|
|
|
|
87
|
|
|
$comments = $this->base->get($item_id, 0, $this->module, 99999, $this->order_by); |
88
|
|
|
|
89
|
|
|
// Read comments template |
90
|
|
|
// Set page id for comments form |
91
|
|
View Code Duplication |
if ($comments != FALSE) { |
92
|
|
|
$this->cache->store('comments_' . $item_id . $this->module, $comments, $this->cache_ttl, 'comments'); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
if ($comments != null) { |
96
|
|
|
$comments_count = count($comments); |
97
|
|
|
} else { |
98
|
|
|
$comments_count = 0; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
$comment_ch = []; |
102
|
|
|
|
103
|
|
View Code Duplication |
if (is_array($comments)) { |
104
|
|
|
$i = 0; |
105
|
|
|
foreach ($comments as $comment) { |
106
|
|
|
if ($comment['parent'] > 0) { |
107
|
|
|
$comment_ch[] = $comment; |
108
|
|
|
unset($comments[$i]); |
109
|
|
|
} |
110
|
|
|
$i++; |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
$data = [ |
115
|
|
|
'comments_arr' => $comments, |
116
|
|
|
'comment_ch' => $comment_ch, |
117
|
|
|
'comment_controller' => $this->comment_controller, |
118
|
|
|
'total_comments' => lang('Total comments: ', 'comments') . count($comments), |
119
|
|
|
'can_comment' => $this->can_comment, |
120
|
|
|
'use_captcha' => $this->use_captcha, |
121
|
|
|
'use_moderation' => $this->use_moderation, |
122
|
|
|
'enable_comments' => $this->enable_comments, |
123
|
|
|
]; |
124
|
|
|
|
125
|
|
View Code Duplication |
if ($this->use_captcha == TRUE) { |
126
|
|
|
$this->dx_auth->captcha(); |
127
|
|
|
$data['cap_image'] = $this->dx_auth->get_captcha_image(); |
128
|
|
|
} |
129
|
|
|
($hook = get_hook('comments_read_com_tpl')) ? eval($hook) : NULL; |
|
|
|
|
130
|
|
|
|
131
|
|
|
$comments = $this->_fetchComments($data); |
132
|
|
|
|
133
|
|
|
($hook = get_hook('comments_assign_tpl_data')) ? eval($hook) : NULL; |
|
|
|
|
134
|
|
|
return [ |
135
|
|
|
'comments' => $comments, |
136
|
|
|
'commentsCount' => $commentsCount[$item_id], |
137
|
|
|
'total_comments' => $comments_count ? $comments_count . ' ' . SStringHelper::Pluralize($comments_count, [lang('comment', 'comments'), lang('comment', 'comments'), lang('comments', 'comments')]) : lang('Leave comment', 'comments'), |
138
|
|
|
'validation_errors' => $this->validation_errors, |
139
|
|
|
]; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
public function renderPosts() { |
143
|
|
|
$this->load->model('base'); |
144
|
|
|
$this->_init_settings(); |
145
|
|
|
|
146
|
|
|
$item_id = $this->parsUrl($this->input->server('HTTP_REFERER')); |
147
|
|
|
|
148
|
|
|
$commentsCount = $this->getTotalCommentsForProducts($item_id); |
149
|
|
|
$comments = $this->base->get($item_id, 0, $this->module, $this->input->post('countcomment')?:null, $this->order_by); |
|
|
|
|
150
|
|
|
|
151
|
|
|
// Read comments template |
152
|
|
|
// Set page id for comments form |
153
|
|
View Code Duplication |
if ($comments != FALSE) { |
154
|
|
|
($hook = get_hook('comments_store_cache')) ? eval($hook) : NULL; |
|
|
|
|
155
|
|
|
$this->cache->store('comments_' . $item_id . $this->module, $comments, $this->cache_ttl, 'comments'); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
if ($comments != null) { |
159
|
|
|
$comments_count = count($comments); |
160
|
|
|
} else { |
161
|
|
|
$comments_count = 0; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
View Code Duplication |
if (is_array($comments)) { |
165
|
|
|
$i = 0; |
166
|
|
|
foreach ($comments as $comment) { |
167
|
|
|
if ($comment['parent'] > 0) { |
168
|
|
|
$comment_ch[] = $comment; |
|
|
|
|
169
|
|
|
unset($comments[$i]); |
170
|
|
|
} |
171
|
|
|
$i++; |
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
$data = [ |
176
|
|
|
'comments_arr' => $comments, |
177
|
|
|
'comment_ch' => $comment_ch, |
178
|
|
|
'comment_controller' => $this->comment_controller, |
179
|
|
|
'total_comments' => lang('Total comments: ', 'comments') . count($comments), |
180
|
|
|
'can_comment' => $this->can_comment, |
181
|
|
|
'use_captcha' => $this->use_captcha, |
182
|
|
|
'use_moderation' => $this->use_moderation, |
183
|
|
|
'enable_comments' => $this->enable_comments, |
184
|
|
|
'visibleMainForm' => $this->input->post('visibleMainForm'), |
185
|
|
|
]; |
186
|
|
|
|
187
|
|
View Code Duplication |
if ($this->use_captcha == TRUE && !$this->dx_auth->is_admin()) { |
188
|
|
|
$this->dx_auth->captcha(); |
189
|
|
|
$data['cap_image'] = $this->dx_auth->get_captcha_image(); |
190
|
|
|
} |
191
|
|
|
($hook = get_hook('comments_read_com_tpl')) ? eval($hook) : NULL; |
|
|
|
|
192
|
|
|
|
193
|
|
|
$comments = $this->_fetchComments($data); |
194
|
|
|
|
195
|
|
|
($hook = get_hook('comments_assign_tpl_data')) ? eval($hook) : NULL; |
|
|
|
|
196
|
|
|
|
197
|
|
|
echo json_encode( |
198
|
|
|
[ |
199
|
|
|
'comments' => $comments, |
200
|
|
|
'total_comments' => $comments_count ? $comments_count . ' ' . SStringHelper::Pluralize($comments_count, [lang('review', 'comments'), lang('reviews', 'comments'), lang('review', 'comments')]) : lang('Leave a comment', 'comments'), |
201
|
|
|
'commentsCount' => $commentsCount[$item_id], |
202
|
|
|
'validation_errors' => $this->validation_errors, |
203
|
|
|
] |
204
|
|
|
); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* Determinate commented page. |
209
|
|
|
* |
210
|
|
|
* if product - return id |
|
|
|
|
211
|
|
|
* @param string $url |
212
|
|
|
* @return string |
213
|
|
|
*/ |
214
|
|
|
public function parsUrl($url) { |
215
|
|
|
|
216
|
|
|
if (strstr($url, '/product/')) { |
217
|
|
|
$url = parse_url($url); |
218
|
|
|
/** Check is lang segment and remove it from url path * */ |
219
|
|
|
$urlArraySegments = explode('/', $url['path']); |
220
|
|
|
|
221
|
|
|
$id = $this->db->select('id, enable_comments') |
222
|
|
|
->where('url', end($urlArraySegments)) |
223
|
|
|
->get('shop_products') |
224
|
|
|
->row(); |
225
|
|
|
|
226
|
|
|
if ($id->enable_comments == 0) { |
227
|
|
|
$this->enable_comments = false; |
228
|
|
|
} |
229
|
|
|
return $id->id; |
230
|
|
|
} |
231
|
|
|
|
232
|
|
View Code Duplication |
if (strstr($url, '/image/')) { |
233
|
|
|
$url = explode(DS, $url); |
234
|
|
|
$url = $url[count($url) - 1]; |
235
|
|
|
|
236
|
|
|
return $url; |
237
|
|
|
} |
238
|
|
View Code Duplication |
if (strstr($url, '/album/')) { |
239
|
|
|
$url = explode(DS, $url); |
240
|
|
|
$url = $url[count($url) - 1]; |
241
|
|
|
|
242
|
|
|
return $url; |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
if ($url == site_url()) { |
246
|
|
|
$id = $this->db->select('main_page_id, comments_status') |
247
|
|
|
->join('content', 'settings.main_page_id=content.id') |
248
|
|
|
->get('settings') |
249
|
|
|
->row(); |
250
|
|
|
|
251
|
|
|
if ($id->comments_status == 0) { |
252
|
|
|
$this->enable_comments = false; |
253
|
|
|
} |
254
|
|
|
return $id->main_page_id; |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
$paths = explode('/', $url); |
258
|
|
|
$paths = $paths[count($paths) - 1]; |
259
|
|
|
|
260
|
|
|
$lang_id = $this->getCommentsLocale() ?: MY_Controller::getCurrentLanguage('id'); |
261
|
|
|
|
262
|
|
|
$page = $this->db->select('id, comments_status, category') |
263
|
|
|
->where('url', $paths) |
264
|
|
|
->where('lang', $lang_id) |
265
|
|
|
->get('content'); |
266
|
|
|
|
267
|
|
|
if ($page) { |
268
|
|
|
$page = $page->row(); |
269
|
|
|
|
270
|
|
|
$pageCategory = $this->db->select('id, comments_default') |
271
|
|
|
->where('id', $page->category) |
272
|
|
|
->get('category'); |
273
|
|
|
|
274
|
|
|
if ($pageCategory) { |
275
|
|
|
$pageCategory = $pageCategory->row(); |
276
|
|
|
$page->comments_status = $pageCategory->comments_default ? TRUE : $page->comments_status; |
277
|
|
|
} |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
if ($page->comments_status == 0) { |
281
|
|
|
$this->enable_comments = FALSE; |
282
|
|
|
} |
283
|
|
|
|
284
|
|
|
return $page->id; |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
/** |
288
|
|
|
* @param string $url |
289
|
|
|
* @return string |
290
|
|
|
*/ |
291
|
|
|
public function getModule($url) { |
292
|
|
|
$url = '/' . $url; |
293
|
|
|
|
294
|
|
|
if (strstr($url, '/shop/')) { |
295
|
|
|
return 'shop'; |
296
|
|
|
} |
297
|
|
|
|
298
|
|
|
if (strstr($url, '/bloh/')) { |
299
|
|
|
return 'core'; |
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
if (strstr($url, '/gallery/')) { |
303
|
|
|
return 'gallery'; |
304
|
|
|
} |
305
|
|
|
|
306
|
|
|
if ($url == site_url()) { |
307
|
|
|
return 'core'; |
308
|
|
|
} |
309
|
|
|
|
310
|
|
|
return 'core'; |
311
|
|
|
} |
312
|
|
|
|
313
|
|
|
/** |
314
|
|
|
* New comments realization |
315
|
|
|
* @return array validation data |
316
|
|
|
*/ |
317
|
|
|
public function addPost() { |
318
|
|
|
|
319
|
|
|
$this->setCommentsLocale(); |
320
|
|
|
$this->load->model('base'); |
321
|
|
|
$this->_init_settings(); |
322
|
|
|
$this->load->library('user_agent'); |
323
|
|
|
$this->load->library('form_validation'); |
324
|
|
|
$this->load->model('base'); |
325
|
|
|
|
326
|
|
|
$referer = explode('?', $this->input->server('HTTP_REFERER')); |
327
|
|
|
$item_id = $this->parsUrl($referer[0]); |
328
|
|
|
|
329
|
|
|
if ($this->period > 0 && !$this->check_comment_period()) { |
330
|
|
|
return [ |
331
|
|
|
'answer' => 'error', |
332
|
|
|
'validation_errors' => ['time_error' => lang('The following comment can be left through', 'comments') . ' ' . $this->period . ' ' . lang('minutes', 'comments')], |
333
|
|
|
]; |
334
|
|
|
} |
335
|
|
|
|
336
|
|
|
// Validate email and nickname from unregistered users. |
337
|
|
|
if (!$this->dx_auth->is_logged_in()) { |
338
|
|
|
$this->form_validation->set_rules('comment_email', lang('Email', 'comments'), 'trim|required|xss_clean|valid_email'); |
339
|
|
|
$this->form_validation->set_rules('comment_author', lang('Your name', 'comments'), 'trim|required|xss_clean|max_length[50]'); |
340
|
|
|
$this->form_validation->set_rules('comment_site', lang('Site', 'comments'), 'trim|xss_clean|max_length[250]'); |
341
|
|
|
} |
342
|
|
|
|
343
|
|
|
// Check captcha code if captcha_check enabled and user in not admin. |
344
|
|
|
if ($this->use_captcha AND ! $this->dx_auth->is_admin()) { |
|
|
|
|
345
|
|
|
$this->form_validation->set_message('callback_captcha_check', lang('Wrong code protection', 'comments')); |
346
|
|
View Code Duplication |
if ($this->dx_auth->use_recaptcha) { |
347
|
|
|
$this->form_validation->set_rules('recaptcha_response_field', lang('Code protection', 'comments'), 'trim|required|xss_clean|callback_captcha_check'); |
348
|
|
|
} else { |
349
|
|
|
$this->form_validation->set_rules('captcha', lang('Code protection', 'comments'), 'trim|required|xss_clean|callback_captcha_check'); |
350
|
|
|
} |
351
|
|
|
} |
352
|
|
|
|
353
|
|
View Code Duplication |
if ($this->max_comment_length != 0) { |
354
|
|
|
$this->form_validation->set_rules('comment_text', lang('Comment', 'comments'), 'trim|required|xss_clean|max_length[' . $this->max_comment_length . ']'); |
355
|
|
|
} else { |
356
|
|
|
$this->form_validation->set_rules('comment_text', lang('Comment', 'comments'), 'trim|required|xss_clean'); |
357
|
|
|
} |
358
|
|
|
|
359
|
|
|
if (!$this->form_validation->run($this)) { |
360
|
|
|
// $this->dx_auth->captcha(); |
361
|
|
|
// $cap_image = $this->dx_auth->get_captcha_image(); |
362
|
|
|
return [ |
363
|
|
|
'answer' => 'error', |
364
|
|
|
'validation_errors' => $this->form_validation->getErrorsArray(), |
365
|
|
|
]; |
366
|
|
|
} else { |
367
|
|
|
if (!$this->dx_auth->is_logged_in()) { |
368
|
|
|
$comment_author = $this->input->post('comment_author'); |
369
|
|
|
$comment_email = $this->input->post('comment_email'); |
370
|
|
|
|
371
|
|
|
// Write on cookie nickname and email |
372
|
|
|
$this->_write_cookie($comment_author, $comment_email, $this->input->post('comment_site')); |
373
|
|
|
} else { |
374
|
|
|
$user = $this->db->get_where('users', ['id' => $this->dx_auth->get_user_id()])->row_array(); |
375
|
|
|
$comment_author = $user['username']; |
|
|
|
|
376
|
|
|
$comment_email = $user['email']; |
|
|
|
|
377
|
|
|
} |
378
|
|
|
|
379
|
|
|
$comment_text = nl2br($this->input->post('comment_text')); |
380
|
|
|
$comment_text_plus = nl2br($this->input->post('comment_text_plus')); |
381
|
|
|
$comment_text_minus = nl2br($this->input->post('comment_text_minus')); |
382
|
|
|
$rate = $this->input->post('ratec'); |
383
|
|
|
if ($rate && SHOP_INSTALLED && class_exists('SProductsQuery') && SProductsQuery::create()->findPk($item_id) !== null) { |
384
|
|
|
$model = SProductsRatingQuery::create()->findPk($item_id); |
385
|
|
|
if ($model === null) { |
386
|
|
|
$model = new SProductsRating; |
|
|
|
|
387
|
|
|
$model->setProductId($item_id); |
388
|
|
|
} |
389
|
|
|
$model->setVotes($model->getVotes() + 1); |
390
|
|
|
$model->setRating($model->getRating() + $rate); |
391
|
|
|
$model->save(); |
392
|
|
|
} |
393
|
|
|
$email = $this->db->select('email') |
394
|
|
|
->get_where('users', ['id' => $this->dx_auth->get_user_id()], 1) |
395
|
|
|
->row(); |
396
|
|
|
|
397
|
|
|
$comment_data = [ |
398
|
|
|
'module' => $this->module, |
399
|
|
|
'user_id' => $this->dx_auth->get_user_id(), // 0 if unregistered |
400
|
|
|
'user_name' => $this->dx_auth->is_logged_in() ? $this->dx_auth->get_username() : $this->input->post('comment_author'), |
401
|
|
|
'user_mail' => $this->dx_auth->is_logged_in() ? $email->email : $this->input->post('comment_email'), |
402
|
|
|
'user_site' => $this->input->post('comment_site'), |
403
|
|
|
'text' => $comment_text, |
404
|
|
|
'text_plus' => $comment_text_plus, |
405
|
|
|
'text_minus' => $comment_text_minus, |
406
|
|
|
'item_id' => $item_id, |
407
|
|
|
'status' => $this->_comment_status(), |
408
|
|
|
'agent' => $this->agent->agent_string(), |
409
|
|
|
'user_ip' => $this->input->ip_address(), |
410
|
|
|
'date' => time(), |
411
|
|
|
'rate' => $this->input->post('ratec'), |
412
|
|
|
'parent' => $this->input->post('comment_parent'), |
413
|
|
|
]; |
414
|
|
|
$this->db->insert('comments', $comment_data); |
415
|
|
|
$this->_recount_comments($item_id, $comment_data['module']); |
416
|
|
|
\CMSFactory\Events::create()->registerEvent(['commentId' => $this->db->insert_id()]); |
417
|
|
|
$this->validation_errors = ''; |
418
|
|
|
|
419
|
|
|
//return sucesfull answer |
420
|
|
|
return [ |
421
|
|
|
'answer' => 'sucesfull', |
422
|
|
|
'moderation_enabled' => $this->_comment_status(), |
423
|
|
|
]; |
424
|
|
|
} |
425
|
|
|
} |
426
|
|
|
|
427
|
|
|
/** |
428
|
|
|
* @throws \Propel\Runtime\Exception\PropelException |
|
|
|
|
429
|
|
|
*/ |
430
|
|
|
public function newPost() { |
431
|
|
|
$this->load->model('base'); |
432
|
|
|
$this->_init_settings(); |
433
|
|
|
|
434
|
|
|
($hook = get_hook('comments_on_add')) ? eval($hook) : NULL; |
|
|
|
|
435
|
|
|
|
436
|
|
|
$this->load->library('user_agent'); |
437
|
|
|
$this->load->library('form_validation'); |
438
|
|
|
$this->load->model('base'); |
439
|
|
|
|
440
|
|
|
$item_id = $this->parsUrl($this->input->server('HTTP_REFERER')); |
441
|
|
|
|
442
|
|
|
if ($this->period > 0) { |
443
|
|
|
if ($this->check_comment_period() == FALSE) { |
444
|
|
|
echo json_encode( |
445
|
|
|
[ |
446
|
|
|
'answer' => 'error', |
447
|
|
|
'validation_errors' => lang('The following comment can be left through', 'comments') . ' ' . $this->period . ' ' . lang('minutes', 'comments'), |
448
|
|
|
] |
449
|
|
|
); |
450
|
|
|
return; |
451
|
|
|
} |
452
|
|
|
} |
453
|
|
|
|
454
|
|
|
// Validate email and nickname from unregistered users. |
455
|
|
View Code Duplication |
if ($this->dx_auth->is_logged_in() == FALSE) { |
456
|
|
|
($hook = get_hook('comments_set_val_rules')) ? eval($hook) : NULL; |
|
|
|
|
457
|
|
|
|
458
|
|
|
$this->form_validation->set_rules('comment_email', lang('Email', 'comments'), 'trim|required|xss_clean|valid_email'); |
459
|
|
|
$this->form_validation->set_rules('comment_author', lang('Your name', 'comments'), 'trim|required|xss_clean|max_length[50]'); |
460
|
|
|
$this->form_validation->set_rules('comment_site', lang('Site', 'comments'), 'trim|xss_clean|max_length[250]'); |
461
|
|
|
} |
462
|
|
|
|
463
|
|
|
// Check captcha code if captcha_check enabled and user in not admin. |
464
|
|
|
if ($this->use_captcha == TRUE AND $this->dx_auth->is_admin() == FALSE) { |
465
|
|
|
($hook = get_hook('comments_set_captcha')) ? eval($hook) : NULL; |
|
|
|
|
466
|
|
|
$this->form_validation->set_message('callback_captcha_check', lang('Wrong code protection', 'comments')); |
467
|
|
View Code Duplication |
if ($this->dx_auth->use_recaptcha) { |
468
|
|
|
$this->form_validation->set_rules('recaptcha_response_field', lang('Code protection', 'comments'), 'trim|required|xss_clean|callback_captcha_check'); |
469
|
|
|
} else { |
470
|
|
|
$this->form_validation->set_rules('captcha', lang('Code protection', 'comments'), 'trim|required|xss_clean|callback_captcha_check'); |
471
|
|
|
} |
472
|
|
|
} |
473
|
|
|
|
474
|
|
View Code Duplication |
if ($this->max_comment_length != 0) { |
475
|
|
|
$this->form_validation->set_rules('comment_text', lang('Comment', 'comments'), 'trim|required|xss_clean|max_length[' . $this->max_comment_length . ']'); |
476
|
|
|
} else { |
477
|
|
|
$this->form_validation->set_rules('comment_text', lang('Comment', 'comments'), 'trim|required|xss_clean'); |
478
|
|
|
} |
479
|
|
|
|
480
|
|
|
if ($this->form_validation->run($this) == FALSE) { |
481
|
|
|
($hook = get_hook('comments_validation_failed')) ? eval($hook) : NULL; |
|
|
|
|
482
|
|
|
//$this->core->error( validation_errors() ); |
483
|
|
|
// $this->template->assign('comment_errors', validation_errors()); |
484
|
|
|
} else { |
485
|
|
View Code Duplication |
if ($this->dx_auth->is_logged_in() == FALSE) { |
486
|
|
|
($hook = get_hook('comments_author_not_logged')) ? eval($hook) : NULL; |
|
|
|
|
487
|
|
|
|
488
|
|
|
$comment_author = trim(htmlspecialchars($this->input->post('comment_author'))); |
489
|
|
|
$comment_email = trim(htmlspecialchars($this->input->post('comment_email'))); |
490
|
|
|
|
491
|
|
|
// Write on cookie nickname and email |
492
|
|
|
$this->_write_cookie($comment_author, $comment_email, $this->input->post('comment_site')); |
493
|
|
|
} else { |
494
|
|
|
($hook = get_hook('comments_author_logged')) ? eval($hook) : NULL; |
|
|
|
|
495
|
|
|
|
496
|
|
|
$user = $this->db->get_where('users', ['id' => $this->dx_auth->get_user_id()])->row_array(); |
497
|
|
|
$comment_author = $user['username']; |
|
|
|
|
498
|
|
|
$comment_email = $user['email']; |
|
|
|
|
499
|
|
|
} |
500
|
|
|
|
501
|
|
|
$comment_text = trim(htmlspecialchars($this->input->post('comment_text'))); |
502
|
|
|
$comment_text = str_replace("\n", '<br/>', $comment_text); |
503
|
|
|
$comment_text_plus = trim(htmlspecialchars($this->input->post('comment_text_plus'))); |
504
|
|
|
$comment_text_plus = str_replace("\n", '<br/>', $comment_text_plus); |
505
|
|
|
$comment_text_minus = trim(htmlspecialchars($this->input->post('comment_text_minus'))); |
506
|
|
|
$comment_text_minus = str_replace("\n", '<br/>', $comment_text_minus); |
507
|
|
|
$rate = $this->input->post('ratec'); |
508
|
|
View Code Duplication |
if ($this->input->post('ratec')) { |
509
|
|
|
if (class_exists('SProductsQuery')) { |
510
|
|
|
if (SProductsQuery::create()->findPk($item_id) !== null) { |
511
|
|
|
$model = SProductsRatingQuery::create()->findPk($item_id); |
512
|
|
|
if ($model === null) { |
513
|
|
|
$model = new SProductsRating; |
|
|
|
|
514
|
|
|
$model->setProductId($item_id); |
515
|
|
|
} |
516
|
|
|
$model->setVotes($model->getVotes() + 1); |
517
|
|
|
$model->setRating($model->getRating() + $rate); |
518
|
|
|
$model->save(); |
519
|
|
|
} |
520
|
|
|
} |
521
|
|
|
} |
522
|
|
|
} |
523
|
|
|
if ($this->input->post('action') == 'newPost') { |
524
|
|
|
$email = $this->db->select('email') |
525
|
|
|
->get_where('users', ['id' => $this->dx_auth->get_user_id()], 1) |
526
|
|
|
->row(); |
527
|
|
|
|
528
|
|
|
if (!validation_errors()) { |
529
|
|
|
$comment_data = [ |
530
|
|
|
'module' => $this->module, |
531
|
|
|
'user_id' => $this->dx_auth->get_user_id(), // 0 if unregistered |
532
|
|
|
'user_name' => $this->dx_auth->is_logged_in() ? $this->dx_auth->get_username() : trim(htmlspecialchars($this->input->post('comment_author'))), |
533
|
|
|
'user_mail' => $this->dx_auth->is_logged_in() ? $email->email : trim(htmlspecialchars($this->input->post('comment_email'))), |
534
|
|
|
'user_site' => htmlspecialchars($this->input->post(comment_site)), |
535
|
|
|
'text' => $comment_text, |
536
|
|
|
'text_plus' => $comment_text_plus, |
537
|
|
|
'text_minus' => $comment_text_minus, |
538
|
|
|
'item_id' => $item_id, |
539
|
|
|
'status' => $this->_comment_status(), |
540
|
|
|
'agent' => $this->agent->agent_string(), |
541
|
|
|
'user_ip' => $this->input->ip_address(), |
542
|
|
|
'date' => time(), |
543
|
|
|
'rate' => $this->input->post('ratec'), |
544
|
|
|
'parent' => $this->input->post('comment_parent'), |
545
|
|
|
]; |
546
|
|
|
|
547
|
|
|
$this->db->insert('comments', $comment_data); |
548
|
|
|
$this->_recount_comments($item_id, $comment_data['module']); |
549
|
|
|
\CMSFactory\Events::create()->registerEvent(['commentId' => $this->db->insert_id()]); |
550
|
|
|
$this->validation_errors = ''; |
551
|
|
|
|
552
|
|
|
//return sucesfull JSON answer |
553
|
|
|
echo json_encode( |
554
|
|
|
['answer' => 'sucesfull'] |
555
|
|
|
); |
556
|
|
|
} else { |
557
|
|
|
|
558
|
|
|
if ($this->dx_auth->use_recaptcha) { |
559
|
|
|
$field_name = 'recaptcha_response_field'; |
|
|
|
|
560
|
|
|
} else { |
561
|
|
|
$field_name = 'captcha'; |
|
|
|
|
562
|
|
|
} |
563
|
|
|
|
564
|
|
|
// if ($this->form_validation->error($field_name)) { |
565
|
|
|
$this->dx_auth->captcha(); |
566
|
|
|
$cap_image = $this->dx_auth->get_captcha_image(); |
567
|
|
|
// } |
568
|
|
|
// if ($this->use_captcha == TRUE && !$this->dx_auth->is_admin()) { |
569
|
|
|
// $this->dx_auth->captcha(); |
570
|
|
|
// $data['cap_image'] = $this->dx_auth->get_captcha_image(); |
571
|
|
|
// } |
572
|
|
|
echo json_encode( |
573
|
|
|
[ |
574
|
|
|
'answer' => 'error', |
575
|
|
|
'validation_errors' => validation_errors(), |
576
|
|
|
'cap_image' => $cap_image, |
577
|
|
|
] |
578
|
|
|
); |
579
|
|
|
} |
580
|
|
|
} |
581
|
|
|
} |
582
|
|
|
|
583
|
|
View Code Duplication |
public function setyes() { |
584
|
|
|
$comid = $this->input->post('comid'); |
585
|
|
|
if ($this->session->userdata('commentl' . $comid) != 1) { |
586
|
|
|
$row = $this->db->where('id', $comid)->get('comments')->row(); |
587
|
|
|
$like = $row->like; |
588
|
|
|
$like = $like + 1; |
589
|
|
|
$data = ['like' => $like]; |
590
|
|
|
$this->db->where('id', $comid); |
591
|
|
|
$this->db->update('comments', $data); |
592
|
|
|
$this->session->set_userdata('commentl' . $comid, 1); |
593
|
|
|
if ($this->input->is_ajax_request()) { |
594
|
|
|
return json_encode(['y_count' => "$like"]); |
595
|
|
|
} else { |
596
|
|
|
$like--; |
597
|
|
|
return json_encode(['y_count' => "$like"]); |
598
|
|
|
} |
599
|
|
|
} |
600
|
|
|
} |
601
|
|
|
|
602
|
|
View Code Duplication |
public function setno() { |
603
|
|
|
$comid = $this->input->post('comid'); |
604
|
|
|
if ($this->session->userdata('commentl' . $comid) != 1) { |
605
|
|
|
$row = $this->db->where('id', $comid)->get('comments')->row(); |
606
|
|
|
$disslike = $row->disslike; |
607
|
|
|
$disslike = $disslike + 1; |
608
|
|
|
$data = ['disslike' => $disslike]; |
609
|
|
|
$this->db->where('id', $comid); |
610
|
|
|
$this->db->update('comments', $data); |
611
|
|
|
$this->session->set_userdata('commentl' . $comid, 1); |
612
|
|
|
if ($this->input->is_ajax_request()) { |
613
|
|
|
return json_encode(['n_count' => "$disslike"]); |
614
|
|
|
} else { |
615
|
|
|
$disslike--; |
616
|
|
|
return json_encode(['n_count' => "$disslike"]); |
617
|
|
|
} |
618
|
|
|
} |
619
|
|
|
} |
620
|
|
|
|
621
|
|
|
/** |
622
|
|
|
* @param array $ids |
623
|
|
|
* @param string $module |
624
|
|
|
* @param int $status |
625
|
|
|
* @return array|void |
626
|
|
|
*/ |
627
|
|
|
public function getTotalCommentsForProducts($ids, $module = 'shop', $status = 0) { |
628
|
|
|
if ($ids == null || !$this->db->table_exists('comments')) { |
629
|
|
|
return; |
|
|
|
|
630
|
|
|
} |
631
|
|
|
|
632
|
|
|
$this->db->select('item_id, COUNT(comments.id) AS `count`'); |
633
|
|
|
$this->db->group_by('item_id'); |
634
|
|
|
$this->db->where_in('item_id', $ids); |
635
|
|
|
$this->db->where('status', $status); |
636
|
|
|
$this->db->where('module = ', $module); |
637
|
|
|
$query = $this->db->get('comments')->result_array(); |
638
|
|
|
|
639
|
|
|
$result = []; |
640
|
|
|
|
641
|
|
View Code Duplication |
foreach ($query as $q) { |
642
|
|
|
$result[$q['item_id']] = $q['count'] . ' ' . SStringHelper::Pluralize((int) $q['count'], [lang('review', 'comments'), lang('reviews', 'comments'), lang('review', 'comments')]); |
643
|
|
|
} |
644
|
|
|
|
645
|
|
View Code Duplication |
foreach ((array) $ids as $id) { |
646
|
|
|
if (!$result[$id]) { |
647
|
|
|
$result[$id] = 0 . ' ' . SStringHelper::Pluralize('0', [lang('review', 'comments'), lang('reviews', 'comments'), lang('comments', 'comments')]); |
648
|
|
|
} |
649
|
|
|
} |
650
|
|
|
|
651
|
|
|
return $result; |
652
|
|
|
} |
653
|
|
|
|
654
|
|
|
/** |
655
|
|
|
* Get count answers to comment by id |
656
|
|
|
* @param integer $commentId |
657
|
|
|
* @return boolean|int |
|
|
|
|
658
|
|
|
*/ |
659
|
|
|
public function getCountCommentAnswersByCommentId($commentId) { |
660
|
|
|
$query = $this->db->where('parent', $commentId)->get('comments')->result_array(); |
661
|
|
|
if ($query) { |
662
|
|
|
return count($query); |
663
|
|
|
} else { |
664
|
|
|
return false; |
665
|
|
|
} |
666
|
|
|
} |
667
|
|
|
|
668
|
|
|
/** |
669
|
|
|
* @return string |
670
|
|
|
*/ |
671
|
|
View Code Duplication |
public function getCommentsLocale() { |
672
|
|
|
|
673
|
|
|
/** @var CI_DB_result $locale */ |
674
|
|
|
$locale = $this->db->get_where('languages', ['identif' => $this->comments_locale]); |
675
|
|
|
|
676
|
|
|
if ($locale->num_rows() > 0) { |
677
|
|
|
|
678
|
|
|
$locale_arr = $locale->row_array(); |
679
|
|
|
return $locale_arr['id']; |
680
|
|
|
|
681
|
|
|
} |
682
|
|
|
return false; |
683
|
|
|
|
684
|
|
|
} |
685
|
|
|
|
686
|
|
|
/** |
687
|
|
|
* @return void |
|
|
|
|
688
|
|
|
*/ |
689
|
|
|
public function setCommentsLocale() { |
690
|
|
|
|
691
|
|
|
$this->comments_locale = MY_Controller::getCurrentLocale(); |
692
|
|
|
} |
693
|
|
|
|
694
|
|
|
} |