inbound email 3

This commit is contained in:
Yuri Kuznetsov
2014-01-30 16:06:28 +02:00
parent 5daa508028
commit 47aa84281a
8 changed files with 57 additions and 26 deletions

View File

@@ -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");
}

View File

@@ -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;
}

View File

@@ -18,7 +18,7 @@ abstract class Base implements Injectable
public function __construct()
{
$this->init();
}
}
protected function init()
{

View File

@@ -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 ."<br>";
$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){

View File

@@ -125,7 +125,7 @@ class RDB extends \Espo\ORM\Repository
protected function afterRemove(Entity $entity)
{
}
}
public function remove(Entity $entity)
{

View File

@@ -10,8 +10,7 @@
"db": false
},
"fromName": {
"type": "varchar",
"db": false
"type": "varchar"
},
"from": {
"type": "varchar",

View File

@@ -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')
);
}

View File

@@ -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;
}