From 47aa84281acbced87e86bccea18d0dc55c2a5435 Mon Sep 17 00:00:00 2001 From: Yuri Kuznetsov Date: Thu, 30 Jan 2014 16:06:28 +0200 Subject: [PATCH] inbound email 3 --- application/Espo/Core/ControllerManager.php | 9 +++-- application/Espo/Core/Controllers/Record.php | 3 +- application/Espo/Core/Services/Base.php | 2 +- .../Modules/Crm/Services/InboundEmail.php | 34 ++++++++++++------- application/Espo/ORM/Repositories/RDB.php | 2 +- .../Resources/metadata/entityDefs/Email.json | 3 +- application/Espo/Services/EmailTemplate.php | 15 ++++---- application/Espo/Services/Record.php | 15 +++++++- 8 files changed, 57 insertions(+), 26 deletions(-) diff --git a/application/Espo/Core/ControllerManager.php b/application/Espo/Core/ControllerManager.php index 91aa8c092e..8fbb780bd0 100644 --- a/application/Espo/Core/ControllerManager.php +++ b/application/Espo/Core/ControllerManager.php @@ -46,9 +46,14 @@ class ControllerManager } if ($data) { - $data = json_decode($data, true); - } + $data = json_decode($data); + } + + if ($data instanceof \stdClass) { + $data = get_object_vars($data); + } + if (!class_exists($controllerClassName)) { throw new NotFound("Controller '$controllerName' is not found"); } diff --git a/application/Espo/Core/Controllers/Record.php b/application/Espo/Core/Controllers/Record.php index 635839e9a0..ab4cbe06d0 100644 --- a/application/Espo/Core/Controllers/Record.php +++ b/application/Espo/Core/Controllers/Record.php @@ -23,8 +23,9 @@ class Record extends Base $service = $this->getServiceFactory()->create($this->name); } else { $service = $this->getServiceFactory()->create('Record'); + $service->setEntityName($this->name); } - $service->setEntityName($this->name); + return $service; } diff --git a/application/Espo/Core/Services/Base.php b/application/Espo/Core/Services/Base.php index f4c2e0feb7..28a847a51c 100644 --- a/application/Espo/Core/Services/Base.php +++ b/application/Espo/Core/Services/Base.php @@ -18,7 +18,7 @@ abstract class Base implements Injectable public function __construct() { $this->init(); - } + } protected function init() { diff --git a/application/Espo/Modules/Crm/Services/InboundEmail.php b/application/Espo/Modules/Crm/Services/InboundEmail.php index ecc0f1e4ac..d25291b14f 100644 --- a/application/Espo/Modules/Crm/Services/InboundEmail.php +++ b/application/Espo/Modules/Crm/Services/InboundEmail.php @@ -8,6 +8,8 @@ use \Espo\Core\Exceptions\Forbidden; class InboundEmail extends \Espo\Services\Record { + + protected function init() { $this->dependencies[] = 'fileManager'; @@ -85,7 +87,6 @@ class InboundEmail extends \Espo\Services\Record $storage->removeMessage(1); } } - } } @@ -142,9 +143,7 @@ class InboundEmail extends \Espo\Services\Record $this->importPartDataToEmail($email, $message); } - $this->getEntityManager()->saveEntity($email); - echo $email->id ."
"; - + $this->getEntityManager()->saveEntity($email); if ($inboundEmail->get('createCase')) { // TODO check case exists @@ -208,7 +207,8 @@ class InboundEmail extends \Espo\Services\Record $email->set('parentType', 'Case'); $email->set('parentId', $case->id); $this->getEntityManager()->saveEntity($email); - + + $case = $this->getEntityManager()->getEntity('Case', $case->id); return $case; } @@ -256,29 +256,34 @@ class InboundEmail extends \Espo\Services\Record try { $replyEmailTemplateId = $inboundEmail->get('replyEmailTemplateId'); if ($replyEmailTemplateId) { - $params = array(); + $entityHash = array(); if ($case) { - $params['Case'] = $case; + $entityHash['Case'] = $case; if ($case->get('contactId')) { $contact = $this->getEntityManager()->getEntity('Contact', $case->get('contactId')); } } if (empty($contact)) { $contact = $this->getEntityManager()->getEntity('Contact'); - $contant->set('lastName', $email->get('fromName')); + $contact->set('name', $email->get('fromName')); } - $params['Person'] = $contact; - $params['Contact'] = $contact; + + $entityHash['Person'] = $contact; + $entityHash['Contact'] = $contact; $emailTemplateService = $this->getServiceFactory()->create('EmailTemplate'); - $replyData = $emailTemplateService->parse($replyEmailTemplateId, $params, true); + + $replyData = $emailTemplateService->parse($replyEmailTemplateId, array('entityHash' => $entityHash), true); $reply = $this->getEntityManager()->getEntity('Email'); $reply->set('to', $email->get('from')); $reply->set('subject', $replyData['subject']); $reply->set('body', $replyData['body']); - $reply->set('attachmentsIds', $replyData['attachmentsIds']); + $reply->set('isHtml', $replyData['isHtml']); + $reply->set('attachmentsIds', $replyData['attachmentsIds']); + + $this->getEntityManager()->saveEntity($reply); $sender = $this->getMailSender()->useGlobal(); $senderParams = array(); @@ -291,6 +296,11 @@ class InboundEmail extends \Espo\Services\Record $sender->setParams($senderParams); $sender->send($reply); + foreach ($reply->get('attachments') as $attachment) { + $this->getEntityManager()->removeEntity($attachment); + } + + $this->getEntityManager()->removeEntity($reply); } } catch (\Exception $e){ diff --git a/application/Espo/ORM/Repositories/RDB.php b/application/Espo/ORM/Repositories/RDB.php index 1457a8e86d..ad2b58c7e0 100644 --- a/application/Espo/ORM/Repositories/RDB.php +++ b/application/Espo/ORM/Repositories/RDB.php @@ -125,7 +125,7 @@ class RDB extends \Espo\ORM\Repository protected function afterRemove(Entity $entity) { - } + } public function remove(Entity $entity) { diff --git a/application/Espo/Resources/metadata/entityDefs/Email.json b/application/Espo/Resources/metadata/entityDefs/Email.json index 1198ac933a..a3cea09e75 100644 --- a/application/Espo/Resources/metadata/entityDefs/Email.json +++ b/application/Espo/Resources/metadata/entityDefs/Email.json @@ -10,8 +10,7 @@ "db": false }, "fromName": { - "type": "varchar", - "db": false + "type": "varchar" }, "from": { "type": "varchar", diff --git a/application/Espo/Services/EmailTemplate.php b/application/Espo/Services/EmailTemplate.php index 0ed725c58e..0f6504d349 100644 --- a/application/Espo/Services/EmailTemplate.php +++ b/application/Espo/Services/EmailTemplate.php @@ -21,14 +21,16 @@ class EmailTemplate extends Record } public function parse($id, array $params = array(), $copyAttachments = false) - { + { $emailTemplate = $this->getEntity($id); if (empty($emailTemplate)) { throw new NotFound(); } $entityList = array(); - + if (!empty($params['entityHash']) && is_array($params['entityHash'])) { + $entityList = $params['entityHash']; + } if (!empty($params['emailAddress'])) { $emailAddress = $this->getEntityManager()->getRepository('EmailAddress')->where(array( @@ -93,20 +95,21 @@ class EmailTemplate extends Record $this->getEntityManager()->saveEntity($clone); $contents = $this->getFileManager()->getContent('data/upload/' . $attachment->id); - if (empty($content)) { + if (empty($contents)) { continue; } $this->getFileManager()->setContent($contents, 'data/upload/' . $clone->id); - $attachmentsIds[] = $clone['id']; + $attachmentsIds[] = $clone->id; } } - } + } return array( 'subject' => $subject, 'body' => $body, - 'attachmentsIds' => $attachmentsIds + 'attachmentsIds' => $attachmentsIds, + 'isHtml' => $emailTemplate->get('isHtml') ); } diff --git a/application/Espo/Services/Record.php b/application/Espo/Services/Record.php index 1326cfa02b..7579711f8f 100644 --- a/application/Espo/Services/Record.php +++ b/application/Espo/Services/Record.php @@ -22,6 +22,20 @@ class Record extends \Espo\Core\Services\Base protected $entityName; private $streamService; + + public function __construct() + { + parent::__construct(); + if (empty($this->entityName)) { + $name = get_class($this); + if (preg_match('@\\\\([\w]+)$@', $name, $matches)) { + $name = $matches[1]; + } + if ($name != 'Record') { + $this->entityName = $name; + } + } + } public function setEntityName($entityName) { @@ -80,7 +94,6 @@ class Record extends \Espo\Core\Services\Base throw new Forbidden(); } } - return $entity; }