From df243d986c10c12f9b5025f113a55eff62183a4f Mon Sep 17 00:00:00 2001
From: Johannes Hoermann <j.hoermann@adito.de>
Date: Fri, 6 Sep 2019 14:14:46 +0200
Subject: [PATCH] Bulkmail fixes

---
 entity/BulkMail_entity/BulkMail_entity.aod    | 11 ++++-
 .../entityfields/bindata/onValueChange.js     | 13 +++++
 .../entityfields/bindata/stateProcess.js      |  2 +-
 .../content/contentTypeProcess.js             | 17 ++-----
 .../content/displayValueProcess.js            |  7 ++-
 .../entityfields/content/stateProcess.js      |  4 +-
 .../entityfields/content/valueProcess.js      | 23 ++++-----
 .../documenttemplate_id/onValueChange.js      |  7 +++
 .../preview/contentTypeProcess.js             |  7 ---
 .../entityfields/bindata/onValueChange.js     |  4 +-
 .../entityfields/content/valueProcess.js      |  4 +-
 process/DocumentTemplate_lib/process.js       | 48 +++++++++++--------
 12 files changed, 85 insertions(+), 62 deletions(-)
 create mode 100644 entity/BulkMail_entity/entityfields/bindata/onValueChange.js
 delete mode 100644 entity/BulkMail_entity/entityfields/preview/contentTypeProcess.js

diff --git a/entity/BulkMail_entity/BulkMail_entity.aod b/entity/BulkMail_entity/BulkMail_entity.aod
index 0323f662f6..f6c1bd93b2 100644
--- a/entity/BulkMail_entity/BulkMail_entity.aod
+++ b/entity/BulkMail_entity/BulkMail_entity.aod
@@ -45,6 +45,9 @@
       <consumer>Templates</consumer>
       <mandatory v="false" />
       <onValueChange>%aditoprj%/entity/BulkMail_entity/entityfields/documenttemplate_id/onValueChange.js</onValueChange>
+      <onValueChangeTypes>
+        <element>MASK</element>
+      </onValueChangeTypes>
     </entityField>
     <entityConsumer>
       <name>Recipients</name>
@@ -114,6 +117,10 @@
       <name>bindata</name>
       <contentType>FILE</contentType>
       <stateProcess>%aditoprj%/entity/BulkMail_entity/entityfields/bindata/stateProcess.js</stateProcess>
+      <onValueChange>%aditoprj%/entity/BulkMail_entity/entityfields/bindata/onValueChange.js</onValueChange>
+      <onValueChangeTypes>
+        <element>MASK</element>
+      </onValueChangeTypes>
       <onValidation>%aditoprj%/entity/BulkMail_entity/entityfields/bindata/onValidation.js</onValidation>
     </entityField>
     <entityFieldGroup>
@@ -130,7 +137,9 @@
     <entityField>
       <name>content</name>
       <title>Content</title>
+      <contentType>HTML</contentType>
       <contentTypeProcess>%aditoprj%/entity/BulkMail_entity/entityfields/content/contentTypeProcess.js</contentTypeProcess>
+      <state>EDITABLE</state>
       <stateProcess>%aditoprj%/entity/BulkMail_entity/entityfields/content/stateProcess.js</stateProcess>
       <valueProcess>%aditoprj%/entity/BulkMail_entity/entityfields/content/valueProcess.js</valueProcess>
       <displayValueProcess>%aditoprj%/entity/BulkMail_entity/entityfields/content/displayValueProcess.js</displayValueProcess>
@@ -241,7 +250,7 @@
     <entityField>
       <name>preview</name>
       <title>Preview</title>
-      <contentTypeProcess>%aditoprj%/entity/BulkMail_entity/entityfields/preview/contentTypeProcess.js</contentTypeProcess>
+      <contentType>HTML</contentType>
       <valueProcess>%aditoprj%/entity/BulkMail_entity/entityfields/preview/valueProcess.js</valueProcess>
     </entityField>
   </entityFields>
diff --git a/entity/BulkMail_entity/entityfields/bindata/onValueChange.js b/entity/BulkMail_entity/entityfields/bindata/onValueChange.js
new file mode 100644
index 0000000000..5e84ced62a
--- /dev/null
+++ b/entity/BulkMail_entity/entityfields/bindata/onValueChange.js
@@ -0,0 +1,13 @@
+import("system.logging");
+import("DocumentTemplate_lib");
+import("system.neon");
+import("system.vars");
+
+// note: could not use only the valueProcess from $field.content because then it refreshed $field.content just before save.
+// --> only set in $field.content.valueProcess if $field.content is null and set it from here only if MASK triggered change
+if(vars.get("$sys.viewmode") == neon.FRAME_VIEWMODE_DATASET)
+{ 
+    [content, type] = DocumentTemplateUtils.getTemplateContent(vars.get("$field.DOCUMENTTEMPLATE_ID"), new FileUpload(vars.get("$local.value")));
+    vars.set("$context.currentTemplateType", type)
+    vars.set("$field.content", content)
+}
diff --git a/entity/BulkMail_entity/entityfields/bindata/stateProcess.js b/entity/BulkMail_entity/entityfields/bindata/stateProcess.js
index aabc8ec342..aeb23379ee 100644
--- a/entity/BulkMail_entity/entityfields/bindata/stateProcess.js
+++ b/entity/BulkMail_entity/entityfields/bindata/stateProcess.js
@@ -4,5 +4,5 @@ import("system.neon");
 
 var state = neon.COMPONENTSTATE_EDITABLE;
 if (vars.get("$field.DOCUMENTTEMPLATE_ID"))
-    state = neon.COMPONENTSTATE_READONLY;
+    state = neon.COMPONENTSTATE_INVISIBLE;
 result.string(state);
\ No newline at end of file
diff --git a/entity/BulkMail_entity/entityfields/content/contentTypeProcess.js b/entity/BulkMail_entity/entityfields/content/contentTypeProcess.js
index 3213243e36..af8f592df3 100644
--- a/entity/BulkMail_entity/entityfields/content/contentTypeProcess.js
+++ b/entity/BulkMail_entity/entityfields/content/contentTypeProcess.js
@@ -1,21 +1,10 @@
+import("system.neon");
 import("Bulkmail_lib");
 import("system.result");
 import("Document_lib");
 import("system.vars");
 import("DocumentTemplate_lib");
-/*
-var mimeType = DocumentUtil.getMimeTypeFromUpload(vars.get("$field.bindata"));
-var type;
-if (mimeType)
-{
-    type = DocumentTemplate.types.fromMimeType(mimeType);
-}
-else
-{
-    let template = BulkMailUtils.getBulkMailTemplate(vars.get("$field.BULKMAILID"), vars.get("$field.DOCUMENTTEMPLATE_ID"), false);
-    type = template.type;
-}
-*/
-result.string(vars.get("$context.currentTemplateType") == DocumentTemplate.types.TXT
+
+result.string(vars.get("$context.currentTemplateType") == DocumentTemplate.types.TXT// && vars.get("$sys.recordstate") != neon.OPERATINGSTATE_VIEW
     ? "LONG_TEXT" 
     : "HTML");
\ No newline at end of file
diff --git a/entity/BulkMail_entity/entityfields/content/displayValueProcess.js b/entity/BulkMail_entity/entityfields/content/displayValueProcess.js
index 792b6ffb62..a2ecf36da7 100644
--- a/entity/BulkMail_entity/entityfields/content/displayValueProcess.js
+++ b/entity/BulkMail_entity/entityfields/content/displayValueProcess.js
@@ -1,3 +1,4 @@
+import("system.logging");
 import("Bulkmail_lib");
 import("system.text");
 import("Employee_lib");
@@ -14,5 +15,9 @@ if (vars.get("$sys.viewmode") == neon.FRAME_VIEWMODE_DATASET)
     
     var contactId = EmployeeUtils.getCurrentContactId();
     var preview = template.getReplacedContentByContactId(contactId, false, true);
-    result.string(preview);
+    
+    if (vars.get("$context.currentTemplateType") == DocumentTemplate.types.TXT)
+        result.string(text.text2html(preview, false));
+    else
+        result.string(preview);
 }
\ No newline at end of file
diff --git a/entity/BulkMail_entity/entityfields/content/stateProcess.js b/entity/BulkMail_entity/entityfields/content/stateProcess.js
index 1674111c06..fa18329824 100644
--- a/entity/BulkMail_entity/entityfields/content/stateProcess.js
+++ b/entity/BulkMail_entity/entityfields/content/stateProcess.js
@@ -3,7 +3,9 @@ import("system.vars");
 import("system.neon");
 import("DocumentTemplate_lib");
 
-if (vars.get("$sys.recordstate") != neon.OPERATINGSTATE_VIEW && vars.get("$context.currentTemplateType") == DocumentTemplate.types.EML)
+if (vars.get("$sys.recordstate") != neon.OPERATINGSTATE_VIEW && 
+    (vars.get("$context.currentTemplateType") == DocumentTemplate.types.EML
+      || vars.get("$context.currentTemplateType") == DocumentTemplate.types.HTML)) // TODO: invisible for html as there are problems with images which are removed by the html-editor
 {
     result.string(neon.COMPONENTSTATE_INVISIBLE);
 }
\ No newline at end of file
diff --git a/entity/BulkMail_entity/entityfields/content/valueProcess.js b/entity/BulkMail_entity/entityfields/content/valueProcess.js
index 9b8c05e630..ec9c8eda42 100644
--- a/entity/BulkMail_entity/entityfields/content/valueProcess.js
+++ b/entity/BulkMail_entity/entityfields/content/valueProcess.js
@@ -6,19 +6,14 @@ import("system.db");
 import("DocumentTemplate_lib");
 import("Bulkmail_lib");
 
-if (vars.get("$sys.viewmode") == neon.FRAME_VIEWMODE_DATASET)
+if (vars.get("$sys.viewmode") == neon.FRAME_VIEWMODE_DATASET && vars.get("$this.value") == null)
 {
-    var upload = new FileUpload(vars.get("$field.bindata"));
-    var template;
-
-    if (upload.bindata)
-        template = DocumentTemplate.fromUpload(upload);
-    else
-        template = BulkMailUtils.getBulkMailTemplate(vars.get("$field.BULKMAILID"), vars.get("$field.DOCUMENTTEMPLATE_ID"), false);
-    
-    vars.set("$context.currentTemplateType", template.type);
-    
-    var content = template.getReplacedContent({});
-    if (content)
-        result.string(content);
+    [content, type] = DocumentTemplateUtils.getTemplateContent(vars.get("$field.DOCUMENTTEMPLATE_ID"), new FileUpload(vars.get("$field.bindata")));
+    vars.set("$context.currentTemplateType", type);
+    result.string(content);
+} 
+else if (!vars.get("$field.DOCUMENTTEMPLATE_ID"))
+{
+    result.string("");
+    vars.set("$context.currentTemplateType", "");
 }
\ No newline at end of file
diff --git a/entity/BulkMail_entity/entityfields/documenttemplate_id/onValueChange.js b/entity/BulkMail_entity/entityfields/documenttemplate_id/onValueChange.js
index f91b0bf6be..a088775e18 100644
--- a/entity/BulkMail_entity/entityfields/documenttemplate_id/onValueChange.js
+++ b/entity/BulkMail_entity/entityfields/documenttemplate_id/onValueChange.js
@@ -7,6 +7,13 @@ import("Entity_lib");
 
 if (vars.get("$sys.recordstate") == neon.OPERATINGSTATE_NEW)
 {
+    // note: could not use only the valueProcess from $field.content because then it refreshed $field.content just before save.
+    // --> only set in $field.content.valueProcess if $field.content is null and set it from here only if MASK triggered change
+    [content, type] = DocumentTemplateUtils.getTemplateContent(vars.get("$local.value"), new FileUpload(vars.get("$field.bindata")));
+    vars.set("$context.currentTemplateType", type);
+    vars.set("$field.content", content)
+    
+    // TODO: load subject without loadTemplate again... maybe return with getTemplateContent() above?
     var templateId = vars.get("local.value");
     var template = DocumentTemplate.loadTemplate(templateId, undefined, false);
     if (template.type == DocumentTemplate.types.EML)
diff --git a/entity/BulkMail_entity/entityfields/preview/contentTypeProcess.js b/entity/BulkMail_entity/entityfields/preview/contentTypeProcess.js
deleted file mode 100644
index 6d7fd7c7c5..0000000000
--- a/entity/BulkMail_entity/entityfields/preview/contentTypeProcess.js
+++ /dev/null
@@ -1,7 +0,0 @@
-import("system.vars");
-import("system.result");
-import("DocumentTemplate_lib");
-
-result.string(vars.get("$context.currentTemplateType") == DocumentTemplate.types.TXT
-    ? "LONG_TEXT" 
-    : "HTML");
\ No newline at end of file
diff --git a/entity/DocumentTemplate_entity/entityfields/bindata/onValueChange.js b/entity/DocumentTemplate_entity/entityfields/bindata/onValueChange.js
index 50fc4cc09f..4634a56420 100644
--- a/entity/DocumentTemplate_entity/entityfields/bindata/onValueChange.js
+++ b/entity/DocumentTemplate_entity/entityfields/bindata/onValueChange.js
@@ -6,5 +6,7 @@ import("system.vars");
 // --> only set in $field.Content.valueProcess if $field.Content is null and set it from here only if MASK triggered change
 if((vars.get("$sys.operatingstate") == neon.OPERATINGSTATE_EDIT || vars.get("$sys.operatingstate") == neon.OPERATINGSTATE_NEW))
 {
-    vars.set("$field.Content", DocumentTemplateUtils.getTemplateContent(vars.get("$field.DOCUMENTTEMPLATEID"), new FileUpload(vars.get("$local.value"))))
+    [content, type] = DocumentTemplateUtils.getTemplateContent(vars.get("$field.DOCUMENTTEMPLATEID"), new FileUpload(vars.get("$local.value")));
+    vars.set("$context.currentTemplateType", type);
+    vars.set("$field.content", content)
 }
diff --git a/entity/DocumentTemplate_entity/entityfields/content/valueProcess.js b/entity/DocumentTemplate_entity/entityfields/content/valueProcess.js
index 2a136f8c77..5158388e7c 100644
--- a/entity/DocumentTemplate_entity/entityfields/content/valueProcess.js
+++ b/entity/DocumentTemplate_entity/entityfields/content/valueProcess.js
@@ -6,5 +6,7 @@ import("system.vars");
 
 if((vars.get("$sys.operatingstate") == neon.OPERATINGSTATE_EDIT || vars.get("$sys.operatingstate") == neon.OPERATINGSTATE_NEW) && vars.get("$this.value") == null)
 {
-    result.string(DocumentTemplateUtils.getTemplateContent(vars.get("$field.DOCUMENTTEMPLATEID"), new FileUpload(vars.get("$field.BINDATA"))))
+    [content, type] = DocumentTemplateUtils.getTemplateContent(vars.get("$field.DOCUMENTTEMPLATEID"), new FileUpload(vars.get("$field.BINDATA")));
+    vars.set("$context.currentTemplateType", type);
+    result.string(content);
 }
diff --git a/process/DocumentTemplate_lib/process.js b/process/DocumentTemplate_lib/process.js
index 02d1352aca..d6b04b6584 100644
--- a/process/DocumentTemplate_lib/process.js
+++ b/process/DocumentTemplate_lib/process.js
@@ -69,7 +69,7 @@ DocumentTemplate.prototype.toString = function (pWithSubtemplates)
 }
 
 DocumentTemplate.prototype._resolveEmbeddedTemplate = function ()
-{    
+{
     // currently we support only txt and html as others would need special caution.
     if (this.content != null && (this.type == DocumentTemplate.types.TXT || this.type == DocumentTemplate.types.HTML))
     {
@@ -88,7 +88,7 @@ DocumentTemplate.prototype._resolveEmbeddedTemplate = function ()
         
         if (this.type == DocumentTemplate.types.HTML)
         {
-            templates.concat(db.table(SqlCondition.begin()
+            templates = templates.concat(db.table(SqlCondition.begin()
                         .andPrepare("DOCUMENTTEMPLATE.KIND", $KeywordRegistry.documentTemplateType$textModular())
                         .andPrepare("DOCUMENTTEMPLATE.CLASSIFICATION", $KeywordRegistry.documentTemplateTypeCategory$htmlTemplate())
                         .buildSql("select DOCUMENTTEMPLATEID, REPLACEMENTNAME from DOCUMENTTEMPLATE")));
@@ -287,10 +287,27 @@ DocumentTemplate.prototype.getReplacedContent = function (pReplacements, pEncode
 {
     // if there exists a _subtemplatedContent we use it because then I assume that the replacements are already based on content + subtemplates
     var content = this._getTemplatedContent(true);
-        
+    
     switch (this.type)
     {
+        case DocumentTemplate.types.EML:
+            let emlContent
+            if (pEmlOnlyBody)
+            {
+                // get only body and treat it as html (next case)
+                var email = Email.fromRFC(content);
+                content = util.encodeBase64String(email.body);
+            }
+            else
+            {
+                emlContent = util.decodeBase64String(content);
+                emlContent = TemplateHelper._replaceText(emlContent, pReplacements, TemplateHelper._getSpecialRegexp(this));
+                if (pEncoded)
+                    emlContent = util.encodeBase64String(emlContent);
+                return emlContent;
+            }
         case DocumentTemplate.types.HTML:
+            // replaces ä, ö, ü, ... with html escape signs
             for (let i in pReplacements)
                 pReplacements[i] = text.text2html(pReplacements[i], false);
         case DocumentTemplate.types.TXT:
@@ -299,19 +316,6 @@ DocumentTemplate.prototype.getReplacedContent = function (pReplacements, pEncode
             if (pEncoded)
                 encodedContent = util.encodeBase64String(encodedContent);
             return encodedContent;
-        case DocumentTemplate.types.EML:
-            let emlContent
-            if (pEmlOnlyBody)
-            {
-                var email = Email.fromRFC(content)
-                emlContent = email.body
-            }
-            else
-                emlContent = util.decodeBase64String(content);
-            emlContent = TemplateHelper._replaceText(emlContent, pReplacements, TemplateHelper._getSpecialRegexp(this));
-            if (pEncoded)
-                emlContent = util.encodeBase64String(emlContent);
-            return emlContent;
         case DocumentTemplate.types.ODT:
             return TemplateHelper._getReplacedODT(this, pReplacements);
         case DocumentTemplate.types.DOCX:
@@ -479,7 +483,6 @@ TemplateHelper._getRequiredPlaceholders = function (pTemplate)
         {
             return foundPlaceholders.indexOf(pPlaceholder.placeholderName) != -1;
         });
-        logging.log(JSON.stringify([placeholders], null, "\t"))
         return placeholders;
     }
     
@@ -770,10 +773,11 @@ DocumentTemplateUtils.updateTemplateData = function (pTemplateId, pFileUpload, p
  * @param {String} pTemplateId the id of the template
  * @param {FileUpload} pFileUpload upload object
  * 
- * @return {String} content or ""
+ * @return {String[]} [content, type] or ["", null]
  */
 DocumentTemplateUtils.getTemplateContent = function (pTemplateId, pFileUpload)
 {
+    logging.log(JSON.stringify([pTemplateId, pFileUpload.isFilled()], null, "\t"))
     var type;
     var bindata;
     
@@ -798,12 +802,14 @@ DocumentTemplateUtils.getTemplateContent = function (pTemplateId, pFileUpload)
         }
     }
     
-    if (type == DocumentTemplate.types.HTML || type == DocumentTemplate.types.TXT)
+    logging.log("asd" + JSON.stringify([type], null, "\t"))
+    
+    if (type == DocumentTemplate.types.HTML || type == DocumentTemplate.types.TXT || type == DocumentTemplate.types.EML)
     {
-        return util.decodeBase64String(bindata);
+        return [util.decodeBase64String(bindata), type];
     }
     
-    return "";
+    return ["", null];
 }
 
 /**
-- 
GitLab