Update for Drupal 6

emerygjr - March 4, 2008 - 21:33
Project:Constant Contact
Version:5.x-1.x-dev
Component:Code
Category:task
Priority:normal
Assigned:Unassigned
Status:active
Description

Is there a drupal 6 version out soon?

#1

greenlight - June 17, 2008 - 14:59

Any update on 6.x compatibility?

#2

sampeckham - July 28, 2008 - 13:25

Try using webform and then add some code into the "Additional Processing" box on your form to send the details to CC.

The code to add can be found here: http://www.jamesbenson.net/projects/ConstantContact/

#3

gbrussel - January 13, 2009 - 14:40

That code looks like the Site Visitor API, is there any code using the REST API?

#4

michaek - January 29, 2009 - 01:06

This module only uses the Site Visitor API, as it relies on James Benson's class. A module that used the REST API would be superior, but I've ported the 5.x version to 6.x using Deadwood and went over the remaining bugs, and it appears to work acceptably, though I have not tested it completely. The only hitch I've noticed is that status messages are not displayed after actions - can anyone give this code a look and explain what's going on? Or perhaps let me know if the messages, such as 'You have been added to our list.', are visible in your install. (This does not appear to be a theming issue, as I have tested with Garland.)

In addition to porting to 6.x, I added a requirements hook to constant_contact.install that makes it obvious that you haven't downloaded the class from James Benson's site.

This module probably needs some attention. Some messages and titles are hard-coded, and it's likely users will want control over these things. Additionally, unsubscribing only works if you're logged in to the Drupal install, which seems a severe shortcoming. I'll try to give attention to these issues.

I've attached the (in progress) 6.x module here, as I don't have a CVS account, and I don't really relish the idea of wrangling with CVS anyhow.

AttachmentSize
constant_contact-6.x.tar_.gz 11.29 KB

#5

majnoona - March 11, 2009 - 20:18

Need this for a new site -- will give your attachment a try but would love to see a real release! I'd even be willing to be your CVS monkey if that's all that's standing in the way...

Thanks

#6

gbrussel - March 16, 2009 - 16:27

For those of you requiring this to work on your site, you can use the Webform module with additional processing like sampeckham mentioned. I've worked it out and I'll post the processing code for those of you that need it.

Form components: First Name (becomes first_name), Last Name (becomes last_name), Email Address (becomes email), Confirm Email Address(becomes confirm_email_address). Once you have those components, save the form, and place this code into the "Additional Validation" field under "Webform advanced settings":

<?php
$email
= $form_values['submitted_tree']['email'];
$first_name = $form_values['submitted_tree']['first_name'];
$last_name = $form_values['submitted_tree']['last_name'];
$confirm = $form_values['submitted_tree']['confirm_email_address'];

if (
$confirm !== $email) {
 
form_set_error('', 'Emails do not match.', $reset = FALSE);
}
?>

Then place this code in the "Additional Processing" field under "Webform advanced settings":

<?php
class ConstantContact {
  var
$add_subscriber_url = "http://ui.constantcontact.com/roving/wdk/API_AddSiteVisitor.jsp";
  var
$remove_subscriber_url = 'http://ui.constantcontact.com/roving/wdk/API_UnsubscribeSiteVisitor.jsp';
  function
setUsername($username) {
   
$this->username = $username;
  }
  function
setPassword($password) {
   
$this->password = $password;
  }
  function
setCategory($category) {
   
$this->category = $category;
  }
  function
getUsername() {
    return
urlencode($this->username);
  }
  function
getPassword() {
    return
urlencode($this->password);
  }
  function
getCategory() {
    return
urlencode($this->category);
  }
  function
add($email, $extra_fields = array()) {
   
$email = urlencode(strip_tags($email));
   
$data = 'loginName=' . $this->getUsername();
   
$data .= '&loginPassword=' . $this->getPassword();
   
$data .= '&ea=' . $email;
   
$data .= '&ic=' . $this->getCategory();
    if(
is_array($extra_fields)):
      foreach(
$extra_fields as $k => $v):
       
$data .= "&" . urlencode(strip_tags($k)) . "=" . urlencode(strip_tags($v));
      endforeach;
    endif;
    return
$this->_send($data, $this->add_subscriber_url);
  }
  function
remove($email) {
   
$email = urlencode(strip_tags($email));
   
$data = 'loginName=' . $this->getUsername();
   
$data .= '&loginPassword=' . $this->getPassword();
   
$data .= '&ea=' . $email;
    return
$this->_send($data, $this->remove_subscriber_url);
  }
  function
_send($data, $url) {
    if(!
function_exists('fopen')):
      exit(
"fopen function does not exist");
    endif;
    if(!
ini_get('allow_url_fopen')):
      exit(
"allow_url_fopen is not enabled in your php config file");
    endif;
   
$handle = fopen("$url?$data", "rb");
   
$contents = '';
    while (!
feof($handle)) {
     
$contents .= fread($handle, 192);
    }
   
fclose($handle);
    if(
trim($contents) == 0):
      return
true;
    endif;
    return
false;
  }
}

$email = $form_values['submitted_tree']['email'];
$first_name = $form_values['submitted_tree']['first_name'];
$last_name = $form_values['submitted_tree']['last_name'];
$confirm = $form_values['submitted_tree']['confirm_email_address'];
$extra_fields = array('First_Name' => $first_name, 'Last_Name' => $last_name);

$ConstantContact = new ConstantContact();
$ConstantContact->setUsername('My CC Username'); /* set your constant contact username */
$ConstantContact->setPassword('My CC password'); /* set your constant contact password */
$ConstantContact->setCategory('CC Interest Category'); /* set your constant contact interest category */

$ConstantContact->add($email, $extra_fields);
if(
$ConstantContact->add($email)):
 
drupal_set_message('New subscriber successfully added.', $type = 'status', $repeat = FALSE);
else:
 
drupal_set_message('Failed to add new subscriber.', $type = 'status', $repeat = FALSE);
endif;
?>

This code will include the ConstantContact class, which sets up the object and URLs to pass the form information to. It then sets up variables and brings in the information from the webform. Next it creates the ConstantContact object and then adds the user and sets a message.

You'll have to modify this code slightly (setUsername, setPassword, setCategory) to work with your CC account, but it should add the user beautifully.

#7

pauldawg - April 10, 2009 - 04:02

Any thoughts on what it would take to make a custom Action that would update or add or remove users from one or more contact lists based on checkboxes in the user's profile, so that action could be triggered whenever a user registered on the site or updated their profile?

#8

pauldawg - April 10, 2009 - 05:43

Actually for anyone looking to do this at registration, check out the Rules module. I just used the code posted here but in an Action to execute PHP code on registration and it worked like a charm!

#9

drewish - May 15, 2009 - 14:15
Title:6.1» Update for Drupal 6
Category:support request» task
Status:active» needs review

here's michaek code as a patch.

AttachmentSize
constant_contact_229996.patch 16.02 KB

#10

theoldfather - June 22, 2009 - 05:49

I was unable to enable allow_url_fopen. Here is an alternate version using cURL. Also added support for subscribing to multiple email lists. You will need to create a select field called Mailing Lists. The value for each select option should be the name of a Contact Contact contact list.

<?php
class ConstantContact {
  var
$add_subscriber_url = "http://ui.constantcontact.com/roving/wdk/API_AddSiteVisitor.jsp";
  var
$remove_subscriber_url = 'http://ui.constantcontact.com/roving/wdk/API_UnsubscribeSiteVisitor.jsp';
  function
setUsername($username) {
   
$this->username = $username;
  }
  function
setPassword($password) {
   
$this->password = $password;
  }
  function
setCategory($category) {
   
$this->category = $category;
  }
  function
getUsername() {
    return
urlencode($this->username);
  }
  function
getPassword() {
    return
urlencode($this->password);
  }
  function
getCategory() {
    return
urlencode($this->category);
  }
  function
add($email, $extra_fields = array()) {
   
$email = urlencode(strip_tags($email));
   
$data = 'loginName=' . $this->getUsername();
   
$data .= '&loginPassword=' . $this->getPassword();
   
$data .= '&ea=' . $email;
   
$data .= '&ic=' . $this->getCategory();
    if(
is_array($extra_fields)):
      foreach(
$extra_fields as $k => $v):
       
$data .= "&" . urlencode(strip_tags($k)) . "=" . urlencode(strip_tags($v));
      endforeach;
    endif;
    return
$this->_send($data, $this->add_subscriber_url);
  }
  function
remove($email) {
   
$email = urlencode(strip_tags($email));
   
$data = 'loginName=' . $this->getUsername();
   
$data .= '&loginPassword=' . $this->getPassword();
   
$data .= '&ea=' . $email;
    return
$this->_send($data, $this->remove_subscriber_url);
  }
  function
_send($data, $url) {
  
    if(
$fp = tmpfile()){
       
$ch = curl_init($url."?".$data);
       
curl_setopt($ch, CURLOPT_STDERR, $fp);
       
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
       
$output = curl_exec($ch);
    }
   
   
$contents = '';   
   
$contents .= $output;
   
curl_close($ch);
   
    if(
trim($contents) == 0):
      return
true;
    endif;
    return
false;
  }
}

$email = $form_values['submitted_tree']['email_address'];
$first_name = $form_values['submitted_tree']['first_name'];
$last_name = $form_values['submitted_tree']['last_name'];
$confirm = $form_values['submitted_tree']['confirm_email_address'];
$extra_fields = array('First_Name' => $first_name, 'Last_Name' => $last_name);
$lists = $form_values['submitted_tree']['mailing_lists'];


$ConstantContact = new ConstantContact();
$ConstantContact->setUsername('CC Username'); /* set your constant contact username */
$ConstantContact->setPassword('CC Password'); /* set your constant contact password */

foreach($lists as $list => $label){
   
$ConstantContact->setCategory($list); /* set your constant contact interest category */   
   
$ConstantContact->add($email, $extra_fields);   
    if(
$ConstantContact->add($email)):
     
drupal_set_message("New subscriber successfully added to <strong>$list</strong>.", $type = 'status', $repeat = True);
    else:
     
drupal_set_message("Failed to add new subscriber to <strong>$list</strong>.", $type = 'status', $repeat = True);
    endif;
}
?>

#11

fp - July 27, 2009 - 00:01

http://www.jamesbenson.net/projects/ConstantContact/ redirects to http://justphp.co.uk/projects/ConstantContact/ and says:

Unfortunately the Constant Contact PHP code has been discontinued and is no longer available from this website. Given enough time I plan to re-code everything to use the new Constant Contact API however I do not know when this will be.

In the meantime you can get it here: http://cvs.drupal.org/viewvc.py/drupal/contributions/modules/constant_co...

#12

fp - July 27, 2009 - 00:30

Also, http://developer.constantcontact.com/doc/siteVisitorAPI says:

THE SITE VISITOR APIs, while still functional, ARE BEING DEPRECATED. Support for the Site Visitor APIs will be withdrawn by the end of 2009. The functionality of the SITE VISITOR APIs has been subsumed by the REST APIs available on this site.

#13

goddess - August 16, 2009 - 02:55

You ROCK! This code worked perfectly for me. Thank you.

#14

justphp - September 3, 2009 - 08:26
Assigned to:Anonymous» justphp
Status:needs review» fixed

I've developed a Constant Contact module for Drupal version 6 or above.

See here for more info:
http://cc.justphpsoftware.com/

#15

fp - September 5, 2009 - 23:48
Assigned to:justphp» Anonymous
Status:fixed» active

Where you can find that it is sold for £29.99 and but you won't find info on the licensing.

This is, in my opinion, not an acceptable resolution, unless, of course, you're planning on releasing it here. If this is not part of your plans, perhaps you'd be looking at finding a new maintainer for this project?

#16

jenpasch - September 9, 2009 - 16:57

With respect to solution #10:

I am getting a webform log message that has an error within this line (75)

(Invalid argument supplied for foreach() in /.../webform.module(1742) : eval()'d code on line 75.)

foreach($lists as $list => $label){
$ConstantContact->setCategory($list); /* set your constant contact interest category */

I was thinking that the variables from my added "mailing_lists" field were passed along to $list, but perhaps they are not.

My question is then: how do I "set [my] constant contact interest category" (which I am guessing are my list names)

thanks for the code!

#17

jeffschuler - October 30, 2009 - 22:09

@fp, re:#15...

For code that is not in Drupal's CVS repository, however, the GPL still applies. Drupal modules and themes are a derivative work of Drupal itself. That means any code in them that "links" with Drupal code is covered by the GPL and if distributed must be distributed under the GPL as well. For PHP code, that effectively means that it is called from Drupal or it calls Drupal, so any module or theme is included, including template files.

There is no requirement that you distribute a theme you develop; however, if you do so, even if you are selling it commercially, then the PHP and Javascript portions of it must be distributed under the GPL. Images and CSS files are data, not code, so the requirements of the GPL on Drupal do not apply to them.

-- Drupal Licensing FAQ

Just sayin.

#18

fp - November 10, 2009 - 02:13

Good to know. Thanks jeffschuler.

 
 

Drupal is a registered trademark of Dries Buytaert.