From 5a84ecde9221c5d5f99a1547c9ef761a0eea2450 Mon Sep 17 00:00:00 2001
From: "S.Listl" <S.Listl@SLISTL.aditosoftware.local>
Date: Thu, 1 Aug 2019 16:29:40 +0200
Subject: [PATCH] BulkMail preset subject from eml

---
 .../BulkMailRecipient_entity.aod              |  1 +
 .../containername_param/valueProcess.js       |  3 ++-
 .../BulkMailTesting_entity.aod                |  2 +-
 entity/BulkMail_entity/BulkMail_entity.aod    |  3 ++-
 .../entityfields/subject/valueProcess.js      | 20 +++++++++++++++++++
 .../recordcontainers/jdito/onInsert.js        |  3 ++-
 process/DocumentTemplate_lib/process.js       | 19 ++++++++----------
 process/Email_lib/process.js                  | 13 ++++++++++++
 8 files changed, 49 insertions(+), 15 deletions(-)
 create mode 100644 entity/BulkMail_entity/entityfields/subject/valueProcess.js

diff --git a/entity/BulkMailRecipient_entity/BulkMailRecipient_entity.aod b/entity/BulkMailRecipient_entity/BulkMailRecipient_entity.aod
index 28d62984e0..cfdc84490d 100644
--- a/entity/BulkMailRecipient_entity/BulkMailRecipient_entity.aod
+++ b/entity/BulkMailRecipient_entity/BulkMailRecipient_entity.aod
@@ -174,6 +174,7 @@
         <dbRecordFieldMapping>
           <name>STATUS.value</name>
           <recordfield>BULKMAILRECIPIENT.STATUS</recordfield>
+          <isFilterable v="true" />
         </dbRecordFieldMapping>
         <dbRecordFieldMapping>
           <name>ORGANISATION_ID.value</name>
diff --git a/entity/BulkMailRecipient_entity/entityfields/statuskeyword/children/containername_param/valueProcess.js b/entity/BulkMailRecipient_entity/entityfields/statuskeyword/children/containername_param/valueProcess.js
index ed3b8531d7..06d12d80f3 100644
--- a/entity/BulkMailRecipient_entity/entityfields/statuskeyword/children/containername_param/valueProcess.js
+++ b/entity/BulkMailRecipient_entity/entityfields/statuskeyword/children/containername_param/valueProcess.js
@@ -1,3 +1,4 @@
+import("KeywordRegistry_basic");
 import("system.result");
 
-result.string("BulkMailSentStatus"); //TODO: keyword registry
\ No newline at end of file
+result.string($KeywordRegistry.bulkMailRecipientStatus()); 
\ No newline at end of file
diff --git a/entity/BulkMailTesting_entity/BulkMailTesting_entity.aod b/entity/BulkMailTesting_entity/BulkMailTesting_entity.aod
index 14a463b102..da34a9438e 100644
--- a/entity/BulkMailTesting_entity/BulkMailTesting_entity.aod
+++ b/entity/BulkMailTesting_entity/BulkMailTesting_entity.aod
@@ -15,7 +15,7 @@
     </entityField>
     <entityField>
       <name>TESTING_CONTACT_ID</name>
-      <title>Testing contact</title>
+      <title>Contact</title>
       <consumer>Contacts</consumer>
       <linkedContext>AnyContact</linkedContext>
       <valueProcess>%aditoprj%/entity/BulkMailTesting_entity/entityfields/testing_contact_id/valueProcess.js</valueProcess>
diff --git a/entity/BulkMail_entity/BulkMail_entity.aod b/entity/BulkMail_entity/BulkMail_entity.aod
index 3263111aa5..828cdf95b8 100644
--- a/entity/BulkMail_entity/BulkMail_entity.aod
+++ b/entity/BulkMail_entity/BulkMail_entity.aod
@@ -31,6 +31,7 @@
     <entityField>
       <name>SUBJECT</name>
       <title>Subject</title>
+      <valueProcess>%aditoprj%/entity/BulkMail_entity/entityfields/subject/valueProcess.js</valueProcess>
     </entityField>
     <entityField>
       <name>DESCRIPTION</name>
@@ -167,7 +168,7 @@
     </entityConsumer>
     <entityField>
       <name>TESTING_CONTACT_ID</name>
-      <title>Testing contact</title>
+      <title>Contact</title>
       <consumer>Contacts</consumer>
       <linkedContext>AnyContact</linkedContext>
     </entityField>
diff --git a/entity/BulkMail_entity/entityfields/subject/valueProcess.js b/entity/BulkMail_entity/entityfields/subject/valueProcess.js
new file mode 100644
index 0000000000..396141a13a
--- /dev/null
+++ b/entity/BulkMail_entity/entityfields/subject/valueProcess.js
@@ -0,0 +1,20 @@
+import("system.result");
+import("Email_lib");
+import("Document_lib");
+import("DocumentTemplate_lib");
+import("system.neon");
+import("system.vars");
+
+if (vars.get("$this.value") == null && vars.get("$sys.recordstate") == neon.OPERATINGSTATE_NEW)
+{
+    var upload = vars.get("$field.BINDATA");
+    var template;
+    if (upload)
+    {
+        var binData = DocumentUtil.getBindataFromUpload(upload);
+        var filename = DocumentUtil.getFilenameFromUpload(upload);
+        var type = DocumentUtil.getFileExtensionFromUpload(filename);
+        if (DocumentTemplate.types.fromFileExtension(type) == DocumentTemplate.types.EML)
+            result.string(Email.fromRFC(binData).subject);
+    }
+}
\ No newline at end of file
diff --git a/entity/Letter_entity/recordcontainers/jdito/onInsert.js b/entity/Letter_entity/recordcontainers/jdito/onInsert.js
index 1248954c4e..98716cf135 100644
--- a/entity/Letter_entity/recordcontainers/jdito/onInsert.js
+++ b/entity/Letter_entity/recordcontainers/jdito/onInsert.js
@@ -8,7 +8,8 @@ import("KeywordRegistry_basic");
 var template = DocumentTemplate.loadTemplate(vars.get("$field.DOCUMENT_TEMPLATE"));
 var contactId = vars.get("$param.ContactId_param");
 var content = template.getReplacedContentByContactId(contactId, true);
-neon.download(content, template.filename);
+if (template.type)
+    neon.download(content, template.filename);
 
 var links = [];
 if (contactId)
diff --git a/process/DocumentTemplate_lib/process.js b/process/DocumentTemplate_lib/process.js
index 6761dfbbd2..2bfd37dd64 100644
--- a/process/DocumentTemplate_lib/process.js
+++ b/process/DocumentTemplate_lib/process.js
@@ -342,17 +342,14 @@ TemplateHelper._getReplacementsByContactIds = function (pTemplate, pContactIds)
  */
 TemplateHelper._getReplacedEML = function (pTemplate, pReplacements, pGetEmail)
 {
-    var mailData = mail.parseRFC(util.decodeBase64String(pTemplate.content));
-    var email;
-    var body = TemplateHelper._replaceText(mailData[mail.MAIL_HTMLTEXT], pReplacements);
-    if (pGetEmail)
-    {
-        var sender = TemplateHelper._replaceText(mailData[mail.MAIL_SENDER], pReplacements);
-        var subject = TemplateHelper._replaceText(mailData[mail.MAIL_SUBJECT], pReplacements);
-        email = new Email(null, sender, subject, body);
-    }
-    else
-        email = body;
+    var email = Email.fromRFC(pTemplate.content);
+    email.body = TemplateHelper._replaceText(email.body, pReplacements);
+    if (!pGetEmail)
+        return email.body;
+    
+    email.sender = TemplateHelper._replaceText(email.sender, pReplacements);
+    email.subject = TemplateHelper._replaceText(email.subject, pReplacements);
+    
     return email;
 }
 
diff --git a/process/Email_lib/process.js b/process/Email_lib/process.js
index bdaf02a758..4c6a8af91f 100644
--- a/process/Email_lib/process.js
+++ b/process/Email_lib/process.js
@@ -75,6 +75,19 @@ function Email (pToRecipients, pSender, pSubject, pBody, pCcRecipients, pBccReci
     this.bccRecipients = pBccRecipients || [];
 }
 
+/**
+ * makes an Email object from a RFC mail (base64 encoded)
+ */
+Email.fromRFC = function (pBase64RFC)
+{
+    var mailData = mail.parseRFC(util.decodeBase64String(pBase64RFC));
+    var body = mailData[mail.MAIL_HTMLTEXT];
+    var sender = mailData[mail.MAIL_SENDER];
+    var subject = mailData[mail.MAIL_SUBJECT];
+    
+    return new Email(null, sender, subject, body);
+}
+
 /**
  * loads a document template into the mail body
  * 
-- 
GitLab