You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

203 lines
6.8KB

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