-
Notifications
You must be signed in to change notification settings - Fork 93
/
ProductComment.php
415 lines (363 loc) · 14.6 KB
/
ProductComment.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
<?php
/**
* Copyright since 2007 PrestaShop SA and Contributors
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License 3.0 (AFL-3.0)
* that is bundled with this package in the file LICENSE.md.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/AFL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to [email protected] so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to https://devdocs.prestashop.com/ for more information.
*
* @author PrestaShop SA and Contributors <[email protected]>
* @copyright Since 2007 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License 3.0 (AFL-3.0)
*/
if (!defined('_PS_VERSION_')) {
exit;
}
class ProductComment extends ObjectModel
{
/** @var int */
public $id;
/** @var int */
public $id_product;
/** @var int */
public $id_customer;
/** @var int */
public $id_guest;
/** @var int */
public $customer_name;
/** @var string */
public $title;
/** @var string */
public $content;
/** @var int */
public $grade;
/** @var bool */
public $validate = false;
/** @var bool */
public $deleted = false;
/** @var string Object creation date */
public $date_add;
/**
* @see ObjectModel::$definition
*/
public static $definition = [
'table' => 'product_comment',
'primary' => 'id_product_comment',
'fields' => [
'id_product' => ['type' => self::TYPE_INT, 'validate' => 'isUnsignedId', 'required' => true],
'id_customer' => ['type' => self::TYPE_INT, 'validate' => 'isUnsignedId', 'required' => true],
'id_guest' => ['type' => self::TYPE_INT],
'customer_name' => ['type' => self::TYPE_STRING],
'title' => ['type' => self::TYPE_STRING],
'content' => ['type' => self::TYPE_STRING, 'validate' => 'isMessage', 'size' => 65535, 'required' => true],
'grade' => ['type' => self::TYPE_FLOAT, 'validate' => 'isFloat'],
'validate' => ['type' => self::TYPE_BOOL, 'validate' => 'isBool'],
'deleted' => ['type' => self::TYPE_BOOL],
'date_add' => ['type' => self::TYPE_DATE],
],
];
/**
* Get comments by IdProduct
*
* @return array|bool
*/
public static function getByProduct($id_product, $p = 1, $n = null, $id_customer = null)
{
if (!Validate::isUnsignedId($id_product)) {
return false;
}
$validate = (bool) Configuration::get('PRODUCT_COMMENTS_MODERATE');
$p = (int) $p;
$n = (int) $n;
$id_customer = (int) $id_customer;
if ($p <= 1) {
$p = 1;
}
if ($n != null && $n <= 0) {
$n = 5;
}
$cache_id = 'ProductComment::getByProduct_' . $id_product . '-' . $p . '-' . $n . '-' . $id_customer . '-' . $validate;
if (!Cache::isStored($cache_id)) {
$result = Db::getInstance((bool) _PS_USE_SQL_SLAVE_)->executeS('
SELECT pc.`id_product_comment`,
(SELECT count(*) FROM `' . _DB_PREFIX_ . 'product_comment_usefulness` pcu WHERE pcu.`id_product_comment` = pc.`id_product_comment` AND pcu.`usefulness` = 1) AS total_useful,
(SELECT count(*) FROM `' . _DB_PREFIX_ . 'product_comment_usefulness` pcu WHERE pcu.`id_product_comment` = pc.`id_product_comment`) AS total_advice, ' .
($id_customer ? '(SELECT count(*) FROM `' . _DB_PREFIX_ . 'product_comment_usefulness` pcuc WHERE pcuc.`id_product_comment` = pc.`id_product_comment` AND pcuc.id_customer = ' . $id_customer . ') AS customer_advice, ' : '') .
($id_customer ? '(SELECT count(*) FROM `' . _DB_PREFIX_ . 'product_comment_report` pcrc WHERE pcrc.`id_product_comment` = pc.`id_product_comment` AND pcrc.id_customer = ' . $id_customer . ') AS customer_report, ' : '') . '
IF(c.id_customer, CONCAT(c.`firstname`, \' \', LEFT(c.`lastname`, 1)), pc.customer_name) customer_name, pc.`content`, pc.`grade`, pc.`date_add`, pc.title
FROM `' . _DB_PREFIX_ . 'product_comment` pc
LEFT JOIN `' . _DB_PREFIX_ . 'customer` c ON c.`id_customer` = pc.`id_customer`
WHERE pc.`id_product` = ' . $id_product . ($validate ? ' AND pc.`validate` = 1' : '') . '
ORDER BY pc.`date_add` DESC
' . ($n ? 'LIMIT ' . (($p - 1) * $n) . ', ' . $n : ''));
Cache::store($cache_id, $result);
}
return Cache::retrieve($cache_id);
}
/**
* Return customer's comment
*
* @return array Comments
*/
public static function getByCustomer($id_product, $id_customer, $get_last = false, $id_guest = false)
{
$cache_id = 'ProductComment::getByCustomer_' . (int) $id_product . '-' . (int) $id_customer . '-' . (bool) $get_last . '-' . (int) $id_guest;
if (!Cache::isStored($cache_id)) {
$results = Db::getInstance()->executeS('
SELECT *
FROM `' . _DB_PREFIX_ . 'product_comment` pc
WHERE pc.`id_product` = ' . (int) $id_product . '
AND ' . (!$id_guest ? 'pc.`id_customer` = ' . (int) $id_customer : 'pc.`id_guest` = ' . (int) $id_guest) . '
ORDER BY pc.`date_add` DESC '
. ($get_last ? 'LIMIT 1' : '')
);
if ($get_last && count($results)) {
$results = array_shift($results);
}
Cache::store($cache_id, $results);
}
return Cache::retrieve($cache_id);
}
/**
* Get Grade By product
*
* @return array|bool
*/
public static function getGradeByProduct($id_product, $id_lang)
{
if (!Validate::isUnsignedId($id_product) ||
!Validate::isUnsignedId($id_lang)) {
return false;
}
$validate = (bool) Configuration::get('PRODUCT_COMMENTS_MODERATE');
return Db::getInstance((bool) _PS_USE_SQL_SLAVE_)->executeS('
SELECT pc.`id_product_comment`, pcg.`grade`, pccl.`name`, pcc.`id_product_comment_criterion`
FROM `' . _DB_PREFIX_ . 'product_comment` pc
LEFT JOIN `' . _DB_PREFIX_ . 'product_comment_grade` pcg ON (pcg.`id_product_comment` = pc.`id_product_comment`)
LEFT JOIN `' . _DB_PREFIX_ . 'product_comment_criterion` pcc ON (pcc.`id_product_comment_criterion` = pcg.`id_product_comment_criterion`)
LEFT JOIN `' . _DB_PREFIX_ . 'product_comment_criterion_lang` pccl ON (pccl.`id_product_comment_criterion` = pcg.`id_product_comment_criterion`)
WHERE pc.`id_product` = ' . $id_product . '
AND pccl.`id_lang` = ' . $id_lang .
($validate ? ' AND pc.`validate` = 1' : ''));
}
public static function getRatings($id_product)
{
$validate = Configuration::get('PRODUCT_COMMENTS_MODERATE');
$sql = 'SELECT AVG(pc.`grade`) AS avg,
MIN(pc.`grade`) AS min,
MAX(pc.`grade`) AS max
FROM `' . _DB_PREFIX_ . 'product_comment` pc
WHERE pc.`id_product` = ' . (int) $id_product . '
AND pc.`deleted` = 0' .
($validate == '1' ? ' AND pc.`validate` = 1' : '');
return Db::getInstance((bool) _PS_USE_SQL_SLAVE_)->getRow($sql);
}
public static function getAveragesByProduct($id_product, $id_lang)
{
/* Get all grades */
$grades = ProductComment::getGradeByProduct((int) $id_product, (int) $id_lang);
$total = ProductComment::getGradedCommentNumber((int) $id_product);
if (!count($grades) || !$total) {
return [];
}
/* Addition grades for each criterion */
$criterionsGradeTotal = [];
$count_grades = count($grades);
for ($i = 0; $i < $count_grades; ++$i) {
if (array_key_exists($grades[$i]['id_product_comment_criterion'], $criterionsGradeTotal) === false) {
$criterionsGradeTotal[$grades[$i]['id_product_comment_criterion']] = (int) ($grades[$i]['grade']);
} else {
$criterionsGradeTotal[$grades[$i]['id_product_comment_criterion']] += (int) ($grades[$i]['grade']);
}
}
/* Finally compute the averages */
$averages = [];
foreach ($criterionsGradeTotal as $key => $criterionGradeTotal) {
$averages[(int) $key] = $criterionGradeTotal / $total;
}
return $averages;
}
/**
* Return number of comments and average grade by products
*
* @return int|bool
*/
public static function getGradedCommentNumber($id_product)
{
if (!Validate::isUnsignedId($id_product)) {
return false;
}
$validate = (int) Configuration::get('PRODUCT_COMMENTS_MODERATE');
$result = Db::getInstance((bool) _PS_USE_SQL_SLAVE_)->getRow('
SELECT COUNT(pc.`id_product`) AS nbr
FROM `' . _DB_PREFIX_ . 'product_comment` pc
WHERE `id_product` = ' . $id_product . ($validate == '1' ? ' AND `validate` = 1' : '') . '
AND `grade` > 0');
return (int) ($result['nbr']);
}
/**
* Get comments by Validation
*
* @return array Comments
*
* @deprecated 6.0.0
*/
public static function getByValidate($validate = '0', $deleted = false, $p = null, $limit = null, $skip_validate = false)
{
$sql = '
SELECT pc.`id_product_comment`, pc.`id_product`, c.id_customer AS customer_id, IF(c.id_customer, CONCAT(c.`firstname`, \' \', c.`lastname`), pc.customer_name) customer_name, pc.`title`, pc.`content`, pc.`grade`, pc.`date_add`, pl.`name`
FROM `' . _DB_PREFIX_ . 'product_comment` pc
LEFT JOIN `' . _DB_PREFIX_ . 'customer` c ON (c.`id_customer` = pc.`id_customer`)
LEFT JOIN `' . _DB_PREFIX_ . 'product_lang` pl ON (pl.`id_product` = pc.`id_product` AND pl.`id_lang` = ' . (int) Context::getContext()->language->id . Shop::addSqlRestrictionOnLang('pl') . ')';
if (!$skip_validate) {
$sql .= ' WHERE pc.`validate` = ' . (int) $validate;
}
$sql .= ' ORDER BY pc.`date_add` DESC';
if ($p && $limit) {
$offset = ($p - 1) * $limit;
$sql .= ' LIMIT ' . (int) $offset . ',' . (int) $limit;
}
return Db::getInstance()->executeS($sql);
}
/**
* Get numbers of comments by Validation
*
* @return int Count of comments
*
* @deprecated 6.0.0
*/
public static function getCountByValidate($validate = '0', $skip_validate = false)
{
$sql = '
SELECT COUNT(*)
FROM `' . _DB_PREFIX_ . 'product_comment`';
if (!$skip_validate) {
$sql .= ' WHERE `validate` = ' . (int) $validate;
}
return (int) Db::getInstance()->getValue($sql);
}
/**
* Get all comments
*
* @return array Comments
*/
public static function getAll()
{
return Db::getInstance()->executeS('
SELECT pc.`id_product_comment`, pc.`id_product`, IF(c.id_customer, CONCAT(c.`firstname`, \' \', c.`lastname`), pc.customer_name) customer_name, pc.`content`, pc.`grade`, pc.`date_add`, pl.`name`
FROM `' . _DB_PREFIX_ . 'product_comment` pc
LEFT JOIN `' . _DB_PREFIX_ . 'customer` c ON (c.`id_customer` = pc.`id_customer`)
LEFT JOIN `' . _DB_PREFIX_ . 'product_lang` pl ON (pl.`id_product` = pc.`id_product` AND pl.`id_lang` = ' . (int) Context::getContext()->language->id . Shop::addSqlRestrictionOnLang('pl') . ')
ORDER BY pc.`date_add` DESC');
}
/**
* Validate a comment
*
* @return bool succeed
*/
public function validate($validate = '1')
{
if (!Validate::isUnsignedId($this->id)) {
return false;
}
$success = (Db::getInstance()->execute('
UPDATE `' . _DB_PREFIX_ . 'product_comment` SET
`validate` = ' . (int) $validate . '
WHERE `id_product_comment` = ' . $this->id));
Hook::exec('actionObjectProductCommentValidateAfter', ['object' => $this]);
return $success;
}
/**
* Delete a comment, grade and report data
*
* @return bool succeed
*
* @deprecated 6.0.0
*/
public function delete()
{
return parent::delete()
&& ProductComment::deleteGrades($this->id)
&& ProductComment::deleteReports($this->id)
&& ProductComment::deleteUsefulness($this->id);
}
/**
* Delete Grades
*
* @return bool succeed
*
* @deprecated 6.0.0
*/
public static function deleteGrades($id_product_comment)
{
if (!Validate::isUnsignedId($id_product_comment)) {
return false;
}
return Db::getInstance()->execute('
DELETE FROM `' . _DB_PREFIX_ . 'product_comment_grade`
WHERE `id_product_comment` = ' . $id_product_comment);
}
/**
* Delete Reports
*
* @return bool succeed
*
* @deprecated 6.0.0
*/
public static function deleteReports($id_product_comment)
{
if (!Validate::isUnsignedId($id_product_comment)) {
return false;
}
return Db::getInstance()->execute('
DELETE FROM `' . _DB_PREFIX_ . 'product_comment_report`
WHERE `id_product_comment` = ' . $id_product_comment);
}
/**
* Delete usefulness
*
* @return bool succeed
*
* @deprecated 6.0.0
*/
public static function deleteUsefulness($id_product_comment)
{
if (!Validate::isUnsignedId($id_product_comment)) {
return false;
}
return Db::getInstance()->execute('
DELETE FROM `' . _DB_PREFIX_ . 'product_comment_usefulness`
WHERE `id_product_comment` = ' . $id_product_comment);
}
/**
* Get reported comments
*
* @return array Comments
*
* @deprecated 6.0.0
*/
public static function getReportedComments()
{
return Db::getInstance((bool) _PS_USE_SQL_SLAVE_)->executeS('
SELECT DISTINCT(pc.`id_product_comment`), pc.`id_product`, IF(c.id_customer, CONCAT(c.`firstname`, \' \', c.`lastname`), pc.customer_name) customer_name, pc.`content`, pc.`grade`, pc.`date_add`, pl.`name`, pc.`title`
FROM `' . _DB_PREFIX_ . 'product_comment_report` pcr
LEFT JOIN `' . _DB_PREFIX_ . 'product_comment` pc
ON pcr.id_product_comment = pc.id_product_comment
LEFT JOIN `' . _DB_PREFIX_ . 'customer` c ON (c.`id_customer` = pc.`id_customer`)
LEFT JOIN `' . _DB_PREFIX_ . 'product_lang` pl ON (pl.`id_product` = pc.`id_product` AND pl.`id_lang` = ' . (int) Context::getContext()->language->id . ' AND pl.`id_lang` = ' . (int) Context::getContext()->language->id . Shop::addSqlRestrictionOnLang('pl') . ')
ORDER BY pc.`date_add` DESC');
}
}