You are here

function _better_statistics_form_statistics_settings_form_alter in Better Statistics 7

Makes alterations to the core Statistics configuration form.

1 call to _better_statistics_form_statistics_settings_form_alter()
better_statistics_form_statistics_settings_form_alter in ./better_statistics.module
Implements hook_form_FORM_ID_alter().

File

./better_statistics.admin.inc, line 12
Admin and config code for the Better Statistics module.

Code

function _better_statistics_form_statistics_settings_form_alter(&$form, &$form_state, $form_id) {

  // Alter the access log settings to indicate a requirement.
  $accesslog_enabler =& $form['access']['statistics_enable_access_log'];
  $accesslog_enabler['#description'] .= ' ' . t('Also required by the Better Statistics module.');
  $accesslog_enabler['#type'] = 'radios';
  $accesslog_enabler['#options'] = array(
    BETTER_STATISTICS_ACCESSLOG_DISABLED => t('Do not log access statistics'),
    BETTER_STATISTICS_ACCESSLOG_DEFAULT => t('Log all requests served by Drupal (those served by Boost/Varnish/etc. will not be logged)'),
    BETTER_STATISTICS_ACCESSLOG_CLIENT => t('Log only HTML page views served to JS-enabled browsers, including those served by Boost/Varnish/etc.'),
    BETTER_STATISTICS_ACCESSLOG_MIXED => t('Log HTML page views served to JS-enabled browsers and all other non-HTML requests served by Drupal'),
  );
  $form['access']['#weight'] = -10;
  $content_enabler =& $form['content']['statistics_count_content_views'];
  $content_enabler['#type'] = 'radios';
  $content_enabler['#options'] = array(
    BETTER_STATISTICS_ENTITY_VIEW_DISABLED => t('Do not collect content hit statistics'),
    BETTER_STATISTICS_ENTITY_VIEW_DEFAULT => t('Collect all requests to content pages'),
    BETTER_STATISTICS_ENTITY_VIEW_CLIENT => t('Collect content statistics only when served to JS-enabled browsers'),
  );

  // Create a fieldset for data collection settings.
  $form['better_statistics'] = array(
    '#type' => 'fieldset',
    '#collapsible' => FALSE,
    '#title' => t('Access log data'),
    '#description' => t('Select the data you would like to collect on each page request. Note that unchecking a field below will permanently delete all data collected for that field.'),
    '#weight' => -8,
    '#tree' => TRUE,
    // Only show this fieldset if the accesslog is enabled.
    '#states' => array(
      'visible' => array(
        ':input[name="statistics_enable_access_log"]' => array(
          '!value' => '0',
        ),
      ),
    ),
  );

  // Get all fields provided by core.
  $fields_modules = array();
  $fields_modules['statistics'] = better_statistics_get_default_fields();

  // Get a list of all modules implementing our hook and their fields.
  $fields_modules += _better_statistics_get_custom_fields_by_module();

  // Get currently active fields from the active store.
  $current = variable_get('better_statistics_fields', better_statistics_get_default_fields());

  // Create a series of checkboxes with all declared fields.
  foreach ($fields_modules as $module => $fields) {

    // Get the module name for display.
    $module_info = system_get_info('module', $module);

    // Get the fields and current defaults for this module.
    $field_set = array();
    $defaults = array();
    foreach ($fields as $field => $data) {

      // If a title is explicitly declared, use it.
      if (isset($data['views_field']['title'])) {
        $title = '<strong>' . t($data['views_field']['title']) . '</strong>';
        $field_set[$field] = $title;
      }
      else {
        $field_set[$field] = '<strong>' . $field . '</strong>';
      }

      // If help text is explicitly declared, use it.
      if (isset($data['views_field']['help'])) {
        $field_set[$field] .= ': ' . t($data['views_field']['help']);
      }
      elseif (isset($data['schema']['description'])) {
        $field_set[$field] .= ': ' . t($data['schema']['description']);
      }

      // Check whether this field is currently active.
      if (isset($current[$field])) {
        $defaults[$field] = $field;
      }
    }

    // Special case: for now, don't allow disabling core accesslog fields.
    $disabled = $module == 'statistics' ? TRUE : FALSE;

    // Special case: Better Statistics started by simply adding its own fields
    // via the regular schema API. We should maintain those defaults if they're
    // actually reflected in the database.
    // @todo This should be removed once we hit a 1.2 or 1.3 release.
    if ($module == 'better_statistics' && empty($defaults)) {
      if (db_field_exists('accesslog', 'cache')) {
        $defaults['cache'] = 'cache';
      }
      if (db_field_exists('accesslog', 'user_agent')) {
        $defaults['user_agent'] = 'user_agent';
      }
    }

    // Make a collapsible fieldset for the module.
    $form['better_statistics'][$module] = array(
      '#type' => 'fieldset',
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
      '#title' => check_plain($module_info['name']),
    );

    // Create checkboxes for all fields detected for this module.
    $form['better_statistics'][$module]['fields'] = array(
      '#type' => 'checkboxes',
      '#options' => $field_set,
      '#disabled' => $disabled,
      '#default_value' => $defaults,
    );
  }
  $form['access_log_restrictions'] = array(
    '#type' => 'fieldset',
    '#title' => t('Access log restrictions'),
    '#description' => t('Define restrictions on when page requests are logged in the access log. As an example, you may want to disable logging in admin areas or, only log access for specific user roles.'),
    '#weight' => -6,
    // Only show this fieldset if the accesslog is enabled.
    '#states' => array(
      'visible' => array(
        ':input[name="statistics_enable_access_log"]' => array(
          '!value' => '0',
        ),
      ),
    ),
  );

  // Cache-based restrictions reference.
  if (variable_get('cache', 0)) {
    $form['access_log_restrictions']['caching'] = array(
      '#type' => 'fieldset',
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
      '#title' => t('Cache'),
    );
    $form['access_log_restrictions']['caching']['log_cached_status'] = array(
      '#markup' => t('Tracking of page requests served from cache is currently !log_cached_status. Visit !the_performance_configuration_page to change settings.', array(
        '!log_cached_status' => '<strong>' . (variable_get('statistics_access_log_restrictions_cache', TRUE) ? t('enabled') : t('disabled')) . '</strong>',
        '!the_performance_configuration_page' => l(t('the performance configuration page'), 'admin/config/development/performance'),
      )),
    );
  }

  // Page-based restrictions.
  $form['access_log_restrictions']['pages'] = array(
    '#type' => 'fieldset',
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
    '#title' => t('Pages'),
  );
  $form['access_log_restrictions']['pages']['statistics_access_log_restrictions_page'] = array(
    '#type' => 'radios',
    '#options' => array(
      0 => t('All pages except those listed'),
      1 => t('Only the listed pages'),
    ),
    '#title' => t('Log requests on specific pages'),
    '#default_value' => variable_get('statistics_access_log_restrictions_page', 0),
  );
  $form['access_log_restrictions']['pages']['statistics_access_log_restrictions_pages'] = array(
    '#type' => 'textarea',
    '#rows' => 2,
    '#default_value' => variable_get('statistics_access_log_restrictions_pages', ''),
    '#description' => t("Specify pages by using their paths. Enter one path per line. The '*' character is a wildcard. Example paths are %blog for the blog page and %blog-wildcard for every personal blog. %front is the front page.", array(
      '%blog' => 'blog',
      '%blog-wildcard' => 'blog/*',
      '%front' => '<front>',
    )),
  );

  // User-based restrictions.
  $form['access_log_restrictions']['users'] = array(
    '#type' => 'fieldset',
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
    '#title' => t('Users'),
    '#group' => 'restrictions_settings',
  );
  $form['access_log_restrictions']['users']['statistics_access_log_restrictions_roles'] = array(
    '#type' => 'checkboxes',
    '#options' => user_roles(),
    '#title' => t('Log requests for specific roles'),
    '#default_value' => variable_get('statistics_access_log_restrictions_roles', array()),
    '#description' => t('Log requests only for the selected role(s). If you select no roles, statistics will be collected for all users.'),
  );
  $form['access_log_restrictions']['users']['statistics_access_log_restrictions_dnt'] = array(
    '#type' => 'checkbox',
    '#title' => t('Respect the "Do Not Track" header'),
    '#default_value' => variable_get('statistics_access_log_restrictions_dnt', FALSE),
    '#description' => t('When checked, statistics will not be collected for users who have "do not track" enabled in their browsers.'),
  );

  // Add a submit handler to handle schema changes as a result of changes here.
  // Fire before the system settings; we don't want it to save variables.
  array_unshift($form['#submit'], 'better_statistics_settings_form_submit');
}