added list function to add subscriber to membership list
authorAnthony Talarico <talarico@gaslightmedia.com>
Tue, 17 May 2016 14:29:58 +0000 (10:29 -0400)
committerAnthony Talarico <talarico@gaslightmedia.com>
Tue, 17 May 2016 14:29:58 +0000 (10:29 -0400)
class-streamsendfeedaddon.php
lib/class_streamsend_api.php

index 8233033..12081e0 100644 (file)
@@ -13,6 +13,7 @@ class GFStreamSendAddOn extends GFFeedAddOn
     protected $_title = 'Gravity Forms StreamSend Integration';
     protected $_short_title = 'StreamSend';
     protected $_errorFlag = false;
+    protected $listID;
 
     private static $_instance = null;
     protected $streamSendFields = array(
@@ -105,18 +106,19 @@ class GFStreamSendAddOn extends GFFeedAddOn
         $apiLogin = rgar($settings, 'glm_streamsend_login');
         $apiKey = rgar($settings, 'glm_streamsend_key');
         $ss = new StreamSend(STREAMSEND_BASE_URL, $apiLogin, $apiKey);
-//        echo '<pre>', print_r($ss->listList(),true), '</pre>';
-        $lists = $ss->listList();
-        $lists = $lists->responseData->list;
-        for($i = 0; $i < count($lists); $i++){
-           
+        $getList = $ss->listList();
+        $listData = $getList->responseData;
+        $lists = array();
+        for($i = 0; $i < count($listData->list[$i]); $i++){
+            $lists[] =array('label'=>$listData->list[$i]->name,'value'=>$listData->list[$i]->id);
         }
-        
+        array_unshift($lists,array('label'=>'Select a List', 'value'=>''));
         
         return array(
             array(
                 'title'  => 'StreamSend Form Settings',
                 'fields' => array(
+                    
                     array(
                         'label'   => esc_html__( 'Feed name', 'StreamSend' ),
                         'type'    => 'text',
@@ -124,6 +126,14 @@ class GFStreamSendAddOn extends GFFeedAddOn
                         'tooltip' => esc_html__( 'Just a name', 'StreamSend' ),
                         'class'   => 'small',
                     ),
+                    array(
+                         'label'   => 'Lists',
+                         'type'    => 'select',
+                         'name'    => 'lists',
+                         'tooltip' => 'Select a list to add the contact',
+                         'choices' => $lists
+                               
+                        ),
                     array(
                         'name'      => 'mappedFields',
                         'label'     => esc_html__( 'Map Fields', 'StreamSend' ),
@@ -185,13 +195,7 @@ class GFStreamSendAddOn extends GFFeedAddOn
                         'checkbox_label' => esc_html__( 'Enable Condition', 'StreamSend' ),
                         'instructions'   => esc_html__( 'Process this StreamSend feed if', 'StreamSend' ),
                     ),
-                    array(
-                        'name'           => 'Lists',
-                        'label'          => esc_html__( 'List', 'StreamSend' ),
-                        'type'           => 'select',
-                        'checkbox_label' => esc_html__( 'Enable Condition', 'StreamSend' ),
-                        'instructions'   => esc_html__( 'Process this StreamSend feed if', 'StreamSend' ),
-                    ),
+                    
                 )
             )
         );
@@ -202,6 +206,8 @@ class GFStreamSendAddOn extends GFFeedAddOn
             'feedName' => __( 'Name', 'StreamSend' ),
         );
     }
+    
+    // validate the api key and login input
     public function is_valid_setting(){
         
         $settings = $this->get_plugin_settings();
@@ -216,6 +222,8 @@ class GFStreamSendAddOn extends GFFeedAddOn
                      echo '<h4 style="color:red; margin: 2px;"> Invalid Login or API Key</h4>';
                echo '</div>';
         }
+        
+        // use JS to auto input the audience ID into the audience field and make the input readOnly
         ?>
         <script type="text/javascript">
             document.addEventListener("DOMContentLoaded", function(event) { 
@@ -223,10 +231,13 @@ class GFStreamSendAddOn extends GFFeedAddOn
             var audienceId = "<?php echo $audienceId; ?>";  
             document.getElementById("glm_streamsend_audience").value = audienceId;
             document.getElementById("glm_streamsend_audience").readOnly = true;
+            
         });
         </script>
         <?php
     }
+    
+    // generate the error notice to be displayed if the api or login input is invalid
     public function errorNotice(){
         if($this->errorFlag){
             echo '<div class="push-alert-red" style="border-left: 1px solid #E6DB55; border-right: 1px solid #E6DB55;"> ';
@@ -241,16 +252,21 @@ class GFStreamSendAddOn extends GFFeedAddOn
     public function process_feed( $feed, $entry, $form ) {
         $feedName           = $feed['meta']['feedName'];
         $field_map = $this->get_field_map_fields( $feed, 'mappedFields' );
-
+        $this->listID = $feed['meta']['lists']; // get the list ID from a selected list
+        
         $merge_vars = array();
         foreach ( $field_map as $name => $field_id ) {
             $merge_vars[ $name ] = $this->get_field_value( $form, $entry, $field_id );
         }
-
+        // if a list is selected there will be an id, then use the contactUpload function since it takes a list id parameter
+        if(!empty($this->listID)){
+            // contact upload function here
+             $return = $this->add_contact( $merge_vars, $this->listID );
+        }
         $return = $this->add_contact( $merge_vars );
     }
 
-    public function add_contact($values)
+    public function add_contact($values, $listId='')
     {
         $settings = $this->get_plugin_settings();
         $apiLogin = rgar( $settings, 'glm_streamsend_login' );
@@ -274,6 +290,8 @@ class GFStreamSendAddOn extends GFFeedAddOn
                     $ret->contact->id,
                     $contactData
                 );
+            }else if(!empty($listId)){
+                $listAdd = $ss->addToList($contactData, $this->listID);
             } else {
                 $contacts = $ss->contactCreate(
                     $contactData,
@@ -282,6 +300,7 @@ class GFStreamSendAddOn extends GFFeedAddOn
                     STREAMSEND_DEFAULT_DELIVER_WELCOME
                 );
             }
+
             if (!$contacts) {
                 return false;
                 //echo '<p><h3>Debug Results</h3>'.$ss->debugBuffer.'</p>';
index 7e4dd1e..78fce49 100644 (file)
@@ -1417,6 +1417,7 @@ class StreamSend {
                 $post_data .= '  <'.trim($k).'>'.trim($v).'</'.trim($k).">\n";
             }
         }
+        
         $post_data .= "  <activate>$activate</activate>\n"
                      ."  <deliver-activation>$deliver</deliver-activation>\n"
                      ."  <deliver-welcome>$welcome</deliver-welcome>\n";
@@ -1425,7 +1426,8 @@ class StreamSend {
         $this->debug('contactCreate() XML POST request data<br /><table border="1"><tr><td><pre>'
                     ."\n".htmlentities($this->postData)."\n"
                     .'</pre></td></tr></table>');
-
+        
+        
        /** This operation requires the "POST" Method */
         if ($this->sendRequest('POST', 'audiences/'.STREAMSEND_AUDIENCE.'/people') === false) {
             $this->debug('contactCreate() Call to sendRequest() failed.');
@@ -1585,6 +1587,40 @@ class StreamSend {
 
         return(clone $this);
     }
+    function addToList($contact, $listId)
+    {
+        
+        $this->debug('addToList() called');    
+        
+        $email = $contact['email-address'];
+        
+        $post_data = "<membership>\n";
+        $post_data .= "  <list-id>$listId</list-id>\n"
+                     ."  <email-address>$email</email-address>\n";
+        $post_data .= "</membership>\n";
+        $this->postData = $post_data;
+        $this->debug('contactCreate() XML POST request data<br /><table border="1"><tr><td><pre>'
+                    ."\n".htmlentities($this->postData)."\n"
+                    .'</pre></td></tr></table>');
+        
+         /** This operation requires the "POST" Method */
+        if ($this->sendRequest('POST', 'audiences/'.STREAMSEND_AUDIENCE.'/lists/'.$listId.'/memberships') === false) {
+            $this->debug('contactCreate() Call to sendRequest() failed.');
+            return false;
+        }
+
+        /**
+         * Check if the response request was successful
+         */
+        if ($this->responseError > 0) {
+            $this->debug('addToList() Call to sendRequest() returned an error code.');
+        } else {
+            $this->debug('addToList() Received good response from sendRequest().');
+        }
+
+        return(clone $this);
+        
+    }
 
 
 
@@ -1946,7 +1982,6 @@ class StreamSend {
             $this->debug('uploadContacts() A valid list of contacts was not provided.');
             return false;
         }
-
         $this->debug('uploadContacts() '.count($contacts).' contacts provided.');
 
         /**
@@ -1956,7 +1991,6 @@ class StreamSend {
             $this->debug('uploadContacts() Call to fieldsList() failed.');
             return false;
         }
-
         /** Email_address is not returned in fieldsList() but is required. Curiously it doesn't have an ID like the other fields */
         $available_fields = array( 'email_address' => 'email_address' );
         foreach( $fields_list->responseData->field as $field ) {
@@ -1971,18 +2005,15 @@ class StreamSend {
         $upload_data = $contact_fields = $contact_data_header = $contact_data_header_sep = '';
         $numb_contacts = $numb_fields = 0;
         $have_email_field = false;
-
         foreach( $contacts as $fields ) {
 
             $field = 0;
             $contact_data = '';
-
             /** If this is not the first contact, make sure we have the same number of fields as the first. */
             if( $numb_contacts > 0 && count($fields) != $numb_fields ) {
                 $this->debug('uploadContacts() Contact # '.($numb_contacts+1).' does not have the same number of fields as the first.');
                 return false;
             }
-
             /** For each field in this contact */
             while( list($k, $v) = each($fields) ) {
 
@@ -2014,7 +2045,6 @@ class StreamSend {
                         $this->debug('uploadContacts() Fields for contact # '.($numb_contacts+1).' did not match first contact.');
                         return false;
                     }
-
                 /** Add field to contact, use "|" separator if it's not the first field */
                 $contact_data .= ($field>0?"\t":'').$v;