Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Mailchimp API help


Reply


571 Views

Avatar
tv

Community Member, 44 Posts

11 December 2011 at 3:29am

I am trying to integrate the Mailchimp API into a Silverstripe form. Using the smartchimp module, I was able to get it working, but I can't figure out how to implement the ajax functionality so that there is no re-direct on form submission. I would like to have the success message or api error message load in a response div above the form. Here's the code for the Smartchimp module:

?php

class SmartChimpSignupPage extends Page {

   static $db = array(
      //   @todo:   provide optional dropdown for entering username/password??
      'MCApiKey'         => 'Varchar(50)',   //   api_key
      'MCListKey'         => 'Varchar(50)',   //   list_unique_id
      'MCSuccessContent'   => 'HTMLText'
   );

   static $mc_api_version = '1.2';

   function getCMSFields() {
      $fields = parent::getCMSFields();

      $fields->addFieldsToTab('Root.Content.MailChimpConfig',array(
         new TextField('MCApiKey', _t('SmartChimp.MCAPIKEY', 'Username')),
         new TextField('MCListKey', _t('SmartChimp.MCLISTKEY', 'Unique ID for List')),
         new HTMLEditorField('MCSuccessContent', _t('SmartChimp.MCSuccessContent', 'Signup Success Content'))
      ));

      $this->extend('updateSmartChimpCMSFields');

      return $fields;
   }

}

class SmartChimpSignupPage_Controller extends Page_Controller {

   function Form() {
      if (Session::get('SmartChimp.SUCCESS')) {
         Session::clear('SmartChimp.SUCCESS');
         return false;
      }

      $form = new Form($this, 'Form',
         new FieldSet(
            //   @todo:   add language support for these fields!
            new TextField('email', 'Email Address'),
            new TextField('fname', 'First Name'),
            new TextField('lname', 'Last Name')
         ),
         new FieldSet(
            new FormAction('SignupAction', 'Sign Up')
         ),
         new RequiredFields('email')
      );

      $this->extend('updateSmartChimpForm', $form);

      return $form;
   }

   public function mcsuccess() {
      if (Session::get('SmartChimp.SUCCESS'))
         $this->Content = $this->MCSuccessContent;

      return $this;
   }

   function SignupAction($raw_data, $form) {
      $data = Convert::raw2sql($raw_data);

      require_once('MCAPI.class.php');

      $api = new MCAPI("$this->MCApiKey");
      $list_key = $this->MCListKey;
      $mergeVars = array(
         'FNAME'   => $data['fname'],
         'LNAME'   => $data['lname']
      );

      $this->extend('updateSmartChimpSignupAction', $data, $mergeVars);

      if (true === $api->listSubscribe($list_key, $data['email'], $mergeVars)) {
         //   success!
         Session::set('SmartChimp.SUCCESS', true);
         return $this->mcsuccess();
      }
      else {
         //   failure!
         $form->sessionMessage($api->errorMessage, 'warning');
         return $this;
      }
   }

}

I am only looking to do a basic listsubscribe() and don't need any of the fancy dancin that some of the other Silverstripe Mailchimp modules that I have seen provide (i.e., integration with members, access to my MC list from the SS CMS. etc).

Has anyone done anything like this? I'm stumped! Thanks.