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