|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace Lc\CaracoleBundle\Controller; |
|
|
namespace Lc\CaracoleBundle\Controller; |
|
|
|
|
|
|
|
|
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
|
|
use Doctrine\ORM\EntityManagerInterface; |
|
|
use Doctrine\ORM\EntityManagerInterface; |
|
|
use EasyCorp\Bundle\EasyAdminBundle\Collection\FieldCollection; |
|
|
use EasyCorp\Bundle\EasyAdminBundle\Collection\FieldCollection; |
|
|
use EasyCorp\Bundle\EasyAdminBundle\Collection\FilterCollection; |
|
|
use EasyCorp\Bundle\EasyAdminBundle\Collection\FilterCollection; |
|
|
|
|
|
|
|
|
use Lc\CaracoleBundle\Doctrine\Extension\FilterSectionInterface; |
|
|
use Lc\CaracoleBundle\Doctrine\Extension\FilterSectionInterface; |
|
|
use Lc\CaracoleBundle\Form\Merchant\DuplicateToOtherMerchantFormType; |
|
|
use Lc\CaracoleBundle\Form\Merchant\DuplicateToOtherMerchantFormType; |
|
|
use Lc\CaracoleBundle\Form\Section\DuplicateToOtherSectionFormType; |
|
|
use Lc\CaracoleBundle\Form\Section\DuplicateToOtherSectionFormType; |
|
|
|
|
|
use Lc\CaracoleBundle\Model\Product\ProductFamilyInterface; |
|
|
use Lc\CaracoleBundle\Resolver\MerchantResolver; |
|
|
use Lc\CaracoleBundle\Resolver\MerchantResolver; |
|
|
use Lc\CaracoleBundle\Resolver\SectionResolver; |
|
|
use Lc\CaracoleBundle\Resolver\SectionResolver; |
|
|
use EasyCorp\Bundle\EasyAdminBundle\Dto\EntityDto; |
|
|
use EasyCorp\Bundle\EasyAdminBundle\Dto\EntityDto; |
|
|
|
|
|
|
|
|
AdminContext $context, |
|
|
AdminContext $context, |
|
|
EntityComponent $entityComponent, |
|
|
EntityComponent $entityComponent, |
|
|
TranslatorAdmin $translatorAdmin, |
|
|
TranslatorAdmin $translatorAdmin, |
|
|
EntityManagerInterface $em |
|
|
|
|
|
|
|
|
EntityManagerInterface $entityManager |
|
|
) { |
|
|
) { |
|
|
if (!$this->isGranted( |
|
|
if (!$this->isGranted( |
|
|
Permission::EA_EXECUTE_ACTION, |
|
|
Permission::EA_EXECUTE_ACTION, |
|
|
|
|
|
|
|
|
throw new InsufficientEntityPermissionException($context); |
|
|
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.'); |
|
|
throw new \ErrorException('L\entité n\'est pas lié à un merchant.'); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
$duplicateOtherMerchantForm = $this->createForm( |
|
|
$duplicateOtherMerchantForm = $this->createForm( |
|
|
DuplicateToOtherMerchantFormType::class, |
|
|
DuplicateToOtherMerchantFormType::class, |
|
|
null, |
|
|
null, |
|
|
array( |
|
|
|
|
|
|
|
|
[ |
|
|
'entityClass' => $context->getEntity()->getFqcn(), |
|
|
'entityClass' => $context->getEntity()->getFqcn(), |
|
|
'entityId' => $context->getEntity()->getInstance()->getId(), |
|
|
'entityId' => $context->getEntity()->getInstance()->getId(), |
|
|
'action' => $context->getRequest()->getUri(), |
|
|
'action' => $context->getRequest()->getUri(), |
|
|
'attr' => ['id' => 'duplicate-other-merchant-form'], |
|
|
'attr' => ['id' => 'duplicate-other-merchant-form'], |
|
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
|
] |
|
|
); |
|
|
); |
|
|
|
|
|
|
|
|
$duplicateOtherMerchantForm->handleRequest($context->getRequest()); |
|
|
$duplicateOtherMerchantForm->handleRequest($context->getRequest()); |
|
|
|
|
|
|
|
|
if ($duplicateOtherMerchantForm->isSubmitted() && $duplicateOtherMerchantForm->isValid()) { |
|
|
if ($duplicateOtherMerchantForm->isSubmitted() && $duplicateOtherMerchantForm->isValid()) { |
|
|
$newEntity = $entityComponent->duplicateEntity($context->getEntity()->getInstance()); |
|
|
$newEntity = $entityComponent->duplicateEntity($context->getEntity()->getInstance()); |
|
|
$em->create($newEntity); |
|
|
|
|
|
|
|
|
$entityManager->create($newEntity); |
|
|
$merchant = $duplicateOtherMerchantForm->get('merchants')->getData(); |
|
|
$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) |
|
|
$url = $this->get(AdminUrlGenerator::class) |
|
|
->setAction(ActionDefinition::EDIT) |
|
|
|
|
|
->setEntityId($newEntity->getId()) |
|
|
|
|
|
|
|
|
->setAction(ActionDefinition::INDEX) |
|
|
->generateUrl(); |
|
|
->generateUrl(); |
|
|
|
|
|
|
|
|
$this->addFlashTranslator( |
|
|
$this->addFlashTranslator( |
|
|
'success', |
|
|
'success', |
|
|
'duplicateToOtherMerchant', |
|
|
'duplicateToOtherMerchant', |
|
|
|
|
|
|
|
|
['%merchant%' => $merchant->getTitle()] |
|
|
['%merchant%' => $merchant->getTitle()] |
|
|
); |
|
|
); |
|
|
|
|
|
|
|
|
//TODO switch merchant route |
|
|
|
|
|
return $this->redirect($url); |
|
|
return $this->redirect($url); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if ($context->getRequest()->isXmlHttpRequest()) { |
|
|
if ($context->getRequest()->isXmlHttpRequest()) { |
|
|
$response['data'] = $this->renderView( |
|
|
$response['data'] = $this->renderView( |
|
|
'@LcCaracole/admin/merchant/modal/duplicate_entity_to_other_merchant.html.twig', |
|
|
'@LcCaracole/admin/merchant/modal/duplicate_entity_to_other_merchant.html.twig', |
|
|
array( |
|
|
|
|
|
|
|
|
[ |
|
|
'form_duplicate_entity_to_other_merchant' => $duplicateOtherMerchantForm->createView(), |
|
|
'form_duplicate_entity_to_other_merchant' => $duplicateOtherMerchantForm->createView(), |
|
|
) |
|
|
|
|
|
|
|
|
] |
|
|
); |
|
|
); |
|
|
|
|
|
|
|
|
return new Response(json_encode($response)); |
|
|
return new Response(json_encode($response)); |
|
|
} else { |
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
else { |
|
|
throw new \ErrorException('La requête doit être effectué en ajax'); |
|
|
throw new \ErrorException('La requête doit être effectué en ajax'); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public function buildIndexActions(Actions $actions): void |
|
|
public function buildIndexActions(Actions $actions): void |
|
|
{ |
|
|
{ |
|
|
parent::buildIndexActions($actions); |
|
|
parent::buildIndexActions($actions); |
|
|
|
|
|
|
|
|
if ($this->isInstanceOf(FilterMerchantInterface::class)) { |
|
|
if ($this->isInstanceOf(FilterMerchantInterface::class)) { |
|
|
$actions->add(Crud::PAGE_INDEX, $this->getDuplicateToOhterMerchantAction()); |
|
|
$actions->add(Crud::PAGE_INDEX, $this->getDuplicateToOhterMerchantAction()); |
|
|
} |
|
|
} |