|
|
@@ -2,6 +2,7 @@ |
|
|
|
|
|
|
|
namespace Lc\CaracoleBundle\Controller; |
|
|
|
|
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
|
|
|
use EasyCorp\Bundle\EasyAdminBundle\Collection\FieldCollection; |
|
|
|
use EasyCorp\Bundle\EasyAdminBundle\Collection\FilterCollection; |
|
|
@@ -20,6 +21,7 @@ use Lc\CaracoleBundle\Doctrine\Extension\FilterMultipleMerchantsInterface; |
|
|
|
use Lc\CaracoleBundle\Doctrine\Extension\FilterSectionInterface; |
|
|
|
use Lc\CaracoleBundle\Form\Merchant\DuplicateToOtherMerchantFormType; |
|
|
|
use Lc\CaracoleBundle\Form\Section\DuplicateToOtherSectionFormType; |
|
|
|
use Lc\CaracoleBundle\Model\Product\ProductFamilyInterface; |
|
|
|
use Lc\CaracoleBundle\Resolver\MerchantResolver; |
|
|
|
use Lc\CaracoleBundle\Resolver\SectionResolver; |
|
|
|
use EasyCorp\Bundle\EasyAdminBundle\Dto\EntityDto; |
|
|
@@ -93,7 +95,7 @@ trait AdminControllerTrait |
|
|
|
AdminContext $context, |
|
|
|
EntityComponent $entityComponent, |
|
|
|
TranslatorAdmin $translatorAdmin, |
|
|
|
EntityManagerInterface $em |
|
|
|
EntityManagerInterface $entityManager |
|
|
|
) { |
|
|
|
if (!$this->isGranted( |
|
|
|
Permission::EA_EXECUTE_ACTION, |
|
|
@@ -106,35 +108,68 @@ trait AdminControllerTrait |
|
|
|
throw new InsufficientEntityPermissionException($context); |
|
|
|
} |
|
|
|
|
|
|
|
if (!$this->isInstanceOf(FilterMerchantInterface::class)) { |
|
|
|
if (!$this->isInstanceOf(FilterMerchantInterface::class) |
|
|
|
&& !$this->isInstanceOf(ProductFamilyInterface::class)) { |
|
|
|
throw new \ErrorException('L\entité n\'est pas lié à un merchant.'); |
|
|
|
} |
|
|
|
|
|
|
|
$duplicateOtherMerchantForm = $this->createForm( |
|
|
|
DuplicateToOtherMerchantFormType::class, |
|
|
|
null, |
|
|
|
array( |
|
|
|
[ |
|
|
|
'entityClass' => $context->getEntity()->getFqcn(), |
|
|
|
'entityId' => $context->getEntity()->getInstance()->getId(), |
|
|
|
'action' => $context->getRequest()->getUri(), |
|
|
|
'attr' => ['id' => 'duplicate-other-merchant-form'], |
|
|
|
|
|
|
|
) |
|
|
|
] |
|
|
|
); |
|
|
|
|
|
|
|
$duplicateOtherMerchantForm->handleRequest($context->getRequest()); |
|
|
|
|
|
|
|
if ($duplicateOtherMerchantForm->isSubmitted() && $duplicateOtherMerchantForm->isValid()) { |
|
|
|
$newEntity = $entityComponent->duplicateEntity($context->getEntity()->getInstance()); |
|
|
|
$em->create($newEntity); |
|
|
|
$entityManager->create($newEntity); |
|
|
|
$merchant = $duplicateOtherMerchantForm->get('merchants')->getData(); |
|
|
|
$newEntity->setMerchant($merchant); |
|
|
|
$em->update($newEntity); |
|
|
|
$em->flush(); |
|
|
|
|
|
|
|
if($this->isInstanceOf(ProductFamilyInterface::class)) { |
|
|
|
$sectionStore = $this->getSectionContainer()->getStore()->setMerchant($merchant); |
|
|
|
|
|
|
|
// Les ProductFamilySectionproperty sont créées en double, on rectifie ici |
|
|
|
// @TODO : j'imagine qu'on peut faire mieux que ça. Résoudre le problème à la base par exemple. |
|
|
|
$productFamilySectionPropertyArray = []; |
|
|
|
foreach($newEntity->getProductFamilySectionProperties() as $productFamilySectionProperty) { |
|
|
|
$productFamilySectionPropertyArray[$productFamilySectionProperty->getId()] = $productFamilySectionProperty; |
|
|
|
$newEntity->removeProductFamilySectionProperty($productFamilySectionProperty); |
|
|
|
} |
|
|
|
|
|
|
|
foreach($productFamilySectionPropertyArray as $productFamilySectionProperty) { |
|
|
|
$oldSection = $productFamilySectionProperty->getSection(); |
|
|
|
$newSection = $sectionStore->getOneByDevAlias($oldSection->getDevAlias()); |
|
|
|
|
|
|
|
if($newSection) { |
|
|
|
$productFamilySectionProperty->setProductFamily($newEntity); |
|
|
|
$productFamilySectionProperty->setSection($newSection); |
|
|
|
$newEntity->addProductFamilySectionProperty($productFamilySectionProperty); |
|
|
|
} |
|
|
|
else { |
|
|
|
$entityManager->remove($productFamilySectionProperty); |
|
|
|
$newEntity->removeProductFamilySectionProperty($productFamilySectionProperty); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
$newEntity->initProductCategories(); |
|
|
|
} |
|
|
|
else { |
|
|
|
$newEntity->setMerchant($merchant); |
|
|
|
} |
|
|
|
|
|
|
|
$entityManager->update($newEntity); |
|
|
|
$entityManager->flush(); |
|
|
|
|
|
|
|
$url = $this->get(AdminUrlGenerator::class) |
|
|
|
->setAction(ActionDefinition::EDIT) |
|
|
|
->setEntityId($newEntity->getId()) |
|
|
|
->setAction(ActionDefinition::INDEX) |
|
|
|
->generateUrl(); |
|
|
|
|
|
|
|
$this->addFlashTranslator( |
|
|
|
'success', |
|
|
|
'duplicateToOtherMerchant', |
|
|
@@ -142,21 +177,20 @@ trait AdminControllerTrait |
|
|
|
['%merchant%' => $merchant->getTitle()] |
|
|
|
); |
|
|
|
|
|
|
|
//TODO switch merchant route |
|
|
|
return $this->redirect($url); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if ($context->getRequest()->isXmlHttpRequest()) { |
|
|
|
$response['data'] = $this->renderView( |
|
|
|
'@LcCaracole/admin/merchant/modal/duplicate_entity_to_other_merchant.html.twig', |
|
|
|
array( |
|
|
|
[ |
|
|
|
'form_duplicate_entity_to_other_merchant' => $duplicateOtherMerchantForm->createView(), |
|
|
|
) |
|
|
|
] |
|
|
|
); |
|
|
|
|
|
|
|
return new Response(json_encode($response)); |
|
|
|
} else { |
|
|
|
} |
|
|
|
else { |
|
|
|
throw new \ErrorException('La requête doit être effectué en ajax'); |
|
|
|
} |
|
|
|
} |
|
|
@@ -235,6 +269,7 @@ trait AdminControllerTrait |
|
|
|
public function buildIndexActions(Actions $actions): void |
|
|
|
{ |
|
|
|
parent::buildIndexActions($actions); |
|
|
|
|
|
|
|
if ($this->isInstanceOf(FilterMerchantInterface::class)) { |
|
|
|
$actions->add(Crud::PAGE_INDEX, $this->getDuplicateToOhterMerchantAction()); |
|
|
|
} |