Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

191 lines
6.7KB

  1. <?php
  2. namespace Lc\SovBundle\Controller\Ticket;
  3. use Doctrine\ORM\EntityManagerInterface;
  4. use EasyCorp\Bundle\EasyAdminBundle\Config\Action;
  5. use EasyCorp\Bundle\EasyAdminBundle\Config\Actions;
  6. use EasyCorp\Bundle\EasyAdminBundle\Config\Assets;
  7. use EasyCorp\Bundle\EasyAdminBundle\Config\Crud;
  8. use EasyCorp\Bundle\EasyAdminBundle\Context\AdminContext;
  9. use EasyCorp\Bundle\EasyAdminBundle\Field\AssociationField;
  10. use EasyCorp\Bundle\EasyAdminBundle\Field\ChoiceField;
  11. use EasyCorp\Bundle\EasyAdminBundle\Field\DateField;
  12. use EasyCorp\Bundle\EasyAdminBundle\Field\IntegerField;
  13. use EasyCorp\Bundle\EasyAdminBundle\Field\TextField;
  14. use EasyCorp\Bundle\EasyAdminBundle\Router\AdminUrlGenerator;
  15. use Lc\SovBundle\Container\Ticket\TicketContainer;
  16. use Lc\SovBundle\Container\Ticket\TicketMessageContainer;
  17. use Lc\SovBundle\Definition\ActionDefinition;
  18. use Lc\SovBundle\Factory\Ticket\TicketFactory;
  19. use Lc\SovBundle\Factory\Ticket\TicketFactoryInterface;
  20. use Lc\SovBundle\Factory\Ticket\TicketMessageFactory;
  21. use Lc\SovBundle\Factory\Ticket\TicketMessageFactoryInterface;
  22. use Lc\SovBundle\Form\Ticket\TicketFormType;
  23. use Lc\SovBundle\Form\Ticket\TicketMessageFormType;
  24. use Lc\SovBundle\Form\Ticket\TicketStatusType;
  25. use Lc\SovBundle\Model\Ticket\TicketInterface;
  26. use Lc\SovBundle\Controller\AbstractAdminController;
  27. use Lc\SovBundle\Model\Ticket\TicketModel;
  28. use Lc\SovBundle\Translation\TranslatorAdmin;
  29. use Symfony\Component\HttpFoundation\JsonResponse;
  30. use Symfony\Component\HttpFoundation\RequestStack;
  31. class TicketAdminController extends AbstractAdminController
  32. {
  33. public static function getEntityFqcn(): string
  34. {
  35. return TicketInterface::class;
  36. }
  37. public function createEntity(string $entityFqcn)
  38. {
  39. return $this->get(TicketContainer::class)->getFactory()->create();
  40. }
  41. public function configureAssets(Assets $assets): Assets
  42. {
  43. $assets = parent::configureAssets($assets);
  44. $assets->addWebpackEncoreEntry('sov-ticket');
  45. return $assets;
  46. }
  47. public function configureFields(string $pageName): iterable
  48. {
  49. $translatorAdmin = $this->get(TranslatorAdmin::class);
  50. return [
  51. IntegerField::new('id')
  52. ->hideOnForm(),
  53. DateField::new('createdAt')->setFormat('short')
  54. ->hideOnForm(),
  55. TextField::new('visitorFirstName')
  56. ->setTemplatePath('@LcSov/admin/ticket/field/username.html.twig')
  57. ->hideOnForm(),
  58. TextField::new('visitorEmail')
  59. ->setTemplatePath('@LcSov/admin/ticket/field/email.html.twig')
  60. ->hideOnForm(),
  61. AssociationField::new('user')
  62. ->hideOnIndex(),
  63. TextField::new('subject'),
  64. TextField::new('lastMessage')
  65. ->setTemplatePath('@LcSov/admin/ticket/field/lastmessage.html.twig')
  66. ->hideOnForm(),
  67. ChoiceField::new('type')
  68. ->autocomplete()
  69. ->setChoices(
  70. $translatorAdmin->transChoices($this->get(TicketContainer::class)->getSolver()->getTypeChoices(), 'Ticket', 'type')
  71. ),
  72. ChoiceField::new('status')
  73. ->autocomplete()
  74. ->setChoices(
  75. $translatorAdmin->transChoices($this->get(TicketContainer::class)->getSolver()->getStatusChoices(), 'Ticket', 'status')
  76. )
  77. ->setTemplatePath('@LcSov/admin/ticket/field/status.html.twig')
  78. ->hideOnForm(),
  79. ];
  80. }
  81. public function configureActions(Actions $actions): Actions
  82. {
  83. $actions
  84. ->add(Crud::PAGE_INDEX, ActionDefinition::DETAIL)
  85. ->remove(Crud::PAGE_INDEX, ActionDefinition::EDIT);
  86. return parent::configureActions($actions);
  87. }
  88. public function new(AdminContext $context)
  89. {
  90. $adminUrlGenerator = $this->get(AdminUrlGenerator::class);
  91. $ticket = $this->createEntity($context->getEntity()->getFqcn());
  92. $form = $this->createForm(TicketFormType::class, $ticket);
  93. $form->handleRequest($context->getRequest());
  94. if ($form->isSubmitted() && $form->isValid()) {
  95. $ticket = $form->getData();
  96. $this->get(EntityManagerInterface::class)->create($ticket);
  97. $this->get(EntityManagerInterface::class)->flush();
  98. $url = $adminUrlGenerator
  99. ->setAction('index')
  100. ->generateUrl();
  101. return $this->redirect($url);
  102. }
  103. return $this->render(
  104. '@LcSov/admin/ticket/new.html.twig',
  105. [
  106. 'form' => $form->createView(),
  107. ]
  108. );
  109. }
  110. public function detail(AdminContext $context)
  111. {
  112. $adminUrlGenerator = $this->get(AdminUrlGenerator::class);
  113. $ticket = $context->getEntity()->getInstance();
  114. $url = $adminUrlGenerator
  115. ->setAction('ticketStatusAction')
  116. ->generateUrl();
  117. $formTicketStatus = $this->createForm(
  118. TicketStatusType::class,
  119. $ticket,
  120. [
  121. 'action' => $url,
  122. 'method' => 'POST',
  123. ]
  124. );
  125. $ticketMessage = $this->get(TicketMessageContainer::class)->getFactory()->create($ticket);
  126. $formAddTicketMessage = $this->createForm(TicketMessageFormType::class, $ticketMessage);
  127. $formAddTicketMessage->handleRequest($this->get(RequestStack::class)->getMainRequest());
  128. if ($formAddTicketMessage->isSubmitted() && $formAddTicketMessage->isValid()) {
  129. $ticketMessage = $formAddTicketMessage->getData();
  130. $ticketMessage->setTicket($ticket);
  131. $ticketMessage->setAnswerByAdmin(true);
  132. $this->get(EntityManagerInterface::class)->create($ticketMessage);
  133. $this->get(EntityManagerInterface::class)->flush();
  134. }
  135. return $this->render(
  136. '@LcSov/admin/ticket/detail.html.twig',
  137. [
  138. 'form_ticket_status' => $formTicketStatus->createView(),
  139. 'form_add_ticket_message' => $formAddTicketMessage->createView(),
  140. 'ticket' => $ticket,
  141. ]
  142. );
  143. }
  144. public function ticketStatusAction()
  145. {
  146. $request = $this->get('request')->getMasterRequest();
  147. $ticket = $request->attributes->get('easyadmin_context')->getEntity()->getInstance();
  148. $formTicketStatusForm = $this->createForm(TicketStatusType::class, $ticket);
  149. $formTicketStatusForm->handleRequest($request);
  150. $success = false;
  151. if ($formTicketStatusForm->isSubmitted() && $formTicketStatusForm->isValid()) {
  152. $this->get('em')->persist($ticket);
  153. $this->get('em')->flush();
  154. $success = true;
  155. }
  156. return new JsonResponse(['success' => $success]);
  157. }
  158. }