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?
| Project: | Constant Contact |
| Version: | 5.x-1.x-dev |
| Component: | Code |
| Category: | task |
| Priority: | normal |
| Assigned: | Unassigned |
| Status: | active |
Is there a drupal 6 version out soon?
#1
Any update on 6.x compatibility?
#2
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
That code looks like the Site Visitor API, is there any code using the REST API?
#4
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.
#5
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
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
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
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
here's michaek code as a patch.
#10
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
http://www.jamesbenson.net/projects/ConstantContact/ redirects to http://justphp.co.uk/projects/ConstantContact/ and says:
In the meantime you can get it here: http://cvs.drupal.org/viewvc.py/drupal/contributions/modules/constant_co...
#12
Also, http://developer.constantcontact.com/doc/siteVisitorAPI says:
#13
You ROCK! This code worked perfectly for me. Thank you.
#14
I've developed a Constant Contact module for Drupal version 6 or above.
See here for more info:
http://cc.justphpsoftware.com/
#15
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
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
@fp, re:#15...
-- Drupal Licensing FAQ
Just sayin.
#18
Good to know. Thanks jeffschuler.