|
- <?php
-
- namespace Lc\ShopBundle\Controller\Admin;
-
- use App\Entity\OrderProduct;
- use App\Entity\Product;
- use Doctrine\DBAL\Types\FloatType;
- use Doctrine\ORM\EntityRepository;
- use EasyCorp\Bundle\EasyAdminBundle\Event\EasyAdminEvents;
- use Lc\ShopBundle\Context\AddressInterface;
- use Lc\ShopBundle\Context\ProductCategoryInterface;
- use Lc\ShopBundle\Context\ProductFamilyInterface;
- use Lc\ShopBundle\Context\ProductInterface;
- use Lc\ShopBundle\Context\TaxRateInterface;
- use Lc\ShopBundle\Context\UserInterface;
- use Lc\ShopBundle\Form\OrderProductType;
- use Lc\ShopBundle\Form\ProductFamilyCategoriesType;
- use Lc\ShopBundle\Form\ProductType;
- use Symfony\Bridge\Doctrine\Form\Type\EntityType;
- use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
- use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
- use Symfony\Component\Form\Extension\Core\Type\CollectionType;
- use Symfony\Component\Form\Extension\Core\Type\DateType;
- use Symfony\Component\Form\Extension\Core\Type\HiddenType;
- use Symfony\Component\Form\Extension\Core\Type\MoneyType;
- use Symfony\Component\Form\Extension\Core\Type\NumberType;
- use Symfony\Component\Form\Extension\Core\Type\SubmitType;
- use Symfony\Component\HttpFoundation\Response;
-
- class OrderController extends AdminController
- {
-
- public function updateEntity($entity)
- {
- foreach($entity->getOrderProducts() as $orderProduct) {
- $orderProduct->setCreatedBy($this->getUser()) ;
- $orderProduct->setUpdatedBy($this->getUser()) ;
- $orderProduct->setTaxRate($orderProduct->getProduct()->getProductFamily()->getTaxRate()) ;
- $orderProduct->setOrderShop($entity) ;
- $this->em->persist($orderProduct);
-
- $entity->addOrderProduct($orderProduct) ;
- }
-
- $this->setUpdated($entity);
-
- parent::updateEntity($entity);
- }
-
- public function persistEntity($entity)
- {
-
- parent::persistEntity($entity) ;
- }
-
- public function createEntityFormBuilder($entity, $view)
- {
- $formBuilder = parent::createEntityFormBuilder($entity, $view);
-
- $formBuilder->add('orderProducts', CollectionType::class, array(
- 'label' => 'Déclinaisons',
- 'entry_type' => OrderProductType::class,
- 'entry_options' => ['label' => false],
- 'allow_add' => true,
- 'allow_delete' => true,
- 'required' => true
- )
- );
-
- $userClass = $this->em->getClassMetadata(UserInterface::class);
- $formBuilder->add('user', EntityType::class, array(
- 'class' => $userClass->name
- ));
-
-
- $addressClass = $this->em->getClassMetadata(AddressInterface::class);
- $formBuilder->add('invoiceAddress', EntityType::class, array(
- 'class' => $addressClass->name,
- 'query_builder' => function (EntityRepository $er) use ($entity) {
- return $er->createQueryBuilder('a')
- ->where('a.user = :user')
- ->setParameter('user', $entity->getUser());
- },
- ));
-
- $formBuilder->add('deliveryAddress', EntityType::class, array(
- 'class' => $addressClass->name,
- 'query_builder' => function (EntityRepository $er) use ($entity) {
- return $er->createQueryBuilder('a')
- ->where('a.user = :user')
- ->setParameter('user', $entity->getUser());
- },
- ));
-
-
- return $formBuilder;
- }
-
-
- public function getUserViaFirstStepForm($entity)
- {
- $userClass = $this->em->getClassMetadata(UserInterface::class);
-
- $userChoiceForm = $this->createFormBuilder($entity)
- ->add('user', EntityType::class, array(
- 'class' => $userClass->name
- ))
- ->add('nextStep', SubmitType::class)
- ->getForm();
- $userChoiceForm->handleRequest($this->request);
- if ($userChoiceForm->isSubmitted() && $userChoiceForm->isValid()) {
- return $userChoiceForm->get('user')->getData();
- }
-
- $parameters = [
- 'form' => $userChoiceForm->createView(),
- 'formView' => 'default',
- 'entity' => $entity,
- ];
-
- return $this->executeDynamicMethod('render<EntityName>Template', ['new', $this->entity['templates']['new'], $parameters]);
- }
-
- protected function newAction()
- {
-
- $this->dispatch(EasyAdminEvents::PRE_NEW);
-
- $entity = $this->executeDynamicMethod('createNew<EntityName>Entity');
-
- $easyadmin = $this->request->attributes->get('easyadmin');
- $easyadmin['item'] = $entity;
- $this->request->attributes->set('easyadmin', $easyadmin);
-
- $fields = $this->entity['new']['fields'];
-
- $newForm = $this->executeDynamicMethod('create<EntityName>NewForm', [$entity, $fields]);
-
- $newForm->handleRequest($this->request);
-
-
- $user = $newForm->get('user')->getData();
-
- if($user == null){
- $user = $this->getUserViaFirstStepForm($entity);
- }
-
- if (!$user instanceof UserInterface) return $user;
- else{
- $entity->setUser($user);
-
-
-
-
-
-
- if ($newForm->isSubmitted() && $newForm->isValid()) {
- dump($entity);
- die();
-
- $this->executeDynamicMethod('persist<EntityName>Entity', [$entity, $newForm]);
- $this->dispatch(EasyAdminEvents::POST_PERSIST, ['entity' => $entity]);
-
- return $this->redirectToReferrer();
- }
-
- $this->dispatch(EasyAdminEvents::POST_NEW, [
- 'entity_fields' => $fields,
- 'form' => $newForm,
- 'entity' => $entity
- ]);
-
- $parameters = [
- 'form' => $newForm->createView(),
- 'entity_fields' => $fields,
- 'entity' => $entity
- ];
-
- return $this->executeDynamicMethod('render<EntityName>Template', ['new', $this->entity['templates']['new'], $parameters]);
- }
- }
-
-
- }
|