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.

198 lines
6.5KB

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