|
1 | 1 | <?php |
| 2 | + |
2 | 3 | /** |
3 | 4 | * Copyright since 2007 PrestaShop SA and Contributors |
4 | 5 | * PrestaShop is an International Registered Trademark & Property of PrestaShop SA |
|
23 | 24 | * @copyright Since 2007 PrestaShop SA and Contributors |
24 | 25 | * @license https://opensource.org/licenses/AFL-3.0 Academic Free License 3.0 (AFL-3.0) |
25 | 26 | */ |
| 27 | + |
26 | 28 | use Doctrine\ORM\EntityManagerInterface; |
27 | 29 | use PrestaShop\Module\ProductComment\Entity\ProductComment; |
28 | 30 | use PrestaShop\Module\ProductComment\Entity\ProductCommentCriterion; |
@@ -60,7 +62,7 @@ public function display() |
60 | 62 | $comment_title = Tools::getValue('comment_title'); |
61 | 63 | $comment_content = Tools::getValue('comment_content'); |
62 | 64 | $customer_name = Tools::getValue('customer_name'); |
63 | | - $criterions = (array) Tools::getValue('criterion'); |
| 65 | + $criterions = Tools::getValue('criterion', []); |
64 | 66 |
|
65 | 67 | /** @var ProductCommentRepository $productCommentRepository */ |
66 | 68 | $productCommentRepository = $this->context->controller->getContainer()->get('product_comment_repository'); |
@@ -141,19 +143,22 @@ private function addCommentGrades(ProductComment $productComment, array $criteri |
141 | 143 | $criterionRepository = $entityManager->getRepository(ProductCommentCriterion::class); |
142 | 144 | $averageGrade = 0; |
143 | 145 |
|
144 | | - foreach ($criterions as $criterionId => $grade) { |
145 | | - $criterion = $criterionRepository->findOneBy(['id' => $criterionId]); |
146 | | - $criterionGrade = new ProductCommentGrade( |
147 | | - $productComment, |
148 | | - $criterion, |
149 | | - $grade |
150 | | - ); |
| 146 | + if (!empty($criterions)) { |
| 147 | + foreach ($criterions as $criterionId => $grade) { |
| 148 | + $criterion = $criterionRepository->findOneBy(['id' => $criterionId]); |
| 149 | + $criterionGrade = new ProductCommentGrade( |
| 150 | + $productComment, |
| 151 | + $criterion, |
| 152 | + $grade |
| 153 | + ); |
| 154 | + |
| 155 | + $entityManager->persist($criterionGrade); |
| 156 | + $averageGrade += $grade; |
| 157 | + } |
151 | 158 |
|
152 | | - $entityManager->persist($criterionGrade); |
153 | | - $averageGrade += $grade; |
| 159 | + $averageGrade /= count($criterions); |
154 | 160 | } |
155 | 161 |
|
156 | | - $averageGrade /= count($criterions); |
157 | 162 | $productComment->setGrade($averageGrade); |
158 | 163 | } |
159 | 164 |
|
@@ -199,7 +204,6 @@ private function validateCriterions(array $criterions) |
199 | 204 | /** @var EntityManagerInterface $entityManager */ |
200 | 205 | $entityManager = $this->container->get('doctrine.orm.entity_manager'); |
201 | 206 | $criterionRepository = $entityManager->getRepository(ProductCommentCriterion::class); |
202 | | - |
203 | 207 | foreach ($criterions as $criterionId => $grade) { |
204 | 208 | // @todo manage validation for criterion restricted on categories or products |
205 | 209 | $criterion = $criterionRepository->findOneBy(['id' => $criterionId]); |
|
0 commit comments