diff --git a/application/Espo/Controllers/EmailTemplate.php b/application/Espo/Controllers/EmailTemplate.php new file mode 100644 index 0000000000..1f0f6ad961 --- /dev/null +++ b/application/Espo/Controllers/EmailTemplate.php @@ -0,0 +1,25 @@ +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'), + )); + } + +} + diff --git a/application/Espo/Core/Controllers/Record.php b/application/Espo/Core/Controllers/Record.php index 05b7c29f72..635839e9a0 100644 --- a/application/Espo/Core/Controllers/Record.php +++ b/application/Espo/Core/Controllers/Record.php @@ -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'; diff --git a/application/Espo/Core/Mail/Sender.php b/application/Espo/Core/Mail/Sender.php index 90811c4387..bd33200e0b 100644 --- a/application/Espo/Core/Mail/Sender.php +++ b/application/Espo/Core/Mail/Sender.php @@ -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'); diff --git a/application/Espo/Entities/EmailTemplate.php b/application/Espo/Entities/EmailTemplate.php new file mode 100644 index 0000000000..4bc97fa1ce --- /dev/null +++ b/application/Espo/Entities/EmailTemplate.php @@ -0,0 +1,8 @@ +get('isAdmin'); diff --git a/application/Espo/Modules/Crm/Entities/Contact.php b/application/Espo/Modules/Crm/Entities/Contact.php index 2e8a0d4e2a..569d13a24f 100644 --- a/application/Espo/Modules/Crm/Entities/Contact.php +++ b/application/Espo/Modules/Crm/Entities/Contact.php @@ -4,4 +4,5 @@ namespace Espo\Modules\Crm\Entities; class Contact extends \Espo\Core\ORM\Entity { + public static $person = true; } diff --git a/application/Espo/Modules/Crm/Entities/Lead.php b/application/Espo/Modules/Crm/Entities/Lead.php index 4f9c5bd98f..bf3ebc0923 100644 --- a/application/Espo/Modules/Crm/Entities/Lead.php +++ b/application/Espo/Modules/Crm/Entities/Lead.php @@ -4,4 +4,5 @@ namespace Espo\Modules\Crm\Entities; class Lead extends \Espo\Core\ORM\Entity { + public static $person = true; } diff --git a/application/Espo/Modules/Crm/Entities/Prospect.php b/application/Espo/Modules/Crm/Entities/Prospect.php index 7d574c5381..cd8c03a5a9 100644 --- a/application/Espo/Modules/Crm/Entities/Prospect.php +++ b/application/Espo/Modules/Crm/Entities/Prospect.php @@ -4,4 +4,5 @@ namespace Espo\Modules\Crm\Entities; class Prospect extends \Espo\Core\ORM\Entity { + public static $person = true; } diff --git a/application/Espo/Resources/layouts/Email/composeSmall.json b/application/Espo/Resources/layouts/Email/composeSmall.json index 709ca6d7e9..d6788e1255 100644 --- a/application/Espo/Resources/layouts/Email/composeSmall.json +++ b/application/Espo/Resources/layouts/Email/composeSmall.json @@ -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"}] ] } ] diff --git a/application/Espo/Resources/layouts/Email/detail.json b/application/Espo/Resources/layouts/Email/detail.json index ba544ca4de..20edc4392b 100644 --- a/application/Espo/Resources/layouts/Email/detail.json +++ b/application/Espo/Resources/layouts/Email/detail.json @@ -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"}] ] } ] diff --git a/application/Espo/Resources/layouts/Email/detailSmall.json b/application/Espo/Resources/layouts/Email/detailSmall.json index 2dab6561ad..b8592c9be1 100644 --- a/application/Espo/Resources/layouts/Email/detailSmall.json +++ b/application/Espo/Resources/layouts/Email/detailSmall.json @@ -5,9 +5,9 @@ [{"name":"dateSent"}], [{"name":"from"}], [{"name":"to"}], - [{"name":"name"}], + [{"name":"name"}], [{"name":"body"}], - [{"name":"attachments","type":"attachmentMultiple"}] + [{"name":"attachments"}] ] } ] diff --git a/application/Espo/Resources/layouts/EmailTemplate/detail.json b/application/Espo/Resources/layouts/EmailTemplate/detail.json new file mode 100644 index 0000000000..9cc1306c12 --- /dev/null +++ b/application/Espo/Resources/layouts/EmailTemplate/detail.json @@ -0,0 +1,20 @@ +[ + { + "label":"", + "rows":[ + [ + { + "name":"name" + } + ], + [{"name":"subject","fullWidth":true}], + [ + { + "name":"isHtml" + } + ], + [{"name":"body","fullWidth":true}], + [{"name":"attachments"}] + ] + } +] diff --git a/application/Espo/Resources/layouts/EmailTemplate/detailSmall.json b/application/Espo/Resources/layouts/EmailTemplate/detailSmall.json new file mode 100644 index 0000000000..bf6ba72998 --- /dev/null +++ b/application/Espo/Resources/layouts/EmailTemplate/detailSmall.json @@ -0,0 +1,20 @@ +[ + { + "label":"", + "rows":[ + [ + { + "name":"name" + } + ], + [{"name":"subject"}], + [ + { + "name":"isHtml" + } + ], + [{"name":"body","fullWidth":true}], + [{"name":"attachments"}] + ] + } +] diff --git a/application/Espo/Resources/metadata/entityDefs/Email.json b/application/Espo/Resources/metadata/entityDefs/Email.json index ad19797030..affbe26572 100644 --- a/application/Espo/Resources/metadata/entityDefs/Email.json +++ b/application/Espo/Resources/metadata/entityDefs/Email.json @@ -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" diff --git a/application/Espo/Resources/metadata/entityDefs/EmailTemplate.json b/application/Espo/Resources/metadata/entityDefs/EmailTemplate.json index 3d8ffc017f..1f85612b27 100644 --- a/application/Espo/Resources/metadata/entityDefs/EmailTemplate.json +++ b/application/Espo/Resources/metadata/entityDefs/EmailTemplate.json @@ -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", diff --git a/application/Espo/Services/EmailTemplate.php b/application/Espo/Services/EmailTemplate.php new file mode 100644 index 0000000000..ef63d8128e --- /dev/null +++ b/application/Espo/Services/EmailTemplate.php @@ -0,0 +1,88 @@ +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; + } +} +