You are here

function mailhandler_node_submit in Mailhandler 7

Same name and namespace in other branches
  1. 5 mailhandler.module \mailhandler_node_submit()
  2. 6 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 474

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);
        watchdog('mailhandler', '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,
        ));
        watchdog('mailhandler', 'Node submission failure: %from may not update %type items.', array(
          '%from' => $fromaddress,
          '%type' => $node->type,
        ), WATCHDOG_WARNING);
      }
    }
    else {
      $account = user_load($node->uid);
      if (node_access('create', $node, $account)) {
        node_save($node);
        watchdog('mailhandler', '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,
        ));
        watchdog('mailhandler', 'Node submission failure: %from may not create %type items.', array(
          '%from' => $fromaddress,
          '%type' => $node->type,
        ), WATCHDOG_WARNING);
      }
    }

    // Return the node is successfully saved
    if (!isset($error_text)) {
      return $node;
    }
  }
  else {
    $error_text = t('Your submission is invalid:');
    watchdog('mailhandler', 'Node submission failure: validation error.', array(), WATCHDOG_WARNING);
  }
  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;
}