<?php

/**
 * @file
 * Contains the administrative functions of the adsense_click module.
 */

/**
 * Menu callback for the adsense_click module settings form.
 *
 * @ingroup forms
 */
function adsense_click_admin_settings() {
  $form['click_tracking'] = array(
    '#type'  => 'fieldset',
    '#title' => t('Click tracking'),
  );

  $form['click_tracking']['adsense_click_tracking'] = array(
    '#type'          => 'checkbox',
    '#title'         => t('Enable AdSense click tracking?'),
    '#default_value' => variable_get('adsense_click_tracking', ADSENSE_CLICK_TRACKING_DEFAULT),
    '#description'   => t('Logs IP and time of <a href=!url>AdSense clicks</a>. This can be very useful in tracking which of your pages generate the most clicks, as well as help if click fraud is suspected.', array('!url' => url('admin/reports/adsense'))),
  );
  $form['click_tracking']['adsense_click_tracking_name_resolve'] = array(
    '#type'          => 'checkbox',
    '#title'         => t('Enable name resolution of IP addresses?'),
    '#default_value' => variable_get('adsense_click_tracking_name_resolve', ADSENSE_CLICK_TRACKING_NAME_RESOLVE_DEFAULT),
    '#description'   => t('Display the domain name instead of the IP address. This can slow down the display of the logs page and tie up a PHP process for several seconds. On some hosting providers it may not be available.'),
  );

  $form['flood_control'] = array(
    '#type'        => 'fieldset',
    '#title'       => t('Flood control'),
    '#description' => t('Limit the number of clicks that can be logged per IP address within a given time window. Useful for filtering click fraud or runaway bots, but the right values are site-specific.'),
  );

  $form['flood_control']['adsense_click_flood_enable'] = array(
    '#type'          => 'checkbox',
    '#title'         => t('Enable flood control'),
    '#default_value' => variable_get('adsense_click_flood_enable', ADSENSE_CLICK_FLOOD_ENABLE_DEFAULT),
    '#description'   => t('When enabled, clicks beyond the threshold from the same IP within the time window will not be recorded.'),
  );

  $form['flood_control']['adsense_click_flood_threshold'] = array(
    '#type'              => 'textfield',
    '#title'             => t('Maximum clicks per IP'),
    '#default_value'     => variable_get('adsense_click_flood_threshold', ADSENSE_CLICK_FLOOD_THRESHOLD_DEFAULT),
    '#description'       => t('Number of clicks to allow per IP address within the time window before ignoring further clicks.'),
    '#size'              => 5,
    '#maxlength'         => 6,
    '#element_validate'  => array('element_validate_integer_positive'),
    '#states'            => array(
      'visible' => array(
        ':input[name="adsense_click_flood_enable"]' => array('checked' => TRUE),
      ),
    ),
  );

  $form['flood_control']['adsense_click_flood_window'] = array(
    '#type'              => 'textfield',
    '#title'             => t('Time window (seconds)'),
    '#default_value'     => variable_get('adsense_click_flood_window', ADSENSE_CLICK_FLOOD_WINDOW_DEFAULT),
    '#description'       => t('Length of the flood control window in seconds. Common values: 3600 = 1 hour, 86400 = 24 hours.'),
    '#size'              => 8,
    '#maxlength'         => 8,
    '#element_validate'  => array('element_validate_integer_positive'),
    '#states'            => array(
      'visible' => array(
        ':input[name="adsense_click_flood_enable"]' => array('checked' => TRUE),
      ),
    ),
  );

  return system_settings_form($form);
}
