From 700346d811ea0e27776ef3eaee10245611f4de9a Mon Sep 17 00:00:00 2001
From: Johannes Hoermann <j.hoermann@adito.de>
Date: Thu, 12 Sep 2019 09:56:38 +0200
Subject: [PATCH] Bulkmail editoer / preview cerrect display

---
 entity/BulkMail_entity/BulkMail_entity.aod           |  8 +++++++-
 .../entityfields/content/contentTypeProcess.js       |  2 +-
 .../entityfields/content/stateProcess.js             | 10 +++++-----
 .../entityfields/content/valueProcess.js             |  5 ++++-
 .../entityfields/downloadtemplate/onActionProcess.js |  6 +++---
 .../entityfields/viewmode_preview/stateProcess.js    | 12 ++++++++++++
 .../entityfields/viewmode_preview/valueProcess.js    |  4 ++++
 .../BulkMailContent_view/BulkMailContent_view.aod    |  4 ++--
 process/DocumentTemplate_lib/process.js              |  2 +-
 9 files changed, 39 insertions(+), 14 deletions(-)
 create mode 100644 entity/BulkMail_entity/entityfields/viewmode_preview/stateProcess.js
 create mode 100644 entity/BulkMail_entity/entityfields/viewmode_preview/valueProcess.js

diff --git a/entity/BulkMail_entity/BulkMail_entity.aod b/entity/BulkMail_entity/BulkMail_entity.aod
index 380476f5e8..2bf806ce64 100644
--- a/entity/BulkMail_entity/BulkMail_entity.aod
+++ b/entity/BulkMail_entity/BulkMail_entity.aod
@@ -139,7 +139,6 @@
       <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>
@@ -254,6 +253,13 @@
       <contentType>HTML</contentType>
       <valueProcess>%aditoprj%/entity/BulkMail_entity/entityfields/preview/valueProcess.js</valueProcess>
     </entityField>
+    <entityField>
+      <name>viewmode_preview</name>
+      <description></description>
+      <contentType>HTML</contentType>
+      <stateProcess>%aditoprj%/entity/BulkMail_entity/entityfields/viewmode_preview/stateProcess.js</stateProcess>
+      <valueProcess>%aditoprj%/entity/BulkMail_entity/entityfields/viewmode_preview/valueProcess.js</valueProcess>
+    </entityField>
   </entityFields>
   <recordContainers>
     <dbRecordContainer>
diff --git a/entity/BulkMail_entity/entityfields/content/contentTypeProcess.js b/entity/BulkMail_entity/entityfields/content/contentTypeProcess.js
index be637432e4..49308df2f6 100644
--- a/entity/BulkMail_entity/entityfields/content/contentTypeProcess.js
+++ b/entity/BulkMail_entity/entityfields/content/contentTypeProcess.js
@@ -5,6 +5,6 @@ import("Document_lib");
 import("system.vars");
 import("DocumentTemplate_lib");
 
-result.string(vars.get("$context.currentTemplateType") == DocumentTemplate.types.TXT && (vars.get("$sys.recordstate") == neon.OPERATINGSTATE_EDIT || vars.get("$sys.recordstate") == neon.OPERATINGSTATE_NEW)
+result.string(vars.get("$context.currentTemplateType") == DocumentTemplate.types.TXT
     ? "LONG_TEXT" 
     : "HTML");
\ 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 237d5230c0..6a64596397 100644
--- a/entity/BulkMail_entity/entityfields/content/stateProcess.js
+++ b/entity/BulkMail_entity/entityfields/content/stateProcess.js
@@ -10,12 +10,12 @@ if (vars.get("$sys.recordstate") != neon.OPERATINGSTATE_VIEW &&
 {
     result.string(neon.COMPONENTSTATE_INVISIBLE);
 }
-// in view mode disable for eml
-else if (vars.get("$sys.recordstate") == neon.OPERATINGSTATE_VIEW && 
-        (vars.get("$context.currentTemplateType") == DocumentTemplate.types.EML 
-        // in view mode also disable for html to work around missing image-support for the html-editor
-            || vars.get("$context.currentTemplateType") == DocumentTemplate.types.HTML))
+// in view mode disable, show preview instead: directly after save (in edit view) the state is changed to OPERATINGSTATE_VIEW and this triggers the initionalisation of the html editor (if it is visible)
+// For EML-files or other large files this is very bad, as the editor needs a very long time to initialize
+else if (vars.get("$sys.recordstate") == neon.OPERATINGSTATE_VIEW)
+{
     result.string(neon.COMPONENTSTATE_INVISIBLE);
+}
 else
 {
     result.string(neon.COMPONENTSTATE_EDITABLE);
diff --git a/entity/BulkMail_entity/entityfields/content/valueProcess.js b/entity/BulkMail_entity/entityfields/content/valueProcess.js
index ec9c8eda42..90841e809f 100644
--- a/entity/BulkMail_entity/entityfields/content/valueProcess.js
+++ b/entity/BulkMail_entity/entityfields/content/valueProcess.js
@@ -3,12 +3,15 @@ import("Document_lib");
 import("system.result");
 import("system.vars");
 import("system.db");
+import("system.util");
 import("DocumentTemplate_lib");
 import("Bulkmail_lib");
 
 if (vars.get("$sys.viewmode") == neon.FRAME_VIEWMODE_DATASET && vars.get("$this.value") == null)
 {
-    [content, type] = DocumentTemplateUtils.getTemplateContent(vars.get("$field.DOCUMENTTEMPLATE_ID"), new FileUpload(vars.get("$field.bindata")));
+    var template = BulkMailUtils.getBulkMailTemplate(vars.get("$field.BULKMAILID"), vars.get("$field.DOCUMENTTEMPLATE_ID"), false, new FileUpload(vars.get("$field.bindata")))
+    var content = util.decodeBase64String(template.content);
+    var type = template.type;
     vars.set("$context.currentTemplateType", type);
     result.string(content);
 } 
diff --git a/entity/BulkMail_entity/entityfields/downloadtemplate/onActionProcess.js b/entity/BulkMail_entity/entityfields/downloadtemplate/onActionProcess.js
index 518d384f32..a7f47f7561 100644
--- a/entity/BulkMail_entity/entityfields/downloadtemplate/onActionProcess.js
+++ b/entity/BulkMail_entity/entityfields/downloadtemplate/onActionProcess.js
@@ -1,7 +1,7 @@
+import("Bulkmail_lib");
 import("system.neon");
 import("system.vars");
-import("DocumentTemplate_lib");
 
-var template = DocumentTemplate.loadTemplate(vars.get("$field.DOCUMENTTEMPLATE_ID"), undefined, false);
-if (template.type)
+var template = BulkMailUtils.getBulkMailTemplate(vars.get("$field.BULKMAILID"), vars.get("$field.DOCUMENTTEMPLATE_ID"), false)
+if (template.content)
     neon.download(template.content, template.filename);
\ No newline at end of file
diff --git a/entity/BulkMail_entity/entityfields/viewmode_preview/stateProcess.js b/entity/BulkMail_entity/entityfields/viewmode_preview/stateProcess.js
new file mode 100644
index 0000000000..7bd814b3e1
--- /dev/null
+++ b/entity/BulkMail_entity/entityfields/viewmode_preview/stateProcess.js
@@ -0,0 +1,12 @@
+import("system.neon");
+import("system.result");
+import("system.vars");
+
+if (vars.get("$sys.recordstate") == neon.OPERATINGSTATE_VIEW)
+{
+    result.string(neon.COMPONENTSTATE_EDITABLE);
+}
+else
+{
+    result.string(neon.COMPONENTSTATE_INVISIBLE);
+}
\ No newline at end of file
diff --git a/entity/BulkMail_entity/entityfields/viewmode_preview/valueProcess.js b/entity/BulkMail_entity/entityfields/viewmode_preview/valueProcess.js
new file mode 100644
index 0000000000..b6f23f8b68
--- /dev/null
+++ b/entity/BulkMail_entity/entityfields/viewmode_preview/valueProcess.js
@@ -0,0 +1,4 @@
+import("system.vars");
+import("system.result");
+
+result.string("VIEW" + vars.get("$field.content.displayValue"));
\ No newline at end of file
diff --git a/neonView/BulkMailContent_view/BulkMailContent_view.aod b/neonView/BulkMailContent_view/BulkMailContent_view.aod
index 06a0bc7b78..a4aed1aa56 100644
--- a/neonView/BulkMailContent_view/BulkMailContent_view.aod
+++ b/neonView/BulkMailContent_view/BulkMailContent_view.aod
@@ -36,8 +36,8 @@
           <entityField>content</entityField>
         </entityFieldLink>
         <entityFieldLink>
-          <name>5ae4212f-4107-447e-af57-9ab60fb8075a</name>
-          <entityField>preview</entityField>
+          <name>3843d183-f2ed-4cc3-9202-e2414e0fb594</name>
+          <entityField>viewmode_preview</entityField>
         </entityFieldLink>
       </fields>
     </genericViewTemplate>
diff --git a/process/DocumentTemplate_lib/process.js b/process/DocumentTemplate_lib/process.js
index dc06c40dfb..b7f8dcdccc 100644
--- a/process/DocumentTemplate_lib/process.js
+++ b/process/DocumentTemplate_lib/process.js
@@ -139,7 +139,7 @@ DocumentTemplate.prototype.getAttachments = function ()
     {
         var attachmentIds = db.array(db.COLUMN, SqlCondition.begin()
                                                  .andPrepare("DOCUMENTTEMPLATELINK.DOCUMENTTEMPLATE_ID_PARENT", this.templateId)
-                                                 .buildSql("select DOCUMENTTEMPLATE_ID_CHILD from DOCUMENTTEMPLATELINK"))
+                                                 .buildSql("select DOCUMENTTEMPLATE_ID_CHILD from DOCUMENTTEMPLATELINK"));
 
         this._attachmentCache = attachmentIds.map(function(pAttachmentId)
         {
-- 
GitLab