<?php
namespace AdminBundle\Admin\Ticket;
use DateTime;
use Sonata\Form\Validator\ErrorElement;
use CoreBundle\Services\Jira\JiraService;
use Exception;
use AdminBundle\Admin\BaseAdmin;
use AdminBundle\Form\Type\FileStrType;
use CoreBundle\Entity\Ticket;
use CoreBundle\Entity\TicketFile;
use FOS\CKEditorBundle\Form\Type\CKEditorType;
use Sonata\AdminBundle\Form\FormMapper;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\DateType;
use Symfony\Component\Form\Extension\Core\Type\FileType;
use Symfony\Component\Form\FormError;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Mime\Address;
use Symfony\Component\Mime\Email;
class TicketAdmin extends BaseAdmin
{
protected JiraService $jiraService;
protected MailerInterface $mailer;
protected \CoreBundle\Model\Ticket $ticketModel;
public function setExtraServices(JiraService $jiraService, MailerInterface $mailer, \CoreBundle\Model\Ticket $ticket)
{
$this->jiraService = $jiraService;
$this->mailer = $mailer;
$this->ticketModel = $ticket;
}
/**
* @param ErrorElement $errorElement
* @param Ticket $object
*/
public function validate(ErrorElement $errorElement, $object)
{
if($object->getWorkType() == 18 && (!$object->getSymbolsCount() OR !is_numeric($object->getSymbolsCount()))) {
$this->getForm()->addError(new FormError("Необхідно вказати кількість символів"));
}
}
/**
* @param Ticket $object
*/
public function prePersist($object): void
{
$User = $this->getUser();
$Dealer = $User->getDealer();
if ($Dealer && !$object->getDealer()) {
$object->setDealer($Dealer);
}
$object->setDateCreate(new DateTime());
$object->setCreator($User);
$object->setState(1);
$this->updateObject($object);
$this->sendCreateNotify($object);
}
/** */
public function postPersist($object): void
{
if (in_array($object->getWorkType(), \CoreBundle\Model\Ticket::$worksForJira)) {
$issueId = $this->jiraService->createIssue($object);
$object->setIssueId($issueId);
$this->em->persist($object);
$this->em->flush();
}
parent::postPersist($object);
}
/**
* @param Ticket $object
*/
public function preUpdate($object): void
{
$id = $object->getId();
if ($id) {
$state = $this->em->getRepository(Ticket::class)->createQueryBuilder('t')
->select('t.state')
->where('t.id = :id')
->setParameter('id', $object->getId())
->getQuery()
->getSingleScalarResult();
if ($state != $object->getState()) {
$this->sendChangeStateNotify($object);
}
}
$this->updateObject($object);
parent::preUpdate($object);
}
public function postUpdate($object): void
{
if (in_array($object->getWorkType(), \CoreBundle\Model\Ticket::$worksForJira)) {
if ($object->getIssueId() && $this->jiraService->checkIfIssueExist($object->getIssueId())) {
$this->jiraService->updateIssue($object);
} else {
$issueId = $this->jiraService->createIssue($object);
$object->setIssueId($issueId);
$this->em->persist($object);
$this->em->flush();
}
} elseif ($object->getIssueId()) {
$jiraService->removeIssue($object->getIssueId());
}
parent::postUpdate($object);
}
public function preRemove($object): void
{
if ($object->getIssueId()) {
$this->jiraService->removeIssue($object->getIssueId());
}
parent::preRemove($object);
}
/**
* @param Ticket $object
*/
private function sendCreateNotify($object)
{
$mailBody = 'Створено нове завдання <b>' . $object->getTitle() . '</b>';
$message = (new Email())
->subject('Створено нове завдання.')
->from(new Address('info@vidi.ua', 'TicketNotify'))
->html($mailBody)
->to('web-leads@vidi.ua');
$this->mailer->send($message);
}
/**
* @param Ticket $object
*/
private function sendChangeStateNotify($object)
{
$this->ticketModel->sendChangeStatusNotify($object);
}
/**
* @param Ticket $object
*/
private function updateObject($object)
{
foreach ($this->getForm()->get('filesAdd')->getData() as $uploadedFile) {
$fileName = $this->upload($uploadedFile);
if ($fileName) {
$ticketFile = new TicketFile();
$ticketFile->setFile($fileName);
$ticketFile->setTicket($object);
$object->addFile($ticketFile);
}
}
}
/**
* Manages the copying of the file to the relevant place on the server
*/
public function upload(UploadedFile $file): string
{
$name = $file->getClientOriginalName();
$rootDir = $this->parameterBag->get('kernel.project_dir');
$file->move(
$rootDir . '/public/uploads/files',
$name
);
return $name;
}
/**
* @param FormMapper $formMapper
* @throws Exception
*/
protected function configureFormFields(FormMapper $formMapper): void
{
$this->checkByRole(
[
'ROLE_SUPER_ADMIN',
'ROLE_TICKET',
'ROLE_CONTENT_MANAGER',
'ROLE_DC_MANAGER',
'ROLE_INSURANCE_ADMIN',
'ROLE_AUTOMARKET_MANAGER'
]
);
$User = $this->security->getUser();
$Dealer = $User->getDealer();
$formMapper
->tab('Основна інформація')
->with('Контент', ['class' => 'col-lg-6'])
->add('title', null, ['label' => 'Назва задачі', 'required' => true])
->add(
'content',
CKEditorType::class,
['label' => 'Описание задачи', 'config_name' => 'default', 'required' => false]
)
->end()
->with('Дод. інформація', ['class' => 'col-lg-6'])
->add(
'task_type',
ChoiceType::class,
['label' => 'Тип робіт', 'choices' => array_flip(\CoreBundle\Model\Ticket::$type)]
)
->add(
'work_type',
ChoiceType::class,
['label' => 'Вид робіт', 'choices' => array_flip(\CoreBundle\Model\Ticket::$works)]
)
->add(
'priority',
ChoiceType::class,
['label' => 'Пріоритет', 'choices' => array_flip(\CoreBundle\Model\Ticket::$priority)]
)
->add('date_finish', DateType::class, [
'label' => 'Строк виконання',
'widget' => 'single_text',
])
->add('symbols_count', null, [
'label' => 'Кількість символів',
]);
if ($User->hasRole('ROLE_SUPER_ADMIN')) {
$formMapper->add('state', ChoiceType::class, ['label' => 'Статус', 'choices' => array_flip(\CoreBundle\Model\Ticket::$state)]);
}
if (!$Dealer || $User->hasRole('ROLE_SUPER_ADMIN') || $User->hasRole('ROLE_TICKET') || $User->hasRole('ROLE_CONTENT_MANAGER')) {
$formMapper->add('dealer', null, ['label' => 'Дилер']);
}
$formMapper
->add('files', FileStrType::class, [
'label' => false,
'vidi_action' => [
'delete' => 'ticket_file_delete',
'download' => 'ticket_file_download',
]
])
->add('filesAdd', FileType::class, [
'label' => 'Добавить файлы',
'mapped' => false,
'multiple' => true,
'required' => false
])
->end()
->end();
}
protected function configure(): void
{
parent::configure(); // TODO: Change the autogenerated stub
$this->setTemplate('list', '@Admin/CRUD/Ticket/ticket-list.html.twig');
$this->setTemplate('show', '@Admin/CRUD/Ticket/ticket-show.html.twig');
}
}