Skip to content

Commit

Permalink
Fixed CI
Browse files Browse the repository at this point in the history
  • Loading branch information
Progi1984 committed Nov 20, 2020
1 parent bed456b commit b0f9a35
Show file tree
Hide file tree
Showing 15 changed files with 321 additions and 302 deletions.
7 changes: 4 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
config_*
vendor
node_modules
/config_*
/node_modules
/vendor
/.php_cs.cache
123 changes: 63 additions & 60 deletions ProductComment.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,68 +29,70 @@

class ProductComment extends ObjectModel
{
/** @var int */
public $id;

/** @var int Product's id */
/** @var int */
public $id_product;

/** @var int Customer's id */
/** @var int */
public $id_customer;

/** @var int Guest's id */
/** @var int */
public $id_guest;

/** @var int Customer name */
/** @var int */
public $customer_name;

/** @var string Title */
/** @var string */
public $title;

/** @var string Content */
/** @var string */
public $content;

/** @var int Grade */
/** @var int */
public $grade;

/** @var bool Validate */
public $validate = 0;
/** @var bool */
public $validate = false;

public $deleted = 0;
/** @var bool */
public $deleted = false;

/** @var string Object creation date */
public $date_add;

/**
* @see ObjectModel::$definition
*/
public static $definition = array(
public static $definition = [
'table' => 'product_comment',
'primary' => 'id_product_comment',
'fields' => array(
'id_product' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId', 'required' => true),
'id_customer' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId', 'required' => true),
'id_guest' => array('type' => self::TYPE_INT),
'customer_name' => array('type' => self::TYPE_STRING),
'title' => array('type' => self::TYPE_STRING),
'content' => array('type' => self::TYPE_STRING, 'validate' => 'isMessage', 'size' => 65535, 'required' => true),
'grade' => array('type' => self::TYPE_FLOAT, 'validate' => 'isFloat'),
'validate' => array('type' => self::TYPE_BOOL, 'validate' => 'isBool'),
'deleted' => array('type' => self::TYPE_BOOL),
'date_add' => array('type' => self::TYPE_DATE),
),
);
'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 Comments
* @return array|bool
*/
public static function getByProduct($id_product, $p = 1, $n = null, $id_customer = null)
{
if (!Validate::isUnsignedId($id_product)) {
return false;
}
$validate = Configuration::get('PRODUCT_COMMENTS_MODERATE');
$validate = (bool) Configuration::get('PRODUCT_COMMENTS_MODERATE');
$p = (int) $p;
$n = (int) $n;
if ($p <= 1) {
Expand All @@ -102,7 +104,7 @@ public static function getByProduct($id_product, $p = 1, $n = null, $id_customer

$cache_id = 'ProductComment::getByProduct_' . (int) $id_product . '-' . (int) $p . '-' . (int) $n . '-' . (int) $id_customer . '-' . (bool) $validate;
if (!Cache::isStored($cache_id)) {
$result = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS('
$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, ' .
Expand All @@ -111,7 +113,7 @@ public static function getByProduct($id_product, $p = 1, $n = null, $id_customer
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` = ' . (int) ($id_product) . ($validate == '1' ? ' AND pc.`validate` = 1' : '') . '
WHERE pc.`id_product` = ' . (int) ($id_product) . ($validate ? ' AND pc.`validate` = 1' : '') . '
ORDER BY pc.`date_add` DESC
' . ($n ? 'LIMIT ' . (int) (($p - 1) * $n) . ', ' . (int) ($n) : ''));
Cache::store($cache_id, $result);
Expand All @@ -123,7 +125,7 @@ public static function getByProduct($id_product, $p = 1, $n = null, $id_customer
/**
* Return customer's comment
*
* @return arrayComments
* @return array Comments
*/
public static function getByCustomer($id_product, $id_customer, $get_last = false, $id_guest = false)
{
Expand Down Expand Up @@ -151,25 +153,25 @@ public static function getByCustomer($id_product, $id_customer, $get_last = fals
/**
* Get Grade By product
*
* @return array Grades
* @return array|bool
*/
public static function getGradeByProduct($id_product, $id_lang)
{
if (!Validate::isUnsignedId($id_product) ||
!Validate::isUnsignedId($id_lang)) {
return false;
}
$validate = Configuration::get('PRODUCT_COMMENTS_MODERATE');
$validate = (bool) Configuration::get('PRODUCT_COMMENTS_MODERATE');

return Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS('
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` = ' . (int) $id_product . '
AND pccl.`id_lang` = ' . (int) $id_lang .
($validate == '1' ? ' AND pc.`validate` = 1' : ''));
($validate ? ' AND pc.`validate` = 1' : ''));
}

public static function getRatings($id_product)
Expand All @@ -184,14 +186,14 @@ public static function getRatings($id_product)
AND pc.`deleted` = 0' .
($validate == '1' ? ' AND pc.`validate` = 1' : '');

return Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow($sql);
return Db::getInstance((bool) _PS_USE_SQL_SLAVE_)->getRow($sql);
}

public static function getAverageGrade($id_product)
{
$validate = Configuration::get('PRODUCT_COMMENTS_MODERATE');

return Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow('
return Db::getInstance((bool) _PS_USE_SQL_SLAVE_)->getRow('
SELECT (SUM(pc.`grade`) / COUNT(pc.`grade`)) AS grade
FROM `' . _DB_PREFIX_ . 'product_comment` pc
WHERE pc.`id_product` = ' . (int) $id_product . '
Expand All @@ -204,12 +206,12 @@ 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 array();
if (!count($grades) || !$total) {
return [];
}

/* Addition grades for each criterion */
$criterionsGradeTotal = array();
$criterionsGradeTotal = [];
$count_grades = count($grades);
for ($i = 0; $i < $count_grades; ++$i) {
if (array_key_exists($grades[$i]['id_product_comment_criterion'], $criterionsGradeTotal) === false) {
Expand All @@ -220,9 +222,9 @@ public static function getAveragesByProduct($id_product, $id_lang)
}

/* Finally compute the averages */
$averages = array();
$averages = [];
foreach ($criterionsGradeTotal as $key => $criterionGradeTotal) {
$averages[(int) ($key)] = (int) ($total) ? ((int) ($criterionGradeTotal) / (int) ($total)) : 0;
$averages[(int) $key] = $criterionGradeTotal / $total;
}

return $averages;
Expand All @@ -231,20 +233,20 @@ public static function getAveragesByProduct($id_product, $id_lang)
/**
* Return number of comments and average grade by products
*
* @return array Info
* @return array|false
*/
public static function getCommentNumber($id_product)
{
if (!Validate::isUnsignedId($id_product)) {
return false;
}
$validate = (int) Configuration::get('PRODUCT_COMMENTS_MODERATE');
$validate = (bool) Configuration::get('PRODUCT_COMMENTS_MODERATE');
$cache_id = 'ProductComment::getCommentNumber_' . (int) $id_product . '-' . $validate;
if (!Cache::isStored($cache_id)) {
$result = (int) Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue('
$result = (int) Db::getInstance((bool) _PS_USE_SQL_SLAVE_)->getValue('
SELECT COUNT(`id_product_comment`) AS "nbr"
FROM `' . _DB_PREFIX_ . 'product_comment` pc
WHERE `id_product` = ' . (int) ($id_product) . ($validate == '1' ? ' AND `validate` = 1' : ''));
WHERE `id_product` = ' . (int) ($id_product) . ($validate ? ' AND `validate` = 1' : ''));
Cache::store($cache_id, $result);
}

Expand All @@ -254,7 +256,7 @@ public static function getCommentNumber($id_product)
/**
* Return number of comments and average grade by products
*
* @return array Info
* @return int|bool
*/
public static function getGradedCommentNumber($id_product)
{
Expand All @@ -263,7 +265,7 @@ public static function getGradedCommentNumber($id_product)
}
$validate = (int) Configuration::get('PRODUCT_COMMENTS_MODERATE');

$result = Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow('
$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` = ' . (int) ($id_product) . ($validate == '1' ? ' AND `validate` = 1' : '') . '
Expand All @@ -284,20 +286,21 @@ public static function getByValidate($validate = '0', $deleted = false, $p = nul
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;

if (!$skip_validate) {
$sql .= ' WHERE pc.`validate` = ' . (int) $validate;
}

$sql .= ' ORDER BY pc.`date_add` DESC';

if($p && $limit) {
if ($p && $limit) {
$offset = ($p - 1) * $limit;
$sql .= ' LIMIT ' . (int) $offset . ',' . (int) $limit;
}

return Db::getInstance()->executeS($sql);
}

/**
* Get numbers of comments by Validation
*
Expand All @@ -306,14 +309,14 @@ public static function getByValidate($validate = '0', $deleted = false, $p = nul
public static function getCountByValidate($validate = '0', $skip_validate = false)
{
$sql = '
SELECT COUNT(*)
SELECT COUNT(*)
FROM `' . _DB_PREFIX_ . 'product_comment`';

if(!$skip_validate) {
$sql .= ' WHERE `validate` = ' . (int) $validate;
if (!$skip_validate) {
$sql .= ' WHERE `validate` = ' . (int) $validate;
}

return Db::getInstance()->getValue($sql);
return (int) Db::getInstance()->getValue($sql);
}

/**
Expand Down Expand Up @@ -347,7 +350,7 @@ public function validate($validate = '1')
`validate` = ' . (int) $validate . '
WHERE `id_product_comment` = ' . (int) $this->id));

Hook::exec('actionObjectProductCommentValidateAfter', array('object' => $this));
Hook::exec('actionObjectProductCommentValidateAfter', ['object' => $this]);

return $success;
}
Expand All @@ -359,10 +362,10 @@ public function validate($validate = '1')
*/
public function delete()
{
parent::delete();
ProductComment::deleteGrades($this->id);
ProductComment::deleteReports($this->id);
ProductComment::deleteUsefulness($this->id);
return parent::delete()
&& ProductComment::deleteGrades($this->id)
&& ProductComment::deleteReports($this->id)
&& ProductComment::deleteUsefulness($this->id);
}

/**
Expand Down Expand Up @@ -472,7 +475,7 @@ public static function isAlreadyUsefulness($id_product_comment, $id_customer)
*/
public static function getReportedComments()
{
return Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS('
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
Expand Down
22 changes: 11 additions & 11 deletions ProductCommentCriterion.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,17 @@ class ProductCommentCriterion extends ObjectModel
/**
* @see ObjectModel::$definition
*/
public static $definition = array(
public static $definition = [
'table' => 'product_comment_criterion',
'primary' => 'id_product_comment_criterion',
'multilang' => true,
'fields' => array(
'id_product_comment_criterion_type' => array('type' => self::TYPE_INT),
'active' => array('type' => self::TYPE_BOOL),
'fields' => [
'id_product_comment_criterion_type' => ['type' => self::TYPE_INT],
'active' => ['type' => self::TYPE_BOOL],
// Lang fields
'name' => array('type' => self::TYPE_STRING, 'lang' => true, 'validate' => 'isGenericName', 'required' => true, 'size' => self::NAME_MAX_LENGTH),
),
);
'name' => ['type' => self::TYPE_STRING, 'lang' => true, 'validate' => 'isGenericName', 'required' => true, 'size' => self::NAME_MAX_LENGTH],
],
];

public function delete()
{
Expand Down Expand Up @@ -230,7 +230,7 @@ public function getProducts()
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 = array();
$products = [];
if ($res) {
foreach ($res as $row) {
$products[] = (int) $row['id_product'];
Expand All @@ -246,7 +246,7 @@ public function getCategories()
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 = array();
$criterions = [];
if ($res) {
foreach ($res as $row) {
$criterions[] = (int) $row['id_category'];
Expand Down Expand Up @@ -275,10 +275,10 @@ public static function getTypes()
// Instance of module class for translations
$module = new ProductComments();

return array(
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'),
);
];
}
}
Loading

0 comments on commit b0f9a35

Please sign in to comment.