From d6556efc30b6ba77730c3857b8cf165b14762df3 Mon Sep 17 00:00:00 2001
From: "d.lechner" <d.lechner@adito.de>
Date: Sat, 30 Mar 2019 11:29:56 +0100
Subject: [PATCH] Added first version for adding campaignparticipant

---
 entity/Person_entity/Person_entity.aod        | 56 ++-----------
 .../addtocampaign/onActionProcess.js          |  4 +
 .../_____LANGUAGE_de/_____LANGUAGE_de.aod     |  8 ++
 process/Campaign_lib/process.js               | 81 ++++++++-----------
 4 files changed, 54 insertions(+), 95 deletions(-)
 create mode 100644 entity/Person_entity/entityfields/addtocampaign/onActionProcess.js

diff --git a/entity/Person_entity/Person_entity.aod b/entity/Person_entity/Person_entity.aod
index 1cee641431..f00e42c4e4 100644
--- a/entity/Person_entity/Person_entity.aod
+++ b/entity/Person_entity/Person_entity.aod
@@ -697,19 +697,16 @@ Usually this is used for filtering COMMUNICATION-entries by a specified contact
       <name>DEPARTMENT</name>
       <title>Department</title>
       <possibleItemsProcess>%aditoprj%/entity/Person_entity/entityfields/department/possibleItemsProcess.js</possibleItemsProcess>
-      <newItemsAllowed v="true" />
     </entityField>
     <entityField>
       <name>POSITION</name>
       <title>Position</title>
       <possibleItemsProcess>%aditoprj%/entity/Person_entity/entityfields/position/possibleItemsProcess.js</possibleItemsProcess>
-      <newItemsAllowed v="true" />
     </entityField>
     <entityField>
       <name>CONTACTROLE</name>
       <title>Contactrole</title>
       <possibleItemsProcess>%aditoprj%/entity/Person_entity/entityfields/contactrole/possibleItemsProcess.js</possibleItemsProcess>
-      <newItemsAllowed v="true" />
     </entityField>
     <entityConsumer>
       <name>OtherContacts</name>
@@ -781,52 +778,13 @@ Usually this is used for filtering COMMUNICATION-entries by a specified contact
         </entityParameter>
       </children>
     </entityConsumer>
-    <entityConsumer>
-      <name>AttributeTree</name>
-      <title>Attribute Tree</title>
-      <fieldType>DEPENDENCY_OUT</fieldType>
-      <dependency>
-        <name>dependency</name>
-        <entityName>AttributeRelationTree_entity</entityName>
-        <fieldName>TreeProvider</fieldName>
-      </dependency>
-      <children>
-        <entityParameter>
-          <name>ObjectRowId_param</name>
-          <valueProcess>%aditoprj%/entity/Person_entity/entityfields/attributetree/children/objectrowid_param/valueProcess.js</valueProcess>
-          <triggerRecalculation v="true" />
-        </entityParameter>
-        <entityParameter>
-          <name>ObjectType_param</name>
-          <valueProcess>%aditoprj%/entity/Person_entity/entityfields/attributetree/children/objecttype_param/valueProcess.js</valueProcess>
-          <triggerRecalculation v="true" />
-        </entityParameter>
-      </children>
-    </entityConsumer>
-    <entityField>
-      <name>DATE_NEW</name>
-      <valueProcess>%aditoprj%/entity/Person_entity/entityfields/date_new/valueProcess.js</valueProcess>
-    </entityField>
-    <entityField>
-      <name>USER_NEW</name>
-      <valueProcess>%aditoprj%/entity/Person_entity/entityfields/user_new/valueProcess.js</valueProcess>
-    </entityField>
-    <entityField>
-      <name>USER_EDIT</name>
-      <valueProcess>%aditoprj%/entity/Person_entity/entityfields/user_edit/valueProcess.js</valueProcess>
-    </entityField>
-    <entityField>
-      <name>DATE_EDIT</name>
-      <valueProcess>%aditoprj%/entity/Person_entity/entityfields/date_edit/valueProcess.js</valueProcess>
-    </entityField>
-    <entityField>
-      <name>DATE_NEW_CONTACT</name>
-      <valueProcess>%aditoprj%/entity/Person_entity/entityfields/date_new_contact/valueProcess.js</valueProcess>
-    </entityField>
-    <entityField>
-      <name>USER_NEW_CONTACT</name>
-      <valueProcess>%aditoprj%/entity/Person_entity/entityfields/user_new_contact/valueProcess.js</valueProcess>
-    </entityField>
+    <entityActionField>
+      <name>addToCampaign</name>
+      <fieldType>ACTION</fieldType>
+      <title>Add to Campaign</title>
+      <onActionProcess>%aditoprj%/entity/Person_entity/entityfields/addtocampaign/onActionProcess.js</onActionProcess>
+      <tooltip>Choose a campaign and a step to add the contact to a  campaign</tooltip>
+    </entityActionField>
   </entityFields>
   <recordContainers>
     <dbRecordContainer>
diff --git a/entity/Person_entity/entityfields/addtocampaign/onActionProcess.js b/entity/Person_entity/entityfields/addtocampaign/onActionProcess.js
new file mode 100644
index 0000000000..1f7f086286
--- /dev/null
+++ b/entity/Person_entity/entityfields/addtocampaign/onActionProcess.js
@@ -0,0 +1,4 @@
+import("system.vars");
+import("Campaign_lib");
+
+CampaignUtils.addParticipants(vars.getString("$field.CONTACTID"));
\ No newline at end of file
diff --git a/language/_____LANGUAGE_de/_____LANGUAGE_de.aod b/language/_____LANGUAGE_de/_____LANGUAGE_de.aod
index bf8a6aadbb..5d855ab8f7 100644
--- a/language/_____LANGUAGE_de/_____LANGUAGE_de.aod
+++ b/language/_____LANGUAGE_de/_____LANGUAGE_de.aod
@@ -6,6 +6,10 @@
   <country></country>
   <variant></variant>
   <keyValueMap>
+    <entry>
+      <key>Add to Campaign</key>
+      <value>Zu Kampagne hinzufügen</value>
+    </entry>
     <entry>
       <key>Company</key>
       <value>Firma</value>
@@ -130,6 +134,10 @@
       <key>Days inactive</key>
       <value>Tage inaktiv</value>
     </entry>
+    <entry>
+      <key>Choose a campaign and a step to add the contact to a  campaign</key>
+      <value>Wählen Sie eine Kampagne und eine Stufe aus um den Kontakt hinzuzufügen</value>
+    </entry>
     <entry>
       <key>Active</key>
       <value>Aktiv</value>
diff --git a/process/Campaign_lib/process.js b/process/Campaign_lib/process.js
index b1029d5075..12abd120ac 100644
--- a/process/Campaign_lib/process.js
+++ b/process/Campaign_lib/process.js
@@ -1,56 +1,45 @@
 import("system.db");
-/*
-* Fügt Mitglieder zu einer Kampagne hinzu.
-*
-* @param {String []} pParticipants req die RELATIONID
-* @param {String} pCampaignID opt die ID der Kampagne
-* @param {String} pStepID opt ID des Schritts
-*
-* @return {integer} Anzahl keine Email vorhanden
-*/
-function addParticipants( pParticipants, pCampaignID, pStepID )
+import("Context_lib");
+
+/**
+ * Methods for campaignmanagement.
+ * Do not create an instance of this!
+ * 
+ * @class
+ */
+function CampaignUtils() {}
+
+/**
+ * Add Contact (Person or Organistaion) to a Campaign
+ * 
+ * @param {String} pRowIds req ContactIds
+ */
+CampaignUtils.addParticipants = function(pRowIds)
 {
-    if ( pStepID == undefined ) 
-        pStepID = selectCampaignStep(pCampaignID);
-    
-    if ( pCampaignID == "" || pStepID == null)
-        return 0;
-    
-    var count = 0;
-    
-    // aktuelle Anzahl - maximale Anzahl
-    var iscount = Number(db.cell("select count(*) from campaignparticipant where campaign_id = '" + pCampaignID + "'"));
-    var maxcount = db.cell("select max_participants from campaign where campaignid = '" + pCampaignID + "'");
-    if ( maxcount != "")	maxcount = Number(maxcount); else maxcount = 9999999;
+    _CampaignUtils._createNew("CampaignParticipant", pRowIds)
+}
 
-    var actdate = vars.getString("$sys.date");
-    var user = vars.getString("$sys.user");
-    var spalten = new Array("CAMPAIGNPARTICIPANTID", "RELATION_ID", "USER_NEW", "DATE_NEW", "CAMPAIGNSTEP_ID", "CAMPAIGN_ID");
-    var typen = db.getColumnTypes("CAMPAIGNPARTICIPANT", spalten);
-    var logcol = new Array("CAMPAIGN_ID", "STEP_ID", "RELATION_ID", "CAMPAIGNPARTICIPANT_ID", "DATE_NEW", "USER_NEW");
-    var logtyp = db.getColumnTypes("CAMPAIGNLOG", logcol);
-    var logvalue = new Array(pCampaignID, pStepID, 0, 0, actdate, user );
+/**
+ * Methods for campaignmanagement.
+ * Do not create an instance of this!
+ * 
+ * @ignore
+ * @class
+ */
+function _CampaignUtils() {}
 
-    for ( var i = 0; i < pParticipants.length; i++ )
+_ActivityTaskUtils._createNew = function(pContext, pRowIds)
+{
+    var params = {};
+    
+    //TODO: pRowIds via JSON to add more then one participant
+    if (pRowIds)
     {
-        if ( iscount < maxcount )
-        {
-            var newID = util.getNewUUID()
-            var werte = [ newID, pParticipants[i] , user, actdate, pStepID, pCampaignID ];
-            count += db.insertData("CAMPAIGNPARTICIPANT", spalten, typen, werte);
-            logvalue[2] = pParticipants[i];
-            logvalue[3] = newID;
-            db.insertData("CAMPAIGNLOG", logcol, logtyp, logvalue);
-            iscount++;
-        }
-        else
-        {
-            question.showMessage(translate.text("Die maximale Anzahl Teilnehmer ist erreicht!"));
-            break;
-        }
+        params["ObjectId_param"] = ContextUtils.getCurrentContextId();
+        params["RowId_param"]    = pRowIds;
     }
     
-    return count;
+    neon.openContext(pContext, null, null, neon.OPERATINGSTATE_NEW, params);
 }
 
 /**
-- 
GitLab