You are here

function mailhandler_node_submit in Mailhandler 6

Same name and namespace in other branches
  1. 5 mailhandler.module \mailhandler_node_submit()
  2. 7 mailhandler.module \mailhandler_node_submit()

Create the node.

1 call to mailhandler_node_submit()
mailhandler_node_process_message in ./mailhandler.module

File

./mailhandler.module, line 971
Mailhandler module code.

Code

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

  // Reset the static cache
  form_set_error(NULL, '', TRUE);
  node_validate($node);
  if (!($error_messages = form_set_error())) {

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

    // Save the node
    if (!empty($node->nid)) {
      if (node_access('update', $node)) {
        node_save($node);
        mailhandler_watchdog_record('Updated %title by %from.', array(
          '%title' => $node->title,
          '%from' => $fromaddress,
        ));
      }
      else {
        $error_text = t('The e-mail address !from may not update !type items.', array(
          '!from' => $fromaddress,
          '!type' => $node->type,
        ));
        mailhandler_watchdog_record('Node submission failure: %from may not update %type items.', array(
          '%from' => $fromaddress,
          '%type' => $node->type,
        ), WATCHDOG_NOTICE);
      }
    }
    else {
      $account = user_load($node->uid);
      if (node_access('create', $node, $account)) {
        node_save($node);
        mailhandler_watchdog_record('Added %title by %from.', array(
          '%title' => $node->title,
          '%from' => $fromaddress,
        ));
      }
      else {
        $error_text = t('The e-mail address !from may not create !type items.', array(
          '!from' => $fromaddress,
          '!type' => $node->type,
        ));
        mailhandler_watchdog_record('Node submission failure: %from may not create %type items.', array(
          '%from' => $fromaddress,
          '%type' => $node->type,
        ), WATCHDOG_NOTICE);
      }
    }

    // Return the node is successfully saved
    if (!isset($error_text)) {
      return $node;
    }
  }
  else {
    $error_text = t('Your submission is invalid:');
    mailhandler_watchdog_record('Node submission failure: validation error.', array(), WATCHDOG_NOTICE);
  }
  if (isset($error_text)) {
    if ($mailbox['replies']) {
      $params = array(
        'body' => $origbody,
        'error_messages' => $error_messages,
        'error_text' => $error_text,
        'from' => $fromaddress,
        'header' => $header,
        'node' => $node,
      );
      drupal_mail('mailhandler', 'mailhandler_error_node', $fromaddress, user_preferred_language($user), $params);
    }
  }

  // return FALSE if the node was not successfully saved
  return FALSE;
}