diff -urp ./mailing_list.install ../mailing_list_enhanced/mailing_list.install --- ./mailing_list.install 2008-09-02 11:30:49.000000000 -0400 +++ ../mailing_list_enhanced/mailing_list.install 2008-09-29 18:04:52.000000000 -0400 @@ -26,28 +26,41 @@ function mailing_list_schema() { 'length' => 255, 'not null' => TRUE, ), + 'notify' => array( + 'description' => t('To notify or not to notify'), + 'type' => 'int', + 'size' => 'tiny', + ), + 'notification_email' => array( + 'description' => t('Where to send notification'), + 'type' => 'varchar', + 'length' => 255, + ), ), 'primary key' => array('id'), ); $schema['mailing_list_emails'] = array( - 'description' => t('Emails subscribed to specific fields'), + 'description' => t('Emails subscribed to specific lists'), 'fields' => array( - 'id' => array( + 'id' => array( 'type' => 'int', + 'type' => 'serial', 'unsigned' => TRUE, 'not null' => TRUE, ), + 'mailing_list_id' => array( + 'description' => t('Which mailing list the address is subscribed to.'), + 'type' => 'int', + 'not null' => TRUE, + ), 'email' => array( - 'description' => t('The title of this mailing list'), + 'description' => t('The email'), 'type' => 'varchar', 'length' => 255, 'not null' => TRUE, ), ), 'primary key' => array('id'), - 'unique keys' => array( - 'sub' => array('id', 'email'), - ), ); diff -urp ./mailing_list.module ../mailing_list_enhanced/mailing_list.module --- ./mailing_list.module 2008-07-20 02:11:35.000000000 -0400 +++ ../mailing_list_enhanced/mailing_list.module 2008-09-29 18:38:21.000000000 -0400 @@ -1,27 +1,92 @@ t('Mailing List'), + 'description' => t('Create, edit, and delete mailing lists.'), 'page callback' => 'mailing_list_admin', - 'access arguments' => array('administer mailing list'), + 'access arguments' => array('administer mailing lists'), ); + $items['admin/settings/mailing-list/%mailing_list/edit'] = array( 'title' => t('Edit Mailing List'), 'page callback' => 'drupal_get_form', 'page arguments'=> array('mailing_list_form', 3), - 'access arguments' => array('administer mailing list'), + 'access arguments' => array('administer mailing lists'), ); + $items['admin/settings/mailing-list/%mailing_list/delete'] = array( + 'title' => t('Delete Mailing List'), + 'page callback' => 'drupal_get_form', + 'page arguments' => array('mailing_list_delete_form', 3), + 'access arguments' => array('administer mailing lists'), + ); + + $items['mailing-list/%mailing_list/unsubscribe'] = array( + 'title' => t('Unsubscribe from a Mailing List'), + 'page callback' => 'mailing_list_unsubscribe', + 'page arguments' => array(1), + 'access arguments' => array('access mailing lists'), + ); + + $items['mailing-list/%mailing_list/export'] = array( + 'title' => t('Export Mailing List'), + 'page callback' => 'mailing_list_export', + 'page arguments' => array(1), + 'access arguments' => array('export mailing list'), + ); return $items; } +/* ********************************* + * mailing_list_block + * + * @return $block + * + * implements block hook + * + * view shows subscription form for + * a specific mailing list + * + * ********************************* + */ function mailing_list_block($op = 'list', $delta = 0, $edit = array()) { $block = array(); switch ($op) { @@ -47,70 +112,79 @@ function mailing_list_block($op = 'list' return $block; } -function mailing_list_subscription_form(&$form_state, $list ) { - $form = array(); - $form['id'] = array( - '#type' => 'hidden', - '#default_value' => $list->id, - ); - $form['list'] = array( - '#type' => 'hidden', - '#value' => $list->list, - ); - $form['email'] = array( - '#title' => t('Email'), - '#type' => 'textfield', - '#size' => 20, - ); - $form['submit'] = array( - '#value' => t('Subscribe'), - '#type' => 'submit', - '#submit' => array('mailing_list_subscription_form_submit'), - ); - $form['#validate'] = array('mailing_list_subscription_form_validate'); - return $form; -} -function mailing_list_subscription_form_validate($form, &$form_state) { - $message = user_validate_mail($form_state['values']['email']); - if ($message) { - form_set_error('email', $message); - } -} -function mailing_list_subscription_form_submit($form, &$form_state) { - if ($form_state['values']['id'] == null) { - drupal_set_message('Error occured while saving your subscription. Please contact sie administrators', 'error'); - return ; - } - $query = "INSERT INTO {mailing_list_emails} (id, email) VALUES (%d, '%s')"; - if (db_query($query, $form_state['values']['id'], $form_state['values']['email'])) { - drupal_set_message('Your Subscription to '. $form_state['values']['list'] .' has been saved'); - } - else { - drupal_set_message('Failed to subscribe to '. $form_state['values']['list'] .' please contact site administrators', 'error'); - } -} + +/* ********************************* + * mailing_list_load + * + * @param $id + * + * @return $return + * + * takes $id and returns mailing list from + * database for use in menu paths + * + * ********************************* + */ function mailing_list_load($id) { $query = "SELECT * FROM {mailing_list} WHERE id = %d"; $return = db_fetch_object(db_query($query, $id)); return $return; } + +/* ****************************************************************** * + * Admin Functions * + * ****************************************************************** * + */ + +/* ********************************* + * mailing_list_admin + * + * @return $output + * + * generates admin form + * + * ********************************* + */ function mailing_list_admin() { $output = ''; $output .= drupal_get_form('mailing_list_form'); $output .= mailing_list_lists(); return $output; } + +/* ********************************* + * mailing_list_lists + * + * @return theme('item_list', $rows, "Available Mailing Lists") + * + * generates themed list of mailing lists + * with links to edit the lists + * + * ********************************* + */ function mailing_list_lists() { $query = "SELECT * FROM {mailing_list}"; $result = db_query($query); $rows = array(); while ($row = db_fetch_object($result)) { - $rows[] = $row->list . ' ' . l('edit', 'admin/settings/mailing-list/'. $row->id .'/edit'); + $rows[] = $row->list . ' | ' . l('edit', 'admin/settings/mailing-list/'. $row->id .'/edit') . ' | ' . l('delete', 'admin/settings/mailing-list/' . $row->id . '/delete') . ' | ' . l('unsubscribe link' , 'mailing-list/' . $row->id . '/unsubscribe') . ' | ' . l('export' , 'mailing-list/' . $row->id . '/export'); } return theme('item_list', $rows, "Available Mailing Lists"); } +/* ********************************* + * mailing_list_form + * + * @param &$form_state + * @param $list = null + * + * @return $form + * + * mailing list creation / editing form + * + * ********************************* + */ function mailing_list_form(&$form_state, $list = null) { $form = array(); @@ -130,8 +204,21 @@ function mailing_list_form(&$form_state, '#title' => t('Mailing List Name'), '#type' => 'textfield', '#required' => TRUE, - '#default_value'=> isset($form_state['values']['list_name']) ? $form_state['values']['list_name']: isset($list)?$list->list:'', + '#default_value'=> isset($form_state['values']['list_name']) ? $form_state['values']['list_name']: isset($list)?$list->list : '', ); + + $form['form']['notify'] = array( + '#type' => 'checkbox', + '#title' => 'Send notification when list changes', + '#default_value' => isset($form_state['values']['notify']) ? $form_state['values']['notify']: ( isset($list) ? $list->notify : 0 ), + ); + + $form['form']['notification_email'] = array( + '#type' => 'textfield', + '#title' => 'Email Address to Receive Notification', + '#default_value' => isset($form_state['values']['notification_email']) ? $form_state['values']['notification_email']: ( isset($list) ? $list->notification_email : '' ), + ); + $form['form']['submit'] = array( '#value' => t('Save'), '#type' => 'submit', @@ -141,16 +228,71 @@ function mailing_list_form(&$form_state, return $form; } +/* ********************************* + * mailing_list_form_validate + * + * @param $form + * @param &$form_state + * + * ********************************* + */ +function mailing_list_form_validate($form, &$form_state){ + + //list id will be set automatically or is already set + //list name doesn't have any restrictions, and will + //be sanitized in the submit function, so that leaves + //checking notify and notification email. + + //save form values into variables for easier reference + $notify = $form_state['values']['notify']; + $notification_email = $form_state['values']['notification_email']; + + //only check notification email if notify is set + if($notify) + { + if(!$notification_email) + { + form_set_error('notification_email','You must provide an email address to which to send notification.'); + } + elseif(!filter_var($notification_email, FILTER_VALIDATE_EMAIL)) + { + form_set_error('notification_email','Notification must be sent to a valid email address. Please check that you\'ve entered it correctly.'); + } + } + +}// function mailing_list_form_validate + +/* ********************************* + * mailing_list_form_submit + * + * @param $form + * @param &$form_state + * + * updates database with new information + * + * ********************************* + */ function mailing_list_form_submit($form, &$form_state) { - if (isset($form_state['values']['id'])) { - $query = "UPDATE {mailing_list} SET list = '%s' WHERE id = %d"; - $result = db_query($query, $form_state['values']['list_name'], $form_state['values']['id']); + + //sanitize list name + $form_state['values']['list_name'] = filter_var($form_state['values']['list_name']); + + //are we updating an existing list or creating a new one? + if (isset($form_state['values']['id'])) + { + //update values if list exists + $query = "UPDATE {mailing_list} SET list = '%s',notify='%d',notification_email='%s' WHERE id = %d"; + $result = db_query($query, $form_state['values']['list_name'], $form_state['values']['notify'], $form_state['values']['notification_email'], $form_state['values']['id']); + } - else { - $query = "INSERT INTO {mailing_list} (list) VALUES ('%s')"; - $result = db_query($query, $form_state['values']['list_name']); + else + { + //insert new values if it doesn't + $query = "INSERT INTO {mailing_list} (list,notify,notification_email) VALUES ('%s','%d','%s')"; + $result = db_query($query, $form_state['values']['list_name'], $form_state['values']['notify'], $form_state['values']['notification_email']); } + //create and set appropriate messages if ($result) { if (isset($form_state['values']['id']) ) { drupal_set_message('Updated Mailing List: '. check_plain($form_state['values']['list_name'])); @@ -167,4 +309,495 @@ function mailing_list_form_submit($form, drupal_set_message('Failed to Create New Mailing List: '. check_plain($form_state['values']['list_name']), 'error'); } } -} \ No newline at end of file +} + +/* ********************************* + * mailing_list_delete_form + * + * @param $form_state + * @param object $list + * + * form to delete a mailing list and all + * associated email addresses from database + * + * ********************************* + */ +function mailing_list_delete_form($form_state, $list) { + + //warning paragraph - markup + $form['warning'] = array( + '#type' => 'markup', + '#value' => '
Are you sure you want to do this?
', + ); + + //hidden field - list id + $form['list_id'] = array( + '#type' => 'hidden', + '#value' => $list->id, + ); + + $form['list_name'] = array( + '#type' => 'hidden', + '#value' => $list->list, + ); + + //first submit button - cancel + $form['cancel'] = array( + '#type' => 'submit', + '#value' => t('Cancel'), + ); + + $form['delete'] = array( + '#type' => 'submit', + '#value' => t('Delete'), + ); + + $form['#redirect'] = 'admin/settings/mailing-list'; + return $form; + +}//function mailing_list_delete_form + +/* ********************************* + * mailing_list_delete_form_submit + * + * @param $form + * @param &$form_state + * + * checks which button was pushed. + * Just redirects if it was cancelled, + * otherwise deletes the mailing list + * and the email addresses associated + * with it. + * + * ********************************* + */ +function mailing_list_delete_form_submit($form, &$form_state) { + + //if the delete buttom was pressed, we delete + //otherwise, do nothing, just redirect as normal. + if ($form_state['clicked_button']['#id'] == 'edit-delete') + { + //the bad submit button was pressed so now we have + //to kill one of our beautiful mailing lists *sob* + + $id = $form_state['values']['list_id']; + $name = $form_state['values']['list_name']; + + //delete all the email address entries + $query = "DELETE FROM {mailing_list_emails} WHERE mailing_list_id='%d'"; + $email_result = db_query($query, $id); + + //delete the mailing list itself + $query = "DELETE FROM {mailing_list} WHERE id='%d'"; + $list_result = db_query($query, $id); + + //delete the block + $query = "DELETE FROM {blocks} WHERE module = 'mailing_list' AND delta='%d'"; + $block_result = db_query($query, $id); + + //generate appropriate messages + if($email_result && $list_result) + { + //success! + drupal_set_message($name . ' has successfully been deleted!'); + + } + else + { + if(!$email_result) + { + drupal_set_message('The email addresses associated with ' . $name . ' could not be deleted.', 'error'); + }// if(!$email_result) + + if(!$list_result) + { + drupal_set_message($name . 'could not be deleted.' , 'error'); + }// if(!$list_result) + + }// if($email_result && $list_result) + + }// if ($form_state['clicked_button']['#id'] == 'edit-delete') + +}// function mailing_list_delete_form_submit + +/* ****************************************************************** * + * Subscribe Functions * + * ****************************************************************** * + */ + +/* ********************************* + * mailing_list_subscription_form + * + * @param &$form_state + * @param $list + * + * @return $form + * + * Shows subscription form in block + * + * ********************************* + */ +function mailing_list_subscription_form(&$form_state, $list) { + $form = array(); + + //mailing list id + $form['id'] = array( + '#type' => 'hidden', + '#default_value' => $list->id, + ); + + //mailing list + $form['list'] = array( + '#type' => 'hidden', + '#value' => $list->list, + ); + + //email text field + $form['email'] = array( + '#title' => t('Email'), + '#type' => 'textfield', + '#size' => 15, + ); + + //submit button + $form['submit'] = array( + '#value' => t('Subscribe'), + '#type' => 'submit', + ); + + return $form; +} + +/* ********************************* + * mailing_list_subscription_form_validate + * + * @param $form + * @param &$form_state + * + * calls built in email validation function + * sets error if it fails + * + * ********************************* + */ +function mailing_list_subscription_form_validate($form, &$form_state) { + $message = user_validate_mail($form_state['values']['email']); + if ($message) { + form_set_error('email', $message); + } +} + +/* ********************************* + * mailing_list_subscription_form_submit + * + * @param $form + * @param $&form_state + * + * inserts email address into database and + * sends notification if requested + * + * ********************************* + */ +function mailing_list_subscription_form_submit($form, &$form_state) { + if ($form_state['values']['id'] == null) { + drupal_set_message('Error occured while saving your subscription. Please contact site administrators', 'error'); + return ; + } + $query = "INSERT INTO {mailing_list_emails} (mailing_list_id, email) VALUES (%d, '%s')"; + if (db_query($query, $form_state['values']['id'], $form_state['values']['email'])) { + mailing_list_notify($form_state['values']['email'],$form_state['values']['id']); + drupal_set_message('Your Subscription to '. $form_state['values']['list'] .' has been saved'); + + } + else { + drupal_set_message('Failed to subscribe to '. $form_state['values']['list'] .' please contact site administrators', 'error'); + } +} + +/* ****************************************************************** * + * Unsubscribe Functions * + * ****************************************************************** * + */ + +/* ********************************* + * mailing_list_unsubscribe + * + * @param $list + * + * checks that the list passed through + * the menu path exists and if so, + * calls drupal get form for the the + * unsubscribe form. If not, it outputs + * an error message. + * + * ********************************* + */ +function mailing_list_unsubscribe($list) { + + //check that the list exists + if($list) + { + //set introductory text + $output = "