View source
<?php
define('PET_VIEWS_DIR', drupal_get_path('module', 'pet') . '/includes/views');
function pet_init() {
drupal_add_css(drupal_get_path('module', 'pet') . '/pet.css');
}
function pet_menu() {
$items = array();
$items['admin/build/pets'] = array(
'title' => 'Previewable email templates',
'page callback' => 'pet_admin_page',
'access callback' => 'user_access',
'access arguments' => array(
'administer previewable email templates',
),
'description' => 'Configure previewable email templates with token support.',
'file' => 'pet.admin.inc',
'type' => MENU_NORMAL_ITEM,
);
$items['admin/build/pets/edit'] = array(
'title' => 'Edit previewable email templates',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'pet_add_form',
),
'access callback' => 'user_access',
'access arguments' => array(
'administer previewable email templates',
),
'file' => 'pet.admin.inc',
'type' => MENU_CALLBACK,
);
$items['admin/build/pets/delete'] = array(
'title' => 'Delete previewable email templates',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'pet_delete_confirm',
),
'access callback' => 'user_access',
'access arguments' => array(
'administer previewable email templates',
),
'file' => 'pet.admin.inc',
'type' => MENU_CALLBACK,
);
$items['admin/build/pets/list'] = array(
'title' => 'List',
'page callback' => 'pet_admin_page',
'access callback' => 'user_access',
'access arguments' => array(
'administer previewable email templates',
),
'file' => 'pet.admin.inc',
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => -1,
);
$items['admin/build/pets/add'] = array(
'title' => 'Add',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'pet_add_form',
),
'access callback' => 'user_access',
'access arguments' => array(
'administer previewable email templates',
),
'file' => 'pet.admin.inc',
'type' => MENU_LOCAL_TASK,
);
$items['pet/%pet'] = array(
'title callback' => 'pet_page_title',
'title arguments' => array(
1,
),
'page callback' => 'drupal_get_form',
'page arguments' => array(
'pet_user_form',
1,
),
'access callback' => 'user_access',
'access arguments' => array(
'use previewable email templates',
),
'file' => 'pet.admin.inc',
'type' => MENU_CALLBACK,
);
return $items;
}
function pet_perm() {
return array(
'administer previewable email templates',
'use previewable email templates',
);
}
function pet_theme() {
return array(
'pet_admin_page' => array(
'arguments' => array(
'pets' => NULL,
),
),
);
}
function pet_load($param) {
if (is_numeric($param)) {
return db_fetch_object(db_query("SELECT * FROM {pets} WHERE pid = %d", $param));
}
else {
return db_fetch_object(db_query("SELECT * FROM {pets} WHERE name='%s'", $param));
}
}
function pet_is_valid($pet) {
return is_object($pet) && !empty($pet->name) && is_numeric($pet->pid);
}
function pet_insert($pet) {
db_query("INSERT INTO {pets} (name, title, subject, body, object_types, recipient_callback) VALUES ('%s', '%s', '%s', '%s', '%s', '%s')", $pet->name, $pet->title, $pet->subject, $pet->body, $pet->object_types, $pet->recipient_callback);
}
function pet_page_title($pet) {
return $pet->title;
}
function pet_send_mail($name, $recipients, $nid = NULL, $subject = NULL, $body = NULL, $from = NULL, $cc = NULL, $bcc = NULL) {
$pet = pet_load($name);
if (!$pet) {
watchdog('pet', 'Unable to load PET %name.', array(
'%name' => $name,
), WATCHDOG_ERROR);
return;
}
if (!is_array($recipients) || count($recipients) < 1) {
watchdog('pet', 'At least one recipient must be provided for PET %name.', array(
'%name' => $name,
), WATCHDOG_NOTICE);
return;
}
$pet->subject = $subject ? $subject : $pet->subject;
$pet->body = $body ? $body : $pet->body;
$params['pet_recipients'] = $recipients;
$params['pet_nid'] = $nid;
$params['pet_from'] = $from ? $from : ($pet->from_override ? $pet->from_override : variable_get('site_mail', $GLOBALS['site_mail']));
$params['pet_cc'] = pet_parse_mails($cc);
$params['pet_bcc'] = pet_parse_mails($bcc);
foreach ($recipients as $recipient) {
if (is_array($recipient)) {
$params['pet_to'] = $recipient['mail'];
$params['pet_uid'] = $recipient['uid'];
}
else {
$mail = preg_replace('/^[0-9]*\\|/', '', $recipient);
$params['pet_to'] = $mail;
$params['pet_uid'] = pet_lookup_uid($mail);
}
pet_send_one_mail($pet, $params);
}
}
function pet_send_one_mail($pet, $params) {
if (!pet_is_valid($pet)) {
watchdog('pet', 'Invalid PET object in pet_send_one_mail().', array(), WATCHDOG_ERROR);
return;
}
if (empty($params['pet_from'])) {
watchdog('pet', 'Missing sender email address in pet_send_one_mail() for PET \'%name\'.', array(
'%name' => $pet->name,
), WATCHDOG_ERROR);
return;
}
if (empty($params['pet_to'])) {
watchdog('pet', 'Missing recipient email address in pet_send_one_mail() for PET \'%name\'.', array(
'%name' => $pet->name,
), WATCHDOG_ERROR);
return;
}
$params['pet'] = $pet;
$substitutions = pet_substitutions($pet, $params);
$params['subject'] = token_replace_multiple($pet->subject, $substitutions);
$params['body'] = token_replace_multiple($pet->body, $substitutions);
drupal_mail('pet', $pet->name, $params['pet_to'], language_default(), $params, $params['pet_from']);
}
function pet_substitutions($pet, $params) {
$uid = $params['pet_uid'];
$nid = $params['pet_nid'];
$substitutions['global'] = NULL;
if (!empty($uid)) {
$user = user_load(array(
'uid' => $uid,
));
$substitutions['user'] = $user;
}
if (!empty($nid)) {
$node = node_load($nid, $revision = NULL, $reset = TRUE);
$substitutions['node'] = $node;
}
$pairs = explode("\n", $pet->object_types);
foreach ($pairs as $pair) {
if (!empty($pair)) {
list($type, $object) = explode('|', $pair);
$object = trim($object);
if ($object == 'node' && !empty($nid)) {
$substitutions[$type] = $node;
}
elseif ($object == 'user' && !empty($uid)) {
$substitutions[$type] = $user;
}
else {
$substitutions[$type] = NULL;
}
}
}
drupal_alter('pet_substitutions', $substitutions, $params);
return $substitutions;
}
function pet_mail($key, &$message, $params) {
$message['subject'] = $params['subject'];
$message['body'][] = $params['body'];
if (is_array($params['pet_cc']) && count($params['pet_cc']) > 0) {
$message['headers']['Cc'] = implode(',', $params['pet_cc']);
}
if (is_array($params['pet_bcc']) && count($params['pet_bcc']) > 0) {
$message['headers']['Bcc'] = implode(',', $params['pet_bcc']);
}
}
function pet_get_pets() {
$pets = array();
$result = db_query("SELECT * FROM {pets} ORDER BY name");
while ($pet = db_fetch_object($result)) {
$pets[] = $pet;
}
return $pets;
}
function pet_ca_action() {
$order_arg = array(
'#entity' => 'uc_order',
'#title' => t('Order'),
);
$actions['pet_email'] = array(
'#title' => t('Send a previewable email template based email'),
'#category' => t('Previewable email template'),
'#callback' => 'pet_action_email',
'#arguments' => array(
'order' => $order_arg,
),
);
return $actions;
}
function pet_action_email($order, $settings) {
$account = uc_order_user_load($order);
$settings['replacements'] = array(
'global' => NULL,
'order' => $order,
'user' => $account,
);
$from = token_replace_multiple($settings['from'], $settings['replacements']);
$from = empty($from) ? uc_store_email_from() : $from;
$addresses = token_replace_multiple($settings['addresses'], $settings['replacements']);
$recipients = array();
foreach (explode("\n", $addresses) as $address) {
$address = trim($address);
if (!empty($address)) {
$recipients[] = $address;
}
}
$pet = pet_load($settings['pet']);
$params = array(
'pet_from' => $from,
'uc_order' => $order,
);
foreach ($recipients as $email) {
$params['pet_to'] = $email;
$params['pet_uid'] = $account->uid;
pet_send_one_mail($pet, $params);
}
}
function pet_action_email_form($form_state, $settings = array()) {
$form['from'] = array(
'#type' => 'textfield',
'#title' => t('Sender'),
'#default_value' => isset($settings['from']) ? $settings['from'] : uc_store_email_from(),
'#description' => t("Enter the 'From' email addresses, or leave blank to use your store email address. You may use order tokens for dynamic email addresses."),
'#required' => TRUE,
);
$form['addresses'] = array(
'#type' => 'textarea',
'#title' => t('Recipients'),
'#default_value' => isset($settings['addresses']) ? $settings['addresses'] : '[order-email]',
'#description' => t('Enter the email addresses to receive the notifications, one on each line. You may use order tokens for dynamic email addresses.'),
'#required' => TRUE,
);
$form['pet'] = array(
'#type' => 'select',
'#title' => t('Previewable email template'),
'#options' => pet_option_list(),
'#default_value' => $settings['pet'],
'#description' => t('Choose the previewable email template to send when this action is fired.'),
'#required' => TRUE,
);
return $form;
}
function pet_option_list() {
$pets = pet_get_pets();
$list = array(
'' => t('- Choose -'),
);
foreach ($pets as $pet) {
$list[$pet->name] = $pet->name;
}
return $list;
}
function pet_pet_substitutions_alter(&$substitutions, $params) {
if (isset($params['uc_order'])) {
$substitutions['order'] = $params['uc_order'];
}
}
function pet_views_api() {
return array(
'api' => 2,
'path' => PET_VIEWS_DIR,
);
}
function pet_lookup_uid($mail) {
return db_result(db_query_range("SELECT uid FROM {users} WHERE mail LIKE '%s'", $mail, 0, 1));
}
function pet_parse_mails($mail_text) {
return preg_split('/[\\n\\r, ]/', $mail_text, -1, PREG_SPLIT_NO_EMPTY);
}