src/AdminBundle/Admin/Ticket/TicketAdmin.php line 24

Open in your IDE?
  1. <?php
  2. namespace AdminBundle\Admin\Ticket;
  3. use DateTime;
  4. use Sonata\Form\Validator\ErrorElement;
  5. use CoreBundle\Services\Jira\JiraService;
  6. use Exception;
  7. use AdminBundle\Admin\BaseAdmin;
  8. use AdminBundle\Form\Type\FileStrType;
  9. use CoreBundle\Entity\Ticket;
  10. use CoreBundle\Entity\TicketFile;
  11. use FOS\CKEditorBundle\Form\Type\CKEditorType;
  12. use Sonata\AdminBundle\Form\FormMapper;
  13. use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
  14. use Symfony\Component\Form\Extension\Core\Type\DateType;
  15. use Symfony\Component\Form\Extension\Core\Type\FileType;
  16. use Symfony\Component\Form\FormError;
  17. use Symfony\Component\HttpFoundation\File\UploadedFile;
  18. use Symfony\Component\Mailer\MailerInterface;
  19. use Symfony\Component\Mime\Address;
  20. use Symfony\Component\Mime\Email;
  21. class TicketAdmin extends BaseAdmin
  22. {
  23.     protected JiraService $jiraService;
  24.     protected MailerInterface $mailer;
  25.     protected \CoreBundle\Model\Ticket $ticketModel;
  26.     public function setExtraServices(JiraService $jiraServiceMailerInterface $mailer\CoreBundle\Model\Ticket $ticket)
  27.     {
  28.         $this->jiraService $jiraService;
  29.         $this->mailer $mailer;
  30.         $this->ticketModel $ticket;
  31.     }
  32.     /**
  33.      * @param ErrorElement $errorElement
  34.      * @param Ticket $object
  35.      */
  36.     public function validate(ErrorElement $errorElement$object)
  37.     {
  38.         if($object->getWorkType() == 18 && (!$object->getSymbolsCount() OR !is_numeric($object->getSymbolsCount()))) {
  39.             $this->getForm()->addError(new FormError("Необхідно вказати кількість символів"));
  40.         }
  41.     }
  42.     /**
  43.      * @param Ticket $object
  44.      */
  45.     public function prePersist($object): void
  46.     {
  47.         $User $this->getUser();
  48.         $Dealer $User->getDealer();
  49.         if ($Dealer && !$object->getDealer()) {
  50.             $object->setDealer($Dealer);
  51.         }
  52.         $object->setDateCreate(new DateTime());
  53.         $object->setCreator($User);
  54.         $object->setState(1);
  55.         $this->updateObject($object);
  56.         $this->sendCreateNotify($object);
  57.     }
  58.     /**  */
  59.     public function postPersist($object): void
  60.     {
  61.         if (in_array($object->getWorkType(), \CoreBundle\Model\Ticket::$worksForJira)) {
  62.             $issueId $this->jiraService->createIssue($object);
  63.             $object->setIssueId($issueId);
  64.             $this->em->persist($object);
  65.             $this->em->flush();
  66.         }
  67.         parent::postPersist($object);
  68.     }
  69.     /**
  70.      * @param Ticket $object
  71.      */
  72.     public function preUpdate($object): void
  73.     {
  74.         $id $object->getId();
  75.         if ($id) {
  76.             $state $this->em->getRepository(Ticket::class)->createQueryBuilder('t')
  77.                 ->select('t.state')
  78.                 ->where('t.id = :id')
  79.                 ->setParameter('id'$object->getId())
  80.                 ->getQuery()
  81.                 ->getSingleScalarResult();
  82.             if ($state != $object->getState()) {
  83.                 $this->sendChangeStateNotify($object);
  84.             }
  85.         }
  86.         $this->updateObject($object);
  87.         parent::preUpdate($object);
  88.     }
  89.     public function postUpdate($object): void
  90.     {
  91.         if (in_array($object->getWorkType(), \CoreBundle\Model\Ticket::$worksForJira)) {
  92.             if ($object->getIssueId() && $this->jiraService->checkIfIssueExist($object->getIssueId())) {
  93.                 $this->jiraService->updateIssue($object);
  94.             } else {
  95.                 $issueId $this->jiraService->createIssue($object);
  96.                 $object->setIssueId($issueId);
  97.                 $this->em->persist($object);
  98.                 $this->em->flush();
  99.             }
  100.         } elseif ($object->getIssueId()) {
  101.             $jiraService->removeIssue($object->getIssueId());
  102.         }
  103.         parent::postUpdate($object);
  104.     }
  105.     public function preRemove($object): void
  106.     {
  107.         if ($object->getIssueId()) {
  108.             $this->jiraService->removeIssue($object->getIssueId());
  109.         }
  110.         parent::preRemove($object);
  111.     }
  112.     /**
  113.      * @param Ticket $object
  114.      */
  115.     private function sendCreateNotify($object)
  116.     {
  117.         $mailBody 'Створено нове завдання <b>' $object->getTitle() . '</b>';
  118.         $message = (new Email())
  119.             ->subject('Створено нове завдання.')
  120.             ->from(new Address('info@vidi.ua''TicketNotify'))
  121.             ->html($mailBody)
  122.             ->to('web-leads@vidi.ua');
  123.         $this->mailer->send($message);
  124.     }
  125.     /**
  126.      * @param Ticket $object
  127.      */
  128.     private function sendChangeStateNotify($object)
  129.     {
  130.         $this->ticketModel->sendChangeStatusNotify($object);
  131.     }
  132.     /**
  133.      * @param Ticket $object
  134.      */
  135.     private function updateObject($object)
  136.     {
  137.         foreach ($this->getForm()->get('filesAdd')->getData() as $uploadedFile) {
  138.             $fileName $this->upload($uploadedFile);
  139.             if ($fileName) {
  140.                 $ticketFile = new TicketFile();
  141.                 $ticketFile->setFile($fileName);
  142.                 $ticketFile->setTicket($object);
  143.                 $object->addFile($ticketFile);
  144.             }
  145.         }
  146.     }
  147.     /**
  148.      * Manages the copying of the file to the relevant place on the server
  149.      */
  150.     public function upload(UploadedFile $file): string
  151.     {
  152.         $name $file->getClientOriginalName();
  153.         $rootDir $this->parameterBag->get('kernel.project_dir');
  154.         $file->move(
  155.             $rootDir '/public/uploads/files',
  156.             $name
  157.         );
  158.         return $name;
  159.     }
  160.     /**
  161.      * @param FormMapper $formMapper
  162.      * @throws Exception
  163.      */
  164.     protected function configureFormFields(FormMapper $formMapper): void
  165.     {
  166.         $this->checkByRole(
  167.             [
  168.                 'ROLE_SUPER_ADMIN',
  169.                 'ROLE_TICKET',
  170.                 'ROLE_CONTENT_MANAGER',
  171.                 'ROLE_DC_MANAGER',
  172.                 'ROLE_INSURANCE_ADMIN',
  173.                 'ROLE_AUTOMARKET_MANAGER'
  174.             ]
  175.         );
  176.         $User $this->security->getUser();
  177.         $Dealer $User->getDealer();
  178.         $formMapper
  179.             ->tab('Основна інформація')
  180.             ->with('Контент', ['class' => 'col-lg-6'])
  181.             ->add('title'null, ['label' => 'Назва задачі''required' => true])
  182.             ->add(
  183.                 'content',
  184.                 CKEditorType::class,
  185.                 ['label' => 'Описание задачи''config_name' => 'default''required' => false]
  186.             )
  187.             ->end()
  188.             ->with('Дод. інформація', ['class' => 'col-lg-6'])
  189.             ->add(
  190.                 'task_type',
  191.                 ChoiceType::class,
  192.                 ['label' => 'Тип робіт''choices' => array_flip(\CoreBundle\Model\Ticket::$type)]
  193.             )
  194.             ->add(
  195.                 'work_type',
  196.                 ChoiceType::class,
  197.                 ['label' => 'Вид робіт''choices' => array_flip(\CoreBundle\Model\Ticket::$works)]
  198.             )
  199.             ->add(
  200.                 'priority',
  201.                 ChoiceType::class,
  202.                 ['label' => 'Пріоритет''choices' => array_flip(\CoreBundle\Model\Ticket::$priority)]
  203.             )
  204.             ->add('date_finish'DateType::class, [
  205.                 'label' => 'Строк виконання',
  206.                 'widget' => 'single_text',
  207.             ])
  208.             ->add('symbols_count'null, [
  209.                 'label' => 'Кількість символів',
  210.             ]);
  211.         if ($User->hasRole('ROLE_SUPER_ADMIN')) {
  212.             $formMapper->add('state'ChoiceType::class, ['label' => 'Статус''choices' => array_flip(\CoreBundle\Model\Ticket::$state)]);
  213.         }
  214.         if (!$Dealer || $User->hasRole('ROLE_SUPER_ADMIN') || $User->hasRole('ROLE_TICKET') || $User->hasRole('ROLE_CONTENT_MANAGER')) {
  215.             $formMapper->add('dealer'null, ['label' => 'Дилер']);
  216.         }
  217.         $formMapper
  218.             ->add('files'FileStrType::class, [
  219.                 'label' => false,
  220.                 'vidi_action' => [
  221.                     'delete' => 'ticket_file_delete',
  222.                     'download' => 'ticket_file_download',
  223.                 ]
  224.             ])
  225.             ->add('filesAdd'FileType::class, [
  226.                 'label' => 'Добавить файлы',
  227.                 'mapped' => false,
  228.                 'multiple' => true,
  229.                 'required' => false
  230.             ])
  231.             ->end()
  232.             ->end();
  233.     }
  234.     protected function configure(): void
  235.     {
  236.         parent::configure(); // TODO: Change the autogenerated stub
  237.         $this->setTemplate('list''@Admin/CRUD/Ticket/ticket-list.html.twig');
  238.         $this->setTemplate('show''@Admin/CRUD/Ticket/ticket-show.html.twig');
  239.     }
  240. }