Email Template

This commit is contained in:
Yuri Kuznetsov
2014-01-28 15:33:00 +02:00
parent 29178d4ca6
commit 147eba4c8d
16 changed files with 208 additions and 11 deletions
@@ -0,0 +1,25 @@
<?php
namespace Espo\Controllers;
use \Espo\Core\Exceptions\Error;
class EmailTemplate extends \Espo\Core\Controllers\Record
{
public function actionParse($params, $data, $request)
{
$id = $request->get('id');
$emailAddress = $request->get('emailAddress');
if (empty($id)) {
throw new Error();
}
return $this->getRecordService()->parse($id, array(
'emailAddress' => $request->get('emailAddress'),
'parentType' => $request->get('parentType'),
'parentId' => $request->get('parentId'),
));
}
}
+1 -1
View File
@@ -7,7 +7,7 @@ use \Espo\Core\Exceptions\Forbidden;
use \Espo\Core\Exceptions\NotFound;
use \Espo\Core\Utils\Util;
abstract class Record extends Base
class Record extends Base
{
public static $defaultAction = 'list';
+8 -2
View File
@@ -120,8 +120,14 @@ class Sender
$body = new MimeMessage;
$parts = array();
$bodyPart = new MimePart($email->get('body'));
$bodyPart->type = 'text/plain';
$bodyPart = new MimePart($email->get('body'));
if ($email->get('isHtml')) {
$bodyPart->type = 'text/html';
} else {
$bodyPart->type = 'text/plain';
}
$parts[] = $bodyPart;
$aCollection = $email->get('attachments');
@@ -0,0 +1,8 @@
<?php
namespace Espo\Entities;
class EmailTemplate extends \Espo\Core\ORM\Entity
{
}
+2
View File
@@ -4,6 +4,8 @@ namespace Espo\Entities;
class User extends \Espo\Core\ORM\Entity
{
public static $person = true;
public function isAdmin()
{
return $this->get('isAdmin');
@@ -4,4 +4,5 @@ namespace Espo\Modules\Crm\Entities;
class Contact extends \Espo\Core\ORM\Entity
{
public static $person = true;
}
@@ -4,4 +4,5 @@ namespace Espo\Modules\Crm\Entities;
class Lead extends \Espo\Core\ORM\Entity
{
public static $person = true;
}
@@ -4,4 +4,5 @@ namespace Espo\Modules\Crm\Entities;
class Prospect extends \Espo\Core\ORM\Entity
{
public static $person = true;
}
@@ -16,8 +16,17 @@
{"name":"bcc"}
],
[{"name":"name","fullWidth":true}],
[
{
"name":"isHtml"
},
{
"name":"selectTemplate",
"view":"Email.Fields.SelectTemplate"
}
],
[{"name":"body","fullWidth":true}],
[{"name":"attachments","type":"attachmentMultiple"}]
[{"name":"attachments"}]
]
}
]
@@ -7,8 +7,13 @@
[{"name":"from"},{"name":"cc"}],
[{"name":"to"},{"name":"bcc"}],
[{"name":"name","fullWidth":true}],
[
{
"name":"isHtml"
}
],
[{"name":"body","fullWidth":true}],
[{"name":"attachments","type":"attachmentMultiple"}]
[{"name":"attachments"}]
]
}
]
@@ -5,9 +5,9 @@
[{"name":"dateSent"}],
[{"name":"from"}],
[{"name":"to"}],
[{"name":"name"}],
[{"name":"name"}],
[{"name":"body"}],
[{"name":"attachments","type":"attachmentMultiple"}]
[{"name":"attachments"}]
]
}
]
@@ -0,0 +1,20 @@
[
{
"label":"",
"rows":[
[
{
"name":"name"
}
],
[{"name":"subject","fullWidth":true}],
[
{
"name":"isHtml"
}
],
[{"name":"body","fullWidth":true}],
[{"name":"attachments"}]
]
}
]
@@ -0,0 +1,20 @@
[
{
"label":"",
"rows":[
[
{
"name":"name"
}
],
[{"name":"subject"}],
[
{
"name":"isHtml"
}
],
[{"name":"body","fullWidth":true}],
[{"name":"attachments"}]
]
}
]
@@ -23,7 +23,12 @@
"db": false
},
"body": {
"type": "text"
"type": "text",
"view": "Fields.Wysiwyg"
},
"isHtml": {
"type": "bool",
"default": true
},
"status": {
"type": "enum",
@@ -31,7 +36,8 @@
"readOnly": true
},
"attachments": {
"type": "linkMultiple"
"type": "linkMultiple",
"view": "Fields.AttachmentMultiple"
},
"parent": {
"type": "linkParent"
@@ -4,15 +4,20 @@
"type": "varchar",
"required": true
},
"subject": {
"type": "varchar"
},
"body": {
"type": "text"
"type": "text",
"view": "Fields.Wysiwyg"
},
"isHtml": {
"type": "bool",
"default": true
},
"attachments": {
"type": "linkMultiple"
"type": "linkMultiple",
"view": "Fields.AttachmentMultiple"
},
"assignedUser": {
"type": "link",
@@ -0,0 +1,88 @@
<?php
namespace Espo\Services;
use \Espo\ORM\Entity;
use \Espo\Core\Exceptions\Error;
use \Espo\Core\Exceptions\NotFound;
class EmailTemplate extends Record
{
public function parse($id, array $params = array())
{
$emailTemplate = $this->getEntity($id);
if (empty($emailTemplate)) {
throw new NotFound();
}
$entityList = array();
if (!empty($params['emailAddress'])) {
$emailAddress = $this->getEntityManager()->getRepository('EmailAddress')->where(array(
'lower' => $params['emailAddress']
))->findOne();
if (!empty($emailAddress)) {
$pdo = $this->getEntityManager()->getPDO();
$sql = "
SELECT * FROM `entity_email_address`
WHERE
`primary` = 1 AND `deleted` = 0 AND `email_address_id` = " . $pdo->quote($emailAddress->id). "
";
$sth = $pdo->prepare($sql);
$sth->execute();
if ($row = $sth->fetch()) {
if (!empty($row['entity_id'])) {
$entity = $this->getEntityManager()->getEntity($row['entity_type'], $row['entity_id']);
if (!empty($entity::$person)) {
$entityList['Person'] = $entity;
}
}
}
}
}
if (!empty($params['parentId']) && !empty($params['parentType'])) {
$parent = $this->getEntityManager()->getEntity($params['parentType'], $params['parentId']);
if (!empty($parent)) {
$entityList[$params['parentType']] = $parent;
$entityList['Parent'] = $parent;
if (empty($entityList['Person']) && !empty($entity::$person)) {
$entityList['Person'] = $parent;
}
}
}
$subject = $emailTemplate->get('subject');
$body = $emailTemplate->get('body');
foreach ($entityList as $type => $entity) {
$subject = $this->parseText($type, $entity, $subject);
}
foreach ($entityList as $type => $entity) {
$body = $this->parseText($type, $entity, $body);
}
return array(
'subject' => $subject,
'body' => $body,
);
}
protected function parseText($type, Entity $entity, $text)
{
$fields = array_keys($entity->getFields());
foreach ($fields as $field) {
$text = str_replace('{' . $type . '.' . $field . '}', $entity->get($field), $text);
}
return $text;
}
}