You are here

function og_context_determine_context in Organic groups 7.2

Same name and namespace in other branches
  1. 7 og_context/og_context.module \og_context_determine_context()

Determine the best matching context of a viewed page.

Parameters

$group_type: The entity type of the group. For example, "node" or "user".

$item: Optional; A menu item that context should be extracted from. If empty defaults to the current menu item by using menu_get_item().

bool $check_access: Determines if access to the group should be done. Defaults to TRUE.

Return value

int The group ID for the current context, or FALSE if not found.

1 call to og_context_determine_context()
og_context in og_context/og_context.module
Get or set group context using the menu system.

File

og_context/og_context.module, line 407
Get a group from a viewed page.

Code

function og_context_determine_context($group_type, $item = NULL, $check_access = TRUE) {

  // Enable url and node context handlers by default.
  $defaults = array(
    'url' => -5,
    'node' => -4,
  );
  if (!($enabled_providers = variable_get('og_context_negotiation_group_context', $defaults))) {
    return;
  }
  if (empty($item)) {
    $item = menu_get_item();
  }
  $providers = og_context_negotiation_info();
  foreach ($enabled_providers as $name => $ignore) {
    if (empty($providers[$name])) {
      continue;
    }
    $provider = $providers[$name];
    $invoke = FALSE;
    if (!empty($provider['menu path'])) {
      foreach ($provider['menu path'] as $path) {
        if ($item && strpos($item['path'], $path) === 0) {
          $invoke = TRUE;

          // Path matches, so we can break.
          break;
        }
      }
    }
    else {

      // Context isn't determined by the menu item.
      $invoke = TRUE;
    }
    $gids = array();
    if ($invoke && ($contexts = call_user_func($provider['callback'])) && !empty($contexts[$group_type])) {

      // Check if one of the group IDs already exists in the session, and if
      // so use it.
      $gids = $contexts[$group_type];
      if (!empty($_SESSION['og_context']['group_type']) && $_SESSION['og_context']['group_type'] == $group_type && in_array($_SESSION['og_context']['gid'], $gids)) {
        $check_gid = $_SESSION['og_context']['gid'];
      }
      else {

        // Grab the first group ID.
        $check_gid = reset($gids);
      }

      // Check if user has access to the group.
      $group = entity_load_single($group_type, $check_gid);
      if (!$check_access || entity_access('view', $group_type, $group)) {

        // We found an accessible context, so we can break.
        $gid = $check_gid;
        break;
      }
    }
  }
  return !empty($gid) ? $gid : FALSE;
}