7
7
use Doctrine \ORM \EntityManagerInterface ;
8
8
use Symfony \Component \HttpFoundation \Request ;
9
9
use Symfony \Component \HttpFoundation \Response ;
10
- use Webkul \UVDesk \CoreFrameworkBundle \Utils \HTMLFilter ;
11
- use Webkul \UVDesk \CoreFrameworkBundle \Utils \TokenGenerator ;
12
10
use Symfony \Component \HttpFoundation \RequestStack ;
11
+ use Webkul \UVDesk \CoreFrameworkBundle \Entity \User ;
13
12
use Symfony \Component \EventDispatcher \GenericEvent ;
14
- use Symfony \Component \DependencyInjection \ContainerInterface ;
15
- use Webkul \UVDesk \CoreFrameworkBundle \Workflow \Events as CoreWorkflowEvents ;
13
+ use Webkul \UVDesk \CoreFrameworkBundle \Entity \Ticket ;
14
+ use Webkul \UVDesk \CoreFrameworkBundle \Entity \Thread ;
15
+ use Webkul \UVDesk \CoreFrameworkBundle \Entity \Website ;
16
16
use Webkul \UVDesk \MailboxBundle \Utils \Mailbox \Mailbox ;
17
+ use Webkul \UVDesk \CoreFrameworkBundle \Utils \HTMLFilter ;
18
+ use Webkul \UVDesk \CoreFrameworkBundle \Entity \SupportRole ;
19
+ use Webkul \UVDesk \CoreFrameworkBundle \Utils \TokenGenerator ;
17
20
use Webkul \UVDesk \MailboxBundle \Utils \MailboxConfiguration ;
21
+ use Symfony \Component \DependencyInjection \ContainerInterface ;
22
+ use Webkul \UVDesk \CoreFrameworkBundle \Workflow \Events as CoreWorkflowEvents ;
18
23
use Webkul \UVDesk \MailboxBundle \Utils \Imap \Configuration as ImapConfiguration ;
19
24
20
25
class MailboxService
@@ -166,8 +171,8 @@ private function searchExistingTickets(array $criterias = [])
166
171
return null ;
167
172
}
168
173
169
- $ ticketRepository = $ this ->entityManager ->getRepository (' UVDeskCoreFrameworkBundle: Ticket' );
170
- $ threadRepository = $ this ->entityManager ->getRepository (' UVDeskCoreFrameworkBundle: Thread' );
174
+ $ ticketRepository = $ this ->entityManager ->getRepository (Ticket::class );
175
+ $ threadRepository = $ this ->entityManager ->getRepository (Thread::class );
171
176
172
177
foreach ($ criterias as $ criteria => $ criteriaValue ) {
173
178
if (empty ($ criteriaValue )) {
@@ -271,7 +276,7 @@ public function processMail($rawEmail)
271
276
return ;
272
277
}
273
278
274
- // Check for self-referencing. Skip email processing if a mailbox is configured by the sender's address.
279
+ // Check for self-referencing. Skip email processing if a mailbox is configured by the sender's address.
275
280
try {
276
281
$ this ->getMailboxByEmail ($ addresses ['from ' ]);
277
282
return ;
@@ -305,28 +310,7 @@ public function processMail($rawEmail)
305
310
$ mailData ['message ' ] = autolink ($ htmlFilter ->addClassEmailReplyQuote ($ parser ->getMessageBody ('text ' )));
306
311
}
307
312
308
- // $mailboxes = $this->getMailboxByEmail($data['replyTo']);
309
- // if(!count($mailboxes)) {
310
- // if($cc) {
311
- // foreach ($cc as $value) {
312
- // $toAdress[] = $value['address'];
313
- // }
314
- // $mailboxes = $this->getMailboxByEmail($toAdress);
315
-
316
- // if(count($mailboxes)) {
317
- // foreach ($mailboxes as $mailbox) {
318
- // foreach ($data['cc'] as $key => $value) {
319
- // if (strpos($value, $mailbox->getEmail()) !== FALSE) {
320
- // unset($data['cc'][$key]);
321
- // }
322
- // }
323
- // }
324
- // $data['replyTo'] = $toAdress;
325
- // }
326
- // }
327
- // }
328
-
329
- $ website = $ this ->entityManager ->getRepository ('UVDeskCoreFrameworkBundle:Website ' )->findOneByCode ('knowledgebase ' );
313
+ $ website = $ this ->entityManager ->getRepository (Website::class)->findOneByCode ('knowledgebase ' );
330
314
331
315
if (!empty ($ mailData ['from ' ]) && $ this ->container ->get ('ticket.service ' )->isEmailBlocked ($ mailData ['from ' ], $ website )) {
332
316
return ;
@@ -345,7 +329,6 @@ public function processMail($rawEmail)
345
329
$ mailData ['threadType ' ] = 'create ' ;
346
330
$ mailData ['referenceIds ' ] = $ mailData ['messageId ' ];
347
331
348
- $ this ->addCollaboratorFlag = 1 ;
349
332
$ thread = $ this ->container ->get ('ticket.service ' )->createTicket ($ mailData );
350
333
351
334
// Trigger ticket created event
@@ -356,7 +339,7 @@ public function processMail($rawEmail)
356
339
$ this ->container ->get ('event_dispatcher ' )->dispatch ('uvdesk.automation.workflow.execute ' , $ event );
357
340
} else if (false === $ ticket ->getIsTrashed () && strtolower ($ ticket ->getStatus ()->getCode ()) != 'spam ' ) {
358
341
$ mailData ['threadType ' ] = 'reply ' ;
359
- $ thread = $ this ->entityManager ->getRepository (' UVDeskCoreFrameworkBundle: Thread' )->findOneByMessageId ($ mailData ['messageId ' ]);
342
+ $ thread = $ this ->entityManager ->getRepository (Thread::class )->findOneByMessageId ($ mailData ['messageId ' ]);
360
343
361
344
if (!empty ($ thread )) {
362
345
// Thread with the same message id exists. Skip processing.
@@ -370,20 +353,45 @@ public function processMail($rawEmail)
370
353
$ mailData ['user ' ] = $ user ;
371
354
$ userDetails = $ user ->getCustomerInstance ()->getPartialDetails ();
372
355
} else {
373
- $ user = $ this ->entityManager ->getRepository (' UVDeskSupportBundle: User' )->findOneByEmail ($ mailData ['from ' ]);
374
-
356
+ $ user = $ this ->entityManager ->getRepository (User::class )->findOneByEmail ($ mailData ['from ' ]);
357
+
375
358
if (!empty ($ user ) && null != $ user ->getAgentInstance ()) {
376
359
$ mailData ['user ' ] = $ user ;
377
360
$ userDetails = $ user ->getAgentInstance ()->getPartialDetails ();
378
361
} else {
379
- // No user found.
380
- // @TODO: Do something about this case.
381
- return ;
362
+ // Add user as a ticket collaborator
363
+ if (empty ($ user )) {
364
+ // Create a new user instance with customer support role
365
+ $ role = $ this ->entityManager ->getRepository (SupportRole::class)->findOneByCode ('ROLE_CUSTOMER ' );
366
+
367
+ $ user = $ this ->container ->get ('user.service ' )->createUserInstance ($ mailData ['from ' ], $ mailData ['name ' ], $ role , [
368
+ 'source ' => 'email ' ,
369
+ 'active ' => true
370
+ ]);
371
+ }
372
+
373
+ $ mailData ['user ' ] = $ user ;
374
+ $ userDetails = $ user ->getCustomerInstance ()->getPartialDetails ();
375
+
376
+ if (false == $ this ->entityManager ->getRepository (Ticket::class)->isTicketCollaborator ($ ticket , $ mailData ['from ' ])) {
377
+ $ ticket ->addCollaborator ($ user );
378
+
379
+ $ this ->entityManager ->persist ($ ticket );
380
+ $ this ->entityManager ->flush ();
381
+
382
+ $ ticket ->lastCollaborator = $ user ;
383
+
384
+ $ event = new GenericEvent (CoreWorkflowEvents \Ticket \Collaborator::getId (), [
385
+ 'entity ' => $ ticket ,
386
+ ]);
387
+
388
+ $ this ->container ->get ('event_dispatcher ' )->dispatch ('uvdesk.automation.workflow.execute ' , $ event );
389
+ }
382
390
}
383
391
}
384
392
385
393
$ mailData ['fullname ' ] = $ userDetails ['name ' ];
386
-
394
+
387
395
$ thread = $ this ->container ->get ('ticket.service ' )->createThread ($ ticket , $ mailData );
388
396
389
397
if ($ thread ->getCreatedBy () == 'customer ' ) {
0 commit comments