View source
<?php
function pet_menu() {
$path = drupal_get_path('module', 'pet') . '/includes';
$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',
'file path' => $path,
'type' => MENU_CALLBACK,
);
$items['admin/config/system/pet'] = array(
'title' => 'Previewable Email Templates',
'description' => t('Manage previewable email templates settings.'),
'access arguments' => array(
'administer previewable email templates',
),
'page callback' => 'system_admin_menu_block_page',
'file' => 'system.admin.inc',
'file path' => drupal_get_path('module', 'system'),
);
$items['admin/config/system/pet/settings'] = array(
'title' => 'PET settings',
'access arguments' => array(
'administer previewable email templates',
),
'page callback' => 'drupal_get_form',
'page arguments' => array(
'pet_settings',
),
'file' => 'pet.admin.inc',
'file path' => $path,
);
return $items;
}
function pet_entity_info() {
$return = array(
'pet' => array(
'label' => t('Previewable Email Template'),
'plural label' => t('Previewable Email Templates'),
'description' => t('Create email templates with token and rules support and optionally preview them before sending.'),
'entity class' => 'PET',
'controller class' => 'EntityAPIControllerExportable',
'base table' => 'pets',
'fieldable' => FALSE,
'exportable' => TRUE,
'entity keys' => array(
'name' => 'name',
'id' => 'pid',
'label' => 'title',
'status' => 'status',
),
'load hook' => 'pet_load',
'uri callback' => 'pet_uri',
'module' => 'pet',
'access callback' => 'pet_access',
'admin ui' => array(
'path' => 'admin/structure/pets',
'file' => 'pet.admin.inc',
'file path' => drupal_get_path('module', 'pet') . '/includes',
'controller class' => 'PETUIController',
),
),
);
return $return;
}
function pet_access($op, $type = NULL, $account = NULL) {
return user_access('administer previewable email templates', $account);
}
function pet_permission() {
$permissions = array(
'administer previewable email templates' => array(
'title' => t('Administer previewable email templates'),
'description' => t('Create, edit and delete previewable email templates.'),
),
'use previewable email templates' => array(
'title' => t('Use previewable email templates'),
'description' => t('Use previewable email templates.'),
),
);
return $permissions;
}
class PET extends Entity {
public $pid;
public $name;
public $title;
public $subject;
public $mail_body;
public $mail_body_plain;
public $send_plain;
public $from_override;
public $cc_default;
public $bcc_default;
public $recipient_callback;
public function __construct($values = array()) {
parent::__construct($values, 'pet');
}
}
class PETUIController extends EntityDefaultUIController {
public function hook_menu() {
$items = parent::hook_menu();
$items[$this->path]['description'] = 'Create previewable email templates with token and rules support.';
return $items;
}
public function __construct($entity_type, $entity_info) {
$this->overviewPagerLimit = 100;
return parent::__construct($entity_type, $entity_info);
}
public function overviewTable($conditions = array()) {
$query = new EntityFieldQuery();
$query
->entityCondition('entity_type', $this->entityType);
$tableSort = array(
'name' => array(
'data' => 'Label',
'type' => 'property',
'specifier' => 'name',
),
'status' => array(
'data' => 'Status',
'type' => 'property',
'specifier' => 'status',
),
);
$query
->tableSort($tableSort);
foreach ($conditions as $key => $value) {
$query
->propertyCondition($key, $value);
}
if ($this->overviewPagerLimit) {
$query
->pager($this->overviewPagerLimit);
}
$results = $query
->execute();
$ids = isset($results[$this->entityType]) ? array_keys($results[$this->entityType]) : array();
$entities = $ids ? entity_load($this->entityType, $ids) : array();
$rows = array();
foreach ($entities as $entity) {
$rows[] = $this
->overviewTableRow($conditions, entity_id($this->entityType, $entity), $entity);
}
$render = array(
'#theme' => 'table',
'#header' => $this
->overviewTableHeaders($conditions, $rows),
'#rows' => $rows,
'#empty' => t('None.'),
);
return $render;
}
protected function overviewTableHeaders($conditions, $rows = array(), $additional_header = array()) {
$header = $additional_header;
array_unshift($header, array(
'data' => t('Label'),
'field' => 'name',
'sort' => 'asc',
));
if (!empty($this->entityInfo['exportable'])) {
$header[] = array(
'data' => t('Status'),
'field' => 'status',
'sort' => 'desc',
);
}
$header[] = array(
'data' => t('Operations'),
'colspan' => $this
->operationCount(),
);
return $header;
}
}
function pet_load($pid) {
$pets = pet_load_multiple(array(
$pid,
), array());
return $pets ? reset($pets) : FALSE;
}
function pet_load_multiple($pids = array(), $conditions = array(), $reset = FALSE) {
return entity_load('pet', $pids, $conditions, $reset);
}
function pet_uri($pet) {
return array(
'path' => 'pet/' . $pet->name,
);
}
function pet_page_title($pet) {
return $pet->title;
}
function pet_parse_mails($mail_text) {
return preg_split('/[\\n\\r, ]/', $mail_text, -1, PREG_SPLIT_NO_EMPTY);
}
function pet_lookup_uid($mail) {
$uid = db_query_range('SELECT uid FROM {users} WHERE mail = :mail', 0, 1, array(
':mail' => $mail,
))
->fetchField();
return $uid;
}
function pet_send_mail($name, $recipients, $options) {
$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 = isset($options['subject']) ? $options['subject'] : $pet->subject;
$pet->mail_body = isset($options['body']) ? $options['body'] : $pet->mail_body;
$pet->mail_body_plain = isset($options['body_plain']) ? $options['body_plain'] : $pet->mail_body_plain;
if (pet_isset_or($options['from'])) {
$from = $options['from'];
}
elseif ($pet->from_override) {
$from = $pet->from_override;
}
else {
$from = variable_get('site_mail', ini_get('sendmail_from'));
}
$params = array(
'pet_from' => $from,
'pet_recipients' => $recipients,
'pet_nid' => pet_isset_or($options['nid']),
'pet_cc' => pet_parse_mails(pet_isset_or($options['cc'])),
'pet_bcc' => pet_parse_mails(pet_isset_or($options['bcc'])),
'pet_reply_to' => pet_isset_or($options['reply_to']),
'pet_options' => $options,
);
$message_status = array();
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);
}
$message_status[$params['pet_to']] = pet_send_one_mail($pet, $params);
}
return $message_status;
}
function pet_send_one_mail($pet, $params) {
$pet_logging = variable_get('pet_logging', 0);
if (!pet_is_valid($pet)) {
if ($pet_logging < 2) {
watchdog('pet', 'Invalid PET object in pet_send_one_mail().', array(), WATCHDOG_ERROR);
}
else {
drupal_set_message(t('Invalid PET object in pet_send_one_mail().'), 'error');
}
return;
}
if (empty($params['pet_from'])) {
if ($pet_logging < 2) {
watchdog('pet', 'Missing sender email address in pet_send_one_mail() for PET \'%name\'.', array(
'%name' => $pet->name,
), WATCHDOG_ERROR);
}
else {
drupal_set_message(t('Missing sender email address in pet_send_one_mail() for PET \'%name\'.', array(
'%name' => $pet->name,
)), 'error');
}
return;
}
if (empty($params['pet_to'])) {
if ($pet_logging < 2) {
watchdog('pet', 'Missing recipient email address in pet_send_one_mail() for PET \'%name\'.', array(
'%name' => $pet->name,
), WATCHDOG_ERROR);
}
else {
drupal_set_message(t('Missing recipient email address in pet_send_one_mail() for PET \'%name\'.', array(
'%name' => $pet->name,
)), 'error');
}
return;
}
if (isset($params['pet_reply_to'])) {
$message['headers']['Reply-To'] = $params['pet_reply_to'];
}
$params['pet'] = $pet;
$substitutions = pet_substitutions($pet, $params);
$params['subject'] = token_replace($pet->subject, $substitutions, array(
'clear' => TRUE,
));
$params['body'] = token_replace($pet->mail_body, $substitutions, array(
'clear' => TRUE,
));
$mail_body_plain = trim($pet->mail_body_plain);
if (!empty($mail_body_plain)) {
$params['plaintext'] = token_replace($pet->mail_body_plain, $substitutions, array(
'clear' => TRUE,
));
}
$params['plain'] = $pet->send_plain;
$message = drupal_mail('pet', $pet->name, $params['pet_to'], language_default(), $params, $params['pet_from']);
if ($message['send'] && $pet_logging == 0) {
watchdog('pet', 'Successfully sent email to %recipient', array(
'%recipient' => $params['pet_to'],
));
}
return $message;
}
function pet_substitutions($pet, $params) {
$uid = pet_isset_or($params['pet_uid']);
$nid = pet_isset_or($params['pet_nid']);
$substitutions['global'] = NULL;
if (!empty($uid)) {
$user = user_load($uid);
$substitutions['user'] = $user;
}
if (!empty($nid)) {
$node = node_load($nid);
$substitutions['node'] = $node;
}
drupal_alter('pet_substitutions', $substitutions, $params);
return $substitutions;
}
function pet_mail($key, &$message, $params) {
$message['subject'] = $params['subject'];
$message['body'][] = $params['body'];
if (isset($params['pet_cc']) && is_array($params['pet_cc']) && count($params['pet_cc']) > 0) {
$message['headers']['Cc'] = implode(',', $params['pet_cc']);
}
if (isset($params['pet_bcc']) && is_array($params['pet_bcc']) && count($params['pet_bcc']) > 0) {
$message['headers']['Bcc'] = implode(',', $params['pet_bcc']);
}
}
function pet_is_valid($pet) {
return is_object($pet) && !empty($pet->name) && is_numeric($pet->pid);
}
function pet_has_mimemail() {
return module_exists('mimemail');
}
function pet_isset_or(&$val, $alternate = NULL) {
return isset($val) ? $val : $alternate;
}