You are here

function mailhandler_comment_submit in Mailhandler 7

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

Create the comment.

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

File

./mailhandler.module, line 440

Code

function mailhandler_comment_submit($node, $header, $mailbox, $origbody) {
  global $user;
  if (!$node->subject) {
    $node->subject = $node->title;
  }

  // When submitting comments, 'comment' means actualy the comment's body, and not the comments status for a node.
  // We need to reset the comment's body, so it doesn't colide with a default 'comment' command.
  $node->comment = $node->body;

  // comment_save will not fall back to permission system if we set the status explicitly
  // See comment_save.  += will not overwrite an existing array property.
  if (property_exists($node, 'status')) {

    // In comment module, status of 1 means unpublished, status of 0 means published.
    $node->status == 1 ? $node->status = 0 : ($node->status = 1);
  }

  // We want the comment to have the email time, not the current time
  $node->timestamp = $node->created;

  // comment_save gets an array
  $edit = (array) $node;

  // post the comment. if unable, send a failure email when so configured
  $cid = comment_save($edit);
  if (!$cid && $mailbox['replies']) {

    // $fromaddress really refers to the mail header which is authoritative for authentication
    list($fromaddress, $fromname) = mailhandler_get_fromaddress($header, $mailbox);
    $error_text = t('Sorry, your comment experienced an error and was not posted. Possible reasons are that you have insufficient permission to post comments or the node is no longer open for comments.');
    $params = array(
      'body' => $origbody,
      'error_messages' => array(),
      'error_text' => $error_text,
      'from' => $fromaddress,
      'header' => $header,
      'node' => $node,
    );
    drupal_mail('mailhandler', 'mailhandler_error_comment', $fromaddress, user_preferred_language($user), $params);
    watchdog('mailhandler', 'Comment submission failure: %subject.', array(
      '%subject' => $edit['subject'],
    ), WATCHDOG_ERROR);
  }
  return $cid;
}