You are here

mailing_list.module in Mailing List 6

Same filename and directory in other branches
  1. 8 mailing_list.module
  2. 7 mailing_list.module

Minimalistic mailing list module.

File

mailing_list.module
View source
<?php

/**
 * @file
 * Minimalistic mailing list module.
 */

/**
 * Implementation of hook_help().
 */
function mailing_list_help($path, $arg) {
  if ($path == 'admin/build/mailing-list/%/import') {
    return t('The import facility allows you to upload a CSV file containing e-mail addresses, and optional names, to add to your mailing list. The CSV file should have the e-mails as its first column, and the names (where available) as its second column. It must not have any header row.');
  }
}

/**
 * Implementation of hook_perm().
 */
function mailing_list_perm() {
  return array(
    'administer mailing lists',
  );
}

/**
 * Implementation of hook_menu().
 */
function mailing_list_menu() {
  $items = array();
  $items['admin/build/mailing-list'] = array(
    'title' => t('Mailing lists'),
    'description' => t('Manage your mailing lists.'),
    'page callback' => 'mailing_list_lists',
    'access arguments' => array(
      'administer mailing lists',
    ),
    'file' => 'mailing_list.admin.inc',
  );
  $items['admin/build/mailing-list/list'] = array(
    'title' => 'List',
    'type' => MENU_DEFAULT_LOCAL_TASK,
    'weight' => -10,
  );
  $items['admin/build/mailing-list/add'] = array(
    'title' => t('Add'),
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'mailing_list_form',
    ),
    'access arguments' => array(
      'administer mailing lists',
    ),
    'type' => MENU_LOCAL_TASK,
    'file' => 'mailing_list.admin.inc',
  );
  $items['admin/build/mailing-list/%mailing_list'] = array(
    'title' => 'List e-mails',
    'type' => MENU_CALLBACK,
    'page callback' => 'mailing_list_emails',
    'page arguments' => array(
      3,
    ),
    'access arguments' => array(
      'administer mailing lists',
    ),
    'file' => 'mailing_list.admin.inc',
  );
  $items['admin/build/mailing-list/%mailing_list/list'] = array(
    'title' => 'List e-mails',
    'type' => MENU_DEFAULT_LOCAL_TASK,
    'weight' => -10,
  );
  $items['admin/build/mailing-list/%mailing_list/add'] = array(
    'title' => t('Add e-mail'),
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'mailing_list_email_form',
      3,
    ),
    'access arguments' => array(
      'administer mailing lists',
    ),
    'type' => MENU_LOCAL_TASK,
    'weight' => -5,
    'file' => 'mailing_list.admin.inc',
  );
  $items['admin/build/mailing-list/%mailing_list/import'] = array(
    'title' => t('Import e-mails'),
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'mailing_list_import_form',
      3,
    ),
    'access arguments' => array(
      'administer mailing lists',
    ),
    'type' => MENU_LOCAL_TASK,
    'weight' => -2,
    'file' => 'mailing_list.admin.inc',
  );
  $items['admin/build/mailing-list/%mailing_list/export'] = array(
    'title' => t('Export list'),
    'page callback' => 'mailing_list_export',
    'page arguments' => array(
      3,
    ),
    'access arguments' => array(
      'administer mailing lists',
    ),
    'type' => MENU_LOCAL_TASK,
    'weight' => 0,
    'file' => 'mailing_list.admin.inc',
  );
  $items['admin/build/mailing-list/%mailing_list/edit'] = array(
    'title' => t('Rename list'),
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'mailing_list_form',
      3,
    ),
    'access arguments' => array(
      'administer mailing lists',
    ),
    'type' => MENU_LOCAL_TASK,
    'weight' => 5,
    'file' => 'mailing_list.admin.inc',
  );
  $items['admin/build/mailing-list/%mailing_list/delete'] = array(
    'title' => 'Delete list',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'mailing_list_delete_confirm',
      3,
    ),
    'access arguments' => array(
      'administer mailing lists',
    ),
    'type' => MENU_LOCAL_TASK,
    'weight' => 10,
    'file' => 'mailing_list.admin.inc',
  );
  $items['admin/build/mailing-list/%mailing_list/%mailing_list_email'] = array(
    'title' => 'Edit e-mail',
    'type' => MENU_CALLBACK,
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'mailing_list_email_form',
      3,
      4,
    ),
    'access arguments' => array(
      'administer mailing lists',
    ),
    'file' => 'mailing_list.admin.inc',
  );
  $items['admin/build/mailing-list/%mailing_list/%mailing_list_email/delete'] = array(
    'title' => 'Delete e-mail',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'mailing_list_email_delete_confirm',
      3,
      4,
    ),
    'access arguments' => array(
      'administer mailing lists',
    ),
    'type' => MENU_CALLBACK,
    'file' => 'mailing_list.admin.inc',
  );
  return $items;
}

/**
 * Implementation of hook_block().
 */
function mailing_list_block($op = 'list', $delta = 0, $edit = array()) {
  $block = array();
  switch ($op) {
    case 'configure':
      $form['mailing_list_show_name_' . $delta] = array(
        '#type' => 'checkbox',
        '#title' => t('Show name field in subscription form'),
        '#default_value' => variable_get('mailing_list_show_name_' . $delta, 1),
        '#description' => t('Whether or not to show a text field in the subscription form that this block displays, letting a subscriber enter his or her name. If the name field is shown, it also becomes required.'),
      );
      return $form;
    case 'save':
      variable_set('mailing_list_show_name_' . $delta, $edit['mailing_list_show_name_' . $delta]);
    case 'list':
      $query = "SELECT * FROM {mailing_list}";
      $result = db_query($query);
      while ($list = db_fetch_object($result)) {
        $block[$list->mlid] = array(
          'info' => t('Mailing list: @name', array(
            '@name' => $list->name,
          )),
        );
      }
      break;
    case 'view':
      $list = mailing_list_load($delta);
      $block = array(
        'subject' => check_plain($list->name),
        'content' => drupal_get_form('mailing_list_subscription_form_' . $delta),
      );
      break;
  }
  return $block;
}

/**
 * Implementation of hook_forms().
 *
 * All subscription forms are build using mailing_list_subscription_form().
 * hook_forms() is required to provide unique form id for each one.
 */
function mailing_list_forms() {
  $query = db_query('SELECT * FROM {mailing_list}');
  while ($list = db_fetch_object($query)) {
    $forms['mailing_list_subscription_form_' . $list->mlid] = array(
      'callback' => 'mailing_list_subscription_form',
      'callback arguments' => array(
        $list,
      ),
    );
  }
  return $forms;
}

/**
 * Return the mailing list object matching a mailing list ID.
 *
 * @param $mlid
 *   The mailing list's ID.
 *
 * @return
 *   The mailing list object, if exists, FALSE otherwise.
 */
function mailing_list_load($mlid) {
  $query = "SELECT * FROM {mailing_list} WHERE mlid = %d";
  $return = db_fetch_object(db_query($query, $mlid));
  return $return;
}

/**
 * Return the mailing list e-mail object matching an e-mail ID.
 *
 * @param $eid
 *   The e-mail's ID.
 *
 * @return
 *   The mailing list e-mail object, if exists, FALSE otherwise.
 */
function mailing_list_email_load($eid) {
  $query = "SELECT * FROM {mailing_list_emails} WHERE eid = %d";
  $return = db_fetch_object(db_query($query, $eid));
  return $return;
}

/**
 * Gets the name for a subscriber e-mail if available, otherwise gets the
 * e-mail address.
 *
 * @param $data
 *   Subscriber object.
 *
 * @return
 *   Subscriber name or subscriber e-mail.
 */
function mailing_list_email_get_name($data) {
  $data = (object) $data;
  return !empty($data->name) ? $data->name : $data->mail;
}

/**
 * Display a form letting a user subscribe to a mailing list.
 */
function mailing_list_subscription_form(&$form_state, $list) {
  $form = array();
  $form['mlid'] = array(
    '#type' => 'hidden',
    '#default_value' => $list->mlid,
  );
  $form['ml_name'] = array(
    '#type' => 'hidden',
    '#value' => $list->name,
  );
  if (variable_get('mailing_list_show_name_' . $list->mlid, 1)) {
    $form['name'] = array(
      '#title' => t('Name'),
      '#type' => 'textfield',
      '#size' => 20,
      '#required' => TRUE,
    );
  }
  else {
    $form['name'] = array(
      '#type' => 'hidden',
      '#value' => '',
    );
  }
  $form['mail'] = array(
    '#title' => t('E-mail'),
    '#type' => 'textfield',
    '#size' => 20,
    '#required' => TRUE,
  );
  $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;
}

/**
 * Validation handler for the subscription form; checks name and e-mail
 * entered.
 */
function mailing_list_subscription_form_validate($form, &$form_state) {
  $message = user_validate_mail($form_state['values']['mail']);
  if ($message) {
    form_set_error('mail', $message);
  }
  elseif (db_fetch_object(db_query("SELECT * FROM {mailing_list_emails} WHERE mlid = %d AND mail = '%s'", $form_state['values']['mlid'], $form_state['values']['mail']))) {
    form_set_error('mail', t('The e-mail %mail already exists in mailing list %name.', array(
      '%mail' => $form_state['values']['mail'],
      '%name' => $form_state['values']['ml_name'],
    )));
  }
}

/**
 * Submit handler for the subscription form; saves a subscription.
 */
function mailing_list_subscription_form_submit($form, &$form_state) {
  if ($form_state['values']['mlid'] == null) {
    drupal_set_message(t('Unable to save mailing list subscription: no mailing list specified.'), 'error');
    return;
  }
  $query = "INSERT INTO {mailing_list_emails} (mlid, name, mail) VALUES (%d, '%s', '%s')";
  if (db_query($query, $form_state['values']['mlid'], $form_state['values']['name'], $form_state['values']['mail'])) {
    drupal_set_message(t('Subscription for %mail saved.', array(
      '%mail' => $form_state['values']['mail'],
    )));
    watchdog('mailing_list', 'Mailing list: %name added via subscription form.', array(
      '%name' => $form_state['values']['mail'],
    ));
  }
  else {
    drupal_set_message(t('Failed to subscribe to mailing list %name.', array(
      '%name' => $form_state['values']['ml_name'],
    )), 'error');
  }
}

Functions

Namesort descending Description
mailing_list_block Implementation of hook_block().
mailing_list_email_get_name Gets the name for a subscriber e-mail if available, otherwise gets the e-mail address.
mailing_list_email_load Return the mailing list e-mail object matching an e-mail ID.
mailing_list_forms Implementation of hook_forms().
mailing_list_help Implementation of hook_help().
mailing_list_load Return the mailing list object matching a mailing list ID.
mailing_list_menu Implementation of hook_menu().
mailing_list_perm Implementation of hook_perm().
mailing_list_subscription_form Display a form letting a user subscribe to a mailing list.
mailing_list_subscription_form_submit Submit handler for the subscription form; saves a subscription.
mailing_list_subscription_form_validate Validation handler for the subscription form; checks name and e-mail entered.