Skip to content

Commit

Permalink
Merge pull request PrestaShop#191 from leemyongpakvn/migrate_manualLa…
Browse files Browse the repository at this point in the history
…ngProcess_by_standardDoctrine_nSFformData

Replace manual lang process by standard DoctrineRelation and SymfonyFormData
  • Loading branch information
nicosomb authored Jan 9, 2024
2 parents e53bfe8 + 28fb214 commit a46ac63
Show file tree
Hide file tree
Showing 8 changed files with 482 additions and 90 deletions.
16 changes: 16 additions & 0 deletions config/admin/services.yml
Original file line number Diff line number Diff line change
@@ -1,2 +1,18 @@
imports:
- { resource: ../common.yml }

services:
product_comment_criterion_form_data_provider:
class: 'PrestaShop\Module\ProductComment\Form\ProductCommentCriterionFormDataProvider'
public: true
arguments:
- '@product_comment_criterion_repository'
- '@prestashop.core.admin.lang.repository'

product_comment_criterion_form_data_handler:
class: 'PrestaShop\Module\ProductComment\Form\ProductCommentCriterionFormDataHandler'
public: true
arguments:
- '@product_comment_criterion_repository'
- '@prestashop.core.admin.lang.repository'
- '@doctrine.orm.default_entity_manager'
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
107 changes: 64 additions & 43 deletions src/Entity/ProductCommentCriterion.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@

namespace PrestaShop\Module\ProductComment\Entity;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use Language;
use Validate;

/**
Expand Down Expand Up @@ -67,34 +67,79 @@ class ProductCommentCriterion
/**
* @var array
*
* Need to be implemented as ORM\OneToMany in the future
* @deprecated 6.0.3 - use criterionLangs instead
*/
private $names;

/**
* @ORM\OneToMany(targetEntity="PrestaShop\Module\ProductComment\Entity\ProductCommentCriterionLang", cascade={"persist", "remove"}, mappedBy="productcommentcriterion")
*/
private $criterionLangs;

/**
* @var array
*
* Need to be implemented as ORM\OneToMany in the future
* @todo implement as ORM\OneToMany in the future
*/
private $categories;

/**
* @var array
*
* Need to be implemented as ORM\OneToMany in the future
* @todo implement as ORM\OneToMany in the future
*/
private $products;

public function __construct()
{
$langIsoIds = Language::getIsoIds();
foreach ($langIsoIds as $langIsoId) {
$this->names[$langIsoId['id_lang']] = $langIsoId['iso_code'];
$this->criterionLangs = new ArrayCollection();
}

/**
* @return ArrayCollection
*/
public function getCriterionLangs()
{
return $this->criterionLangs;
}

/**
* @return ProductCommentCriterionLang|null
*/
public function getCriterionLangByLangId(int $langId)
{
foreach ($this->criterionLangs as $criterionLang) {
if ($langId === $criterionLang->getLang()->getId()) {
return $criterionLang;
}
}

return null;
}

public function addCriterionLang(ProductCommentCriterionLang $criterionLang): self
{
$criterionLang->setProductCommentCriterion($this);
$this->criterionLangs->add($criterionLang);

return $this;
}

public function getCriterionName(): string
{
if ($this->criterionLangs->count() <= 0) {
return '';
}

$criterionLang = $this->criterionLangs->first();

return $criterionLang->getName();
}

/**
* @return array
*
* @deprecated 6.0.3 - migrated to Form\ProductCommentCriterionFormDataProvider
*/
public function getNames()
{
Expand All @@ -105,6 +150,8 @@ public function getNames()
* @param array $langNames
*
* @return ProductCommentCriterion
*
* @deprecated 6.0.3
*/
public function setNames($langNames)
{
Expand All @@ -123,10 +170,8 @@ public function getCategories()

/**
* @param array $selectedCategories
*
* @return ProductCommentCriterion
*/
public function setCategories($selectedCategories)
public function setCategories($selectedCategories): self
{
$this->categories = $selectedCategories;

Expand All @@ -143,71 +188,47 @@ public function getProducts()

/**
* @param array $selectedProducts
*
* @return ProductCommentCriterion
*/
public function setProducts($selectedProducts)
public function setProducts($selectedProducts): self
{
$this->products = $selectedProducts;

return $this;
}

/**
* @return int
*/
public function getId()
public function getId(): int
{
return $this->id;
}

/**
* @return int
*/
public function getType()
public function getType(): int
{
return $this->type;
}

/**
* @param int $type
*
* @return ProductCommentCriterion
*/
public function setType($type)
public function setType(int $type): self
{
$this->type = $type;

return $this;
}

/**
* @return bool
*/
public function isActive()
public function isActive(): bool
{
return $this->active;
}

/**
* @param bool $active
*
* @return ProductCommentCriterion
*/
public function setActive($active)
public function setActive(bool $active): self
{
$this->active = $active;

return $this;
}

/**
* @return bool
*/
public function isValid()
public function isValid(): bool
{
foreach ($this->names as $value) {
if (!Validate::isGenericName($value)) {
foreach ($this->criterionLangs as $criterionLang) {
if (!Validate::isGenericName($criterionLang->getName())) {
return false;
}
}
Expand Down
Loading

0 comments on commit a46ac63

Please sign in to comment.