You are here

commerce_invoice_receipt.rules.inc in Commerce Invoice Receipt 7.2

Same filename and directory in other branches
  1. 7 commerce_invoice_receipt.rules.inc

Rules integration for Invoice Receipt.

File

commerce_invoice_receipt.rules.inc
View source
<?php

/**
 * @file
 * Rules integration for Invoice Receipt.
 *
 * @addtogroup rules
 * @{
 */

/**
 * Implements hook_rules_event_info().
 */
function commerce_invoice_receipt_rules_event_info() {
  return array(
    'commerce_invoice_receipt_rules_event_invoice_from_front' => array(
      'label' => t('Invoice receipt is sent from admin'),
      'help' => t('Triggers when an email is sent using custom function.'),
      'group' => t('Commerce Invoice Receipt'),
      'variables' => array(
        'commerce_order' => array(
          'type' => 'commerce_order',
          'label' => t('Order'),
          'description' => t('The order for which the invoice receipt will be sent.'),
        ),
        'email' => array(
          'type' => 'text',
          'label' => t('Email'),
          'description' => t('The e-mail address where the invoice receipt will be sent to. The formatting of this string must comply with RFC 2822.'),
        ),
      ),
    ),
    'commerce_invoice_receipt_rules_event_invoice_from_admin' => array(
      'label' => t('Invoice receipt is sent from front-end'),
      'help' => t('Triggers when an email is sent using custom function.'),
      'group' => t('Commerce Invoice Receipt'),
      'variables' => array(
        'commerce_order' => array(
          'type' => 'commerce_order',
          'label' => t('Order'),
          'description' => t('The order for which the invoice receipt will be sent.'),
        ),
        'email' => array(
          'type' => 'text',
          'label' => t('Email'),
          'description' => t('The e-mail address where the invoice receipt will be sent to. The formatting of this string must comply with RFC 2822.'),
        ),
      ),
    ),
  );
}

/**
 * Implements hook_rules_action_info().
 */
function commerce_invoice_receipt_rules_action_info() {
  return array(
    'commerce_invoice_receipt_action_mail' => array(
      'label' => t('Send invoice receipt email'),
      'group' => t('Commerce Invoice Receipt'),
      'parameter' => array(
        'commerce_order' => array(
          'type' => 'commerce_order',
          'label' => t('Order'),
          'description' => t('The order for which we want to send the invoice receipt.'),
        ),
        'to' => array(
          'type' => 'text',
          'label' => t('To'),
          'description' => t('The e-mail address or addresses where the invoice will be sent to. The formatting of this string must comply with RFC 2822. Separate multiple addresses with a comma.'),
          'default value' => '[commerce-order:owner] <[commerce-order:mail]>',
        ),
        'cc' => array(
          'type' => 'text',
          'label' => t('CC'),
          'description' => t('The e-mail address or addresses where a carbon copy of the invoice will be sent to. The formatting of this string must comply with RFC 2822. Separate multiple addresses with a comma.'),
          'optional' => TRUE,
        ),
        'bcc' => array(
          'type' => 'text',
          'label' => t('BCC'),
          'description' => t('The e-mail address or addresses where a blind carbon copy of the invoice will be sent to. The formatting of this string must comply with RFC 2822. Separate multiple addresses with a comma.'),
          'optional' => TRUE,
        ),
        'subject' => array(
          'type' => 'text',
          'label' => t('Subject'),
          'description' => t('Email subject.'),
          'default value' => t('!site_name: Order #!order_id', array(
            '!site_name' => '[site:name]',
            '!order_id' => '[commerce-order:order-id]',
          )),
        ),
        'from' => array(
          'type' => 'text',
          'label' => t('From'),
          'description' => t('Email from address. Leave it empty to use the site-wide configured address.'),
          'optional' => TRUE,
        ),
      ),
    ),
  );
}

/**
 * Rules action callback for sending the receipt email.
 */
function commerce_invoice_receipt_action_mail($order, $to, $cc = NULL, $bcc = NULL, $subject, $from = NULL) {

  // Get the email address to which we need to send the email.
  $to = str_replace(array(
    "\r",
    "\n",
  ), '', $to);

  // Prepare the recipients.
  $recipients = explode(',', $to);

  // Get the email address from which we are sending the invoice.
  $from = !empty($from) ? str_replace(array(
    "\r",
    "\n",
  ), '', $from) : NULL;

  // Prepare the parameters.
  $params = array(
    'subject' => $subject,
    'order' => $order,
  );

  // CC/BCC.
  if ($cc) {
    $params['cc'] = $cc;
  }
  if ($bcc) {
    $params['bcc'] = $bcc;
  }

  // Check which language the user is preferred; if the site is multilingual,
  // this will allow us to send the email in the language which is set in user
  // preferences (if the site allows users to set that). Otherwise, this will
  // fall back to the default language used on the whole website.
  $account = user_load($order->uid);
  $language = user_preferred_language($account);

  // Send the emails.
  foreach ($recipients as $recipient) {

    // Trim the recipient email address, in case there is a space after the
    // comma.
    $recipient = trim($recipient);

    // Send the email.
    if ($recipient) {
      $message = drupal_mail('commerce_invoice_receipt', 'invoice', $recipient, $language, $params, $from);
      if ($message['result']) {
        watchdog('rules', 'Invoice successfully sent to %recipient. Carbon copy: %cc. Blind carbon copy: %bcc.', array(
          '%recipient' => $recipient,
          '%cc' => $cc ? $cc : 'empty',
          '%bcc' => $bcc ? $bcc : 'empty',
        ));
      }
    }
  }
}

/**
 * @}
 */