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.

204 line
6.7KB

  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\TicketFactory;
  15. use Lc\SovBundle\Factory\Ticket\TicketFactoryInterface;
  16. use Lc\SovBundle\Factory\Ticket\TicketMessageFactoryInterface;
  17. use Lc\SovBundle\Form\Ticket\TicketFormType;
  18. use Lc\SovBundle\Form\Ticket\TicketMessageFormType;
  19. use Lc\SovBundle\Form\Ticket\TicketStatusType;
  20. use Lc\SovBundle\Model\Ticket\TicketInterface;
  21. use Lc\SovBundle\Controller\AbstractAdminController;
  22. use Lc\SovBundle\Model\Ticket\TicketModel;
  23. use Lc\SovBundle\Translation\TranslatorAdmin;
  24. use Symfony\Component\HttpFoundation\JsonResponse;
  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. TicketMessageFactoryInterface $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 createEntity(string $entityFqcn)
  48. {
  49. $factory = new TicketFactory();
  50. return $factory->create();
  51. }
  52. public function configureAssets(Assets $assets): Assets
  53. {
  54. $assets = parent::configureAssets($assets);
  55. $assets->addWebpackEncoreEntry('sov-ticket');
  56. return $assets;
  57. }
  58. public function configureFields(string $pageName): iterable
  59. {
  60. return [
  61. IntegerField::new('id')
  62. ->hideOnForm(),
  63. DateField::new('createdAt')->setFormat('short')
  64. ->hideOnForm(),
  65. TextField::new('visitorFirstName')
  66. ->setTemplatePath('@LcSov/admin/ticket/field/username.html.twig')
  67. ->hideOnForm(),
  68. TextField::new('visitorEmail')
  69. ->setTemplatePath('@LcSov/admin/ticket/field/email.html.twig')
  70. ->hideOnForm(),
  71. AssociationField::new('user')
  72. ->hideOnIndex(),
  73. TextField::new('subject'),
  74. TextField::new('lastMessage')
  75. ->setTemplatePath('@LcSov/admin/ticket/field/lastmessage.html.twig')
  76. ->hideOnForm(),
  77. ChoiceField::new('type')
  78. ->autocomplete()
  79. ->setChoices(
  80. $this->translatorAdmin->transChoices(TicketModel::getTypeChoices(), 'Ticket', 'type')
  81. ),
  82. ChoiceField::new('status')
  83. ->autocomplete()
  84. ->setChoices(
  85. $this->translatorAdmin->transChoices(TicketModel::getStatusChoices(), 'Ticket', 'status')
  86. )
  87. ->setTemplatePath('@LcSov/admin/ticket/field/status.html.twig')
  88. ->hideOnForm(),
  89. ];
  90. }
  91. public function configureActions(Actions $actions): Actions
  92. {
  93. $actions
  94. ->add(Crud::PAGE_INDEX, Action::DETAIL)
  95. ->remove(Crud::PAGE_INDEX, Action::EDIT);
  96. return parent::configureActions($actions);
  97. }
  98. public function createEntityFromFactory()
  99. {
  100. return $this->ticketFactory->create();
  101. }
  102. public function new(AdminContext $context)
  103. {
  104. $ticket = $this->createEntityFromFactory();
  105. $form = $this->createForm(TicketFormType::class, $ticket);
  106. $form->handleRequest($context->getRequest());
  107. if ($form->isSubmitted() && $form->isValid()) {
  108. $ticket = $form->getData();
  109. $this->get('em')->persist($ticket);
  110. $this->get('em')->flush();
  111. $url = $this->adminUrlGenerator
  112. ->setAction('index')
  113. ->generateUrl();
  114. return $this->redirect($url);
  115. }
  116. return $this->render(
  117. '@LcSov/admin/ticket/new.html.twig',
  118. [
  119. 'form' => $form->createView(),
  120. ]
  121. );
  122. }
  123. public function detail(AdminContext $context)
  124. {
  125. $ticket = $context->getEntity()->getInstance();
  126. $url = $this->adminUrlGenerator
  127. ->setAction('ticketStatusAction')
  128. ->generateUrl();
  129. $formTicketStatus = $this->createForm(
  130. TicketStatusType::class,
  131. $ticket,
  132. [
  133. 'action' => $url,
  134. 'method' => 'POST',
  135. ]
  136. );
  137. $ticketMessage = $this->ticketMessageFactory->create();
  138. $formAddTicketMessage = $this->createForm(TicketMessageFormType::class, $ticketMessage);
  139. $formAddTicketMessage->handleRequest($this->get('request')->getMasterRequest());
  140. if ($formAddTicketMessage->isSubmitted() && $formAddTicketMessage->isValid()) {
  141. $ticketMessage = $formAddTicketMessage->getData();
  142. $ticketMessage->setTicket($ticket);
  143. $ticketMessage->setAnswerByAdmin(true);
  144. $this->get('em')->persist($ticketMessage);
  145. $this->get('em')->flush();
  146. }
  147. return $this->render(
  148. '@LcSov/admin/ticket/detail.html.twig',
  149. [
  150. 'form_ticket_status' => $formTicketStatus->createView(),
  151. 'form_add_ticket_message' => $formAddTicketMessage->createView(),
  152. 'ticket' => $ticket,
  153. ]
  154. );
  155. }
  156. public function ticketStatusAction()
  157. {
  158. $request = $this->get('request')->getMasterRequest();
  159. $ticket = $request->attributes->get('easyadmin_context')->getEntity()->getInstance();
  160. $formTicketStatusForm = $this->createForm(TicketStatusType::class, $ticket);
  161. $formTicketStatusForm->handleRequest($request);
  162. $success = false;
  163. if ($formTicketStatusForm->isSubmitted() && $formTicketStatusForm->isValid()) {
  164. $this->get('em')->persist($ticket);
  165. $this->get('em')->flush();
  166. $success = true;
  167. }
  168. return new JsonResponse(['success' => $success]);
  169. }
  170. }