/[drupal]/contributions/modules/ubercart/uc_notify/uc_notify.module
ViewVC logotype

Contents of /contributions/modules/ubercart/uc_notify/uc_notify.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


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

  ViewVC Help
Powered by ViewVC 1.1.2