diff --git a/productcomments.php b/productcomments.php index 473f5ed..75745ac 100644 --- a/productcomments.php +++ b/productcomments.php @@ -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 = []; @@ -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')); @@ -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')); @@ -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); @@ -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, ]; } @@ -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); diff --git a/src/Repository/ProductCommentCriterionRepository.php b/src/Repository/ProductCommentCriterionRepository.php index 7446f67..c978e5b 100644 --- a/src/Repository/ProductCommentCriterionRepository.php +++ b/src/Repository/ProductCommentCriterionRepository.php @@ -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(' @@ -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; @@ -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; @@ -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(); @@ -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 @@ -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) @@ -302,8 +308,6 @@ public function getProducts(int $id_criterion) } /** - * @param int $id_criterion - * * @return array */ public function getCategories(int $id_criterion) @@ -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(); }