| [ Index ] |
PHP Cross Reference of Drupal 6 (gatewave) |
[Summary view] [Print] [Text view]
1 <?php 2 // $Id: webform.emails.inc,v 1.9.2.10 2010/08/30 20:22:15 quicksketch Exp $ 3 4 /** 5 * @file 6 * Provides interface and database handling for e-mail settings of a webform. 7 * 8 * @author Nathan Haug <nate@lullabot.com> 9 */ 10 11 /** 12 * Overview form of all components for this webform. 13 */ 14 function webform_emails_form($form_state, $node) { 15 module_load_include('inc', 'webform', 'includes/webform.components'); 16 17 $form = array( 18 '#tree' => TRUE, 19 '#node' => $node, 20 'components' => array(), 21 ); 22 23 $form['nid'] = array( 24 '#type' => 'value', 25 '#value' => $node->nid, 26 ); 27 28 foreach ($node->webform['emails'] as $eid => $email) { 29 $email_addresses = array_filter(explode(',', check_plain($email['email']))); 30 foreach ($email_addresses as $key => $email_address) { 31 $email_addresses[$key] = webform_format_email_address($email_address, NULL, $node, NULL, FALSE); 32 } 33 34 $form['emails'][$eid]['email'] = array( 35 '#value' => implode('<br />', $email_addresses), 36 ); 37 $form['emails'][$eid]['subject'] = array( 38 '#value' => check_plain(webform_format_email_subject($email['subject'], $node)), 39 ); 40 $form['emails'][$eid]['from'] = array( 41 '#value' => check_plain(webform_format_email_address($email['from_address'], $email['from_name'], $node, NULL, FALSE)), 42 ); 43 } 44 45 $form['add'] = array( 46 '#theme' => 'webform_email_add_form', 47 '#tree' => FALSE, 48 ); 49 50 $form['add']['email_option'] = array( 51 '#type' => 'radios', 52 '#options' => array( 53 'custom' => t('Address'), 54 'component' => t('Component value'), 55 ), 56 '#default_value' => 'custom', 57 ); 58 59 $form['add']['email_custom'] = array( 60 '#type' => 'textfield', 61 '#size' => 24, 62 '#maxlength' => 500, 63 ); 64 65 $form['add']['email_component'] = array( 66 '#type' => 'select', 67 '#options' => webform_component_list($node, 'email_address', FALSE), 68 ); 69 70 if (empty($form['add']['email_component']['#options'])) { 71 $form['add']['email_component']['#options'][''] = t('No available components'); 72 $form['add']['email_component']['#disabled'] = TRUE; 73 } 74 75 $form['add_button'] = array( 76 '#type' => 'submit', 77 '#value' => t('Add'), 78 '#weight' => 45, 79 ); 80 81 $form['#validate'] = array('webform_email_address_validate'); 82 83 return $form; 84 } 85 86 /** 87 * Theme the node components form. Use a table to organize the components. 88 * 89 * @param $form 90 * The form array. 91 * @return 92 * Formatted HTML form, ready for display. 93 */ 94 function theme_webform_emails_form($form) { 95 // Add CSS to display submission info. Don't preprocess because this CSS file is used rarely. 96 drupal_add_css(drupal_get_path('module', 'webform') . '/css/webform-admin.css', 'theme', 'all', FALSE); 97 drupal_add_js(drupal_get_path('module', 'webform') . '/js/webform-admin.js', 'module', 'header', FALSE, TRUE, FALSE); 98 99 $node = $form['#node']; 100 101 $header = array(t('E-mail to'), t('Subject'), t('From'), array('data' => t('Operations'), 'colspan' => 2)); 102 $rows = array(); 103 104 if (!empty($form['emails'])) { 105 foreach (element_children($form['emails']) as $eid) { 106 // Add each component to a table row. 107 $rows[] = array( 108 drupal_render($form['emails'][$eid]['email']), 109 drupal_render($form['emails'][$eid]['subject']), 110 drupal_render($form['emails'][$eid]['from']), 111 l(t('Edit'), 'node/' . $node->nid . '/webform/emails/' . $eid), 112 l(t('Delete'), 'node/' . $node->nid . '/webform/emails/' . $eid . '/delete'), 113 ); 114 } 115 } 116 else { 117 $rows[] = array(array('data' => t('Currently not sending e-mails, add an e-mail recipient below.'), 'colspan' => 5)); 118 } 119 120 // Add a row containing form elements for a new item. 121 $row_data = array( 122 array('colspan' => 3, 'data' => drupal_render($form['add'])), 123 array('colspan' => 2, 'data' => drupal_render($form['add_button'])), 124 ); 125 $rows[] = array('data' => $row_data, 'class' => 'webform-add-form'); 126 127 $output = ''; 128 $output .= theme('table', $header, $rows, array('id' => 'webform-emails')); 129 $output .= drupal_render($form); 130 return $output; 131 } 132 133 /** 134 * Theme the add new e-mail settings form on the node/x/webform/emails page. 135 */ 136 function theme_webform_email_add_form($form) { 137 // Add a default value to the custom e-mail textfield. 138 $form['email_custom']['#attributes']['rel'] = t('email@example.com'); 139 $form['email_custom']['#attributes']['class'] = 'webform-set-active webform-default-value'; 140 $form['email_option']['custom']['#title'] = $form['email_option']['custom']['#title'] . ': ' . drupal_render($form['email_custom']); 141 142 // Render the component value. 143 $form['email_component']['#attributes']['class'] = 'webform-set-active'; 144 $form['email_option']['component']['#title'] = $form['email_option']['component']['#title'] . ': ' . drupal_render($form['email_component']); 145 146 // For spacing consistency, every option is wrapped in webform-container-inline. 147 foreach (element_children($form['email_option']) as $option) { 148 $form['email_option'][$option]['#prefix'] = '<div class="webform-container-inline">'; 149 $form['email_option'][$option]['#suffix'] = '</div>'; 150 } 151 152 return drupal_render($form); 153 } 154 155 /** 156 * Submit handler for webform_emails_form(). 157 */ 158 function webform_emails_form_submit($form, &$form_state) { 159 if ($form_state['values']['email_option'] == 'custom') { 160 $email = $form_state['values']['email_custom']; 161 } 162 else { 163 $email = $form_state['values']['email_component']; 164 } 165 $form_state['redirect'] = array('node/' . $form['#node']->nid . '/webform/emails/new', 'option=' . urlencode($form_state['values']['email_option']) . '&email=' . urlencode(trim($email))); 166 } 167 168 /** 169 * Form for configuring an e-mail setting and template. 170 */ 171 function webform_email_edit_form($form_state, $node, $email = array()) { 172 module_load_include('inc', 'webform', 'includes/webform.components'); 173 174 $form = array( 175 '#tree' => TRUE, 176 ); 177 $form['node'] = array( 178 '#type' => 'value', 179 '#value' => $node, 180 ); 181 $form['eid'] = array( 182 '#type' => 'value', 183 '#value' => isset($email['eid']) ? $email['eid'] : NULL, 184 ); 185 186 // All these fields work essentially the same, with a radio button set, 187 // a textfield for custom values, and a select list for a component. 188 foreach (array('email', 'subject', 'from_address', 'from_name') as $field) { 189 switch ($field) { 190 case 'email': 191 $default_value = NULL; 192 $title = t('E-mail to address'); 193 $description = t('Form submissions will be e-mailed to this address. Any email, select, or hidden form element may be selected as the recipient address. Multiple e-mail addresses may be separated by commas.'); 194 break; 195 case 'subject': 196 $default_value = _webform_filter_values(webform_variable_get('webform_default_subject'), $node); 197 $title = t('E-mail subject'); 198 $description = t('Any textfield, select, or hidden form element may be selected as the subject for e-mails.'); 199 break; 200 case 'from_address': 201 $default_value = _webform_filter_values(webform_variable_get('webform_default_from_address'), $node); 202 $title = t('E-mail from address'); 203 $description = t('Any email, select, or hidden form element may be selected as the sender\'s e-mail address.'); 204 break; 205 case 'from_name': 206 $default_value = _webform_filter_values(webform_variable_get('webform_default_from_name'), $node); 207 $title = t('E-mail from name'); 208 $description = t('Any textfield, select, or hidden form element may be selected as the sender\'s name for e-mails.'); 209 break; 210 } 211 212 $form[$field . '_option'] = array( 213 '#title' => $title, 214 '#type' => 'radios', 215 '#default_value' => is_numeric($email[$field]) ? 'component' : ((empty($default_value) || ($email[$field] != 'default' && isset($email[$field]))) ? 'custom' : 'default'), 216 '#description' => $description, 217 ); 218 if (!empty($default_value)) { 219 $form[$field . '_option']['#options']['default'] = $default_value; 220 } 221 $form[$field . '_option']['#options']['custom'] = 'custom'; 222 $form[$field . '_option']['#options']['component'] = 'component'; 223 224 $form[$field . '_custom'] = array( 225 '#type' => 'textfield', 226 '#size' => 40, 227 '#default_value' => (!is_numeric($email[$field]) && $email[$field] != 'default') ? $email[$field] : NULL, 228 '#maxlength' => $field == 'email' ? 500 : 255, 229 ); 230 $options = webform_component_list($node, $field == 'from_address' || $field == 'email' ? 'email_address' : 'email_name', FALSE); 231 $form[$field . '_component'] = array( 232 '#type' => 'select', 233 '#default_value' => is_numeric($email[$field]) ? $email[$field] : NULL, 234 '#options' => empty($options) ? array('' => t('No available components')) : $options, 235 '#disabled' => empty($options) ? TRUE : FALSE, 236 '#weight' => 6, 237 ); 238 } 239 240 // Do not show the "E-mail from name" if using the short e-mail format. 241 if (variable_get('webform_email_address_format', 'long') == 'short') { 242 $form['from_name_option']['#access'] = FALSE; 243 $form['from_name_custom']['#access'] = FALSE; 244 $form['from_name_component']['#access'] = FALSE; 245 } 246 247 // Add the template fieldset. 248 $form['template'] = array( 249 '#type' => 'fieldset', 250 '#title' => t('E-mail template'), 251 '#collapsible' => TRUE, 252 '#collapsed' => !empty($email['cid']) && empty($email['template']), 253 '#description' => t('An e-mail template can customize the display of e-mails.'), 254 '#weight' => 15, 255 '#tree' => FALSE, 256 '#attributes' => array('id' => 'webform-template-fieldset'), 257 ); 258 259 $form['template']['template_option'] = array( 260 '#type' => 'select', 261 '#options' => array( 262 'default' => t('Default template'), 263 'custom' => t('Custom template'), 264 ), 265 ); 266 267 $default_template = theme(array('webform_mail_' . $node->nid, 'webform_mail', 'webform_mail_message'), $node, NULL, $email); 268 $template = $email['template'] == 'default' ? $default_template : $email['template']; 269 $form['template']['template'] = array( 270 '#type' => 'textarea', 271 '#rows' => max(10, min(20, count(explode("\n", $template)))), 272 '#default_value' => $template, 273 ); 274 275 $form['template']['html'] = array( 276 '#type' => 'checkbox', 277 '#title' => t('Send e-mail as HTML'), 278 '#default_value' => $email['html'], 279 '#access' => module_exists('mimemail') && !variable_get('webform_format_override', 0), 280 ); 281 282 $form['template']['attachments'] = array( 283 '#type' => 'checkbox', 284 '#title' => t('Include files as attachments'), 285 '#default_value' => $email['attachments'], 286 '#access' => module_exists('mimemail'), 287 ); 288 289 $form['template']['tokens'] = array( 290 '#value' => theme('webform_token_help', $node), 291 ); 292 293 $form['template']['components'] = array( 294 '#type' => 'select', 295 '#title' => t('Included e-mail values'), 296 '#options' => webform_component_list($node, 'email', TRUE), 297 '#default_value' => array_diff(array_keys($node->webform['components']), $email['excluded_components']), 298 '#multiple' => TRUE, 299 '#size' => 10, 300 '#description' => t('The selected components will be included in the %email_values token. Individual values may still be printed if explicitly specified as a %email[key] in the template.'), 301 '#process' => array('webform_component_select'), 302 ); 303 304 // TODO: Allow easy re-use of existing templates. 305 $form['templates']['#tree'] = TRUE; 306 $form['templates']['default'] = array( 307 '#type' => 'textarea', 308 '#value' => $default_template, 309 '#resizable' => FALSE, 310 '#weight' => 19, 311 ); 312 313 // Add the submit button. 314 $form['submit'] = array( 315 '#type' => 'submit', 316 '#value' => t('Save e-mail settings'), 317 '#weight' => 20, 318 ); 319 320 $form['#validate'] = array('webform_email_address_validate', 'webform_email_edit_form_validate'); 321 322 return $form; 323 } 324 325 /** 326 * Theme the Webform mail settings section of the node form. 327 */ 328 function theme_webform_email_edit_form($form) { 329 drupal_add_css(drupal_get_path('module', 'webform') . '/css/webform-admin.css', 'theme', 'all', FALSE); 330 drupal_add_js(drupal_get_path('module', 'webform') . '/js/webform-admin.js', 'module', 'header', FALSE, TRUE, FALSE); 331 drupal_add_js(array('webform' => array('revertConfirm' => t('Are you sure you want to revert any changes to your template back to the default?'))), 'setting'); 332 333 // Loop through fields, rendering them into radio button options. 334 foreach (array('email', 'subject', 'from_address', 'from_name') as $field) { 335 foreach (array('custom' => t('Custom'), 'component' => t('Component')) as $option => $title) { 336 $form[$field . '_' . $option]['#attributes']['class'] = 'webform-set-active'; 337 $form[$field . '_option'][$option]['#title'] = $title . ': ' . drupal_render($form[$field . '_' . $option]); 338 } 339 // For spacing consistency, every option is wrapped in webform-container-inline. 340 foreach (element_children($form[$field . '_option']) as $option) { 341 $form[$field . '_option'][$option]['#prefix'] = '<div class="webform-container-inline">'; 342 $form[$field . '_option'][$option]['#suffix'] = '</div>'; 343 } 344 // Wrap the default option in a placeholder tag.. 345 if (isset($form[$field . '_option']['#options']['default'])) { 346 $form[$field . '_option']['default']['#title'] = t('Default') . ': ' . theme('placeholder', $form[$field . '_option']['default']['#title']); 347 } 348 } 349 350 $details = ''; 351 $details .= drupal_render($form['subject_option']); 352 $details .= drupal_render($form['from_address_option']); 353 $details .= drupal_render($form['from_name_option']); 354 $form['details'] = array( 355 '#type' => 'fieldset', 356 '#title' => t('E-mail header details'), 357 '#weight' => 10, 358 '#children' => $details, 359 '#collapsible' => FALSE, 360 ); 361 362 // Ensure templates are completely hidden. 363 $form['templates']['#prefix'] = '<div id="webform-email-templates" style="display: none">'; 364 $form['templates']['#suffix'] = '</div>'; 365 366 return drupal_render($form); 367 } 368 369 /** 370 * Validate handler for webform_email_edit_form() and webform_emails_form(). 371 */ 372 function webform_email_address_validate($form, &$form_state) { 373 if ($form_state['values']['email_option'] == 'custom') { 374 $email = trim($form_state['values']['email_custom']); 375 if (empty($email)) { 376 form_set_error('email_custom', t('When adding a new custom e-mail, the e-mail field is required.')); 377 } 378 else { 379 $emails = array_filter(explode(',', $email)); 380 foreach ($emails as $email) { 381 if (!valid_email_address(trim($email))) { 382 form_set_error('email_custom', t('The entered e-mail address "@email" does not appear valid.', array('@email' => $email))); 383 } 384 } 385 } 386 } 387 } 388 389 /** 390 * Validate handler for webform_email_edit_form(). 391 */ 392 function webform_email_edit_form_validate($form, &$form_state) { 393 if ($form_state['values']['from_address_option'] == 'custom' && !valid_email_address($form_state['values']['from_address_custom'])) { 394 form_set_error('from_address_custom', t('The entered e-mail address "@email" does not appear valid.', array('@email' => $form_state['values']['from_address_custom']))); 395 } 396 } 397 398 /** 399 * Submit handler for webform_email_edit_form(). 400 */ 401 function webform_email_edit_form_submit($form, &$form_state) { 402 // Merge the e-mail, name, address, and subject options into single values. 403 $email = array( 404 'eid' => $form_state['values']['eid'], 405 'nid' => $form_state['values']['node']->nid, 406 ); 407 408 foreach (array('email', 'from_name', 'from_address', 'subject') as $field) { 409 $option = $form_state['values'][$field . '_option']; 410 if ($option == 'default') { 411 $email[$field] = 'default'; 412 } 413 else { 414 $email[$field] = $form_state['values'][$field . '_' . $option]; 415 } 416 } 417 418 // Ensure templates are unaffected by differences in line breaks. 419 $form_state['values']['template'] = str_replace(array("\r", "\n"), array('', "\n"), $form_state['values']['template']); 420 $form_state['values']['templates']['default'] = str_replace(array("\r", "\n"), array('', "\n"), $form_state['values']['templates']['default']); 421 422 // Set the template value. 423 // TODO: Support reuse of templates. 424 if (strcmp(trim($form_state['values']['templates']['default']), trim($form_state['values']['template'])) == 0) { 425 $email['template'] = 'default'; 426 } 427 else { 428 $email['template'] = $form_state['values']['template']; 429 } 430 431 // Save the attachment and HTML options provided by MIME mail. 432 $email['html'] = empty($form_state['values']['html']) ? 0 : 1; 433 $email['attachments'] = empty($form_state['values']['attachments']) ? 0 : 1; 434 435 // Save the list of included components. 436 // We actually maintain an *exclusion* list, so any new components will 437 // default to being included in the %email_values token until unchecked. 438 $included = array_keys(array_filter((array) $form_state['values']['components'])); 439 $excluded = array_diff(array_keys($form_state['values']['node']->webform['components']), $included); 440 $email['excluded_components'] = $excluded; 441 442 if (empty($form_state['values']['eid'])) { 443 drupal_set_message(t('Email settings added.')); 444 $cid = webform_email_insert($email); 445 } 446 else { 447 drupal_set_message(t('Email settings updated.')); 448 webform_email_update($email); 449 } 450 451 $form_state['redirect'] = array('node/' . $form_state['values']['node']->nid . '/webform/emails'); 452 } 453 454 /** 455 * Form for deleting an e-mail setting. 456 */ 457 function webform_email_delete_form($form_state, $node, $email) { 458 $eid = $email['eid']; 459 460 $form = array(); 461 $form['node'] = array( 462 '#type' => 'value', 463 '#value' => $node, 464 ); 465 $form['email'] = array( 466 '#type' => 'value', 467 '#value' => $email, 468 ); 469 470 $question = t('Delete e-mail settings?'); 471 if (is_numeric($email['email'])) { 472 $description = t('This will immediately delete the e-mail settings based on the @component component.', array('@component' => $email['email'])); 473 } 474 else { 475 $description = t('This will immediately delete the e-mail settings sending to the @address address.', array('@address' => $email['email'])); 476 } 477 478 return confirm_form($form, $question, 'node/' . $node->nid . '/webform/emails', $description, t('Delete')); 479 } 480 481 /** 482 * Submit handler for webform_component_delete_form(). 483 */ 484 function webform_email_delete_form_submit($form, &$form_state) { 485 drupal_set_message(t('E-mail settings deleted.')); 486 webform_email_delete($form_state['values']['node'], $form_state['values']['email']); 487 $form_state['redirect'] = 'node/' . $form_state['values']['node']->nid . '/webform/emails'; 488 } 489 490 /** 491 * Load an e-mail setting from the database or initialize a new e-mail. 492 */ 493 function webform_email_load($eid, $nid) { 494 $node = node_load($nid); 495 if ($eid == 'new') { 496 $email = array( 497 'email' => '', 498 'subject' => 'default', 499 'from_name' => 'default', 500 'from_address' => 'default', 501 'template' => 'default', 502 'excluded_components' => array(), 503 'html' => variable_get('webform_default_format', 0), 504 'attachments' => 0, 505 ); 506 } 507 else { 508 $email = isset($node->webform['emails'][$eid]) ? $node->webform['emails'][$eid] : FALSE; 509 if (variable_get('webform_format_override', 0)) { 510 $email['html'] = variable_get('webform_default_format', 0); 511 } 512 } 513 514 return $email; 515 } 516 517 /** 518 * Insert a new e-mail setting into the database. 519 * 520 * @param $email 521 * An array of settings for sending an e-mail. 522 */ 523 function webform_email_insert($email) { 524 db_lock_table('webform_emails'); 525 $email['eid'] = isset($email['eid']) ? $email['eid'] : db_result(db_query('SELECT MAX(eid) FROM {webform_emails} WHERE nid = %d', $email['nid'])) + 1; 526 $email['excluded_components'] = implode(',', $email['excluded_components']); 527 drupal_write_record('webform_emails', $email); 528 db_unlock_tables(); 529 return $email['eid']; 530 } 531 532 /** 533 * Update an existing e-mail setting with new values. 534 * 535 * @param $email 536 * An array of settings for sending an e-mail containing a nid, eid, and all 537 * other fields from the e-mail form. 538 */ 539 function webform_email_update($email) { 540 $email['excluded_components'] = implode(',', $email['excluded_components']); 541 return drupal_write_record('webform_emails', $email, array('nid', 'eid')); 542 } 543 544 /** 545 * Delete an e-mail setting. 546 */ 547 function webform_email_delete($node, $email) { 548 db_query('DELETE FROM {webform_emails} WHERE nid = %d AND eid = %d', $node->nid, $email['eid']); 549 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated: Thu Mar 24 11:18:33 2011 | Cross-referenced by PHPXref 0.7 |