287 lines
10 KiB
PHP
287 lines
10 KiB
PHP
<?php
|
|
/**
|
|
* Copyright ETS Software Technology Co., Ltd
|
|
*
|
|
* NOTICE OF LICENSE
|
|
*
|
|
* This file is not open source! Each license that you purchased is only available for 1 website only.
|
|
* If you want to use this file on more websites (or projects), you need to purchase additional licenses.
|
|
* You are not allowed to redistribute, resell, lease, license, sub-license or offer our resources to any third party.
|
|
*
|
|
* DISCLAIMER
|
|
*
|
|
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
|
|
* versions in the future.
|
|
*
|
|
* @author ETS Software Technology Co., Ltd
|
|
* @copyright ETS Software Technology Co., Ltd
|
|
* @license Valid for 1 website (or project) for each purchase of license
|
|
*/
|
|
|
|
class EtsRVProductCommentCriterion extends ObjectModel
|
|
{
|
|
const NAME_MAX_LENGTH = 64;
|
|
const ENTIRE_CATALOG_TYPE = 1;
|
|
const CATEGORIES_TYPE = 2;
|
|
const PRODUCTS_TYPE = 3;
|
|
|
|
public $id;
|
|
public $id_product_comment_criterion_type;
|
|
public $name;
|
|
public $active = true;
|
|
public $deleted = 0;
|
|
|
|
/**
|
|
* @see ObjectModel::$definition
|
|
*/
|
|
public static $definition = array(
|
|
'table' => 'ets_rv_product_comment_criterion',
|
|
'primary' => 'id_ets_rv_product_comment_criterion',
|
|
'multilang' => true,
|
|
'fields' => array(
|
|
'id_product_comment_criterion_type' => array('type' => self::TYPE_INT),
|
|
'active' => array('type' => self::TYPE_BOOL, 'validate' => 'isBool'),
|
|
'deleted' => array('type' => self::TYPE_BOOL, 'validate' => 'isBool'),
|
|
// Lang fields
|
|
'name' => array('type' => self::TYPE_STRING, 'lang' => true, 'validate' => 'isGenericName', 'required' => true, 'size' => self::NAME_MAX_LENGTH),
|
|
),
|
|
);
|
|
|
|
public function delete()
|
|
{
|
|
if (!($res = Db::getInstance()->execute('UPDATE `' . _DB_PREFIX_ . 'ets_rv_product_comment_criterion` SET `deleted` = 1 WHERE `id_ets_rv_product_comment_criterion` = ' . (int)$this->id))) {
|
|
return false;
|
|
}
|
|
|
|
return $res;
|
|
}
|
|
|
|
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_ . 'ets_rv_product_comment_criterion_category`
|
|
WHERE id_ets_rv_product_comment_criterion = ' . (int)$previousUpdate->id);
|
|
} elseif ($previousUpdate->id_product_comment_criterion_type == 3) {
|
|
return Db::getInstance()->execute('
|
|
DELETE FROM `' . _DB_PREFIX_ . 'ets_rv_product_comment_criterion_product`
|
|
WHERE id_ets_rv_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)) {
|
|
die(Tools::displayError());
|
|
}
|
|
|
|
return Db::getInstance()->execute('
|
|
INSERT INTO `' . _DB_PREFIX_ . 'ets_rv_product_comment_criterion_product` (`id_ets_rv_product_comment_criterion`, `id_product`)
|
|
VALUES(' . (int)$this->id . ',' . (int)$id_product . ')
|
|
');
|
|
}
|
|
|
|
/**
|
|
* Link a Comment Criterion to a category
|
|
*
|
|
* @return bool succeed
|
|
*/
|
|
public function addCategory($id_category)
|
|
{
|
|
if (!Validate::isUnsignedId($id_category)) {
|
|
die(Tools::displayError());
|
|
}
|
|
|
|
return Db::getInstance()->execute('
|
|
INSERT INTO `' . _DB_PREFIX_ . 'ets_rv_product_comment_criterion_category` (`id_ets_rv_product_comment_criterion`, `id_category`)
|
|
VALUES(' . (int)$this->id . ',' . (int)$id_category . ')
|
|
');
|
|
}
|
|
|
|
/**
|
|
* Add grade to a criterion
|
|
*
|
|
* @return bool succeed
|
|
*/
|
|
public function addGrade($id_product_comment, $grade)
|
|
{
|
|
if (!Validate::isUnsignedId($id_product_comment)) {
|
|
die(Tools::displayError());
|
|
}
|
|
if ($grade < 0) {
|
|
$grade = 0;
|
|
} elseif ($grade > 10) {
|
|
$grade = 10;
|
|
}
|
|
|
|
return Db::getInstance()->execute('
|
|
INSERT INTO `' . _DB_PREFIX_ . 'ets_rv_product_comment_grade`
|
|
(`id_ets_rv_product_comment`, `id_ets_rv_product_comment_criterion`, `grade`) VALUES(
|
|
' . (int)($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)) {
|
|
die(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 = 'EtsRVProductCommentCriterion::getByProduct_' . (int)$id_product . '-' . (int)$id_lang;
|
|
if (!Cache::isStored($cache_id)) {
|
|
$result = Db::getInstance()->executeS('
|
|
SELECT pcc.`id_ets_rv_product_comment_criterion`, pccl.`name`
|
|
FROM `' . _DB_PREFIX_ . 'ets_rv_product_comment_criterion` pcc
|
|
LEFT JOIN `' . _DB_PREFIX_ . 'ets_rv_product_comment_criterion_lang` pccl ON (pcc.id_ets_rv_product_comment_criterion = pccl.id_ets_rv_product_comment_criterion AND pccl.`id_lang` = ' . (int)($id_lang) . ')
|
|
LEFT JOIN `' . _DB_PREFIX_ . 'ets_rv_product_comment_criterion_product` pccp ON (pcc.`id_ets_rv_product_comment_criterion` = pccp.`id_ets_rv_product_comment_criterion` AND pccp.`id_product` = ' . (int)$id_product . ')
|
|
LEFT JOIN `' . _DB_PREFIX_ . 'ets_rv_product_comment_criterion_category` pccc ON (pcc.`id_ets_rv_product_comment_criterion` = pccc.`id_ets_rv_product_comment_criterion`)
|
|
LEFT JOIN `' . _DB_PREFIX_ . 'product' . $table . '` ' . $alias . ' ON (' . $alias . '.id_category_default = pccc.id_category AND ' . $alias . '.id_product = ' . (int)$id_product . ')
|
|
WHERE pcc.active = 1
|
|
AND (
|
|
pccp.id_product IS NOT NULL
|
|
OR ps.id_product IS NOT NULL
|
|
OR pcc.id_product_comment_criterion_type = 1
|
|
)
|
|
');
|
|
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, $not_deleted = true)
|
|
{
|
|
if (!Validate::isUnsignedId($id_lang)) {
|
|
die(Tools::displayError());
|
|
}
|
|
|
|
$sql = '
|
|
SELECT pcc.`id_ets_rv_product_comment_criterion`, pcc.id_product_comment_criterion_type, pccl.`name`, pcc.active
|
|
FROM `' . _DB_PREFIX_ . 'ets_rv_product_comment_criterion` pcc
|
|
JOIN `' . _DB_PREFIX_ . 'ets_rv_product_comment_criterion_lang` pccl ON (pcc.id_ets_rv_product_comment_criterion = pccl.id_ets_rv_product_comment_criterion)
|
|
WHERE pccl.`id_lang` = ' . (int)$id_lang . ($active ? ' AND active = 1' : '') . ($not_deleted ? ' AND deleted = 0' : '') . ($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 static function getImportCriterions($active = false, $not_deleted = true)
|
|
{
|
|
$sql = '
|
|
SELECT `id_ets_rv_product_comment_criterion`
|
|
FROM `' . _DB_PREFIX_ . 'ets_rv_product_comment_criterion`
|
|
WHERE 1' . ($active ? ' AND `active` = 1' : '') . ($not_deleted ? ' AND `deleted` = 0' : '') . '
|
|
ORDER BY `id_ets_rv_product_comment_criterion` ASC';
|
|
|
|
$criterions = [];
|
|
$res = Db::getInstance()->executeS($sql);
|
|
if ($res) {
|
|
foreach ($res as $r) {
|
|
$criterions[$r['id_ets_rv_product_comment_criterion']] = 0;
|
|
}
|
|
}
|
|
return $criterions;
|
|
}
|
|
|
|
public function getProducts()
|
|
{
|
|
$res = Db::getInstance()->executeS('
|
|
SELECT pccp.id_product, pccp.id_ets_rv_product_comment_criterion
|
|
FROM `' . _DB_PREFIX_ . 'ets_rv_product_comment_criterion_product` pccp
|
|
WHERE pccp.id_ets_rv_product_comment_criterion = ' . (int)$this->id);
|
|
$products = array();
|
|
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_ets_rv_product_comment_criterion
|
|
FROM `' . _DB_PREFIX_ . 'ets_rv_product_comment_criterion_category` pccc
|
|
WHERE pccc.id_ets_rv_product_comment_criterion = ' . (int)$this->id);
|
|
$criterions = array();
|
|
if ($res) {
|
|
foreach ($res as $row) {
|
|
$criterions[] = (int)$row['id_category'];
|
|
}
|
|
}
|
|
|
|
return $criterions;
|
|
}
|
|
|
|
public function deleteCategories()
|
|
{
|
|
return Db::getInstance()->execute('
|
|
DELETE FROM `' . _DB_PREFIX_ . 'ets_rv_product_comment_criterion_category`
|
|
WHERE `id_ets_rv_product_comment_criterion` = ' . (int)$this->id);
|
|
}
|
|
|
|
public function deleteProducts()
|
|
{
|
|
return Db::getInstance()->execute('
|
|
DELETE FROM `' . _DB_PREFIX_ . 'ets_rv_product_comment_criterion_product`
|
|
WHERE `id_ets_rv_product_comment_criterion` = ' . (int)$this->id);
|
|
}
|
|
|
|
public static function getTypes()
|
|
{
|
|
// Instance of module class for translations
|
|
$module = new Ets_reviews();
|
|
|
|
return array(
|
|
1 => $module->l('Valid for the entire catalog', 'EtsRVProductCommentCriterion'),
|
|
2 => $module->l('Restricted to some categories', 'EtsRVProductCommentCriterion'),
|
|
3 => $module->l('Restricted to some products', 'EtsRVProductCommentCriterion'),
|
|
);
|
|
}
|
|
|
|
public static function deleteAll()
|
|
{
|
|
return Db::getInstance()->delete('ets_rv_product_comment_criterion', '', false);
|
|
}
|
|
}
|