Skip to content

Instantly share code, notes, and snippets.

@clrockwell
Created April 2, 2014 03:34
Show Gist options
  • Save clrockwell/5fab92ab12836b91d882 to your computer and use it in GitHub Desktop.
Save clrockwell/5fab92ab12836b91d882 to your computer and use it in GitHub Desktop.
As attendee registers, they are also added as a PHPList user; this is old
<?php
// $Id$
/**
* @file
*
* Add/Remove email addresses to/from phplist
* from Drupal pages
*
*/
/*
* Implementation of hook_menu()
*/
function phplistintegrate_menu() {
$items['newsletter-unsubscribe'] = array(
'title' => 'Attendee Newsletter - Change preferences',
'page callback' => 'drupal_get_form',
'page arguments' => array('phplistintegrate_unsubscribe_user'),
'access callback' => TRUE,
);
$items['newsletter-subscribe'] = array(
'title' => 'Attendee Newsletter - Subscribe',
'page callback' => 'drupal_get_form',
'page arguments' => array('phplistintegrate_subscribe_user'),
'access callback' => TRUE,
);
return $items;
}
/**
* Form Callback
*/
function phplistintegrate_subscribe_user($form, $form_state) {
$form['title'] = array(
'#markup' => '<h1>Subscribe to the MATS Newsletter</h1><p>The official MATS newsletter will provide valuable information leading up to the show.',
);
$form['email'] = array(
'#type' => 'textfield',
'#title' => t('Please enter your email address'),
'#required' => TRUE,
);
$form['email_confirm'] = array(
'#type' => 'textfield',
'#title' => t('Please confirm your email address'),
'#required' => TRUE,
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => 'Subscribe',
'#title' => t('Subscribe'),
);
return $form;
}
/**
* Implementation of hook_form_alter()
*/
function phplistintegrate_form_alter(&$form, &$form_state, &$form_id) {
if ($form_id == 'phplistintegrate_subscribe_user') {
honeypot_add_form_protection($form, $form_state, array('honeypot'));
}
}
/**
* Form validation
*/
function phplistintegrate_subscribe_user_validate(&$form, &$form_state) {
//emails must match and be valid
$v = $form_state['values'];
if ($v['email'] != $v['email_confirm']) {
form_error($form['email_confirm'], "The email addresses do not match");
}
else {
// if they match, then verify them
if (!valid_email_address($v['email'])) {
form_error($form['email'], 'The supplied email address does not appear to be valid');
}
}
}
/**
* Form submit
*/
function phplistintegrate_subscribe_user_submit($form, $form_state) {
phplistintegrate_add_user($form_state['values']['email']);
}
function phplistintegrate_unsubscribe_user($form_state) {
$uid = $_GET['uid'];
$form['title'] = array(
'#value' => '<h1>Attendee Newsletter Preferences</h1>',
);
$form['uid'] = array(
'#type' => 'hidden',
'#value' => $uid,
);
$form['intro'] = array(
'#value' => 'We are sorry to know that you no longer wish to receive communication from the Mid-America Trucking Show. Simply enter your email address below and click \'Unsubscribe\' to be removed from all future correspondence. Please note that you will NO LONGER receive any updates or registration information for the Mid-America Trucking Show',
);
$form['email'] = array(
'#type' => 'textfield',
'#title' => t('Please enter your email address'),
'#required' => TRUE,
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => 'Unsubscribe',
'#title' => t('Unsubscribe'),
);
return $form;
}
function phplistintegrate_unsubscribe_user_submit($form, $form_state) {
db_set_active('phplist');
$lid = 3;
$phplistid = db_result(db_query("SELECT * FROM {phplist_user_user} WHERE uniqid = '%s'", $form_state['values']['uid']));
db_query("DELETE FROM {phplist_listuser} WHERE userid=%d AND listid=%d", $phplistid, $lid);
db_set_active();
drupal_set_message('You have been successfully removed from future Attendee correspondence');
}
//need userid
function phplistintegrate_add_user($email,$listid = 3, $error_message = true) {
// first check
if(!phplistintegrate_does_user_exist($email)) {
$uniqid = user_password(32);
$confirmed = 1;
$blacklisted = 0;
$bouncecount = 0;
$htmlemail = 1;
$disabled = 0;
$fields = array(
'email' => $email,
'confirmed' => $confirmed,
'blacklisted' => $blacklisted,
'bouncecount' => $bouncecount,
'entered' => date('Y-m-d H:i:s', strtotime('now')),
'uniqid' => $uniqid,
'htmlemail' => $htmlemail,
'disabled' => $disabled
);
db_set_active('phplist');
$phplist_userid = db_insert('phplist_user_user')
->fields($fields)
->execute();
// add user to list
db_insert('phplist_listuser')
->fields(array(
'userid' => $phplist_userid,
'listid' => $listid,
'entered' => date('Y-m-d H:i:s', strtotime('now')),
))
->execute();
db_set_active();
if ($error_message) {
drupal_set_message('Thank you for subscribing to the MATS Newsletter. You can also <a href="httP://twitter.com/truckingshow">Follow Us on Twitter</a> and <a href="http://facebook.com/MidAmericaTruckingShow">Like Us on Facebook</a> to keep up-to-date!');
}
}
else {
// user already exists
if ($error_message) {
drupal_set_message("$email is already subscribed to the MATS mailing list. The first email goes out in December, and the last will be about a week before the show.");
}
}
}
function phplistintegrate_does_user_exist($email) {
db_set_active('phplist');
$record = db_query("SELECT * FROM {phplist_user_user} WHERE email = :email", array(':email' => $email))->fetchObject();
db_set_active();
if (!$record)
{
return false;
}
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment