You are here

function comment_spamapi_form_alter in Spam 5.3

Same name and namespace in other branches
  1. 6 content/spam_content_comment.inc \comment_spamapi_form_alter()

Form alter gets it's own function so we can reference &$form without causing errors in PHP4 installations. (If we use spamapi, we have to set a default, which PHP4 doesn't support.)

File

modules/spam_comment.inc, line 214

Code

function comment_spamapi_form_alter($form_id, &$form) {

  // Hook to scan comment before it is inserted into the database.
  if ($form_id == 'comment_form' && is_array($form)) {
    $form['admin']['status']['#options'][9] = t('Spam');
    $comment = $form['#post'];
    if (is_array($comment) && $comment['op'] == t('Post comment')) {
      $_SESSION['spam_form'] = $form;
      $cid = $form['cid']['#value'];

      // No need to scan a comment if it's being manually marked as spam.
      if ($comment['status'] == 9) {
        if (!spam_score_is_spam(_spam_comment_score($cid))) {

          // Mark previously not-spam comment as spam
          spam_mark_as_spam('comment', $cid);
        }
      }
      else {
        if (spam_score_is_spam(_spam_comment_score($cid))) {

          // Mark previously spam comment as not-spam
          spam_mark_as_not_spam('comment', $cid);
        }
        else {
          spam_scan($comment, 'comment');
        }
      }
    }
    else {
      if (isset($_SESSION['spam_form'])) {
        unset($_SESSION['spam_form']);
      }
    }
  }
  else {
    if ($form_id == 'comment_admin_overview' || $form_id == 'spam_comment_admin_overview') {
      $parameters = $form['#parameters'];
      if (is_array($parameters)) {
        if (!in_array('new', $parameters)) {
          $form['options']['operation']['#options']['markasnotspam'] = 'Mark the selected comments as not spam';
        }
        if (!in_array('spam', $parameters)) {
          $form['options']['operation']['#options']['markasspam'] = 'Mark the selected comments as spam';
        }
        if (in_array('new', $parameters)) {
          $form['options']['operation']['#options']['teachnotspam'] = 'Teach filters selected comments are not spam.';
        }
      }
      if (is_array($form['#post']) && is_array($form['#post']['comments'])) {
        foreach ($form['#post']['comments'] as $cid) {
          $score = _spam_comment_score($cid);
          if ($form['#post']['operation'] == 'markasspam') {
            if (!spam_score_is_spam($score)) {
              spam_mark_as_spam('comment', $cid);
            }
          }
          else {
            if ($form['#post']['operation'] == 'markasnotspam') {
              if (spam_score_is_spam($score)) {
                spam_mark_as_not_spam('comment', $cid);
              }
            }
            else {
              if ($form['#post']['operation'] == 'teachnotspam') {
                spam_mark_as_not_spam('comment', $cid);
              }
            }
          }
        }
      }
    }
  }
}