|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203 |
- <?php
-
- namespace Lc\SovBundle\Controller\Ticket;
-
- use EasyCorp\Bundle\EasyAdminBundle\Config\Action;
- use EasyCorp\Bundle\EasyAdminBundle\Config\Actions;
- use EasyCorp\Bundle\EasyAdminBundle\Config\Assets;
- use EasyCorp\Bundle\EasyAdminBundle\Config\Crud;
- use EasyCorp\Bundle\EasyAdminBundle\Context\AdminContext;
- use EasyCorp\Bundle\EasyAdminBundle\Field\AssociationField;
- use EasyCorp\Bundle\EasyAdminBundle\Field\ChoiceField;
- use EasyCorp\Bundle\EasyAdminBundle\Field\DateField;
- use EasyCorp\Bundle\EasyAdminBundle\Field\IntegerField;
- use EasyCorp\Bundle\EasyAdminBundle\Field\TextField;
- use EasyCorp\Bundle\EasyAdminBundle\Router\AdminUrlGenerator;
- use Lc\SovBundle\Factory\Ticket\TicketFactory;
- use Lc\SovBundle\Factory\Ticket\TicketFactoryInterface;
- use Lc\SovBundle\Factory\Ticket\TicketMessageFactoryInterface;
- use Lc\SovBundle\Form\Ticket\TicketFormType;
- use Lc\SovBundle\Form\Ticket\TicketMessageFormType;
- use Lc\SovBundle\Form\Ticket\TicketStatusType;
- use Lc\SovBundle\Model\Ticket\TicketInterface;
- use Lc\SovBundle\Controller\AbstractAdminController;
- use Lc\SovBundle\Model\Ticket\TicketModel;
- use Lc\SovBundle\Translation\TranslatorAdmin;
- use Symfony\Component\HttpFoundation\JsonResponse;
-
- class TicketAdminController extends AbstractAdminController
- {
- protected $ticketFactory;
- protected $ticketMessageFactory;
- protected $adminUrlGenerator;
- protected $em;
- protected $translatorAdmin;
-
- public function __construct(
- TicketFactoryInterface $ticketFactory,
- TicketMessageFactoryInterface $ticketMessageFactory,
- AdminUrlGenerator $adminUrlGenerator,
- TranslatorAdmin $translatorAdmin
- ) {
- $this->ticketFactory = $ticketFactory;
- $this->ticketMessageFactory = $ticketMessageFactory;
- $this->adminUrlGenerator = $adminUrlGenerator;
- $this->translatorAdmin = $translatorAdmin;
- }
-
- public static function getEntityFqcn(): string
- {
- return TicketInterface::class;
- }
-
- public function createEntity(string $entityFqcn)
- {
- $factory = new TicketFactory();
- return $factory->create();
- }
-
- public function configureAssets(Assets $assets): Assets
- {
- $assets = parent::configureAssets($assets);
-
- $assets->addWebpackEncoreEntry('sov-ticket');
-
- return $assets;
- }
-
- public function configureFields(string $pageName): iterable
- {
- return [
- IntegerField::new('id')
- ->hideOnForm(),
- DateField::new('createdAt')->setFormat('short')
- ->hideOnForm(),
- TextField::new('visitorFirstName')
- ->setTemplatePath('@LcSov/admin/ticket/field/username.html.twig')
- ->hideOnForm(),
- TextField::new('visitorEmail')
- ->setTemplatePath('@LcSov/admin/ticket/field/email.html.twig')
- ->hideOnForm(),
- AssociationField::new('user')
- ->hideOnIndex(),
- TextField::new('subject'),
- TextField::new('lastMessage')
- ->setTemplatePath('@LcSov/admin/ticket/field/lastmessage.html.twig')
- ->hideOnForm(),
- ChoiceField::new('type')
- ->autocomplete()
- ->setChoices(
- $this->translatorAdmin->transChoices(TicketModel::getTypeChoices(), 'Ticket', 'type')
- ),
- ChoiceField::new('status')
- ->autocomplete()
- ->setChoices(
- $this->translatorAdmin->transChoices(TicketModel::getStatusChoices(), 'Ticket', 'status')
-
- )
- ->setTemplatePath('@LcSov/admin/ticket/field/status.html.twig')
- ->hideOnForm(),
- ];
- }
-
- public function configureActions(Actions $actions): Actions
- {
- $actions
- ->add(Crud::PAGE_INDEX, Action::DETAIL)
- ->remove(Crud::PAGE_INDEX, Action::EDIT);
-
- return parent::configureActions($actions);
- }
-
- public function createEntityFromFactory()
- {
- return $this->ticketFactory->create();
- }
-
- public function new(AdminContext $context)
- {
- $ticket = $this->createEntityFromFactory();
-
- $form = $this->createForm(TicketFormType::class, $ticket);
-
- $form->handleRequest($context->getRequest());
- if ($form->isSubmitted() && $form->isValid()) {
- $ticket = $form->getData();
-
- $this->get('em')->persist($ticket);
- $this->get('em')->flush();
-
- $url = $this->adminUrlGenerator
- ->setAction('index')
- ->generateUrl();
-
- return $this->redirect($url);
- }
-
- return $this->render(
- '@LcSov/admin/ticket/new.html.twig',
- [
- 'form' => $form->createView(),
- ]
- );
- }
-
- public function detail(AdminContext $context)
- {
- $ticket = $context->getEntity()->getInstance();
-
- $url = $this->adminUrlGenerator
- ->setAction('ticketStatusAction')
- ->generateUrl();
-
- $formTicketStatus = $this->createForm(
- TicketStatusType::class,
- $ticket,
- [
- 'action' => $url,
- 'method' => 'POST',
- ]
- );
-
- $ticketMessage = $this->ticketMessageFactory->create();
- $formAddTicketMessage = $this->createForm(TicketMessageFormType::class, $ticketMessage);
- $formAddTicketMessage->handleRequest($this->get('request')->getMasterRequest());
-
- if ($formAddTicketMessage->isSubmitted() && $formAddTicketMessage->isValid()) {
- $ticketMessage = $formAddTicketMessage->getData();
- $ticketMessage->setTicket($ticket);
- $ticketMessage->setAnswerByAdmin(true);
- $this->get('em')->persist($ticketMessage);
- $this->get('em')->flush();
- }
-
- return $this->render(
- '@LcSov/admin/ticket/detail.html.twig',
- [
- 'form_ticket_status' => $formTicketStatus->createView(),
- 'form_add_ticket_message' => $formAddTicketMessage->createView(),
- 'ticket' => $ticket,
- ]
- );
- }
-
-
- public function ticketStatusAction()
- {
- $request = $this->get('request')->getMasterRequest();
- $ticket = $request->attributes->get('easyadmin_context')->getEntity()->getInstance();
-
- $formTicketStatusForm = $this->createForm(TicketStatusType::class, $ticket);
- $formTicketStatusForm->handleRequest($request);
-
- $success = false;
- if ($formTicketStatusForm->isSubmitted() && $formTicketStatusForm->isValid()) {
- $this->get('em')->persist($ticket);
- $this->get('em')->flush();
- $success = true;
- }
-
- return new JsonResponse(['success' => $success]);
- }
-
- }
|