You are here

function better_statistics_enable_fields in Better Statistics 7

Enables a list of statistics fields.

Parameters

$fields: An array of field names to enable.

Return value

An array of statistics fields that were enabled.

2 calls to better_statistics_enable_fields()
drush_better_statistics_stats_en in ./better_statistics.drush.inc
Drush command callback for stats-en.
_better_statistics_settings_form_submit in ./better_statistics.admin.inc
Submit handler for Better Statistics configuration settings.

File

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

Code

function better_statistics_enable_fields($fields) {

  // Get all declared and active fields.
  $declared = _better_statistics_get_custom_fields_by_module();
  $active = variable_get('better_statistics_fields', better_statistics_get_default_fields());
  $successfully_enabled = array();

  // Accept a single field enable.
  if (!is_array($fields)) {
    $fields = array(
      $fields,
    );
  }

  // Load field schema data for all fields being enabled.
  $fields_added = array();
  foreach ($fields as $name) {
    foreach ($declared as $module => $module_fields) {
      if (isset($module_fields[$name])) {
        $fields_added[$name] = $module_fields[$name];
      }
    }
  }

  // Loop through each field.
  foreach ($fields_added as $field => $data) {

    // Verify the field is valid.
    $schema_set = isset($data['schema']) ? is_array($data['schema']) : FALSE;
    $callable = isset($data['callback']) ? is_callable($data['callback']) : FALSE;
    $field_exists = db_field_exists('accesslog', $field);
    if ($schema_set && $callable && !$field_exists) {

      // Add the field to the database.
      db_add_field('accesslog', $field, $data['schema']);

      // Add the field to the active store.
      $active += array(
        $field => $data,
      );

      // Log the field addition in watchdog.
      watchdog('better statistics', 'Successfully added field %field to accesslog.', array(
        '%field' => $field,
      ), WATCHDOG_NOTICE);

      // Add key to the list of fields successfully added.
      $successfully_enabled[] = $field;
    }
    elseif ($field_exists) {

      // Add the field to the active store.
      $active += array(
        $field => $data,
      );

      // Add key to the list of fields successfully added.
      $successfully_enabled[] = $field;
    }
    else {

      // Alert the failure.
      watchdog('better statistics', 'Unable to add field %field to accesslog.', array(
        '%field' => $field,
      ), WATCHDOG_WARNING);
    }
  }

  // Save the active store with any changes.
  variable_set('better_statistics_fields', $active);

  // It can be convenient to call the field update function after configuration.
  _better_statistics_update_fields();
  _better_statistics_update_methods();

  // Clear the Views data cache so fields are registered.
  if (module_exists('views')) {
    cache_clear_all('views_data:', 'cache_views', TRUE);
  }
  return $successfully_enabled;
}