|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310 |
- <?php
-
- 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;
- use EasyCorp\Bundle\EasyAdminBundle\Config\Action;
- use EasyCorp\Bundle\EasyAdminBundle\Config\Actions;
- use EasyCorp\Bundle\EasyAdminBundle\Config\Crud;
- use EasyCorp\Bundle\EasyAdminBundle\Config\KeyValueStore;
- use EasyCorp\Bundle\EasyAdminBundle\Context\AdminContext;
- use EasyCorp\Bundle\EasyAdminBundle\Exception\ForbiddenActionException;
- use EasyCorp\Bundle\EasyAdminBundle\Exception\InsufficientEntityPermissionException;
- use EasyCorp\Bundle\EasyAdminBundle\Router\AdminUrlGenerator;
- use EasyCorp\Bundle\EasyAdminBundle\Security\Permission;
- use Lc\CaracoleBundle\Definition\ActionDefinition;
- use Lc\CaracoleBundle\Doctrine\Extension\FilterMerchantInterface;
- 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;
- use EasyCorp\Bundle\EasyAdminBundle\Dto\SearchDto;
- use Lc\SovBundle\Component\EntityComponent;
- use Lc\SovBundle\Repository\RepositoryQueryInterface;
- use Lc\SovBundle\Solver\Setting\SettingSolver;
- use Lc\SovBundle\Translation\TranslatorAdmin;
- use Symfony\Component\HttpFoundation\Response;
-
- trait AdminControllerTrait
- {
- use ControllerTrait;
-
- public function configureResponseParameters(KeyValueStore $responseParameters): KeyValueStore
- {
- $responseParameters = parent::configureResponseParameters($responseParameters);
-
- $this->configureResponseParametersFilterSection($responseParameters);
-
- return $responseParameters;
- }
-
- public function configureResponseParametersFilterSection(KeyValueStore $responseParameters)
- {
- if ($this->isInstanceOf(FilterSectionInterface::class)) {
- $responseParameters->set('display_switch_section', true);
- }
- }
-
- public function configureResponseParametersDisableShowAllSections(KeyValueStore $responseParameters)
- {
- if($this->getSectionCurrent() == null) {
- $responseParameters->set('replace_content_with_message', "Vous devez sélectionner une section pour afficher ces données.");
- }
- }
-
- public function createIndexRepositoryQuery(
- SearchDto $searchDto,
- EntityDto $entityDto,
- FieldCollection $fields,
- FilterCollection $filters
- ): RepositoryQueryInterface {
- $repositoryQuery = parent::createIndexRepositoryQuery(
- $searchDto,
- $entityDto,
- $fields,
- $filters
- );
-
- $sectionCurrent = $this->get(SectionResolver::class)->getCurrent();
-
- if ($this->isInstanceOf(FilterMerchantInterface::class)
- || $this->isInstanceOf(FilterMultipleMerchantsInterface::class)) {
- $repositoryQuery->filterByMerchant($this->getMerchantCurrent());
- }
-
- if ($sectionCurrent && $this->isInstanceOf(FilterSectionInterface::class)) {
- $repositoryQuery->filterBySection($sectionCurrent);
- }
-
- if ($this->isOutOfSection() && $this->isInstanceOf(FilterSectionInterface::class) && !$this->isInstanceOf(FilterMerchantInterface::class)) {
- $repositoryQuery->filterByMerchantViaSection($this->getMerchantCurrent());
- }
-
- return $repositoryQuery;
- }
-
-
- public function duplicateToOtherMerchant(
- AdminContext $context,
- EntityComponent $entityComponent,
- TranslatorAdmin $translatorAdmin,
- EntityManagerInterface $entityManager
- ) {
- if (!$this->isGranted(
- Permission::EA_EXECUTE_ACTION,
- ['action' => "duplicate", 'entity' => $context->getEntity()]
- )) {
- throw new ForbiddenActionException($context);
- }
-
- if (!$context->getEntity()->isAccessible()) {
- throw new InsufficientEntityPermissionException($context);
- }
-
- 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,
- [
- '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());
- $entityManager->create($newEntity);
- $merchant = $duplicateOtherMerchantForm->get('merchants')->getData();
-
- 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::INDEX)
- ->generateUrl();
-
- $this->addFlashTranslator(
- 'success',
- 'duplicateToOtherMerchant',
- $this->getTranslationEntityName(),
- ['%merchant%' => $merchant->getTitle()]
- );
-
- return $this->redirect($url);
- }
-
- if ($context->getRequest()->isXmlHttpRequest()) {
- $response['data'] = $this->renderView(
- '@LcCaracole/admin/merchant/modal/duplicate_entity_to_other_merchant.html.twig',
- [
- 'form_duplicate_entity_to_other_merchant' => $duplicateOtherMerchantForm->createView(),
- ]
- );
-
- return new Response(json_encode($response));
- }
- else {
- throw new \ErrorException('La requête doit être effectué en ajax');
- }
- }
-
- public function duplicateToOtherSection(
- AdminContext $context,
- EntityComponent $entityComponent,
- TranslatorAdmin $translatorAdmin,
- EntityManagerInterface $em
- ) {
- if (!$this->isGranted(
- Permission::EA_EXECUTE_ACTION,
- ['action' => ActionDefinition::DUPLICATE, 'entity' => $context->getEntity()]
- )) {
- throw new ForbiddenActionException($context);
- }
-
- if (!$context->getEntity()->isAccessible()) {
- throw new InsufficientEntityPermissionException($context);
- }
-
- if (!$this->isInstanceOf(FilterSectionInterface::class)) {
- throw new \ErrorException('L\entité n\'est pas lié à un merchant.');
- }
- $duplicateOtherSectionForm = $this->createForm(
- DuplicateToOtherSectionFormType::class,
- null,
- array(
- 'entityClass' => $context->getEntity()->getFqcn(),
- 'entityId' => $context->getEntity()->getInstance()->getId(),
- 'action' => $context->getRequest()->getUri(),
- 'attr' => ['id' => 'duplicate-other-section-form'],
- )
- );
-
- $duplicateOtherSectionForm->handleRequest($context->getRequest());
-
- if ($duplicateOtherSectionForm->isSubmitted() && $duplicateOtherSectionForm->isValid()) {
- $newEntity = $entityComponent->duplicateEntity($context->getEntity()->getInstance());
- $em->create($newEntity);
- $section = $duplicateOtherSectionForm->get('sections')->getData();
- $newEntity->setSection($section);
- $em->update($newEntity);
- $em->flush();
-
- $url = $this->get(AdminUrlGenerator::class)
- ->setAction(ActionDefinition::EDIT)
- ->setEntityId($newEntity->getId())
- ->generateUrl();
- $this->addFlashTranslator(
- 'success',
- 'duplicateToOtherSection',
- $this->getTranslationEntityName(),
- ['%section%' => $section->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_section.html.twig',
- array(
- 'form_duplicate_entity_to_other_section' => $duplicateOtherSectionForm->createView(),
- )
- );
-
- return new Response(json_encode($response));
- } else {
- throw new \ErrorException('La requête doit être effectué en ajax');
- }
- }
-
- public function buildIndexActions(Actions $actions): void
- {
- parent::buildIndexActions($actions);
-
- if ($this->isInstanceOf(FilterMerchantInterface::class)) {
- $actions->add(Crud::PAGE_INDEX, $this->getDuplicateToOhterMerchantAction());
- }
-
- if ($this->isInstanceOf(FilterSectionInterface::class)) {
- $actions->add(Crud::PAGE_INDEX, $this->getDuplicateToOhterSectionAction());
- }
- }
-
- public function getDuplicateToOhterMerchantAction(): Action
- {
- $duplicateAction = Action::new(
- ActionDefinition::DUPLICATE_TO_OTHER_MERCHANT,
- $this->get(TranslatorAdmin::class)->transAction(ActionDefinition::DUPLICATE_TO_OTHER_MERCHANT),
- 'fa fa-fw fa-copy'
- )
- ->linkToCrudAction(ActionDefinition::DUPLICATE_TO_OTHER_MERCHANT)
- ->setCssClass('text-info in-dropdown duplicate-to-other-merchant duplicate-modal-action');
-
- return $duplicateAction;
- }
-
- public function getDuplicateToOhterSectionAction(): Action
- {
- $duplicateAction = Action::new(
- ActionDefinition::DUPLICATE_TO_OTHER_SECTION,
- $this->get(TranslatorAdmin::class)->transAction(ActionDefinition::DUPLICATE_TO_OTHER_SECTION),
- 'fa fa-fw fa-copy'
- )
- ->linkToCrudAction(ActionDefinition::DUPLICATE_TO_OTHER_SECTION)
- ->setCssClass('text-info in-dropdown duplicate-to-other-section duplicate-modal-action');
-
- return $duplicateAction;
- }
-
-
- }
-
|