You are here

function webform_email_edit_form_submit in Webform 7.4

Same name and namespace in other branches
  1. 6.3 includes/webform.emails.inc \webform_email_edit_form_submit()
  2. 7.3 includes/webform.emails.inc \webform_email_edit_form_submit()

Submit handler for webform_email_edit_form().

File

includes/webform.emails.inc, line 543
Provides interface and database handling for e-mail settings of a webform.

Code

function webform_email_edit_form_submit($form, &$form_state) {

  // Ensure a webform record exists.
  $node = $form['#node'];
  webform_ensure_record($node);

  // Remove duplicate email To: addresses.
  $form_state['values']['email_custom'] = implode(',', array_unique(array_map('trim', explode(',', $form_state['values']['email_custom']))));

  // Merge the e-mail, name, address, and subject options into single values.
  $email = array(
    'eid' => $form_state['values']['eid'],
    'nid' => $node->nid,
  );
  foreach (array(
    'email',
    'from_name',
    'from_address',
    'subject',
  ) as $field) {
    $option = $form_state['values'][$field . '_option'];
    if ($option == 'default') {
      $email[$field] = 'default';
    }
    else {
      $email[$field] = $form_state['values'][$field . '_' . $option];

      // Merge the email mapping(s) into single value(s)
      $cid = $form_state['values'][$field . '_' . $option];
      if (is_numeric($cid) && isset($form_state['values'][$field . '_mapping'][$cid])) {
        $email['extra'][$field . '_mapping'] = $form_state['values'][$field . '_mapping'][$cid];
      }
    }
  }

  // Ensure templates are unaffected by differences in line breaks.
  $form_state['values']['template'] = str_replace(array(
    "\r",
    "\n",
  ), array(
    '',
    "\n",
  ), $form_state['values']['template']);
  $form_state['values']['templates']['default'] = str_replace(array(
    "\r",
    "\n",
  ), array(
    '',
    "\n",
  ), $form_state['values']['templates']['default']);

  // Set the template value.
  // @todo Support reuse of templates.
  if (strcmp(trim($form_state['values']['templates']['default']), trim($form_state['values']['template'])) == 0) {
    $email['template'] = 'default';
  }
  else {
    $email['template'] = $form_state['values']['template'];
  }

  // Save the attachment and HTML options provided by MIME mail.
  $email['html'] = empty($form_state['values']['html']) ? 0 : 1;
  $email['attachments'] = empty($form_state['values']['attachments']) ? 0 : 1;

  // Save the list of included components.
  // We actually maintain an *exclusion* list, so any new components will
  // default to being included in the [submission:values] token until unchecked.
  $included = array_keys(array_filter((array) $form_state['values']['components']));
  $excluded = array_diff(array_keys($node->webform['components']), $included);
  $email['excluded_components'] = $excluded;
  $email['exclude_empty'] = empty($form_state['values']['exclude_empty']) ? 0 : 1;
  $email['status'] = empty($form_state['values']['status']) ? 0 : 1;
  if ($form_state['values']['clone']) {
    drupal_set_message(t('Email settings cloned.'));
    $form_state['values']['eid'] = webform_email_clone($email);
  }
  elseif (empty($form_state['values']['eid'])) {
    drupal_set_message(t('Email settings added.'));
    $form_state['values']['eid'] = webform_email_insert($email);
  }
  else {
    drupal_set_message(t('Email settings updated.'));
    webform_email_update($email);
  }

  // Refresh the entity cache, should it be cached in persistent storage.
  entity_get_controller('node')
    ->resetCache(array(
    $node->nid,
  ));
  $form_state['redirect'] = array(
    'node/' . $node->nid . '/webform/emails',
  );
}