vendor/uvdesk/mailbox-component/Controller/MailboxChannelXHR.php line 39

Open in your IDE?
  1. <?php
  2. namespace Webkul\UVDesk\MailboxBundle\Controller;
  3. use Symfony\Component\HttpFoundation\Request;
  4. use Symfony\Component\HttpFoundation\Response;
  5. use Symfony\Component\HttpFoundation\JsonResponse;
  6. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  7. use Webkul\UVDesk\MailboxBundle\Utils\MailboxConfiguration;
  8. use Webkul\UVDesk\MailboxBundle\Services\MailboxService;
  9. use Symfony\Contracts\Translation\TranslatorInterface;
  10. use Symfony\Component\HttpKernel\KernelInterface;
  11. class MailboxChannelXHR extends AbstractController
  12. {
  13.     private $mailboxService;
  14.     private $translator;
  15.     private $kernel;
  16.     public function __construct(KernelInterface $kernelMailboxService $mailboxServiceTranslatorInterface $translator)
  17.     {
  18.         $this->mailboxService $mailboxService;
  19.         $this->translator $translator;
  20.         $this->kernel $kernel;
  21.     }
  22.     public function processRawContentMail(Request $request)
  23.     {
  24.         $rawEmail file_get_contents($this->kernel->getProjectDir(). "/rawEmailContent.txt");
  25.         if ($rawEmail != false &&  !empty($rawEmail)) {
  26.            $this->mailboxService->processMail($rawEmail);
  27.         }else{
  28.             dump("Empty Text file not allow");
  29.         } 
  30.         exit(0);
  31.     }
  32.     public function processMailXHR(Request $request)
  33.     {
  34.         if ("POST" != $request->getMethod()) {
  35.             return new JsonResponse([
  36.                 'success' => false
  37.                 'message' => 'Request not supported.'
  38.             ], 500);
  39.         } else if (null == $request->get('email')) {
  40.             return new JsonResponse([
  41.                 'success' => false
  42.                 'message' => 'Missing required email data in request content.'
  43.             ], 500);
  44.         }
  45.         try {
  46.             $processedThread $this->mailboxService->processMail($request->get('email'));
  47.         } catch (\Exception $e) {
  48.             return new JsonResponse([
  49.                 'success' => false
  50.                 'message' => $e->getMessage()
  51.             ], 500);
  52.         }
  53.         $responseMessage $processedThread['message'];
  54.         if (!empty($processedThread['content']['from'])) {
  55.             $responseMessage "Received email from <info>" $processedThread['content']['from']. "</info>. " $responseMessage;
  56.         }
  57.         if (!empty($processedThread['content']['ticket']) && !empty($processedThread['content']['thread'])) {
  58.             $responseMessage .= " <comment>[tickets/" $processedThread['content']['ticket'] . "/#" $processedThread['content']['thread'] . "]</comment>";
  59.         } else if (!empty($processedThread['content']['ticket'])) {
  60.             $responseMessage .= " <comment>[tickets/" $processedThread['content']['ticket'] . "]</comment>";
  61.         }
  62.         return new JsonResponse([
  63.             'success' => true
  64.             'message' => $responseMessage
  65.         ]);
  66.     }
  67.     
  68.     public function loadMailboxesXHR(Request $request)
  69.     {
  70.         $collection array_map(function ($mailbox) {
  71.             return [
  72.                 'id' => $mailbox->getId(),
  73.                 'name' => $mailbox->getName(),
  74.                 'isEnabled' => $mailbox->getIsEnabled(),
  75.                 'isDeleted' => $mailbox->getIsDeleted() ? $mailbox->getIsDeleted() : false,
  76.             ];
  77.         }, $this->mailboxService->parseMailboxConfigurations()->getMailboxes());
  78.         return new JsonResponse($collection ?? []);
  79.     }
  80.     public function removeMailboxConfiguration($idRequest $request)
  81.     {
  82.         $mailboxService $this->mailboxService;
  83.         $existingMailboxConfiguration $mailboxService->parseMailboxConfigurations();
  84.         foreach ($existingMailboxConfiguration->getMailboxes() as $configuration) {
  85.             if ($configuration->getId() == $id) {
  86.                 $mailbox $configuration;
  87.                 break;
  88.             }
  89.         }
  90.         if (empty($mailbox)) {
  91.             return new JsonResponse([
  92.                 'alertClass' => 'danger',
  93.                 'alertMessage' => "No mailbox found with id '$id'.",
  94.             ], 404);
  95.         }
  96.         $mailboxConfiguration = new MailboxConfiguration();
  97.         foreach ($existingMailboxConfiguration->getMailboxes() as $configuration) {
  98.             if ($configuration->getId() == $id) {
  99.                 continue;
  100.             }
  101.             $mailboxConfiguration->addMailbox($configuration);
  102.         }
  103.         file_put_contents($mailboxService->getPathToConfigurationFile(), (string) $mailboxConfiguration);
  104.         return new JsonResponse([
  105.             'alertClass' => 'success',
  106.             'alertMessage' => $this->translator->trans('Mailbox configuration removed successfully.'),
  107.         ]);
  108.     }
  109. }