Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/1.0' into HEAD
Browse files Browse the repository at this point in the history
  • Loading branch information
Sanjeev Papnoi committed Apr 2, 2021
2 parents 23b90e8 + 5f69080 commit 0515b76
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 38 deletions.
45 changes: 37 additions & 8 deletions Controller/MailboxChannel.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,45 @@
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Webkul\UVDesk\MailboxBundle\Utils\Mailbox\Mailbox;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Webkul\UVDesk\MailboxBundle\Utils\MailboxConfiguration;
use Webkul\UVDesk\MailboxBundle\Utils\Imap\Configuration as ImapConfiguration;
use Webkul\UVDesk\MailboxBundle\Services\MailboxService;
use Symfony\Component\Translation\TranslatorInterface;
use Webkul\UVDesk\CoreFrameworkBundle\SwiftMailer\SwiftMailer as SwiftMailerService;
use Webkul\UVDesk\CoreFrameworkBundle\Services\UserService;

class MailboxChannel extends Controller
class MailboxChannel extends AbstractController
{
private $mailboxService;
private $translator;
private $swiftMailer;
private $userService;

public function __construct(UserService $userService, MailboxService $mailboxService, TranslatorInterface $translator, SwiftMailerService $swiftMailer)
{
$this->userService = $userService;
$this->mailboxService = $mailboxService;
$this->translator = $translator;
$this->swiftMailer = $swiftMailer;
}

public function loadMailboxes()
{
if (!$this->userService->isAccessAuthorized('ROLE_ADMIN')) {
return $this->redirect($this->generateUrl('helpdesk_member_dashboard'));
}

return $this->render('@UVDeskMailbox//listConfigurations.html.twig');
}

public function createMailboxConfiguration(Request $request)
{
$swiftmailerConfigurationCollection = $this->get('swiftmailer.service')->parseSwiftMailerConfigurations();
if (!$this->userService->isAccessAuthorized('ROLE_ADMIN')) {
return $this->redirect($this->generateUrl('helpdesk_member_dashboard'));
}

$swiftmailerConfigurationCollection = $this->swiftMailer->parseSwiftMailerConfigurations();

if ($request->getMethod() == 'POST') {
$params = $request->request->all();
Expand All @@ -42,7 +67,7 @@ public function createMailboxConfiguration(Request $request)
}

if (!empty($imapConfiguration) && !empty($swiftmailerConfiguration)) {
$mailboxService = $this->get('uvdesk.mailbox');
$mailboxService = $this->mailboxService;
$mailboxConfiguration = $mailboxService->parseMailboxConfigurations();

($mailbox = new Mailbox(!empty($params['id']) ? $params['id'] : null))
Expand All @@ -55,7 +80,7 @@ public function createMailboxConfiguration(Request $request)

file_put_contents($mailboxService->getPathToConfigurationFile(), (string) $mailboxConfiguration);

$this->addFlash('success', $this->get('translator')->trans('Mailbox successfully created.'));
$this->addFlash('success', $this->translator->trans('Mailbox successfully created.'));
return new RedirectResponse($this->generateUrl('helpdesk_member_mailbox_settings'));
}
}
Expand All @@ -67,9 +92,13 @@ public function createMailboxConfiguration(Request $request)

public function updateMailboxConfiguration($id, Request $request)
{
$mailboxService = $this->get('uvdesk.mailbox');
if (!$this->userService->isAccessAuthorized('ROLE_ADMIN')) {
return $this->redirect($this->generateUrl('helpdesk_member_dashboard'));
}

$mailboxService = $this->mailboxService;
$existingMailboxConfiguration = $mailboxService->parseMailboxConfigurations();
$swiftmailerConfigurationCollection = $this->get('swiftmailer.service')->parseSwiftMailerConfigurations();
$swiftmailerConfigurationCollection = $this->swiftMailer->parseSwiftMailerConfigurations();

foreach ($existingMailboxConfiguration->getMailboxes() as $configuration) {
if ($configuration->getId() == $id) {
Expand Down Expand Up @@ -129,7 +158,7 @@ public function updateMailboxConfiguration($id, Request $request)

file_put_contents($mailboxService->getPathToConfigurationFile(), (string) $mailboxConfiguration);

$this->addFlash('success', $this->get('translator')->trans('Mailbox successfully updated.'));
$this->addFlash('success', $this->translator->trans('Mailbox successfully updated.'));

return new RedirectResponse($this->generateUrl('helpdesk_member_mailbox_settings'));
}
Expand Down
23 changes: 17 additions & 6 deletions Controller/MailboxChannelXHR.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,30 @@
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Webkul\UVDesk\MailboxBundle\Utils\MailboxConfiguration;
use Webkul\UVDesk\MailboxBundle\Services\MailboxService;
use Symfony\Component\Translation\TranslatorInterface;

class MailboxChannelXHR extends Controller
class MailboxChannelXHR extends AbstractController
{
private $mailboxService;
private $translator;

public function __construct(MailboxService $mailboxService, TranslatorInterface $translator)
{
$this->mailboxService = $mailboxService;
$this->translator = $translator;
}

public function processMailXHR(Request $request)
{
// Return HTTP_OK Response
$response = new Response(Response::HTTP_OK);
$response->send();

if ("POST" == $request->getMethod() && null != $request->get('email')) {
$this->get('uvdesk.mailbox')->processMail($request->get('email'));
$this->mailboxService->processMail($request->get('email'));
}

exit(0);
Expand All @@ -31,14 +42,14 @@ public function loadMailboxesXHR(Request $request)
'name' => $mailbox->getName(),
'isEnabled' => $mailbox->getIsEnabled(),
];
}, $this->get('uvdesk.mailbox')->parseMailboxConfigurations()->getMailboxes());
}, $this->mailboxService->parseMailboxConfigurations()->getMailboxes());

return new JsonResponse($collection ?? []);
}

public function removeMailboxConfiguration($id, Request $request)
{
$mailboxService = $this->get('uvdesk.mailbox');
$mailboxService = $this->mailboxService;
$existingMailboxConfiguration = $mailboxService->parseMailboxConfigurations();

foreach ($existingMailboxConfiguration->getMailboxes() as $configuration) {
Expand Down Expand Up @@ -70,7 +81,7 @@ public function removeMailboxConfiguration($id, Request $request)

return new JsonResponse([
'alertClass' => 'success',
'alertMessage' => $this->get('translator')->trans('Mailbox configuration removed successfully.'),
'alertMessage' => $this->translator->trans('Mailbox configuration removed successfully.'),
]);
}
}
16 changes: 11 additions & 5 deletions EventListener/Swiftmailer.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,22 @@
use Symfony\Component\DependencyInjection\ContainerInterface;
use Webkul\UVDesk\CoreFrameworkBundle\SwiftMailer\Event\ConfigurationRemovedEvent;
use Webkul\UVDesk\CoreFrameworkBundle\SwiftMailer\Event\ConfigurationUpdatedEvent;
use Webkul\UVDesk\CoreFrameworkBundle\SwiftMailer\SwiftMailer as SwiftMailerService;
use Webkul\UVDesk\MailboxBundle\Services\MailboxService;

class Swiftmailer
{
protected $container;
protected $requestStack;
protected $swiftMailer;
private $mailboxService;

public final function __construct(ContainerInterface $container, RequestStack $requestStack)
public final function __construct(ContainerInterface $container, RequestStack $requestStack, SwiftMailerService $swiftMailer, MailboxService $mailboxService)
{
$this->container = $container;
$this->requestStack = $requestStack;
$this->swiftMailer = $swiftMailer;
$this->mailboxService = $mailboxService;
}

public function onSwiftMailerConfigurationUpdated(ConfigurationUpdatedEvent $event)
Expand All @@ -32,7 +38,7 @@ public function onSwiftMailerConfigurationUpdated(ConfigurationUpdatedEvent $eve
return;
}

$mailboxConfiguration = $this->container->get('uvdesk.mailbox')->parseMailboxConfigurations(true);
$mailboxConfiguration = $this->mailboxService->parseMailboxConfigurations(true);

foreach ($mailboxConfiguration->getMailboxes() as $existingMailbox) {
if ($existingMailbox->getSwiftmailerConfiguration()->getId() == $existingConfiguration->getId()) {
Expand All @@ -50,7 +56,7 @@ public function onSwiftMailerConfigurationUpdated(ConfigurationUpdatedEvent $eve
}

if (true === $isUpdateRequiredFlag) {
file_put_contents($this->container->get('uvdesk.mailbox')->getPathToConfigurationFile(), (string) $mailboxConfiguration);
file_put_contents($this->mailboxService->getPathToConfigurationFile(), (string) $mailboxConfiguration);
}

return;
Expand All @@ -60,7 +66,7 @@ public function onSwiftMailerConfigurationRemoved(ConfigurationRemovedEvent $eve
{
$isUpdateRequiredFlag = false;
$configuration = $event->getSwiftMailerConfiguration();
$mailboxConfiguration = $this->container->get('uvdesk.mailbox')->parseMailboxConfigurations();
$mailboxConfiguration = $this->mailboxService->parseMailboxConfigurations();

foreach ($mailboxConfiguration->getMailboxes() as $existingMailbox) {
if (null != $existingMailbox->getSwiftmailerConfiguration() && $existingMailbox->getSwiftmailerConfiguration()->getId() == $configuration->getId()) {
Expand All @@ -77,7 +83,7 @@ public function onSwiftMailerConfigurationRemoved(ConfigurationRemovedEvent $eve
}

if (true === $isUpdateRequiredFlag) {
file_put_contents($this->container->get('uvdesk.mailbox')->getPathToConfigurationFile(), (string) $mailboxConfiguration);
file_put_contents($this->mailboxService->getPathToConfigurationFile(), (string) $mailboxConfiguration);
}

return;
Expand Down
2 changes: 1 addition & 1 deletion Resources/views/manageConfigurations.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@
msg: 'Please specify a valid email address.'
},
{
pattern: /^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/,
pattern: /^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/,
msg: 'Please specify a valid email address.'
}
],
Expand Down
44 changes: 26 additions & 18 deletions Services/MailboxService.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use Symfony\Component\DependencyInjection\ContainerInterface;
use Webkul\UVDesk\CoreFrameworkBundle\Workflow\Events as CoreWorkflowEvents;
use Webkul\UVDesk\MailboxBundle\Utils\Imap\Configuration as ImapConfiguration;
use Webkul\UVDesk\CoreFrameworkBundle\SwiftMailer\SwiftMailer as SwiftMailerService;

class MailboxService
{
Expand All @@ -31,12 +32,14 @@ class MailboxService
private $requestStack;
private $entityManager;
private $mailboxCollection = [];
private $swiftMailer;

public function __construct(ContainerInterface $container, RequestStack $requestStack, EntityManagerInterface $entityManager)
public function __construct(ContainerInterface $container, RequestStack $requestStack, EntityManagerInterface $entityManager, SwiftMailerService $swiftMailer)
{
$this->container = $container;
$this->requestStack = $requestStack;
$this->entityManager = $entityManager;
$this->swiftMailer = $swiftMailer;
}

public function getPathToConfigurationFile()
Expand All @@ -60,7 +63,7 @@ public function parseMailboxConfigurations(bool $ignoreInvalidAttributes = false

// Read configurations from package config.
$mailboxConfiguration = new MailboxConfiguration();
$swiftmailerService = $this->container->get('swiftmailer.service');
$swiftmailerService = $this->swiftMailer;
$swiftmailerConfigurations = $swiftmailerService->parseSwiftMailerConfigurations();

foreach (Yaml::parse(file_get_contents($path))['uvdesk_mailbox']['mailboxes'] ?? [] as $id => $params) {
Expand Down Expand Up @@ -263,7 +266,7 @@ public function processMail($rawEmail)
$from = $this->parseAddress('from') ?: $this->parseAddress('sender');
$addresses = [
'from' => $this->getEmailAddress($from),
'to' => $this->parseAddress('to'),
'to' => empty($this->parseAddress('X-Forwarded-To')) ? $this->parseAddress('to') : $this->parseAddress('X-Forwarded-To'),
'cc' => $this->parseAddress('cc'),
'delivered-to' => $this->parseAddress('delivered-to'),
];
Expand Down Expand Up @@ -301,13 +304,14 @@ public function processMail($rawEmail)
}

// Process Mail - References
$addresses['to'][0] = strtolower($addresses['to'][0]);
$mailData['replyTo'] = $addresses['to'];
$mailData['messageId'] = $parser->getHeader('message-id') ?: null;
$mailData['inReplyTo'] = htmlspecialchars_decode($parser->getHeader('in-reply-to'));
$mailData['referenceIds'] = htmlspecialchars_decode($parser->getHeader('references'));
$mailData['cc'] = array_filter(explode(',', $parser->getHeader('cc'))) ?: [];
$mailData['bcc'] = array_filter(explode(',', $parser->getHeader('bcc'))) ?: [];

// Process Mail - User Details
$mailData['source'] = 'email';
$mailData['createdBy'] = 'customer';
Expand Down Expand Up @@ -344,11 +348,12 @@ public function processMail($rawEmail)
$mailData['threadType'] = 'create';
$mailData['referenceIds'] = $mailData['messageId'];

$ticketSubjectRefrenceExist = $this->searchticketSubjectRefrence($mailData['from'], $mailData['subject']);
// @Todo For same subject with same customer check
// $ticketSubjectRefrenceExist = $this->searchticketSubjectRefrence($mailData['from'], $mailData['subject']);

if(!empty($ticketSubjectRefrenceExist)) {
return;
}
// if(!empty($ticketSubjectRefrenceExist)) {
// return;
// }

$thread = $this->container->get('ticket.service')->createTicket($mailData);

Expand Down Expand Up @@ -378,6 +383,7 @@ public function processMail($rawEmail)

if (!empty($user) && null != $user->getAgentInstance()) {
$mailData['user'] = $user;
$mailData['createdBy'] = 'agent';
$userDetails = $user->getAgentInstance()->getPartialDetails();
} else {
// Add user as a ticket collaborator
Expand Down Expand Up @@ -414,17 +420,19 @@ public function processMail($rawEmail)
$mailData['fullname'] = $userDetails['name'];

$thread = $this->container->get('ticket.service')->createThread($ticket, $mailData);

if ($thread->getCreatedBy() == 'customer') {
$event = new GenericEvent(CoreWorkflowEvents\Ticket\CustomerReply::getId(), [
'entity' => $ticket,
]);
} else {
$event = new GenericEvent(CoreWorkflowEvents\Ticket\AgentReply::getId(), [
'entity' => $ticket,
]);

if($thread->getThreadType() == 'reply') {
if ($thread->getCreatedBy() == 'customer') {
$event = new GenericEvent(CoreWorkflowEvents\Ticket\CustomerReply::getId(), [
'entity' => $ticket,
]);
} else {
$event = new GenericEvent(CoreWorkflowEvents\Ticket\AgentReply::getId(), [
'entity' => $ticket,
]);
}
}

// Trigger thread reply event
$this->container->get('event_dispatcher')->dispatch('uvdesk.automation.workflow.execute', $event);
}
Expand Down
5 changes: 5 additions & 0 deletions UIComponents/Dashboard/Search/Mailbox.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ public static function getRouteName() : string
return 'helpdesk_member_mailbox_settings';
}

public static function getRoles() : array
{
return ['ROLE_ADMIN'];
}

public function getChildrenRoutes() : array
{
return [];
Expand Down
5 changes: 5 additions & 0 deletions UIComponents/Dashboard/Search/SwiftMailer.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ public static function getRouteName() : string
return 'helpdesk_member_swiftmailer_settings';
}

public static function getRoles() : array
{
return ['ROLE_ADMIN'];
}

public function getChildrenRoutes() : array
{
return [];
Expand Down

0 comments on commit 0515b76

Please sign in to comment.