From 08c33c69a8146f7c4cdcf84e58b7fd8268a24ebe Mon Sep 17 00:00:00 2001
From: Johannes Hoermann <j.hoermann@adito.de>
Date: Wed, 27 Nov 2019 11:18:07 +0100
Subject: [PATCH] warning on ampaign participants which have any
 commrestriction

---
 .../CampaignParticipant_entity.aod            | 10 +++---
 .../advertisingban_icon/colorProcess.js       |  6 ++--
 .../displayValueProcess.js                    | 12 +++++++
 .../advertisingban_icon/valueProcess.js       | 19 ------------
 .../advertisingban.value/expression.js        |  6 ----
 .../advertisingban_icon.value/expression.js   | 11 +++++++
 entity/Person_entity/Person_entity.aod        | 10 ++++++
 .../displayValueProcess.js                    |  8 +++++
 .../expression.js                             | 31 +++++++++++++++++++
 .../_____LANGUAGE_EXTRA.aod                   |  6 ++++
 .../_____LANGUAGE_de/_____LANGUAGE_de.aod     |  7 +++++
 .../_____LANGUAGE_en/_____LANGUAGE_en.aod     |  6 ++++
 .../PersonPreview_view/PersonPreview_view.aod |  2 ++
 process/Contact_lib/process.js                |  4 +--
 14 files changed, 102 insertions(+), 36 deletions(-)
 create mode 100644 entity/CampaignParticipant_entity/entityfields/advertisingban_icon/displayValueProcess.js
 delete mode 100644 entity/CampaignParticipant_entity/entityfields/advertisingban_icon/valueProcess.js
 delete mode 100644 entity/CampaignParticipant_entity/recordcontainers/participantsdbrecordcontainer/recordfieldmappings/advertisingban.value/expression.js
 create mode 100644 entity/CampaignParticipant_entity/recordcontainers/participantsdbrecordcontainer/recordfieldmappings/advertisingban_icon.value/expression.js
 create mode 100644 entity/Person_entity/entityfields/commrestrictions_active/displayValueProcess.js
 create mode 100644 entity/Person_entity/recordcontainers/db/recordfieldmappings/commrestrictions_active.value/expression.js

diff --git a/entity/CampaignParticipant_entity/CampaignParticipant_entity.aod b/entity/CampaignParticipant_entity/CampaignParticipant_entity.aod
index 560c1dc549..f1b69e47f9 100644
--- a/entity/CampaignParticipant_entity/CampaignParticipant_entity.aod
+++ b/entity/CampaignParticipant_entity/CampaignParticipant_entity.aod
@@ -190,7 +190,7 @@
       <name>ADVERTISINGBAN_ICON</name>
       <colorProcess>%aditoprj%/entity/CampaignParticipant_entity/entityfields/advertisingban_icon/colorProcess.js</colorProcess>
       <contentType>IMAGE</contentType>
-      <valueProcess>%aditoprj%/entity/CampaignParticipant_entity/entityfields/advertisingban_icon/valueProcess.js</valueProcess>
+      <displayValueProcess>%aditoprj%/entity/CampaignParticipant_entity/entityfields/advertisingban_icon/displayValueProcess.js</displayValueProcess>
     </entityField>
     <entityConsumer>
       <name>ParticipantsCommRestiction</name>
@@ -277,10 +277,6 @@
           <name>CAMPAIGNPARTICIPANTID.displayValue</name>
           <recordfield>CAMPAIGNPARTICIPANT.CAMPAIGNPARTICIPANTID</recordfield>
         </dbRecordFieldMapping>
-        <dbRecordFieldMapping>
-          <name>ADVERTISINGBAN.value</name>
-          <expression>%aditoprj%/entity/CampaignParticipant_entity/recordcontainers/participantsdbrecordcontainer/recordfieldmappings/advertisingban.value/expression.js</expression>
-        </dbRecordFieldMapping>
         <dbRecordFieldMapping>
           <name>STANDARD_PHONE_COMMUNICATION.displayValue</name>
           <expression>%aditoprj%/entity/CampaignParticipant_entity/recordcontainers/participantsdbrecordcontainer/recordfieldmappings/standard_phone_communication.displayvalue/expression.js</expression>
@@ -289,6 +285,10 @@
           <name>STANDARD_EMAIL_COMMUNICATION.displayValue</name>
           <expression>%aditoprj%/entity/CampaignParticipant_entity/recordcontainers/participantsdbrecordcontainer/recordfieldmappings/standard_email_communication.displayvalue/expression.js</expression>
         </dbRecordFieldMapping>
+        <dbRecordFieldMapping>
+          <name>ADVERTISINGBAN_ICON.value</name>
+          <expression>%aditoprj%/entity/CampaignParticipant_entity/recordcontainers/participantsdbrecordcontainer/recordfieldmappings/advertisingban_icon.value/expression.js</expression>
+        </dbRecordFieldMapping>
       </recordFieldMappings>
     </dbRecordContainer>
   </recordContainers>
diff --git a/entity/CampaignParticipant_entity/entityfields/advertisingban_icon/colorProcess.js b/entity/CampaignParticipant_entity/entityfields/advertisingban_icon/colorProcess.js
index 3eb3a718c4..1bcf3daac5 100644
--- a/entity/CampaignParticipant_entity/entityfields/advertisingban_icon/colorProcess.js
+++ b/entity/CampaignParticipant_entity/entityfields/advertisingban_icon/colorProcess.js
@@ -4,12 +4,10 @@ import("system.db");
 import("system.vars");
 import("system.neon");
 import("Campaign_lib");
-
-var commres = CampaignUtils.checkforCommRestrictions(vars.get("$field.CONTACT_ID"), vars.get("$field.CAMPAIGNSTEP_ID"));
                 
-if(commres)
+if(vars.get("$field.ADVERTISINGBAN_ICON") > 0)
 {
-    result.string(neon.PRIORITY_HIGH_COLOR);
+    result.string(neon.PRIORITY_MEDIUM_COLOR);
 }
 else
 {
diff --git a/entity/CampaignParticipant_entity/entityfields/advertisingban_icon/displayValueProcess.js b/entity/CampaignParticipant_entity/entityfields/advertisingban_icon/displayValueProcess.js
new file mode 100644
index 0000000000..837878d3b7
--- /dev/null
+++ b/entity/CampaignParticipant_entity/entityfields/advertisingban_icon/displayValueProcess.js
@@ -0,0 +1,12 @@
+import("system.result");
+import("system.vars");
+
+// Now show warning always, if any commrestriction exists. No matter which medium the current step has.
+if (vars.get("$field.ADVERTISINGBAN_ICON") == 0)
+{
+    result.string("VAADIN:CHECK");
+}
+else
+{
+    result.string("VAADIN:WARNING");
+}
\ No newline at end of file
diff --git a/entity/CampaignParticipant_entity/entityfields/advertisingban_icon/valueProcess.js b/entity/CampaignParticipant_entity/entityfields/advertisingban_icon/valueProcess.js
deleted file mode 100644
index 9fdd845ab1..0000000000
--- a/entity/CampaignParticipant_entity/entityfields/advertisingban_icon/valueProcess.js
+++ /dev/null
@@ -1,19 +0,0 @@
-import("system.vars");
-import("system.result");
-import("KeywordRegistry_basic");
-import("Keyword_lib");
-import("Sql_lib");
-import("system.db");
-import("Campaign_lib");
-
-var commres = CampaignUtils.checkforCommRestrictions(vars.get("$field.CONTACT_ID"), vars.get("$field.CAMPAIGNSTEP_ID"));
-                                
-if(commres)
-{
-    var keywordAttributes = KeywordUtils.getAttributeRelationsByKey(commres, $KeywordRegistry.communicationMediumCampaign())
-    result.string(keywordAttributes.AdvertisingBanIcon);
-}
-else
-{
-    result.string("VAADIN:CHECK");
-}
\ No newline at end of file
diff --git a/entity/CampaignParticipant_entity/recordcontainers/participantsdbrecordcontainer/recordfieldmappings/advertisingban.value/expression.js b/entity/CampaignParticipant_entity/recordcontainers/participantsdbrecordcontainer/recordfieldmappings/advertisingban.value/expression.js
deleted file mode 100644
index 6d7b0fbce0..0000000000
--- a/entity/CampaignParticipant_entity/recordcontainers/participantsdbrecordcontainer/recordfieldmappings/advertisingban.value/expression.js
+++ /dev/null
@@ -1,6 +0,0 @@
-import("system.result");
-
-result.string("(select MEDIUM from CAMPAIGNSTEP \n\
-                join COMMRESTRICTION on MEDIUM = STEPMEDIUM \n\
-                where COMMRESTRICTION.CONTACT_ID = CAMPAIGNPARTICIPANT.CONTACT_ID \n\
-                and CAMPAIGNSTEP.CAMPAIGNSTEPID = CAMPAIGNPARTICIPANT.CAMPAIGNSTEP_ID)");
\ No newline at end of file
diff --git a/entity/CampaignParticipant_entity/recordcontainers/participantsdbrecordcontainer/recordfieldmappings/advertisingban_icon.value/expression.js b/entity/CampaignParticipant_entity/recordcontainers/participantsdbrecordcontainer/recordfieldmappings/advertisingban_icon.value/expression.js
new file mode 100644
index 0000000000..2e544b656b
--- /dev/null
+++ b/entity/CampaignParticipant_entity/recordcontainers/participantsdbrecordcontainer/recordfieldmappings/advertisingban_icon.value/expression.js
@@ -0,0 +1,11 @@
+import("system.vars");
+import("Contact_lib");
+import("Sql_lib");
+import("system.result");
+
+// Now show warning always, if any commrestriction exists. No matter which medium the current step has.
+result.string("(" + newSelect("count(*)")
+                        .from("CONTACT")
+                        .where("CONTACT.CONTACTID = CAMPAIGNPARTICIPANT.CONTACT_ID")
+                        .and(newWhere(ContactUtils.getCommRestrictionCondition(undefined, false, vars.get("$sys.date"))))
+                        .toString() + ")");
\ No newline at end of file
diff --git a/entity/Person_entity/Person_entity.aod b/entity/Person_entity/Person_entity.aod
index 80ba6e1aea..be93a1f209 100644
--- a/entity/Person_entity/Person_entity.aod
+++ b/entity/Person_entity/Person_entity.aod
@@ -1148,6 +1148,12 @@ Usually this is used for filtering COMMUNICATION-entries by a specified contact
     <entityField>
       <name>LEAD_LEADID</name>
     </entityField>
+    <entityField>
+      <name>COMMRESTRICTIONS_ACTIVE</name>
+      <title>Commrestrictions</title>
+      <color>$priority-high-color</color>
+      <displayValueProcess>%aditoprj%/entity/Person_entity/entityfields/commrestrictions_active/displayValueProcess.js</displayValueProcess>
+    </entityField>
   </entityFields>
   <recordContainers>
     <dbRecordContainer>
@@ -1400,6 +1406,10 @@ Usually this is used for filtering COMMUNICATION-entries by a specified contact
           <isFilterable v="true" />
           <filtertype>BASIC</filtertype>
         </consumerMapping>
+        <dbRecordFieldMapping>
+          <name>COMMRESTRICTIONS_ACTIVE.value</name>
+          <expression>%aditoprj%/entity/Person_entity/recordcontainers/db/recordfieldmappings/commrestrictions_active.value/expression.js</expression>
+        </dbRecordFieldMapping>
       </recordFieldMappings>
       <filterExtensions>
         <filterExtensionSet>
diff --git a/entity/Person_entity/entityfields/commrestrictions_active/displayValueProcess.js b/entity/Person_entity/entityfields/commrestrictions_active/displayValueProcess.js
new file mode 100644
index 0000000000..7c4af0e4fa
--- /dev/null
+++ b/entity/Person_entity/entityfields/commrestrictions_active/displayValueProcess.js
@@ -0,0 +1,8 @@
+import("system.vars");
+import("system.translate");
+import("system.result");
+
+if (vars.get("$field.COMMRESTRICTIONS_ACTIVE").trim())
+{
+    result.string(translate.text("No advertising by") + " " + vars.get("$field.COMMRESTRICTIONS_ACTIVE").trim());
+}
diff --git a/entity/Person_entity/recordcontainers/db/recordfieldmappings/commrestrictions_active.value/expression.js b/entity/Person_entity/recordcontainers/db/recordfieldmappings/commrestrictions_active.value/expression.js
new file mode 100644
index 0000000000..183c051b39
--- /dev/null
+++ b/entity/Person_entity/recordcontainers/db/recordfieldmappings/commrestrictions_active.value/expression.js
@@ -0,0 +1,31 @@
+import("system.vars");
+import("system.result");
+import("Sql_lib");
+import("Keyword_lib");
+import("KeywordRegistry_basic");
+
+var mediumList = KeywordUtils.getEntryNamesAndIdsByContainer($KeywordRegistry.communicationMediumCampaign());
+var sqlMasking = new SqlMaskingUtils();
+
+var parts = [];
+
+var orgContactSubselect = newSelect("orgContact.CONTACTID")
+                                .from("CONTACT anyContact")
+                                .join("CONTACT orgContact", newWhere("anyContact.ORGANISATION_ID = orgContact.ORGANISATION_ID")
+                                                                .and("orgContact.PERSON_ID IS NULL"))
+                                .where("anyContact.CONTACTID = CONTACT.CONTACTID")
+    
+mediumList.forEach(function(pMedium) 
+{
+    var subquery = newSelect("COMMRESTRICTION.MEDIUM, COMMRESTRICTIONID")
+                        .from("COMMRESTRICTION")
+                        .where("COMMRESTRICTION.MEDIUM", pMedium[0])
+                        .and("COMMRESTRICTION.STARTDATE", vars.get("$sys.date"), "# <= ?")
+                        .and(newWhere()
+                                .or("COMMRESTRICTION.CONTACT_ID = CONTACT.CONTACTID")
+                                .or("COMMRESTRICTION.CONTACT_ID", orgContactSubselect));
+                                
+    parts.push("case when exists(" + subquery.toString() + ") then '" + pMedium[1] + "' else '' end");
+})
+
+result.string(sqlMasking.concat(parts, ", ", false));
\ No newline at end of file
diff --git a/language/_____LANGUAGE_EXTRA/_____LANGUAGE_EXTRA.aod b/language/_____LANGUAGE_EXTRA/_____LANGUAGE_EXTRA.aod
index 6f7dae0b4a..4bda0f67b2 100644
--- a/language/_____LANGUAGE_EXTRA/_____LANGUAGE_EXTRA.aod
+++ b/language/_____LANGUAGE_EXTRA/_____LANGUAGE_EXTRA.aod
@@ -5915,6 +5915,12 @@
     <entry>
       <key>File from this template could not be found anymore. Please go to the template and upload a new file.</key>
     </entry>
+    <entry>
+      <key>Commrestrictions</key>
+    </entry>
+    <entry>
+      <key>No advertising by</key>
+    </entry>
   </keyValueMap>
   <font name="Dialog" style="0" size="11" />
   <sqlModels>
diff --git a/language/_____LANGUAGE_de/_____LANGUAGE_de.aod b/language/_____LANGUAGE_de/_____LANGUAGE_de.aod
index 70a21bfacd..01f7641510 100644
--- a/language/_____LANGUAGE_de/_____LANGUAGE_de.aod
+++ b/language/_____LANGUAGE_de/_____LANGUAGE_de.aod
@@ -7431,6 +7431,13 @@
       <key>File from this template could not be found anymore. Please go to the template and upload a new file.</key>
       <value>Die Datei dieser Vorlage konnte nicht mehr gefunden werde. Bitte laden Sie bei der Dokumentvorlage eine neue Datei hoch.</value>
     </entry>
+    <entry>
+      <key>Commrestrictions</key>
+    </entry>
+    <entry>
+      <key>No advertising by</key>
+      <value>Keine Werbung durch</value>
+    </entry>
   </keyValueMap>
   <font name="Dialog" style="0" size="11" />
 </language>
diff --git a/language/_____LANGUAGE_en/_____LANGUAGE_en.aod b/language/_____LANGUAGE_en/_____LANGUAGE_en.aod
index afc6d30b6a..d77cfa098a 100644
--- a/language/_____LANGUAGE_en/_____LANGUAGE_en.aod
+++ b/language/_____LANGUAGE_en/_____LANGUAGE_en.aod
@@ -5965,6 +5965,12 @@
     <entry>
       <key>File from this template could not be found anymore. Please go to the template and upload a new file.</key>
     </entry>
+    <entry>
+      <key>Commrestrictions</key>
+    </entry>
+    <entry>
+      <key>No advertising by</key>
+    </entry>
   </keyValueMap>
   <font name="Dialog" style="0" size="11" />
 </language>
diff --git a/neonView/PersonPreview_view/PersonPreview_view.aod b/neonView/PersonPreview_view/PersonPreview_view.aod
index 3ba66814fe..8d097377da 100644
--- a/neonView/PersonPreview_view/PersonPreview_view.aod
+++ b/neonView/PersonPreview_view/PersonPreview_view.aod
@@ -17,6 +17,7 @@
       <descriptionField>ORGANISATION_CONTACTID</descriptionField>
       <favoriteAction1>newActivity</favoriteAction1>
       <entityField>#ENTITY</entityField>
+      <informationField>COMMRESTRICTIONS_ACTIVE</informationField>
     </cardViewTemplate>
     <neonViewReference>
       <name>5a1b7683-2fec-4763-9b45-e4c7a18d70fd</name>
@@ -36,6 +37,7 @@
     <genericViewTemplate>
       <name>Info</name>
       <showDrawer v="true" />
+      <hideEmptyFields v="true" />
       <entityField>#ENTITY</entityField>
       <fields>
         <entityFieldLink>
diff --git a/process/Contact_lib/process.js b/process/Contact_lib/process.js
index 1f8eef3ec9..47e0a359f3 100644
--- a/process/Contact_lib/process.js
+++ b/process/Contact_lib/process.js
@@ -456,7 +456,7 @@ ContactUtils.getDefaultAddressId = function(pContactId)
 /**
  * makes a SqlCondition that checks if there's (not) a commrestriction for a contact
  * 
- * @param {String} pMedium medium to check
+ * @param {String} [pMedium=undefined] medium to check if undefined, don't check it
  * @param {boolean} [pNoRestriction=false] if true, the condition gets every contact that has no
  *                                     a commrestriction, otherwise every contact that has a commrestriction
  * @param {String|Number} [pStartDate=current date] date to check against the start date of the commrestriction
@@ -471,7 +471,7 @@ ContactUtils.getCommRestrictionCondition = function (pMedium, pNoRestriction, pS
         .select("COMMRESTRICTIONID")
         .from("COMMRESTRICTION")
         .where(SqlCondition.begin()
-            .andPrepare("COMMRESTRICTION.MEDIUM", pMedium)
+            .andPrepareIfSet("COMMRESTRICTION.MEDIUM", pMedium)
             .andPrepare("COMMRESTRICTION.STARTDATE", pStartDate, "# <= ?")
             .and(SqlCondition.begin()
                     // check contactid
-- 
GitLab