forked from PrestaShop/productcomments
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ProductCommentCriterion.php
284 lines (257 loc) · 9.9 KB
/
ProductCommentCriterion.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
<?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)
*/
class ProductCommentCriterion extends ObjectModel
{
const NAME_MAX_LENGTH = 64;
public $id;
public $id_product_comment_criterion_type;
public $name;
public $active = true;
/**
* @see ObjectModel::$definition
*/
public static $definition = [
'table' => 'product_comment_criterion',
'primary' => 'id_product_comment_criterion',
'multilang' => true,
'fields' => [
'id_product_comment_criterion_type' => ['type' => self::TYPE_INT],
'active' => ['type' => self::TYPE_BOOL],
// Lang fields
'name' => ['type' => self::TYPE_STRING, 'lang' => true, 'validate' => 'isGenericName', 'required' => true, 'size' => self::NAME_MAX_LENGTH],
],
];
public function delete()
{
if (!parent::delete()) {
return false;
}
if ($this->id_product_comment_criterion_type == 2) {
if (!Db::getInstance()->execute('
DELETE FROM ' . _DB_PREFIX_ . 'product_comment_criterion_category
WHERE id_product_comment_criterion=' . (int) $this->id)) {
return false;
}
} elseif ($this->id_product_comment_criterion_type == 3) {
if (!Db::getInstance()->execute('
DELETE FROM ' . _DB_PREFIX_ . 'product_comment_criterion_product
WHERE id_product_comment_criterion=' . (int) $this->id)) {
return false;
}
}
return Db::getInstance()->execute('
DELETE FROM `' . _DB_PREFIX_ . 'product_comment_grade`
WHERE `id_product_comment_criterion` = ' . (int) $this->id);
}
public function update($nullValues = false)
{
$previousUpdate = new self((int) $this->id);
if (!parent::update($nullValues)) {
return false;
}
if ($previousUpdate->id_product_comment_criterion_type != $this->id_product_comment_criterion_type) {
if ($previousUpdate->id_product_comment_criterion_type == 2) {
return Db::getInstance()->execute('
DELETE FROM ' . _DB_PREFIX_ . 'product_comment_criterion_category
WHERE id_product_comment_criterion = ' . (int) $previousUpdate->id);
} elseif ($previousUpdate->id_product_comment_criterion_type == 3) {
return Db::getInstance()->execute('
DELETE FROM ' . _DB_PREFIX_ . 'product_comment_criterion_product
WHERE id_product_comment_criterion = ' . (int) $previousUpdate->id);
}
}
return true;
}
/**
* Link a Comment Criterion to a product
*
* @return bool succeed
*/
public function addProduct($id_product)
{
if (!Validate::isUnsignedId($id_product)) {
exit(Tools::displayError());
}
return Db::getInstance()->execute('
INSERT INTO `' . _DB_PREFIX_ . 'product_comment_criterion_product` (`id_product_comment_criterion`, `id_product`)
VALUES(' . (int) $this->id . ',' . $id_product . ')
');
}
/**
* Link a Comment Criterion to a category
*
* @return bool succeed
*/
public function addCategory($id_category)
{
if (!Validate::isUnsignedId($id_category)) {
exit(Tools::displayError());
}
return Db::getInstance()->execute('
INSERT INTO `' . _DB_PREFIX_ . 'product_comment_criterion_category` (`id_product_comment_criterion`, `id_category`)
VALUES(' . (int) $this->id . ',' . $id_category . ')
');
}
/**
* Add grade to a criterion
*
* @return bool succeed
*/
public function addGrade($id_product_comment, $grade)
{
if (!Validate::isUnsignedId($id_product_comment)) {
exit(Tools::displayError());
}
if ($grade < 0) {
$grade = 0;
} elseif ($grade > 10) {
$grade = 10;
}
return Db::getInstance()->execute('
INSERT INTO `' . _DB_PREFIX_ . 'product_comment_grade`
(`id_product_comment`, `id_product_comment_criterion`, `grade`) VALUES(
' . $id_product_comment . ',
' . (int) $this->id . ',
' . (int) ($grade) . ')');
}
/**
* Get criterion by Product
*
* @return array Criterion
*/
public static function getByProduct($id_product, $id_lang)
{
if (!Validate::isUnsignedId($id_product) ||
!Validate::isUnsignedId($id_lang)) {
exit(Tools::displayError());
}
$alias = 'p';
$table = '';
// check if version > 1.5 to add shop association
if (version_compare(_PS_VERSION_, '1.5', '>')) {
$table = '_shop';
$alias = 'ps';
}
$cache_id = 'ProductCommentCriterion::getByProduct_' . $id_product . '-' . $id_lang;
if (!Cache::isStored($cache_id)) {
$result = Db::getInstance()->executeS('
SELECT pcc.`id_product_comment_criterion`, pccl.`name`
FROM `' . _DB_PREFIX_ . 'product_comment_criterion` pcc
LEFT JOIN `' . _DB_PREFIX_ . 'product_comment_criterion_lang` pccl
ON (pcc.id_product_comment_criterion = pccl.id_product_comment_criterion)
LEFT JOIN `' . _DB_PREFIX_ . 'product_comment_criterion_product` pccp
ON (pcc.`id_product_comment_criterion` = pccp.`id_product_comment_criterion` AND pccp.`id_product` = ' . $id_product . ')
LEFT JOIN `' . _DB_PREFIX_ . 'product_comment_criterion_category` pccc
ON (pcc.`id_product_comment_criterion` = pccc.`id_product_comment_criterion`)
LEFT JOIN `' . _DB_PREFIX_ . 'product' . $table . '` ' . $alias . '
ON (' . $alias . '.id_category_default = pccc.id_category AND ' . $alias . '.id_product = ' . $id_product . ')
WHERE pccl.`id_lang` = ' . $id_lang . '
AND (
pccp.id_product IS NOT NULL
OR ps.id_product IS NOT NULL
OR pcc.id_product_comment_criterion_type = 1
)
AND pcc.active = 1
GROUP BY pcc.id_product_comment_criterion
');
Cache::store($cache_id, $result);
}
return Cache::retrieve($cache_id);
}
/**
* Get Criterions
*
* @return array Criterions
*/
public static function getCriterions($id_lang, $type = false, $active = false)
{
if (!Validate::isUnsignedId($id_lang)) {
exit(Tools::displayError());
}
$sql = '
SELECT pcc.`id_product_comment_criterion`, pcc.id_product_comment_criterion_type, pccl.`name`, pcc.active
FROM `' . _DB_PREFIX_ . 'product_comment_criterion` pcc
JOIN `' . _DB_PREFIX_ . 'product_comment_criterion_lang` pccl ON (pcc.id_product_comment_criterion = pccl.id_product_comment_criterion)
WHERE pccl.`id_lang` = ' . $id_lang . ($active ? ' AND active = 1' : '') . ($type ? ' AND id_product_comment_criterion_type = ' . (int) $type : '') . '
ORDER BY pccl.`name` ASC';
$criterions = Db::getInstance()->executeS($sql);
$types = self::getTypes();
foreach ($criterions as $key => $data) {
$criterions[$key]['type_name'] = $types[$data['id_product_comment_criterion_type']];
}
return $criterions;
}
public function getProducts()
{
$res = Db::getInstance()->executeS('
SELECT pccp.id_product, pccp.id_product_comment_criterion
FROM `' . _DB_PREFIX_ . 'product_comment_criterion_product` pccp
WHERE pccp.id_product_comment_criterion = ' . (int) $this->id);
$products = [];
if ($res) {
foreach ($res as $row) {
$products[] = (int) $row['id_product'];
}
}
return $products;
}
public function getCategories()
{
$res = Db::getInstance()->executeS('
SELECT pccc.id_category, pccc.id_product_comment_criterion
FROM `' . _DB_PREFIX_ . 'product_comment_criterion_category` pccc
WHERE pccc.id_product_comment_criterion = ' . (int) $this->id);
$criterions = [];
if ($res) {
foreach ($res as $row) {
$criterions[] = (int) $row['id_category'];
}
}
return $criterions;
}
public function deleteCategories()
{
return Db::getInstance()->execute('
DELETE FROM `' . _DB_PREFIX_ . 'product_comment_criterion_category`
WHERE `id_product_comment_criterion` = ' . (int) $this->id);
}
public function deleteProducts()
{
return Db::getInstance()->execute('
DELETE FROM `' . _DB_PREFIX_ . 'product_comment_criterion_product`
WHERE `id_product_comment_criterion` = ' . (int) $this->id);
}
public static function getTypes()
{
// Instance of module class for translations
$module = new ProductComments();
return [
1 => $module->getTranslator()->trans('Valid for the entire catalog', [], 'Modules.Productcomments.Admin'),
2 => $module->getTranslator()->trans('Restricted to some categories', [], 'Modules.Productcomments.Admin'),
3 => $module->getTranslator()->trans('Restricted to some products', [], 'Modules.Productcomments.Admin'),
];
}
}