Skip to content

Commit

Permalink
mark deprcated functions, replace migrated functions call by new one
Browse files Browse the repository at this point in the history
  • Loading branch information
leemyongpakvn committed Dec 9, 2023
1 parent d7a89ad commit a99f017
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 47 deletions.
48 changes: 35 additions & 13 deletions productcomments.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,10 @@ public function getCacheId($id_product = null)
protected function _postProcess()
{
$id_product_comment = (int) Tools::getValue('id_product_comment');
$id_product_comment_criterion = (int) Tools::getValue('id_product_comment_criterion');
$commentRepository = $this->get('product_comment_repository');
$criterionRepository = $this->get('product_comment_criterion_repository');
$criterionFormHandler = $this->get('product_comment_criterion_form_data_handler');

if (Tools::isSubmit('submitModerate')) {
$errors = [];
Expand Down Expand Up @@ -215,7 +217,12 @@ protected function _postProcess()
Tools::redirectAdmin($this->context->link->getAdminLink('AdminModules', true, [], ['configure' => $this->name]));
}
} elseif (Tools::isSubmit('submitEditCriterion')) {
$criterion = $criterionRepository->findRelation((int) Tools::getValue('id_product_comment_criterion'));
if ($id_product_comment_criterion > 0) {
$criterion = $criterionRepository->find($id_product_comment_criterion);
} else {
$criterion = new ProductCommentCriterion();
}

$criterion->setType((int) Tools::getValue('id_product_comment_criterion_type'));
$criterion->setActive(Tools::getValue('active'));

Expand All @@ -224,7 +231,12 @@ protected function _postProcess()
foreach ($languages as $key => $value) {
$name[$value['id_lang']] = Tools::getValue('name_' . $value['id_lang']);
}
$criterion->setNames($name);

if ($id_product_comment_criterion > 0) {
$criterionFormHandler->updateLangs($criterion, $name);
} else {
$criterionFormHandler->createLangs($criterion, $name);
}

if (!$criterion->isValid()) {
$this->_html .= $this->displayError($this->trans('The criterion cannot be saved', [], 'Modules.Productcomments.Admin'));
Expand All @@ -238,14 +250,18 @@ protected function _postProcess()
}
}
} elseif (Tools::isSubmit('deleteproductcommentscriterion')) {
$criterion = $criterionRepository->findRelation((int) Tools::getValue('id_product_comment_criterion'));
$criterion = $criterionRepository->find($id_product_comment_criterion);
if ($criterionRepository->delete($criterion)) {
$this->_html .= $this->displayConfirmation($this->trans('Criterion deleted', [], 'Modules.Productcomments.Admin'));
Tools::redirectAdmin($this->context->link->getAdminLink('AdminModules', true, [], ['configure' => $this->name]));
} else {
$this->_html .= $this->displayError($this->trans('Criterion cannot be deleted', [], 'Modules.Productcomments.Admin'));
}
} elseif (Tools::isSubmit('statusproductcommentscriterion')) {
$criterion = $criterionRepository->findRelation((int) Tools::getValue('id_product_comment_criterion'));
$criterion = $criterionRepository->find($id_product_comment_criterion);
$criterion->setActive(!$criterion->isActive());
Tools::redirectAdmin($this->context->link->getAdminLink('AdminModules', true, [], ['configure' => $this->name, 'tab_module' => $this->tab, 'conf' => 4, 'module_name' => $this->name]));
$criterionRepository->updateGeneral($criterion);

Tools::redirectAdmin($this->context->link->getAdminLink('AdminModules', true, [], ['configure' => $this->name]));
} elseif ($id_product_comment = (int) Tools::getValue('approveComment')) {
$comment = $commentRepository->find($id_product_comment);
$commentRepository->validate($comment, 1);
Expand Down Expand Up @@ -602,16 +618,22 @@ public function getConfigFieldsValues()
];
}

public function getCriterionFieldsValues($id = 0)
public function getCriterionFieldsValues(int $id = 0)
{
$criterionRepos = $this->get('product_comment_criterion_repository');
$criterion = $criterionRepos->findRelation($id);
$criterionFormProvider = $this->get('product_comment_criterion_form_data_provider');

if ($id > 0) {
$criterionData = $criterionFormProvider->getData($id);
} else {
$criterionData = $criterionFormProvider->getDefaultData();
}

return [
'name' => $criterion->getNames(),
'id_product_comment_criterion_type' => $criterion->getType(),
'active' => $criterion->isActive(),
'id_product_comment_criterion' => $criterion->getId(),
'name' => $criterionData['name'],
'id_product_comment_criterion_type' => $criterionData['type'],
'active' => $criterionData['active'],
'id_product_comment_criterion' => $id,
];
}

Expand Down Expand Up @@ -702,7 +724,7 @@ public function renderCriterionForm($id_criterion = 0)

$criterionRepository = $this->get('product_comment_criterion_repository');

$criterion = $criterionRepository->findRelation($id_criterion);
$criterion = $criterionRepository->find($id_criterion);
$selected_categories = $criterionRepository->getCategories($id_criterion);

$product_table_values = Product::getSimpleProducts($this->langId);
Expand Down
61 changes: 27 additions & 34 deletions src/Repository/ProductCommentCriterionRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ public function remove(ProductCommentCriterion $entity, bool $flush = false): vo
}
}

/**
* @deprecated 6.0.3 - cascade remove by Entity setting instead
*/
private function deleteLangs($criterion): int
{
return $this->connection->executeUpdate('
Expand All @@ -112,33 +115,34 @@ private function deleteProducts($criterion): int
private function deleteGrades($criterion): int
{
return $this->connection->executeUpdate('
DELETE FROM `' . _DB_PREFIX_ . 'product_comment_criterion_grade`
DELETE FROM `' . _DB_PREFIX_ . 'product_comment_grade`
WHERE `id_product_comment_criterion` = ' . $criterion->getId());
}

/* Remove a criterion and Delete its manual relation _lang, _category, _product, _grade */
/* Remove a criterion and Delete its manual relation _category, _product, _grade */
public function delete(ProductCommentCriterion $criterion): int
{
$res = 0;

$criterionType = $criterion->getType();

$this->remove($criterion, true);

$res += $this->deleteLangs($criterion);

if ($criterionType == ProductCommentCriterion::CATEGORIES_TYPE) {
$res += $this->deleteCategories($criterion);
} elseif ($criterionType == ProductCommentCriterion::PRODUCTS_TYPE) {
$res += $this->deleteProducts($criterion);
} else {
$res = 1;
}

$res += $this->deleteGrades($criterion);

$this->remove($criterion, true);

// todo: return void, and use try catch Exception instead
return $res;
}

/* Update a criterion and Update its manual relation _lang, _category, _product, _grade */
/* Update a criterion and Update its manual relation _category, _product */
public function update(ProductCommentCriterion $criterion): int
{
$res = 0;
Expand All @@ -148,20 +152,23 @@ public function update(ProductCommentCriterion $criterion): int
$this->getEntityManager()->persist($criterion);
$this->getEntityManager()->flush();

$res += $this->deleteLangs($criterion);
$res += $this->updateLangs($criterion);

if ($criterionType == ProductCommentCriterion::CATEGORIES_TYPE) {
$res += $this->deleteCategories($criterion);
$res += $this->updateCategories($criterion);
} elseif ($criterionType == ProductCommentCriterion::PRODUCTS_TYPE) {
$res += $this->deleteProducts($criterion);
$res += $this->updateProducts($criterion);
} else {
$res = 1;
}

// todo: return void, and use try catch Exception instead
return $res;
}

/**
* @deprecated 6.0.3 - migrated to Form\ProductCommentCriterionFormDataHandler
*/
private function updateLangs($criterion): int
{
$res = 0;
Expand Down Expand Up @@ -217,15 +224,18 @@ private function updateProducts($criterion): int
return $res;
}

public function updateGeneral(ProductCommentCriterion $criterion): void
{
$this->getEntityManager()->persist($criterion);
$this->getEntityManager()->flush();
}

/**
* @param int $idProduct
* @param int $idLang
*
* @return array
*
* @throws \PrestaShopException
*/
public function getByProduct($idProduct, $idLang)
public function getByProduct(int $idProduct, int $idLang)
{
/** @var QueryBuilder $qb */
$qb = $this->connection->createQueryBuilder();
Expand Down Expand Up @@ -255,11 +265,9 @@ public function getByProduct($idProduct, $idLang)
}

/**
* Get Criterions
*
* @return array Criterions
*/
public function getCriterions($id_lang, $type = false, $active = false)
public function getCriterions(int $id_lang, $type = false, $active = false)
{
$sql = '
SELECT pcc.`id_product_comment_criterion`, pcc.id_product_comment_criterion_type, pccl.`name`, pcc.active
Expand All @@ -278,8 +286,6 @@ public function getCriterions($id_lang, $type = false, $active = false)
}

/**
* @param int $id_criterion
*
* @return array
*/
public function getProducts(int $id_criterion)
Expand All @@ -302,8 +308,6 @@ public function getProducts(int $id_criterion)
}

/**
* @param int $id_criterion
*
* @return array
*/
public function getCategories(int $id_criterion)
Expand Down Expand Up @@ -340,25 +344,14 @@ public function getTypes()
}

/**
* Get Criterion with names in active languages
*
* @return ProductCommentCriterion
*
* @deprecated 6.0.3 - use standard find() instead
*/
public function findRelation($id_criterion)
{
if ($id_criterion > 0) {
$criterion = $this->find($id_criterion);
$sql = '
SELECT `id_lang`, `name`
FROM `' . _DB_PREFIX_ . 'product_comment_criterion_lang` pccl
WHERE pccl.id_product_comment_criterion = ' . $id_criterion . '
ORDER BY pccl.`id_lang` ASC';
$langNames = $this->connection->executeQuery($sql)->fetchAll();
$langArray = [];
foreach ($langNames as $langName) {
$langArray[$langName['id_lang']] = $langName['name'];
}
$criterion->setNames($langArray);
} else {
$criterion = new ProductCommentCriterion();
}
Expand Down

0 comments on commit a99f017

Please sign in to comment.