You are here

function mailhandler_node_submit in Mailhandler 5

Same name and namespace in other branches
  1. 6 mailhandler.module \mailhandler_node_submit()
  2. 7 mailhandler.module \mailhandler_node_submit()
1 call to mailhandler_node_submit()
mailhandler_retrieve in ./mailhandler.module
Retrieve all msgs from a given mailbox and process them.

File

./mailhandler.module, line 146

Code

function mailhandler_node_submit($node, $header, $mailbox, $origbody) {
  list($fromaddress, $fromname) = mailhandler_get_fromaddress($header, $mailbox);

  //dprint_r($node); //DEBUG

  // Drupal 5.x & 6.x don't support multiple validations: each node_validate()
  // call will ADD error messages to previous ones, so if some validation error
  // occours in one message it will be reported in all messages after it.
  // Since there is no way to reset form errors, the only method to avoid this
  // problem is working with $_SESSION['messages'], used by form_set_error().
  // See http://drupal.org/node/271975 for more info.
  // Warning: with this method, if the same error message is reported for 2+ different
  // fields it will be detected only for the last one.
  if (!isset($_SESSION['messages'])) {
    $_SESSION['messages'] = array();
  }
  $saved_errors = is_array($_SESSION['messages']['error']) ? $_SESSION['messages']['error'] : array();
  $_SESSION['messages']['error'] = array();
  node_validate($node);
  $error = array();
  if (count($_SESSION['messages']['error'])) {
    $allerrors = form_get_errors();
    foreach ($_SESSION['messages']['error'] as $message) {
      $keys = array_keys($allerrors, $message);
      if (!$keys || !count($keys)) {

        // Not a validation error (but an error, i'll print it)
        $saved_errors[] = $message;
      }
      else {

        // This is a validation error, i take the last field with it (previous fields
        // should be about previous validations)
        $error[$keys[count($keys) - 1]] = $message;
      }
    }
  }
  if (is_array($saved_errors) && count($saved_errors)) {
    $_SESSION['messages']['error'] = $saved_errors;
  }
  else {
    unset($_SESSION['messages']['error']);
  }
  if (!$error) {

    // Prepare the node for save and allow modules make changes
    $node = node_submit($node);

    // Save the node
    if ($node->nid) {
      if (node_access('update', $node)) {
        node_save($node);
        watchdog('mailhandler', t("Mailhandler: Updated '%t' by %f", array(
          '%t' => $node->title,
          '%f' => $fromaddress,
        )), WATCHDOG_NOTICE);
      }
      else {
        $errortxt = t("The e-mail address '%f' may not update %t items.", array(
          '%f' => $fromaddress,
          '%t' => $node->type,
        ));
      }
    }
    else {
      if (node_access('create', $node)) {
        node_save($node);
        watchdog('mailhandler', t("Mailhandler: Added '%t' by %f", array(
          '%t' => $node->title,
          '%f' => $fromaddress,
        )), WATCHDOG_NOTICE);
      }
      else {
        $errortxt = t("The e-mail address '%f' may not create %t items.", array(
          '%f' => $fromaddress,
          '%t' => $node->type,
        ));
      }
    }
  }
  else {
    $errortxt = t("Your submission is invalid: \n\n");
    foreach ($error as $key => $value) {
      $errortxt .= "{$key}: {$value}\n";
    }
  }
  if ($errortxt) {
    watchdog('mailhandler', "Mailhandler: {$errortxt}", WATCHDOG_ERROR);
    if ($mailbox['replies']) {
      $errortxt .= t("\n\nYou sent:\n\nFrom: %f\nSubject: %t\nBody:\n%b", array(
        '%f' => $fromaddress,
        '%t' => $header->subject,
        '%b' => $origbody,
      ));
      drupal_mail('mailhandler_error_node', $fromaddress, t('Email submission to %sn failed - %subj', array(
        '%sn' => variable_get('site_name', 'Drupal'),
        '%subj' => $node->title,
      )), $errortxt);
    }
  }
}