You are here

function custom_admin_filter in Spam 5.3

Create or edit a custom spam filter.

1 string reference to 'custom_admin_filter'
custom_menu in filters/custom/custom.module
Drupal _menu() hook.

File

filters/custom/custom.module, line 281

Code

function custom_admin_filter($cid = NULL) {
  if ($cid) {
    drupal_set_title('Edit');
    $custom = db_fetch_object(db_query('SELECT * FROM {spam_custom} WHERE cid = %d', $cid));
    if (!isset($custom->cid)) {
      drupal_set_message(t('Failed to load custom filter.'));
      drupal_goto('admin/settings/spam/filters/custom');
    }
  }
  else {
    drupal_set_title('Create');
  }
  $form = array();
  $form['filter'] = array(
    '#type' => 'textfield',
    '#title' => t('Filter'),
    '#description' => t('Enter a custom filter string. You can enter a word, a phrase, or a regular expression.'),
    '#default_value' => $custom->cid ? $custom->filter : '',
    '#required' => TRUE,
  );
  $form['style'] = array(
    '#type' => 'radios',
    '#title' => t('Filter type'),
    '#description' => t('For a custom filter to match exactly what you type, select <code>plain text</code>.  If you would like to define a regular expression, your filter must be formatted as a <a href="http://www.php.net/manual/en/ref.pcre.php">Perl-compatible regular expression</a>.'),
    '#options' => array(
      SPAM_CUSTOM_STYLE_PLAIN => t('Plain text'),
      SPAM_CUSTOM_STYLE_REGEX => t('Regular expression'),
    ),
    '#default_value' => $custom->cid ? $custom->style : SPAM_CUSTOM_STYLE_PLAIN,
    '#required' => TRUE,
  );
  $options = array(
    SPAM_CUSTOM_SCAN_CONTENT => 'Content',
    SPAM_CUSTOM_SCAN_REFERRER => t('Referrer'),
    SPAM_CUSTOM_SCAN_USERAGENT => t('User agent'),
  );
  $scan = array();
  if ($custom->scan & SPAM_CUSTOM_SCAN_CONTENT) {
    $scan[] = SPAM_CUSTOM_SCAN_CONTENT;
  }
  if ($custom->scan & SPAM_CUSTOM_SCAN_REFERRER) {
    $scan[] = SPAM_CUSTOM_SCAN_REFERRER;
  }
  if ($custom->scan & SPAM_CUSTOM_SCAN_USERAGENT) {
    $scan[] = SPAM_CUSTOM_SCAN_USERAGENT;
  }
  $form['scan'] = array(
    '#type' => 'checkboxes',
    '#title' => t('Scan'),
    '#description' => t('Specify where you\'d like to apply your custom filter.'),
    '#options' => $options,
    '#required' => TRUE,
    '#default_value' => !empty($scan) ? $scan : array(
      SPAM_CUSTOM_SCAN_CONTENT,
    ),
  );
  $options = array();
  $form['status'] = array(
    '#type' => 'radios',
    '#title' => t('Status'),
    '#description' => t('Select the status to apply when your custom filter matches site content.  Filters are tested in the order they are displayed above, thus if content matches a filter that says to mark it as spam, and another to mark it as not spam, the first to match will be the actual status applied.'),
    '#options' => array(
      SPAM_CUSTOM_STATUS_DISABLED => t('Disabled'),
      SPAM_CUSTOM_STATUS_SPAM => t('Mark as spam'),
      SPAM_CUSTOM_STATUS_PROBABLY => t('Mark as probably spam'),
      SPAM_CUSTOM_STATUS_PROBABLYNOT => t('Mark as probably not spam'),
      SPAM_CUSTOM_STATUS_NOTSPAM => t('Mark as not spam'),
    ),
    '#default_value' => $custom->cid ? $custom->status : SPAM_CUSTOM_STATUS_SPAM,
    '#required' => TRUE,
  );
  $form['weight'] = array(
    '#type' => 'weight',
    '#title' => t('Weight'),
    '#description' => t('Give your custom filter a weight.  "Lighter" filters with smaller weights will run before "heavier" filters with larger weights.'),
    '#default_value' => $custom->weight,
  );
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => $custom->cid ? t('Update filter') : t('Create filter'),
  );
  if ($custom->cid) {
    $form['cid'] = array(
      '#type' => 'hidden',
      '#value' => $custom->cid,
    );
  }
  return $form;
}