Browse Source

Système compte prépayés

feature/export_comptable
Fab 4 years ago
parent
commit
6d33d4a50e
1 changed files with 121 additions and 0 deletions
  1. +121
    -0
      ShopBundle/Controller/Backend/UserMerchantController.php

+ 121
- 0
ShopBundle/Controller/Backend/UserMerchantController.php View File

@@ -3,14 +3,135 @@
namespace Lc\ShopBundle\Controller\Backend;

use App\Repository\UserRepository;
use Doctrine\ORM\EntityManagerInterface;
use EasyCorp\Bundle\EasyAdminBundle\Event\EasyAdminEvents;
use FOS\UserBundle\Model\UserManagerInterface;
use Lc\ShopBundle\Context\MerchantUtilsInterface;
use Lc\ShopBundle\Context\NewsInterface;
use Lc\ShopBundle\Context\OrderUtilsInterface;
use Lc\ShopBundle\Context\UserInterface;
use Lc\ShopBundle\Services\CreditUtils;
use Lc\ShopBundle\Services\Utils;
use Mailjet\MailjetSwiftMailer\SwiftMailer\MailjetTransport;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Security\Core\Security;
use Symfony\Contracts\Translation\TranslatorInterface;

class UserMerchantController extends AdminController
{
protected $creditUtils;

public function __construct(Security $security, UserManagerInterface $userManager, EntityManagerInterface $em, Utils $utils, MerchantUtilsInterface $merchantUtils, MailjetTransport $mailjetTransport, OrderUtilsInterface $orderUtils, TranslatorInterface $translator, CreditUtils $creditUtils)
{
parent::__construct($security, $userManager, $em, $utils, $merchantUtils, $mailjetTransport, $orderUtils, $translator);
$this->creditUtils = $creditUtils;
}

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);

//ETAPE 1 Choix de l'utilisateur
$user = $newForm->get('user')->getData();

if ($user == null) {
$user = $this->getUserViaFirstStepForm($entity);
}

if (!$user instanceof UserInterface) return $user;
else {

$userMerchant = $this->creditUtils->activeCredit($user);

return $this->redirectToRoute('easyadmin', [
'action' => 'edit',
'entity' => $this->entity['name'],
'id' => $userMerchant->getId()
]);
}
}

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]);
}

/**
* The method that is executed when the user performs a 'show' action on an entity.
*
* @return Response
*/
public function showAction()
{
$this->dispatch(EasyAdminEvents::PRE_SHOW);

$id = $this->request->query->get('id');
$easyadmin = $this->request->attributes->get('easyadmin');
$entity = $easyadmin['item'];

$fields = $this->entity['show']['fields'];
$deleteForm = $this->createDeleteForm($this->entity['name'], $id);

$this->dispatch(EasyAdminEvents::POST_SHOW, [
'deleteForm' => $deleteForm,
'fields' => $fields,
'entity' => $entity,
]);

$parameters = [
'entity' => $entity,
'fields' => $fields,
'delete_form' => $deleteForm->createView()
];

return $this->executeDynamicMethod('render<EntityName>Template', ['show', $this->entity['templates']['show'], $parameters]);
}

/**
* Réécriture de edit action pr rediriger vers le show */
public function editAction()
{
$id = $this->request->query->get('id');
$entity = $this->request->query->get('entity');

return $this->redirectToRoute('easyadmin', [
'action' => 'show',
'entity' => $entity,
'id' => $id
]);
}

}

Loading…
Cancel
Save