Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 16 additions & 12 deletions controllers/front/PostComment.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* Copyright since 2007 PrestaShop SA and Contributors
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
Expand All @@ -23,6 +24,7 @@
* @copyright Since 2007 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License 3.0 (AFL-3.0)
*/

use Doctrine\ORM\EntityManagerInterface;
use PrestaShop\Module\ProductComment\Entity\ProductComment;
use PrestaShop\Module\ProductComment\Entity\ProductCommentCriterion;
Expand Down Expand Up @@ -60,7 +62,7 @@ public function display()
$comment_title = Tools::getValue('comment_title');
$comment_content = Tools::getValue('comment_content');
$customer_name = Tools::getValue('customer_name');
$criterions = (array) Tools::getValue('criterion');
$criterions = Tools::getValue('criterion', []);

/** @var ProductCommentRepository $productCommentRepository */
$productCommentRepository = $this->context->controller->getContainer()->get('product_comment_repository');
Expand Down Expand Up @@ -141,19 +143,22 @@ private function addCommentGrades(ProductComment $productComment, array $criteri
$criterionRepository = $entityManager->getRepository(ProductCommentCriterion::class);
$averageGrade = 0;

foreach ($criterions as $criterionId => $grade) {
$criterion = $criterionRepository->findOneBy(['id' => $criterionId]);
$criterionGrade = new ProductCommentGrade(
$productComment,
$criterion,
$grade
);
if (!empty($criterions)) {
foreach ($criterions as $criterionId => $grade) {
$criterion = $criterionRepository->findOneBy(['id' => $criterionId]);
$criterionGrade = new ProductCommentGrade(
$productComment,
$criterion,
$grade
);

$entityManager->persist($criterionGrade);
$averageGrade += $grade;
}

$entityManager->persist($criterionGrade);
$averageGrade += $grade;
$averageGrade /= count($criterions);
}

$averageGrade /= count($criterions);
$productComment->setGrade($averageGrade);
}

Expand Down Expand Up @@ -199,7 +204,6 @@ private function validateCriterions(array $criterions)
/** @var EntityManagerInterface $entityManager */
$entityManager = $this->container->get('doctrine.orm.entity_manager');
$criterionRepository = $entityManager->getRepository(ProductCommentCriterion::class);

foreach ($criterions as $criterionId => $grade) {
// @todo manage validation for criterion restricted on categories or products
$criterion = $criterionRepository->findOneBy(['id' => $criterionId]);
Expand Down
15 changes: 9 additions & 6 deletions views/js/post-comment.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,12 +128,15 @@ jQuery(document).ready(function () {
$(fieldSelector).removeClass('error');
$(fieldSelector).addClass('valid');
}

if (!ratingChosen) {
criterionsInfo.show();
isValid = false;
} else {
criterionsInfo.hide();
const fieldCriterion = $('#criterions_list').length;

if (fieldCriterion) {
if (!ratingChosen) {
criterionsInfo.show();
isValid = false;
} else {
criterionsInfo.hide();
}
}
});

Expand Down