You are here

function _tracking_code_enabled_by_region in Tracking Code 7

Process active tracking code snippets into an array.

Return value

array an array of active tracking code objects, organized by region

2 calls to _tracking_code_enabled_by_region()
tracking_code_init in ./tracking_code.module
Implements hook_init().
tracking_code_page_alter in ./tracking_code.module
Implements hook_page_alter().

File

./tracking_code.module, line 178
main module file for tracking_code module

Code

function _tracking_code_enabled_by_region() {
  $snippets =& drupal_static(__FUNCTION__);
  if (isset($snippets)) {
    return $snippets;
  }
  $node = menu_get_object();
  $snippets = array(
    'header' => array(),
    'page_top' => array(),
    'page_bottom' => array(),
  );
  $results = db_select('tracking_code', 't')
    ->fields('t')
    ->condition('status', '1', '=')
    ->execute();

  // Check page-level visibility.
  foreach ($results as $snippet) {
    $content_types = unserialize($snippet->content_types);
    $roles = unserialize($snippet->roles);
    $selected_types = array();
    $selected_roles = array();

    // Build list of content types.
    foreach ($content_types as $type => $status) {
      if ($status) {
        $selected_types[] = $type;
      }
    }

    // Build list of roles.
    foreach ($roles as $role_id => $status) {
      if ($status) {
        $selected_roles[] = $role_id;
      }
    }

    // Need to match clean URLs as well as Drupal paths.
    $path = drupal_strtolower(drupal_get_path_alias($_GET['q']));
    $page_match = drupal_match_path($path, $snippet->pages);
    if ($path != $_GET['q']) {
      $page_match = $page_match || drupal_match_path($_GET['q'], $snippet->pages);
    }
    switch ($snippet->visibility) {
      case TRACKING_CODE_VISIBILITY_NOTLISTED:
        if (!$page_match) {
          $snippets[$snippet->region][$snippet->tcid] = $snippet;
        }
        break;
      case TRACKING_CODE_VISIBILITY_LISTED:
        if ($page_match) {
          $snippets[$snippet->region][$snippet->tcid] = $snippet;
        }
        break;
    }

    // Unset snippet if not in selected content types.
    if (!empty($selected_types)) {
      if (!$node || !in_array($node->type, $selected_types)) {
        unset($snippets[$snippet->region][$snippet->tcid]);
      }
    }

    // Unset snippet if not in selected user roles.
    if (!empty($selected_roles)) {
      global $user;
      $show = FALSE;
      foreach ($user->roles as $role_id => $role) {
        if (in_array($role_id, $selected_roles)) {
          $show = TRUE;
          break;
        }
      }
      if (!$show) {
        unset($snippets[$snippet->region][$snippet->tcid]);
      }
    }
  }
  return $snippets;
}