View source
<?php
function commerce_billy_mail_menu() {
$items['admin/commerce/config/billy-invoice/billy-mail'] = array(
'title' => 'Billy Mail',
'description' => 'Configure Commerce Billy Mail settings.',
'access arguments' => array(
'administer site configuration',
),
'page callback' => 'drupal_get_form',
'page arguments' => array(
'commerce_billy_mail_settings_form',
),
'type' => MENU_LOCAL_TASK,
'file' => 'commerce_billy_mail.admin.inc',
);
$items['admin/commerce/orders/%commerce_order/edit/resend-pdf-invoice'] = array(
'title' => 'Resend invoice mail',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'commerce_billy_mail_resend_form',
3,
),
'access callback' => 'commerce_billy_mail_resend_access',
'access arguments' => array(
3,
),
'type' => MENU_LOCAL_ACTION,
'context' => MENU_CONTEXT_PAGE | MENU_CONTEXT_INLINE,
'weight' => 10,
'file' => 'commerce_billy_mail.module',
'file path' => drupal_get_path('module', 'commerce_billy_mail') . '/',
'theme callback' => 'commerce_billy_mail_custom_theme',
);
return $items;
}
function commerce_billy_mail_permission() {
return array(
'commerce_billy_mail_resend_invoice_mail' => array(
'title' => t('Resend invoice mail'),
'description' => t('Resend invoice mails from the order details view. Requires further conditions besides this permission (e.g. order state & order access).'),
),
);
}
function commerce_billy_mail_resend_access($order) {
return commerce_billy_pdf_view_access($order) && user_access('commerce_billy_mail_resend_invoice_mail');
}
function _commerce_billy_mail_send_order_invoice($order) {
try {
global $language;
if (!empty($order->order_id)) {
$order = commerce_order_load($order->order_id);
}
else {
drupal_set_message('Given order-id was empty. The order could not be loaded or identified. Please check your configuration. No invoice was sent!', 'error');
watchdog('commerce_billy_mail', 'Given order-id was empty. The order could not be loaded or identified. Please check your configuration. No invoice was sent!', WATCHDOG_ERROR);
return false;
}
if (empty($order) || !is_object($order)) {
drupal_set_message('Given order was empty or no object. No invoice was sent!', 'error');
watchdog('commerce_billy_mail', 'Given order was empty or no object. No invoice was sent!', WATCHDOG_ERROR);
return false;
}
$order_account_uid = $order->uid;
$order_account = user_load($order_account_uid);
$order_account_language = user_preferred_language($order_account);
$temp_language = $language;
$language = $order_account_language;
if (module_exists('i18n_variable')) {
$subject = i18n_variable_get('commerce_billy_mail_subject', $language->language, NULL);
if (is_null($subject)) {
$subject = variable_get('commerce_billy_mail_subject', '');
}
$body = i18n_variable_get('commerce_billy_mail_body', $language->language, NULL);
if (is_null($body)) {
$body = variable_get('commerce_billy_mail_body', '');
}
}
else {
$subject = variable_get('commerce_billy_mail_subject', '');
$body = variable_get('commerce_billy_mail_body', '');
}
if (variable_get('commerce_billy_mail_attach_pdf_invoice', TRUE)) {
$attachments = _commerce_billy_mail_get_attachments($order);
}
else {
$attachments = array();
}
$subject = token_replace($subject, array(
'user' => $order_account,
'commerce_order' => $order,
), array(
'language' => $order_account_language,
'clear' => TRUE,
));
$body = token_replace($body, array(
'user' => $order_account,
'commerce_order' => $order,
), array(
'language' => $order_account_language,
'clear' => TRUE,
));
$plaintext = variable_get('commerce_billy_mail_plaintext', FALSE);
$params = array(
'subject' => $subject,
'body' => $body,
'plain' => !empty($plaintext),
'attachments' => $attachments,
'context' => array(
'subject' => $subject,
'body' => $body,
'order' => $order,
),
);
foreach (array(
'cc',
'bcc',
) as $header) {
$recipients = trim(variable_get("commerce_billy_mail_{$header}", ''));
if (!empty($recipients)) {
$params[$header] = $recipients;
}
}
$from = variable_get('commerce_billy_mail_from', variable_get('site_mail', ini_get('sendmail_from')));
$mailkey = 'commerce_billy_send_order_invoice';
$send = true;
$message = drupal_mail('commerce_billy_mail', $mailkey, $order->mail, $order_account_language, $params, $from, $send);
$language = $temp_language;
if (!$message['result']) {
watchdog('commerce_billy_mail', 'Error sending e-mail (from %from to %to).', array(
'%from' => $message['from'],
'%to' => $message['to'],
), WATCHDOG_ERROR);
throw new Exception(t('Unable to send e-mail. Contact the site administrator if the problem persists.'));
}
return TRUE;
} catch (Exception $exception) {
_drupal_log_error(_drupal_decode_exception($exception), FALSE);
return FALSE;
}
}
function commerce_billy_mail_mail($key, &$message, $params) {
switch ($key) {
default:
if (isset($params['subject'])) {
$message['subject'] = $params['subject'];
}
if (isset($params['body'])) {
$message['body'][] = $params['body'];
}
if (isset($params['cc'])) {
$message['headers']['Cc'] = $params['cc'];
}
if (isset($params['bcc'])) {
$message['headers']['Bcc'] = $params['bcc'];
}
if (isset($params['plain'])) {
$message['plain'] = $params['plain'];
}
if (isset($params['attachments'])) {
$message['attachments'][] = $params['attachments'];
}
break;
}
}
function commerce_billy_mail_custom_theme() {
return variable_get('theme_default');
}
function _commerce_billy_mail_get_attachments($order) {
$attachments = array();
list($html, $filename) = commerce_billy_mail_pdf_prepare($order);
$pdf_filecontent = commerce_billy_mail_pdf_create($html, $filename);
$filemime = 'application/pdf';
$attachments[] = array(
'filecontent' => $pdf_filecontent,
'filename' => $filename,
'name' => $filename,
'filemime' => $filemime,
'type' => $filemime,
'list' => TRUE,
);
return $attachments;
}
function commerce_billy_mail_pdf_prepare($order) {
if (module_exists('commerce_billy_pdf')) {
module_load_include('module', 'commerce_billy_pdf');
}
else {
drupal_set_message('PDF email attachment could not be createt, because commerce_billy_pdf is disabled.', 'error');
}
$html = commerce_billy_pdf_html($order);
$filename = preg_replace('/[^a-z0-9]/', '_', drupal_strtolower('invoice_' . $order->order_number)) . '.pdf';
if (module_exists('transliteration')) {
$filename = transliteration_clean_filename($filename);
}
return array(
$html,
$filename,
);
}
function commerce_billy_mail_pdf_create($html, $filename) {
return commerce_billy_pdf_output($html, $filename, TRUE);
}
function commerce_billy_mail_resend_form($form, &$form_state, $order) {
$form = array();
$form['order'] = array(
'#type' => 'value',
'#value' => $order,
);
$form['send'] = array(
'#type' => 'submit',
'#value' => t('Resend invoice now'),
);
return $form;
}
function commerce_billy_mail_resend_form_submit($form, &$form_state) {
$order = $form_state['values']['order'];
$success = _commerce_billy_mail_send_order_invoice($order);
if ($success) {
drupal_set_message(t('The PDF invoice has been resent successfully.'), 'status');
}
else {
drupal_set_message(t('The PDF could not be resent.'), 'error');
}
$form_state['redirect'] = 'admin/commerce/orders/' . $order->order_id;
}
function commerce_billy_mail_admin_paths_alter(&$paths) {
$paths['admin/commerce/orders/*/pdf-invoice'] = FALSE;
}
function commerce_billy_mail_theme_registry_alter(&$theme_registry) {
if (_commerce_billy_mail_is_theme_registry_workaround_available()) {
$selected_theme_registry_workaround_theme_items = variable_get('commerce_billy_mail_use_theme_registry_workaround_theme_items', array());
$theme_item_keys = _commerce_billy_mail_get_pdf_theme_items();
if (!empty($selected_theme_registry_workaround_theme_items) && !empty($theme_item_keys)) {
$selected_theme_registry_workaround_theme_items = array_intersect($selected_theme_registry_workaround_theme_items, $theme_item_keys);
$theme_template_dir = variable_get('commerce_billy_mail_theme_templates_dir', '/templates');
foreach ($selected_theme_registry_workaround_theme_items as $theme_item_key) {
$theme_registry[$theme_item_key]['path'] = drupal_get_path('theme', commerce_billy_mail_custom_theme()) . $theme_template_dir;
}
}
}
}
function _commerce_billy_mail_get_pdf_theme_items() {
if (_commerce_billy_mail_is_theme_registry_workaround_available()) {
module_load_include('module', 'commerce_billy_pdf');
if (function_exists('commerce_billy_pdf_theme')) {
$theme_items = commerce_billy_pdf_theme();
$theme_item_keys = array_keys($theme_items);
$result = array();
if (!empty($theme_item_keys)) {
foreach ($theme_item_keys as $theme_item_key) {
$result[$theme_item_key] = $theme_item_key;
}
}
return $result;
}
}
return array();
}
function _commerce_billy_mail_is_theme_registry_workaround_available() {
$admin_theme = variable_get('admin_theme', 0);
return !empty($admin_theme) && variable_get('commerce_billy_mail_attach_pdf_invoice', TRUE) && module_exists('commerce_billy_pdf');
}