You are here

public function Analytic::getProperties in Analytics 6

Return a keyed array of info related to properties.

This functionr returns an array of data related to properties including the property_id, property_name, and property_instance_id for the current event.

Parameters

$key string Which property the returned array will be keyed by.:

Return value

array An array with the keys responding to the

1 call to Analytic::getProperties()
Analytic::writeEventRecord in includes/analytic.inc
Store the analytic data for an event.

File

includes/analytic.inc, line 67
Class definition for analytics.

Class

Analytic
@file Class definition for analytics.

Code

public function getProperties($key = 'property_name') {
  if (isset($this->data)) {

    // Make a copy for properties that we don't know about yet.
    $new_properties = $this->data;
    $query = db_select('analytics_properties', 'ap')
      ->condition('property_name', array_keys($this->data), 'IN');
    $query
      ->addField('api', 'id', 'property_instance_id');
    $query
      ->addField('ap', 'id', 'property_id');
    $query
      ->addField('ap', 'property_name', 'property_name');
    $query
      ->join('analytics_property_instance', 'api', 'ap.id = api.property_id AND api.event_id = :event_id', array(
      ':event_id' => $this->event_id,
    ));
    $result = $query
      ->execute();
    foreach ($result as $item) {
      $data_properties[] = array(
        'property_id' => $item->property_id,
        'property_instance_id' => $item->property_instance_id,
        'property_name' => $item->property_name,
      );
      unset($new_properties[$item->property_name]);
    }

    // See if there are properties that aren't in the DB yet.
    if (!empty($new_properties)) {
      foreach (array_keys($new_properties) as $property_name) {
        $result = db_select('analytics_properties', 'ap')
          ->fields('ap')
          ->condition('property_name', $property_name)
          ->execute()
          ->fetch();
        $property_id = $result->id;
        if (empty($property_id)) {

          // Unknown property save first, then add instance
          $property_id = db_insert('analytics_properties')
            ->fields(array(
            'property_name' => $property_name,
          ))
            ->execute();
        }
        $property_instance_id = db_insert('analytics_property_instance')
          ->fields(array(
          'event_id' => $this->event_id,
          'property_id' => $property_id,
        ))
          ->execute();
        $data_properties[] = array(
          'property_id' => $property_id,
          'property_instance_id' => $property_instance_id,
          'property_name' => $property_name,
        );
        unset($new_properties[$property_name]);
      }
    }
  }
  else {
    if (empty($this->event_id)) {
      watchdog('analytics', 'Attempted to fetch properties without setting event_name or ID first.', array(), WATCHDOG_ERROR);
      return FALSE;
    }
    $query = db_select('analytics_properties', 'ap');
    $query
      ->join('analytics_property_instance', 'api', 'ap.id = api.property_id AND api.event_id = :event_id', array(
      ':event_id' => $this->event_id,
    ));
    $query
      ->addField('api', 'id', 'property_instance_id');
    $query
      ->addField('ap', 'id', 'property_id');
    $query
      ->addField('ap', 'property_name', 'property_name');
    $result = $query
      ->execute();
    foreach ($result as $item) {
      $data_properties[] = array(
        'property_id' => $item->property_id,
        'property_instance_id' => $item->property_instance_id,
        'property_name' => $item->property_name,
      );
      unset($new_properties[$item->property_name]);
    }
  }
  foreach ($data_properties as &$item) {
    if (isset($this->data)) {
      $item['data'] = $this->data[$item['property_name']];
    }
    switch ($key) {
      case 'property_name':
        $properties['property_name'][$item['property_name']] = $item;
        break;
      case 'property_id':
        $properties['property_id'][$item['property_id']] = $item;
        break;
      case 'property_instance_id':
        $properties['property_instance_id'][$item['property_instance_id']] = $item;
        break;
    }
  }
  return $properties[$key];
}