You are here

uc_notify.module in Ubercart 5

Handles configuration and execution of email notifications.

Development sponsored by the Ubercart project. http://www.ubercart.org

File

uc_notify/uc_notify.module
View source
<?php

/**
 * @file
 * Handles configuration and execution of email notifications.
 *
 * Development sponsored by the Ubercart project.  http://www.ubercart.org
 */

/*******************************************************************************
 * Hook Functions (Drupal)
 ******************************************************************************/

/**
 * Implementation of hook_menu().
 */
function uc_notify_menu($may_cache) {
  if ($may_cache) {
    $items[] = array(
      'path' => 'admin/store/settings/notify',
      'title' => t('Notification settings'),
      'access' => user_access('configure notifications'),
      'description' => t('Configure the notification settings.'),
      'callback' => 'uc_notify_settings_overview',
      'type' => MENU_NORMAL_ITEM,
    );
    $items[] = array(
      'path' => 'admin/store/settings/notify/overview',
      'title' => t('Overview'),
      'access' => user_access('administer store'),
      'description' => t('View the notification settings.'),
      'type' => MENU_DEFAULT_LOCAL_TASK,
      'weight' => -10,
    );
    $items[] = array(
      'path' => 'admin/store/settings/notify/edit',
      'title' => t('Edit'),
      'callback' => 'drupal_get_form',
      'callback arguments' => array(
        'uc_notify_settings_form',
      ),
      'access' => user_access('administer store'),
      'description' => t('Edit the notification settings.'),
      'type' => MENU_LOCAL_TASK,
      'weight' => -5,
    );
    $items[] = array(
      'path' => 'admin/store/settings/notify/edit/general',
      'title' => t('General settings'),
      'access' => user_access('administer store'),
      'description' => t('Edit the general notification settings.'),
      'type' => MENU_DEFAULT_LOCAL_TASK,
      'weight' => -10,
    );
    $items[] = array(
      'path' => 'admin/store/settings/notify/edit/checkout',
      'title' => t('Checkout'),
      'callback' => 'drupal_get_form',
      'callback arguments' => array(
        'uc_notify_checkout_form',
      ),
      'access' => user_access('administer store'),
      'description' => t('Edit the checkout notification settings.'),
      'type' => MENU_LOCAL_TASK,
      'weight' => -5,
    );
    $items[] = array(
      'path' => 'admin/store/settings/notify/edit/update',
      'title' => t('Order update'),
      'access' => user_access('administer store'),
      'callback' => 'drupal_get_form',
      'callback arguments' => array(
        'uc_notify_order_update_form',
      ),
      'description' => t('Edit the order update notification settings.'),
      'type' => MENU_LOCAL_TASK,
      'weight' => -3,
    );
  }
  return $items;
}

/**
 * Implementation of hook_perm().
 */
function uc_notify_perm() {
  return array(
    'configure notifications',
  );
}

/**
 * Implementation of hook_form_alter().
 */
function uc_notify_form_alter($form_id, &$form) {
  switch ($form_id) {
    case 'uc_order_view_update_form':
      $form['controls']['notify'] = array(
        '#type' => 'checkbox',
        '#title' => t('Send e-mail notification on update.'),
        '#weight' => 0,
      );
      $form['#submit']['uc_notify_order_update'] = array();
      break;
  }
}

/*******************************************************************************
 * Hook Functions (Ubercart)
 ******************************************************************************/

/**
 * Implementation of hook_uc_message().
 */
function uc_notify_uc_message() {
  $messages['order_update_email'] = t("[order-first-name] [order-last-name],\n\nYour order number [order-link] at [store-name] has been updated.\n\nOrder status: [order-status]\n\nOrder comment:\n[order-last-comment]\n\nBrowse to the following page to login to your account and view your order details:\n[site-login]\n\n\nThanks again,\n\n[store-name]\n[site-slogan]");
  return $messages;
}

/**
 * Implementation of hook_order().
 */
function uc_notify_order($op, $arg1, $arg2) {
  switch ($op) {
    case 'update':
      foreach (uc_order_status_list('general') as $data) {
        $statuses[] = $data['id'];
      }
      if (uc_order_status_data($arg1->order_status, 'state') == 'in_checkout' && in_array($arg2, $statuses)) {
        uc_notify_checkout($arg1);
      }
      break;
  }
}

/*******************************************************************************
 * Callback Functions, Forms, and Tables
 ******************************************************************************/

/**
 * Display the notification settings overview page.
 */
function uc_notify_settings_overview() {
  $help_page = variable_get('uc_notify_store_help_page', '');
  $sections[] = array(
    'edit' => 'admin/store/settings/notify/edit',
    'title' => t('General settings'),
    'items' => array(
      t('Store help page is !page.', array(
        '!page' => empty($help_page) ? t('not set') : t('set to !link', array(
          '!link' => l($help_page, $help_page),
        )),
      )),
    ),
  );
  $emails = preg_split('/\\s+/', variable_get('uc_notify_admin_checkout_emails', variable_get('uc_store_email', '')));
  $cust_template = variable_get('uc_notify_cust_checkout_template', 'customer');
  $admin_template = variable_get('uc_notify_admin_checkout_template', 'admin');
  $sections[] = array(
    'edit' => 'admin/store/settings/notify/edit/checkout',
    'title' => t('Checkout notifications'),
    'items' => array(
      t('Customer checkout notifications are !status.', array(
        '!status' => variable_get('uc_notify_cust_checkout_enabled', TRUE) ? t('enabled') : t('disabled'),
      )),
      t('Customer e-mail template: !template', array(
        '!template' => $cust_template == '0' ? t('custom') : $cust_template,
      )),
      t('Admin checkout notifications are !status.', array(
        '!status' => variable_get('uc_notify_admin_checkout_enabled', FALSE) ? t('enabled') : t('disabled'),
      )),
      t('Admin e-mail template: !template', array(
        '!template' => $admin_template == '0' ? t('custom') : $admin_template,
      )),
      t('Admin e-mails are sent to:') . theme('item_list', $emails),
    ),
  );
  if (trim(variable_get('uc_notify_order_update_subject', 'subject')) == '' || trim(variable_get('uc_notify_order_update_body', 'body')) == '') {
    $message = t('<b>Warning:</b> your current settings are incomplete.');
  }
  else {
    $message = t('Click to review your current settings.');
  }
  $sections[] = array(
    'edit' => 'admin/store/settings/notify/edit/update',
    'title' => t('Order update notifications'),
    'items' => array(
      t('Order update notifications are !status.', array(
        '!status' => variable_get('uc_notify_order_update_enabled', TRUE) ? t('enabled') : t('disabled'),
      )),
      $message,
    ),
  );
  $output = theme('uc_settings_overview', $sections);
  return $output;
}
function uc_notify_settings_form() {
  global $base_url;
  $form['uc_notify_store_help_page'] = array(
    '#type' => 'textfield',
    '#title' => t('Store help page'),
    '#description' => t('The Drupal page for the store help link.'),
    '#default_value' => variable_get('uc_notify_store_help_page', ''),
    '#size' => 32,
    '#field_prefix' => url(NULL, NULL, NULL, TRUE) . (variable_get('clean_url', 0) ? '' : '?q='),
  );
  return system_settings_form($form);
}
function uc_notify_checkout_form() {
  $form['cust'] = array(
    '#type' => 'fieldset',
    '#title' => t('Customer checkout notification'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
  );
  $form['cust']['uc_notify_cust_checkout_enabled'] = array(
    '#type' => 'checkbox',
    '#title' => t('Send order confirmation e-mails to customers after checkout.'),
    '#default_value' => variable_get('uc_notify_cust_checkout_enabled', TRUE),
  );
  $form['cust']['uc_notify_cust_checkout_subject'] = array(
    '#type' => 'textfield',
    '#title' => t('Subject'),
    '#default_value' => variable_get('uc_notify_cust_checkout_subject', t('Your Order at [store-name]')),
  );
  $form['cust']['uc_notify_cust_checkout_template'] = array(
    '#type' => 'select',
    '#title' => t('Invoice template'),
    '#description' => t('Select the invoice template to use for the e-mail.'),
    '#options' => uc_order_template_options(TRUE),
    '#default_value' => variable_get('uc_notify_cust_checkout_template', 'customer'),
  );
  $form['cust']['uc_notify_cust_checkout_custom'] = array(
    '#type' => 'textarea',
    '#title' => t('Custom template (if selected above)'),
    '#description' => l(t('Uses order and global tokens.'), 'admin/store/help/tokens'),
    '#default_value' => variable_get('uc_notify_cust_checkout_custom', ''),
    '#rows' => 5,
  );
  $form['cust']['uc_notify_cust_checkout_format'] = filter_form(variable_get('uc_notify_cust_checkout_format', 3), NULL, array(
    'uc_notify_cust_checkout_format',
  ));
  $form['admin'] = array(
    '#type' => 'fieldset',
    '#title' => t('Admin checkout notification'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
  );
  $form['admin']['uc_notify_admin_checkout_enabled'] = array(
    '#type' => 'checkbox',
    '#title' => t('Send new order notifications to the e-mail addresses specified below.'),
    '#default_value' => variable_get('uc_notify_admin_checkout_enabled', FALSE),
  );
  $form['admin']['uc_notify_admin_checkout_subject'] = array(
    '#type' => 'textfield',
    '#title' => t('Subject'),
    '#default_value' => variable_get('uc_notify_admin_checkout_subject', t('New Order at [store-name]')),
  );
  $form['admin']['uc_notify_admin_checkout_template'] = array(
    '#type' => 'select',
    '#title' => t('Invoice template'),
    '#description' => t('Select the invoice template to use for the e-mail.'),
    '#options' => uc_order_template_options(TRUE),
    '#default_value' => variable_get('uc_notify_admin_checkout_template', 'admin'),
  );
  $form['admin']['uc_notify_admin_checkout_custom'] = array(
    '#type' => 'textarea',
    '#title' => t('Custom template (if selected above)'),
    '#description' => l(t('Uses order and global tokens.'), 'admin/store/help/tokens'),
    '#default_value' => variable_get('uc_notify_admin_checkout_custom', ''),
    '#rows' => 5,
  );
  $form['admin']['uc_notify_admin_checkout_format'] = filter_form(variable_get('uc_notify_admin_checkout_format', 3), NULL, array(
    'uc_notify_admin_checkout_format',
  ));
  $form['admin']['uc_notify_admin_checkout_emails'] = array(
    '#type' => 'textarea',
    '#title' => t('Notification recipients'),
    '#description' => t('E-mail recipients for admin order notifications, one per line.'),
    '#default_value' => variable_get('uc_notify_admin_checkout_emails', variable_get('uc_store_email', '')),
    '#rows' => 3,
  );
  return system_settings_form($form);
}
function uc_notify_order_update_form() {
  $form['uc_notify_order_update_enabled'] = array(
    '#type' => 'checkbox',
    '#title' => t('Send order update e-mails to customers when specified.'),
    '#default_value' => variable_get('uc_notify_order_update_enabled', TRUE),
  );
  $form['uc_notify_order_update_subject'] = array(
    '#type' => 'textfield',
    '#title' => t('Subject'),
    '#default_value' => variable_get('uc_notify_order_update_subject', t('Order #[order-id] Update')),
  );
  $form['uc_notify_order_update_body'] = array(
    '#type' => 'textarea',
    '#title' => t('Body'),
    '#description' => l(t('Uses order and global tokens.'), 'admin/store/help/tokens'),
    '#default_value' => variable_get('uc_notify_order_update_body', uc_get_message('order_update_email')),
    '#rows' => 10,
  );
  $form['uc_notify_order_update_format'] = filter_form(variable_get('uc_notify_order_update_format', 3), NULL, array(
    'uc_notify_order_update_format',
  ));
  return system_settings_form($form);
}

/**
 * Return the headers for the notification emails.
 */
function uc_notify_headers($extras = array()) {
  $extras = $extras + array(
    'Content-Type' => 'text/html; charset=UTF-8; format=flowed',
  );
  return $extras;
}

/**
 * Send new order confirmation e-mail to customer and/or administrator.
 */
function uc_notify_checkout($order) {
  if (variable_get('uc_notify_cust_checkout_enabled', TRUE)) {
    if (($template = variable_get('uc_notify_cust_checkout_template', 'customer')) == '0') {
      $body = variable_get('uc_notify_cust_checkout_custom', '');
      $body = check_markup($body, variable_get('uc_notify_cust_checkout_format', 3), FALSE);
    }
    else {
      $body = uc_order_load_invoice($order, 'checkout-mail', $template);
    }
    $body = token_replace_multiple($body, array(
      'global' => NULL,
      'order' => $order,
    ));
    $subject = variable_get('uc_notify_cust_checkout_subject', t('Your Order at [store-name]'));
    $subject = token_replace_multiple($subject, array(
      'global' => NULL,
      'order' => $order,
    ));
    $email_to = $order->primary_email;
    $headers = uc_notify_headers();
    $sent = drupal_mail('checkout', $email_to, $subject, $body, uc_store_email_from(), $headers);
    if ($sent) {
      $changes[] = t('Checkout message sent to @email.', array(
        '@email' => $order->primary_email,
      ));
    }
    else {
      $changes[] = t('Checkout e-mail notification to @email failed.', array(
        '@email' => $order->primary_email,
      ));
    }
    uc_order_log_changes($order->order_id, $changes);
  }

  // Send administrator e-mails.
  if (variable_get('uc_notify_admin_checkout_enabled', FALSE)) {
    if (($template = variable_get('uc_notify_admin_checkout_template', 'admin')) == '0') {
      $body = variable_get('uc_notify_admin_checkout_custom', '');
      $body = check_markup($body, variable_get('uc_notify_admin_checkout_format', 3), FALSE);
    }
    else {
      $body = uc_order_load_invoice($order, 'admin-mail', $template);
    }
    $body = token_replace_multiple($body, array(
      'global' => NULL,
      'order' => $order,
    ));
    $subject = variable_get('uc_notify_admin_checkout_subject', t('New Order at [store-name]'));
    $subject = token_replace_multiple($subject, array(
      'global' => NULL,
      'order' => $order,
    ));
    $email_from = uc_store_email_from();
    $headers = uc_notify_headers();
    $emails = preg_split('/\\s+/', variable_get('uc_notify_admin_checkout_emails', variable_get('uc_store_email', '')));
    foreach ($emails as $email_to) {
      if (!empty($email_to) && valid_email_address($email_to)) {
        drupal_mail('admin_checkout', $email_to, $subject, $body, $email_from, $headers);
      }
    }
  }
}

// Sends an e-mail to the customer when their order is updated.
function uc_notify_order_update($form_id, $form_values) {
  $order = uc_order_load($form_values['order_id']);
  if ($order !== FALSE && $form_values['notify']) {
    if (!variable_get('uc_notify_order_update_enabled', TRUE)) {
      drupal_set_message(t('Order update e-mail notifications are currently disabled.'));
      return;
    }

    // Get the update e-mail body.
    $body = variable_get('uc_notify_order_update_body', uc_get_message('order_update_email'));

    // Convert tokens to their values.
    $body = token_replace_multiple($body, array(
      'global' => NULL,
      'order' => $order,
    ));
    $subject = variable_get('uc_notify_order_update_subject', t('Order #[order-id] Update'));
    $subject = token_replace_multiple($subject, array(
      'global' => NULL,
      'order' => $order,
    ));
    $email_to = $order->primary_email;
    $headers = uc_notify_headers();
    $sent = drupal_mail('order_comment', $order->primary_email, check_plain($subject), check_markup($body, variable_get('uc_notify_order_update_format', 3), FALSE), uc_store_email_from(), uc_notify_headers());
    if ($sent) {
      $changes[] = t('Customer notified of order update.');
    }
    else {
      $changes[] = t('Email notification to @email failed.', array(
        '@email' => $order->primary_email,
      ));
    }
    uc_order_log_changes($order->order_id, $changes);
  }
}

Functions

Namesort descending Description
uc_notify_checkout Send new order confirmation e-mail to customer and/or administrator.
uc_notify_checkout_form
uc_notify_form_alter Implementation of hook_form_alter().
uc_notify_headers Return the headers for the notification emails.
uc_notify_menu Implementation of hook_menu().
uc_notify_order Implementation of hook_order().
uc_notify_order_update
uc_notify_order_update_form
uc_notify_perm Implementation of hook_perm().
uc_notify_settings_form
uc_notify_settings_overview Display the notification settings overview page.
uc_notify_uc_message Implementation of hook_uc_message().