first commit

This commit is contained in:
2024-11-05 12:22:50 +01:00
commit e5682a3912
19641 changed files with 2948548 additions and 0 deletions

View File

@@ -0,0 +1,66 @@
<?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 license@prestashop.com 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 <contact@prestashop.com>
* @copyright Since 2007 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License 3.0 (AFL-3.0)
*/
use PrestaShop\Module\ProductComment\Repository\ProductCommentRepository;
class ProductCommentsCommentGradeModuleFrontController extends ModuleFrontController
{
public function display()
{
$idProducts = Tools::getValue('id_products');
/* @var ProductCommentRepository $productCommentRepository */
header('Content-Type: application/json');
if (!is_array($idProducts)) {
return $this->ajaxRender(null);
}
$idProducts = array_unique(array_map('intval', $idProducts));
$productCommentRepository = $this->context->controller->getContainer()->get('product_comment_repository');
$productsCommentsNb = $productCommentRepository->getCommentsNumberForProducts($idProducts, Configuration::get('PRODUCT_COMMENTS_MODERATE'));
$averageGrade = $productCommentRepository->getAverageGrades($idProducts, Configuration::get('PRODUCT_COMMENTS_MODERATE'));
$resultFormated = [];
foreach ($idProducts as $i => $id) {
$resultFormated[] = [
'id_product' => $id,
'comments_nb' => $productsCommentsNb[$id],
'average_grade' => $averageGrade[$id],
];
}
$this->ajaxRender(
json_encode(
[
'products' => $resultFormated,
]
)
);
}
}

View File

@@ -0,0 +1,94 @@
<?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 license@prestashop.com 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 <contact@prestashop.com>
* @copyright Since 2007 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License 3.0 (AFL-3.0)
*/
use PrestaShop\Module\ProductComment\Repository\ProductCommentRepository;
class ProductCommentsListCommentsModuleFrontController extends ModuleFrontController
{
public function display()
{
$idProduct = (int) Tools::getValue('id_product');
$page = (int) Tools::getValue('page', 1);
$isLastNameAnonymous = Configuration::get('PRODUCT_COMMENTS_ANONYMISATION');
/** @var ProductCommentRepository $productCommentRepository */
$productCommentRepository = $this->context->controller->getContainer()->get('product_comment_repository');
$productComments = $productCommentRepository->paginate(
$idProduct,
$page,
(int) Configuration::get('PRODUCT_COMMENTS_COMMENTS_PER_PAGE'),
(bool) Configuration::get('PRODUCT_COMMENTS_MODERATE')
);
$productCommentsNb = $productCommentRepository->getCommentsNumber(
$idProduct,
(bool) Configuration::get('PRODUCT_COMMENTS_MODERATE')
);
$responseArray = [
'comments_nb' => $productCommentsNb,
'comments_per_page' => Configuration::get('PRODUCT_COMMENTS_COMMENTS_PER_PAGE'),
'comments' => [],
];
foreach ($productComments as $productComment) {
$dateAdd = new \DateTime($productComment['date_add'], new \DateTimeZone('UTC'));
$dateAdd->setTimezone(new \DateTimeZone(date_default_timezone_get()));
$dateFormatter = new \IntlDateFormatter(
$this->context->language->locale,
\IntlDateFormatter::SHORT,
\IntlDateFormatter::SHORT
);
$productComment['title'] = htmlentities($productComment['title']);
$productComment['content'] = htmlentities($productComment['content']);
$productComment['date_add'] = $dateFormatter->format($dateAdd);
if ($isLastNameAnonymous && isset($productComment['lastname'])) {
$productComment['lastname'] = substr($productComment['lastname'], 0, 1) . '.';
}
// if registered customer : return customer first and last name instead of using customer_name
if (!empty($productComment['lastname'])) {
$productComment['customer_name'] = htmlentities($productComment['firstname'] . ' ' . $productComment['lastname']);
} else {
$productComment['customer_name'] = htmlentities($productComment['customer_name']);
}
$usefulness = $productCommentRepository->getProductCommentUsefulness($productComment['id_product_comment']);
$productComment = array_merge($productComment, $usefulness);
if (empty($productComment['customer_name']) && !isset($productComment['firstname']) && !isset($productComment['lastname'])) {
$productComment['customer_name'] = $this->trans('Deleted account', [], 'Modules.Productcomments.Shop');
}
$responseArray['comments'][] = $productComment;
}
header('Content-Type: application/json');
$this->ajaxRender(
json_encode(
$responseArray
)
);
}
}

View File

@@ -0,0 +1,213 @@
<?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 license@prestashop.com 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 <contact@prestashop.com>
* @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;
use PrestaShop\Module\ProductComment\Entity\ProductCommentGrade;
use PrestaShop\Module\ProductComment\Repository\ProductCommentRepository;
class ProductCommentsPostCommentModuleFrontController extends ModuleFrontController
{
public function display()
{
header('Content-Type: application/json');
if (!(int) $this->context->cookie->id_customer && !Configuration::get('PRODUCT_COMMENTS_ALLOW_GUESTS')) {
$this->ajaxRender(
json_encode(
[
'success' => false,
'error' => $this->trans(
'You need to be [1]logged in[/1] or [2]create an account[/2] to post your review.',
[
'[1]' => '<a href="' . $this->context->link->getPageLink('my-account') . '">',
'[/1]' => '</a>',
'[2]' => '<a href="' . $this->context->link->getPageLink('authentication&create_account=1') . '">',
'[/2]' => '</a>',
],
'Modules.Productcomments.Shop'
),
]
)
);
return false;
}
$id_product = (int) Tools::getValue('id_product');
$comment_title = Tools::getValue('comment_title');
$comment_content = Tools::getValue('comment_content');
$customer_name = Tools::getValue('customer_name');
$criterions = (array) Tools::getValue('criterion');
/** @var ProductCommentRepository $productCommentRepository */
$productCommentRepository = $this->context->controller->getContainer()->get('product_comment_repository');
$isPostAllowed = $productCommentRepository->isPostAllowed(
$id_product,
(int) $this->context->cookie->id_customer,
(int) $this->context->cookie->id_guest
);
if (!$isPostAllowed) {
$this->ajaxRender(
json_encode(
[
'success' => false,
'error' => $this->trans('You are not allowed to post a review at the moment, please try again later.', [], 'Modules.Productcomments.Shop'),
]
)
);
return false;
}
/** @var EntityManagerInterface $entityManager */
$entityManager = $this->container->get('doctrine.orm.entity_manager');
//Create product comment
$productComment = new ProductComment();
$productComment
->setProductId($id_product)
->setTitle($comment_title)
->setContent($comment_content)
->setCustomerName($customer_name)
->setCustomerId($this->context->cookie->id_customer)
->setGuestId($this->context->cookie->id_guest)
->setDateAdd(new \DateTime('now', new \DateTimeZone('UTC')))
;
//Validate comment
$errors = array_merge($this->validateComment($productComment), $this->validateCriterions($criterions));
if (!empty($errors)) {
$this->ajaxRender(
json_encode(
[
'success' => false,
'errors' => $errors,
]
)
);
return false;
}
$entityManager->persist($productComment);
$this->addCommentGrades($productComment, $criterions);
$entityManager->flush();
$this->ajaxRender(
json_encode(
[
'success' => true,
'product_comment' => $productComment->toArray(),
]
)
);
}
/**
* @param ProductComment $productComment
* @param array $criterions
*
* @throws Exception
*/
private function addCommentGrades(ProductComment $productComment, array $criterions)
{
/** @var EntityManagerInterface $entityManager */
$entityManager = $this->container->get('doctrine.orm.entity_manager');
$criterionRepository = $entityManager->getRepository(ProductCommentCriterion::class);
$averageGrade = 0;
foreach ($criterions as $criterionId => $grade) {
$criterion = $criterionRepository->findOneBy(['id' => $criterionId]);
$criterionGrade = new ProductCommentGrade(
$productComment,
$criterion,
$grade
);
$entityManager->persist($criterionGrade);
$averageGrade += $grade;
}
$averageGrade /= count($criterions);
$productComment->setGrade($averageGrade);
}
/**
* Manual validation for now, this would be nice to use Symfony validator with the annotation
*
* @param ProductComment $productComment
*
* @return array
*/
private function validateComment(ProductComment $productComment)
{
$errors = [];
if (empty($productComment->getTitle())) {
$errors[] = $this->trans('Title cannot be empty', [], 'Modules.Productcomments.Shop');
} elseif (strlen($productComment->getTitle()) > ProductComment::TITLE_MAX_LENGTH) {
$errors[] = $this->trans('Title cannot be more than %s characters', [ProductComment::TITLE_MAX_LENGTH], 'Modules.Productcomments.Shop');
}
if (!$productComment->getCustomerId()) {
if (empty($productComment->getCustomerName())) {
$errors[] = $this->trans('Customer name cannot be empty', [], 'Modules.Productcomments.Shop');
} elseif (strlen($productComment->getCustomerName()) > ProductComment::CUSTOMER_NAME_MAX_LENGTH) {
$errors[] = $this->trans('Customer name cannot be more than %s characters', [ProductComment::CUSTOMER_NAME_MAX_LENGTH], 'Modules.Productcomments.Shop');
}
}
return $errors;
}
/**
* Valdiate criterions values
*
* @todo manage validation for criterion restricted on categories or products
*
* @param array $criterions
*
* @return array
*/
private function validateCriterions(array $criterions)
{
$errors = [];
/** @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]);
if (empty($criterion)) {
$errors[] = $this->trans('Criterions not available', [], 'Modules.Productcomments.Shop');
}
}
return $errors;
}
}

View File

@@ -0,0 +1,107 @@
<?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 license@prestashop.com 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 <contact@prestashop.com>
* @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\ProductCommentReport;
class ProductCommentsReportCommentModuleFrontController extends ModuleFrontController
{
public function display()
{
header('Content-Type: application/json');
$customerId = (int) $this->context->cookie->id_customer;
if (!$customerId) {
$this->ajaxRender(
json_encode(
[
'success' => false,
'error' => $this->trans('You need to be logged in to report a review.', [], 'Modules.Productcomments.Shop'),
]
)
);
return false;
}
$id_product_comment = (int) Tools::getValue('id_product_comment');
/** @var EntityManagerInterface $entityManager */
$entityManager = $this->container->get('doctrine.orm.entity_manager');
$productCommentEntityRepository = $entityManager->getRepository(ProductComment::class);
/** @var ProductComment|null $productComment */
$productComment = $productCommentEntityRepository->findOneBy(['id' => $id_product_comment]);
if (!$productComment) {
$this->ajaxRender(
json_encode(
[
'success' => false,
'error' => $this->trans('Cannot find the requested product review.', [], 'Modules.Productcomments.Shop'),
]
)
);
return false;
}
$productCommentAbuseRepository = $entityManager->getRepository(ProductCommentReport::class);
/** @var ProductCommentReport|null $productCommentAbuse */
$productCommentAbuse = $productCommentAbuseRepository->findOneBy([
'comment' => $id_product_comment,
'customerId' => $customerId,
]);
if ($productCommentAbuse) {
$this->ajaxRender(
json_encode(
[
'success' => false,
'error' => $this->trans('You already reported this review as abusive.', [], 'Modules.Productcomments.Shop'),
]
)
);
return false;
}
$productCommentAbuse = new ProductCommentReport(
$productComment,
$customerId
);
$entityManager->persist($productCommentAbuse);
$entityManager->flush();
$this->ajaxRender(
json_encode(
[
'success' => true,
'id_product_comment' => $id_product_comment,
]
)
);
}
}

View File

@@ -0,0 +1,130 @@
<?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 license@prestashop.com 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 <contact@prestashop.com>
* @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\ProductCommentUsefulness;
use PrestaShop\Module\ProductComment\Repository\ProductCommentRepository;
class ProductCommentsUpdateCommentUsefulnessModuleFrontController extends ModuleFrontController
{
public function display()
{
header('Content-Type: application/json');
if (!Configuration::get('PRODUCT_COMMENTS_USEFULNESS')) {
$this->ajaxRender(
json_encode(
[
'success' => false,
'error' => $this->trans('This feature is not enabled.', [], 'Modules.Productcomments.Shop'),
]
)
);
return false;
}
$customerId = (int) $this->context->cookie->id_customer;
if (!$customerId) {
$this->ajaxRender(
json_encode(
[
'success' => false,
'error' => $this->trans(
'You need to be [1]logged in[/1] or [2]create an account[/2] to give your appreciation of a review.',
[
'[1]' => '<a href="' . $this->context->link->getPageLink('my-account') . '">',
'[/1]' => '</a>',
'[2]' => '<a href="' . $this->context->link->getPageLink('authentication&create_account=1') . '">',
'[/2]' => '</a>',
],
'Modules.Productcomments.Shop'
),
]
)
);
return false;
}
$id_product_comment = (int) Tools::getValue('id_product_comment');
$usefulness = (bool) Tools::getValue('usefulness');
/** @var EntityManagerInterface $entityManager */
$entityManager = $this->container->get('doctrine.orm.entity_manager');
$productCommentEntityRepository = $entityManager->getRepository(ProductComment::class);
/** @var ProductComment|null $productComment */
$productComment = $productCommentEntityRepository->findOneBy(['id' => $id_product_comment]);
if (!$productComment) {
$this->ajaxRender(
json_encode(
[
'success' => false,
'error' => $this->trans('Cannot find the requested product review.', [], 'Modules.Productcomments.Shop'),
]
)
);
return false;
}
$productCommentUsefulnesRepository = $entityManager->getRepository(ProductCommentUsefulness::class);
/** @var ProductCommentUsefulness|null $productCommentUsefulness */
$productCommentUsefulness = $productCommentUsefulnesRepository->findOneBy([
'comment' => $id_product_comment,
'customerId' => $customerId,
]);
if ($productCommentUsefulness) {
$productCommentUsefulness->setUsefulness($usefulness);
} else {
$productCommentUsefulness = new ProductCommentUsefulness(
$productComment,
$customerId,
$usefulness
);
$entityManager->persist($productCommentUsefulness);
}
$entityManager->flush();
/** @var ProductCommentRepository $productCommentRepository */
$productCommentRepository = $this->context->controller->getContainer()->get('product_comment_repository');
$commentUsefulness = $productCommentRepository->getProductCommentUsefulness($id_product_comment);
$this->ajaxRender(
json_encode(
array_merge(
[
'success' => true,
'id_product_comment' => $id_product_comment,
],
$commentUsefulness
)
)
);
}
}

View File

@@ -0,0 +1,34 @@
<?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 license@prestashop.com 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 <contact@prestashop.com>
* @copyright Since 2007 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License 3.0 (AFL-3.0)
*/
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
header('Cache-Control: no-store, no-cache, must-revalidate');
header('Cache-Control: post-check=0, pre-check=0', false);
header('Pragma: no-cache');
header('Location: ../');
exit;