uc_notify.module

Tracking 5.x-1.x branch
  1. drupal
    1. 5 contributions/ubercart/uc_notify/uc_notify.module

Handles configuration and execution of email notifications.

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

Functions & methods

NameDescription
uc_notify_checkoutSend new order confirmation e-mail to customer and/or administrator.
uc_notify_checkout_form
uc_notify_form_alterImplementation of hook_form_alter().
uc_notify_headersReturn the headers for the notification emails.
uc_notify_menuImplementation of hook_menu().
uc_notify_orderImplementation of hook_order().
uc_notify_order_update
uc_notify_order_update_form
uc_notify_permImplementation of hook_perm().
uc_notify_settings_form
uc_notify_settings_overviewDisplay the notification settings overview page.
uc_notify_uc_messageImplementation of hook_uc_message().

File

View source
  1. <?php
  2. /**
  3. * @file
  4. * Handles configuration and execution of email notifications.
  5. *
  6. * Development sponsored by the Ubercart project. http://www.ubercart.org
  7. */
  8. /*******************************************************************************
  9. * Hook Functions (Drupal)
  10. ******************************************************************************/
  11. /**
  12. * Implementation of hook_menu().
  13. */
  14. function uc_notify_menu($may_cache) {
  15. if ($may_cache) {
  16. $items[] = array(
  17. 'path' => 'admin/store/settings/notify',
  18. 'title' => t('Notification settings'),
  19. 'access' => user_access('configure notifications'),
  20. 'description' => t('Configure the notification settings.'),
  21. 'callback' => 'uc_notify_settings_overview',
  22. 'type' => MENU_NORMAL_ITEM,
  23. );
  24. $items[] = array(
  25. 'path' => 'admin/store/settings/notify/overview',
  26. 'title' => t('Overview'),
  27. 'access' => user_access('administer store'),
  28. 'description' => t('View the notification settings.'),
  29. 'type' => MENU_DEFAULT_LOCAL_TASK,
  30. 'weight' => -10,
  31. );
  32. $items[] = array(
  33. 'path' => 'admin/store/settings/notify/edit',
  34. 'title' => t('Edit'),
  35. 'callback' => 'drupal_get_form',
  36. 'callback arguments' => array('uc_notify_settings_form'),
  37. 'access' => user_access('administer store'),
  38. 'description' => t('Edit the notification settings.'),
  39. 'type' => MENU_LOCAL_TASK,
  40. 'weight' => -5,
  41. );
  42. $items[] = array(
  43. 'path' => 'admin/store/settings/notify/edit/general',
  44. 'title' => t('General settings'),
  45. 'access' => user_access('administer store'),
  46. 'description' => t('Edit the general notification settings.'),
  47. 'type' => MENU_DEFAULT_LOCAL_TASK,
  48. 'weight' => -10,
  49. );
  50. $items[] = array(
  51. 'path' => 'admin/store/settings/notify/edit/checkout',
  52. 'title' => t('Checkout'),
  53. 'callback' => 'drupal_get_form',
  54. 'callback arguments' => array('uc_notify_checkout_form'),
  55. 'access' => user_access('administer store'),
  56. 'description' => t('Edit the checkout notification settings.'),
  57. 'type' => MENU_LOCAL_TASK,
  58. 'weight' => -5,
  59. );
  60. $items[] = array(
  61. 'path' => 'admin/store/settings/notify/edit/update',
  62. 'title' => t('Order update'),
  63. 'access' => user_access('administer store'),
  64. 'callback' => 'drupal_get_form',
  65. 'callback arguments' => array('uc_notify_order_update_form'),
  66. 'description' => t('Edit the order update notification settings.'),
  67. 'type' => MENU_LOCAL_TASK,
  68. 'weight' => -3,
  69. );
  70. }
  71. return $items;
  72. }
  73. /**
  74. * Implementation of hook_perm().
  75. */
  76. function uc_notify_perm() {
  77. return array('configure notifications');
  78. }
  79. /**
  80. * Implementation of hook_form_alter().
  81. */
  82. function uc_notify_form_alter($form_id, &$form) {
  83. switch ($form_id) {
  84. case 'uc_order_view_update_form':
  85. $form['controls']['notify'] = array(
  86. '#type' => 'checkbox',
  87. '#title' => t('Send e-mail notification on update.'),
  88. '#weight' => 0,
  89. );
  90. $form['#submit']['uc_notify_order_update'] = array();
  91. break;
  92. }
  93. }
  94. /*******************************************************************************
  95. * Hook Functions (Ubercart)
  96. ******************************************************************************/
  97. /**
  98. * Implementation of hook_uc_message().
  99. */
  100. function uc_notify_uc_message() {
  101. $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]");
  102. return $messages;
  103. }
  104. /**
  105. * Implementation of hook_order().
  106. */
  107. function uc_notify_order($op, $arg1, $arg2) {
  108. switch ($op) {
  109. case 'update':
  110. foreach (uc_order_status_list('general') as $data) {
  111. $statuses[] = $data['id'];
  112. }
  113. if (uc_order_status_data($arg1->order_status, 'state') == 'in_checkout' &&
  114. in_array($arg2, $statuses)) {
  115. uc_notify_checkout($arg1);
  116. }
  117. break;
  118. }
  119. }
  120. /*******************************************************************************
  121. * Callback Functions, Forms, and Tables
  122. ******************************************************************************/
  123. /**
  124. * Display the notification settings overview page.
  125. */
  126. function uc_notify_settings_overview() {
  127. $help_page = variable_get('uc_notify_store_help_page', '');
  128. $sections[] = array(
  129. 'edit' => 'admin/store/settings/notify/edit',
  130. 'title' => t('General settings'),
  131. 'items' => array(
  132. 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))))),
  133. ),
  134. );
  135. $emails = preg_split('/\s+/', variable_get('uc_notify_admin_checkout_emails', variable_get('uc_store_email', '')));
  136. $cust_template = variable_get('uc_notify_cust_checkout_template', 'customer');
  137. $admin_template = variable_get('uc_notify_admin_checkout_template', 'admin');
  138. $sections[] = array(
  139. 'edit' => 'admin/store/settings/notify/edit/checkout',
  140. 'title' => t('Checkout notifications'),
  141. 'items' => array(
  142. t('Customer checkout notifications are !status.', array('!status' => variable_get('uc_notify_cust_checkout_enabled', TRUE) ? t('enabled') : t('disabled'))),
  143. t('Customer e-mail template: !template', array('!template' => $cust_template == '0' ? t('custom') : $cust_template)),
  144. t('Admin checkout notifications are !status.', array('!status' => variable_get('uc_notify_admin_checkout_enabled', FALSE) ? t('enabled') : t('disabled'))),
  145. t('Admin e-mail template: !template', array('!template' => $admin_template == '0' ? t('custom') : $admin_template)),
  146. t('Admin e-mails are sent to:') . theme('item_list', $emails),
  147. ),
  148. );
  149. if (trim(variable_get('uc_notify_order_update_subject', 'subject')) == '' ||
  150. trim(variable_get('uc_notify_order_update_body', 'body')) == '') {
  151. $message = t('<b>Warning:</b> your current settings are incomplete.');
  152. }
  153. else {
  154. $message = t('Click to review your current settings.');
  155. }
  156. $sections[] = array(
  157. 'edit' => 'admin/store/settings/notify/edit/update',
  158. 'title' => t('Order update notifications'),
  159. 'items' => array(
  160. t('Order update notifications are !status.', array('!status' => variable_get('uc_notify_order_update_enabled', TRUE) ? t('enabled') : t('disabled'))),
  161. $message,
  162. ),
  163. );
  164. $output = theme('uc_settings_overview', $sections);
  165. return $output;
  166. }
  167. function uc_notify_settings_form() {
  168. global $base_url;
  169. $form['uc_notify_store_help_page'] = array(
  170. '#type' => 'textfield',
  171. '#title' => t('Store help page'),
  172. '#description' => t('The Drupal page for the store help link.'),
  173. '#default_value' => variable_get('uc_notify_store_help_page', ''),
  174. '#size' => 32,
  175. '#field_prefix' => url(NULL, NULL, NULL, TRUE) . (variable_get('clean_url', 0) ? '' : '?q='),
  176. );
  177. return system_settings_form($form);
  178. }
  179. function uc_notify_checkout_form() {
  180. $form['cust'] = array(
  181. '#type' => 'fieldset',
  182. '#title' => t('Customer checkout notification'),
  183. '#collapsible' => TRUE,
  184. '#collapsed' => TRUE,
  185. );
  186. $form['cust']['uc_notify_cust_checkout_enabled'] = array(
  187. '#type' => 'checkbox',
  188. '#title' => t('Send order confirmation e-mails to customers after checkout.'),
  189. '#default_value' => variable_get('uc_notify_cust_checkout_enabled', TRUE),
  190. );
  191. $form['cust']['uc_notify_cust_checkout_subject'] = array(
  192. '#type' => 'textfield',
  193. '#title' => t('Subject'),
  194. '#default_value' => variable_get('uc_notify_cust_checkout_subject', t('Your Order at [store-name]')),
  195. );
  196. $form['cust']['uc_notify_cust_checkout_template'] = array(
  197. '#type' => 'select',
  198. '#title' => t('Invoice template'),
  199. '#description' => t('Select the invoice template to use for the e-mail.'),
  200. '#options' => uc_order_template_options(TRUE),
  201. '#default_value' => variable_get('uc_notify_cust_checkout_template', 'customer'),
  202. );
  203. $form['cust']['uc_notify_cust_checkout_custom'] = array(
  204. '#type' => 'textarea',
  205. '#title' => t('Custom template (if selected above)'),
  206. '#description' => l(t('Uses order and global tokens.'), 'admin/store/help/tokens'),
  207. '#default_value' => variable_get('uc_notify_cust_checkout_custom', ''),
  208. '#rows' => 5,
  209. );
  210. $form['cust']['uc_notify_cust_checkout_format'] = filter_form(variable_get('uc_notify_cust_checkout_format', 3), NULL, array('uc_notify_cust_checkout_format'));
  211. $form['admin'] = array(
  212. '#type' => 'fieldset',
  213. '#title' => t('Admin checkout notification'),
  214. '#collapsible' => TRUE,
  215. '#collapsed' => TRUE,
  216. );
  217. $form['admin']['uc_notify_admin_checkout_enabled'] = array(
  218. '#type' => 'checkbox',
  219. '#title' => t('Send new order notifications to the e-mail addresses specified below.'),
  220. '#default_value' => variable_get('uc_notify_admin_checkout_enabled', FALSE),
  221. );
  222. $form['admin']['uc_notify_admin_checkout_subject'] = array(
  223. '#type' => 'textfield',
  224. '#title' => t('Subject'),
  225. '#default_value' => variable_get('uc_notify_admin_checkout_subject', t('New Order at [store-name]')),
  226. );
  227. $form['admin']['uc_notify_admin_checkout_template'] = array(
  228. '#type' => 'select',
  229. '#title' => t('Invoice template'),
  230. '#description' => t('Select the invoice template to use for the e-mail.'),
  231. '#options' => uc_order_template_options(TRUE),
  232. '#default_value' => variable_get('uc_notify_admin_checkout_template', 'admin'),
  233. );
  234. $form['admin']['uc_notify_admin_checkout_custom'] = array(
  235. '#type' => 'textarea',
  236. '#title' => t('Custom template (if selected above)'),
  237. '#description' => l(t('Uses order and global tokens.'), 'admin/store/help/tokens'),
  238. '#default_value' => variable_get('uc_notify_admin_checkout_custom', ''),
  239. '#rows' => 5,
  240. );
  241. $form['admin']['uc_notify_admin_checkout_format'] = filter_form(variable_get('uc_notify_admin_checkout_format', 3), NULL, array('uc_notify_admin_checkout_format'));
  242. $form['admin']['uc_notify_admin_checkout_emails'] = array(
  243. '#type' => 'textarea',
  244. '#title' => t('Notification recipients'),
  245. '#description' => t('E-mail recipients for admin order notifications, one per line.'),
  246. '#default_value' => variable_get('uc_notify_admin_checkout_emails', variable_get('uc_store_email', '')),
  247. '#rows' => 3,
  248. );
  249. return system_settings_form($form);
  250. }
  251. function uc_notify_order_update_form() {
  252. $form['uc_notify_order_update_enabled'] = array(
  253. '#type' => 'checkbox',
  254. '#title' => t('Send order update e-mails to customers when specified.'),
  255. '#default_value' => variable_get('uc_notify_order_update_enabled', TRUE),
  256. );
  257. $form['uc_notify_order_update_subject'] = array(
  258. '#type' => 'textfield',
  259. '#title' => t('Subject'),
  260. '#default_value' => variable_get('uc_notify_order_update_subject', t('Order #[order-id] Update')),
  261. );
  262. $form['uc_notify_order_update_body'] = array(
  263. '#type' => 'textarea',
  264. '#title' => t('Body'),
  265. '#description' => l(t('Uses order and global tokens.'), 'admin/store/help/tokens'),
  266. '#default_value' => variable_get('uc_notify_order_update_body', uc_get_message('order_update_email')),
  267. '#rows' => 10,
  268. );
  269. $form['uc_notify_order_update_format'] = filter_form(variable_get('uc_notify_order_update_format', 3), NULL, array('uc_notify_order_update_format'));
  270. return system_settings_form($form);
  271. }
  272. /**
  273. * Return the headers for the notification emails.
  274. */
  275. function uc_notify_headers($extras = array()) {
  276. $extras = $extras + array(
  277. 'Content-Type' => 'text/html; charset=UTF-8; format=flowed',
  278. );
  279. return $extras;
  280. }
  281. /**
  282. * Send new order confirmation e-mail to customer and/or administrator.
  283. */
  284. function uc_notify_checkout($order) {
  285. if (variable_get('uc_notify_cust_checkout_enabled', TRUE)) {
  286. if (($template = variable_get('uc_notify_cust_checkout_template', 'customer')) == '0') {
  287. $body = variable_get('uc_notify_cust_checkout_custom', '');
  288. $body = check_markup($body, variable_get('uc_notify_cust_checkout_format', 3), FALSE);
  289. }
  290. else {
  291. $body = uc_order_load_invoice($order, 'checkout-mail', $template);
  292. }
  293. $body = token_replace_multiple($body, array('global' => NULL, 'order' => $order));
  294. $subject = variable_get('uc_notify_cust_checkout_subject', t('Your Order at [store-name]'));
  295. $subject = token_replace_multiple($subject, array('global' => NULL, 'order' => $order));
  296. $email_to = $order->primary_email;
  297. $headers = uc_notify_headers();
  298. $sent = drupal_mail('checkout', $email_to, $subject, $body, uc_store_email_from(), $headers);
  299. if ($sent) {
  300. $changes[] = t('Checkout message sent to @email.', array('@email' => $order->primary_email));
  301. }
  302. else {
  303. $changes[] = t('Checkout e-mail notification to @email failed.', array('@email' => $order->primary_email));
  304. }
  305. uc_order_log_changes($order->order_id, $changes);
  306. }
  307. // Send administrator e-mails.
  308. if (variable_get('uc_notify_admin_checkout_enabled', FALSE)) {
  309. if (($template = variable_get('uc_notify_admin_checkout_template', 'admin')) == '0') {
  310. $body = variable_get('uc_notify_admin_checkout_custom', '');
  311. $body = check_markup($body, variable_get('uc_notify_admin_checkout_format', 3), FALSE);
  312. }
  313. else {
  314. $body = uc_order_load_invoice($order, 'admin-mail', $template);
  315. }
  316. $body = token_replace_multiple($body, array('global' => NULL, 'order' => $order));
  317. $subject = variable_get('uc_notify_admin_checkout_subject', t('New Order at [store-name]'));
  318. $subject = token_replace_multiple($subject, array('global' => NULL, 'order' => $order));
  319. $email_from = uc_store_email_from();
  320. $headers = uc_notify_headers();
  321. $emails = preg_split('/\s+/', variable_get('uc_notify_admin_checkout_emails', variable_get('uc_store_email', '')));
  322. foreach ($emails as $email_to) {
  323. if (!empty($email_to) && valid_email_address($email_to)) {
  324. drupal_mail('admin_checkout', $email_to, $subject, $body, $email_from, $headers);
  325. }
  326. }
  327. }
  328. }
  329. // Sends an e-mail to the customer when their order is updated.
  330. function uc_notify_order_update($form_id, $form_values) {
  331. $order = uc_order_load($form_values['order_id']);
  332. if ($order !== FALSE && $form_values['notify']) {
  333. if (!variable_get('uc_notify_order_update_enabled', TRUE)) {
  334. drupal_set_message(t('Order update e-mail notifications are currently disabled.'));
  335. return;
  336. }
  337. // Get the update e-mail body.
  338. $body = variable_get('uc_notify_order_update_body', uc_get_message('order_update_email'));
  339. // Convert tokens to their values.
  340. $body = token_replace_multiple($body, array('global' => NULL, 'order' => $order));
  341. $subject = variable_get('uc_notify_order_update_subject', t('Order #[order-id] Update'));
  342. $subject = token_replace_multiple($subject, array('global' => NULL, 'order' => $order));
  343. $email_to = $order->primary_email;
  344. $headers = uc_notify_headers();
  345. $sent = drupal_mail('order_comment', $order->primary_email, check_plain($subject),
  346. check_markup($body, variable_get('uc_notify_order_update_format', 3), FALSE),
  347. uc_store_email_from(), uc_notify_headers());
  348. if ($sent) {
  349. $changes[] = t('Customer notified of order update.');
  350. }
  351. else {
  352. $changes[] = t('Email notification to @email failed.', array('@email' => $order->primary_email));
  353. }
  354. uc_order_log_changes($order->order_id, $changes);
  355. }
  356. }