diff --git a/og_context/og_context.module b/og_context/og_context.module index 0f76e39..3c11b1c 100644 --- a/og_context/og_context.module +++ b/og_context/og_context.module @@ -221,73 +221,69 @@ function og_context($group_type = 'node', $group = NULL, $account = NULL, $check // User account is sent. $account = $account ? $account : user_load($user->uid); - $id = FALSE; - if ($group) { - list($id) = entity_extract_ids($group_type, $group); - } - $cache_keys = array( $group_type, - $id, $account->uid, - $check_access, + intval($check_access), ); - $cache_key = implode(':', $cache_keys); $context = &drupal_static(__FUNCTION__, array()); - if (!array_key_exists($cache_key, $context)) { - // Mark that this context hasn't been checked yet. - $context[$cache_key] = FALSE; - } + // Clear the static cache for this group type when forcing a group, + // and then store it for later use. + if (!empty($group) && og_is_group($group_type, $group)) { + unset($context[$group_type]); - if (empty($group) && $context[$cache_key] !== FALSE) { - // Set og_context_is_init to TRUE to define that function was already executed. - og_context_is_init(TRUE); - // Determine if we can return cached values. - if (empty($context[$cache_key])) { - // We already tried to find a context, but couldn't. - return FALSE; + foreach (range(0, 1) as $access) { + $context[$group_type]['detected_context'][$access] = $group; } + } - if ($context[$cache_key]['group_type'] == $group_type) { - // Return the cached values. - return $context[$cache_key]; - } + $key_exists = NULL; + $current_context = drupal_array_get_nested_value($context, $cache_keys); + + // If we found a match in the cache key, return it. + if (isset($current_context)) { + // Return the cached values. + return $current_context; } - // Set the context to array, so we can know this function has been already - // executed. - $context[$cache_key] = array(); + // If a group was found previsouly for this group_type and access checking + // type, use it. + if (!empty($context[$group_type]['detected_context'][$check_access])) { + $group = $context[$group_type]['detected_context'][$check_access]; + } + // Otherwise, try to detect the group automatically. + // TODO There's no way to forward $account to og_context_determine_context. + else { + $gid = og_context_determine_context($group_type, NULL, $check_access); + $group = $gid ? entity_load_single($group_type, $gid) : FALSE; + } - if (!empty($group)) { - // Set og_context_is_init to TRUE to define that function was already executed. - og_context_is_init(TRUE); - if (!og_is_group($group_type, $group)) { - // The passed entity isn't a valid group. - return FALSE; - } + // If we need to check access to the group, then do so now. + if ($check_access && $group && !entity_access('view', $group_type, $group, $account)) { + $group = FALSE; + } - if ($check_access && !entity_access('view', $group_type, $group, $account)) { - // User doesn't have access to the group. - return FALSE; - } + if ($group) { + list($group_id) = entity_extract_ids($group_type, $group); + $current_context = array('group_type' => $group_type, 'gid' => $group_id); + } + else { + $current_context = FALSE; + } - list($id) = entity_extract_ids($group_type, $group); - $context[$cache_key] = array('group_type' => $group_type, 'gid' => $id); - } - // Get context from context handlers. - elseif ($gid = og_context_determine_context($group_type, NULL, $check_access)) { - // Set og_context_is_init to TRUE to define that function was already executed. - og_context_is_init(TRUE); - $context[$cache_key] = array('group_type' => $group_type, 'gid' => $gid); - if ($user->uid) { - // Save the group ID in the authenticated user's session. - $_SESSION['og_context'] = array('group_type' => $group_type, 'gid' => $gid); - } + // Save the group ID in the authenticated user's session. + // TODO That makes no sense if $account was set ? + if ($current_context && $user->uid) { + // Save the group ID in the authenticated user's session. + $_SESSION['og_context'] = $current_context; } - return $context[$cache_key]; + // Save the context we've just built in our static cache. + drupal_array_set_nested_value($context, $cache_keys, $current_context); + + return $current_context; } /**