You are here

function better_statistics_better_statistics_ajax in Better Statistics 7

Implements hook_better_statistics_ajax().

File

./better_statistics.statistics.inc, line 250
Statistics API functions and hooks for the Better Statistics module.

Code

function better_statistics_better_statistics_ajax($type, $payload) {
  switch ($type) {

    // @todo When API backwards compatibility can be broken, there should be no
    // need to duplicate this code from hook_exit().
    case 'accesslog':

      // Allow all modules to react before logging begins.
      module_invoke_all('better_statistics_prelog');

      // Get all declared fields and their computed values.
      $fields = better_statistics_get_fields_data();

      // Pluck only values passed that correspond to statistics fields that are
      // actually configured, then sanitize them.
      $configured_from_ajax = array_intersect_key($payload, $fields);
      foreach ($configured_from_ajax as &$value) {
        $value = filter_xss($value);
      }

      // Merge values from AJAX fields into server-side generated fields.
      $payload = array_merge($fields, $configured_from_ajax);

      // Allow modules to log statistics data.
      module_invoke_all('better_statistics_log', $payload);
      break;
    case 'entity_view':
      $entity_type = isset($payload['entity_type']) ? $payload['entity_type'] : '';
      $entity_id = isset($payload['entity_id']) ? $payload['entity_id'] : '';
      $timestamp = isset($payload['timestamp']) ? $payload['timestamp'] : REQUEST_TIME;
      if ($entity_type == 'node' && is_numeric($entity_id)) {

        // A node has been viewed, so update the node's counters.
        db_merge('node_counter')
          ->key(array(
          'nid' => $payload['entity_id'],
        ))
          ->fields(array(
          'daycount' => 1,
          'totalcount' => 1,
          'timestamp' => $timestamp,
        ))
          ->expression('daycount', 'daycount + 1')
          ->expression('totalcount', 'totalcount + 1')
          ->execute();
      }
      break;
  }
}