View source
<?php
require 'context.core.inc';
define('CONTEXT_GET', 0);
define('CONTEXT_SET', 1);
define('CONTEXT_ISSET', 2);
define('CONTEXT_CLEAR', 3);
define('CONTEXT_STORAGE_DEFAULT', 0);
define('CONTEXT_STORAGE_OVERRIDDEN', 1);
define('CONTEXT_STORAGE_NORMAL', 2);
define('CONTEXT_STATUS_DISABLED', 0);
define('CONTEXT_STATUS_ENABLED', 1);
function context_context($op = CONTEXT_GET, $namespace = null, $attribute = null, $value = null) {
static $context;
$context = !$context ? array() : $context;
switch ($op) {
case CONTEXT_GET:
if (!$namespace) {
return $context;
}
else {
if (isset($context[(string) $namespace])) {
if (is_array($context[(string) $namespace]) && isset($context[(string) $namespace][(string) $attribute])) {
return $context[(string) $namespace][(string) $attribute];
}
elseif (!$attribute) {
return $context[(string) $namespace];
}
}
}
break;
case CONTEXT_SET:
if (is_string($namespace) || is_int($namespace)) {
if (!$attribute) {
$context[(string) $namespace] = array();
return true;
}
if (!$value) {
if (is_string($attribute) || is_int($attribute)) {
$context[(string) $namespace][(string) $attribute] = true;
return true;
}
elseif (is_array($attribute) || is_object($attribute)) {
$context[(string) $namespace] = $attribute;
return true;
}
}
if ((is_string($attribute) || is_int($attribute)) && $value) {
$context[$namespace][$attribute] = $value;
return true;
}
}
break;
case CONTEXT_ISSET:
if (!$namespace) {
return false;
}
if (!$attribute) {
return isset($context[$namespace]);
}
return isset($context[$namespace][$attribute]);
case CONTEXT_CLEAR:
$context = array();
return true;
}
return false;
}
function context_set($namespace, $attribute = null, $value = null) {
return context_context(CONTEXT_SET, $namespace, $attribute, $value);
}
function context_get($namespace = null, $attribute = null) {
return context_context(CONTEXT_GET, $namespace, $attribute, null);
}
function context_isset($namespace = null, $attribute = null) {
return context_context(CONTEXT_ISSET, $namespace, $attribute, null);
}
function context_exists($namespace = null, $attribute = null) {
return context_context(CONTEXT_ISSET, $namespace, $attribute, null);
}
function context_clear() {
return context_context(CONTEXT_CLEAR);
}
function context_init() {
context_set_by_condition('sitewide', 1);
$path = array();
$path['real'] = $_GET['q'];
$path['alias'] = drupal_get_path_alias($_GET['q']);
if ($path['real'] == $path['alias']) {
unset($path['alias']);
}
foreach ($path as $path_option) {
$args = explode('/', $path_option);
if (count($args)) {
$test = array_shift($args);
context_set_by_condition('path', $test);
while (count($args)) {
$test .= '/' . array_shift($args);
context_set_by_condition('path', $test);
}
}
}
}
function context_flush_caches() {
context_invalidate_cache();
return array();
}
function context_load_context($context, $reset = FALSE) {
static $cache = array();
if (is_numeric($context)) {
$cid = $context;
if (!isset($cache[$cid])) {
$context = db_fetch_object(db_query("SELECT * FROM {context} WHERE cid = %d", $cid));
}
else {
return $cache[$cid];
}
}
else {
if (is_object($context) && isset($context->cid)) {
$context = db_fetch_object(db_query("SELECT * FROM {context} WHERE cid = %d", $context->cid));
}
else {
if (is_object($context) && $context->namespace && $context->attribute && $context->value) {
$args = array(
$context->namespace,
$context->attribute,
$context->value,
);
$context = db_fetch_object(db_query("SELECT * FROM {context} WHERE namespace = '%s' AND attribute = '%s' AND value = '%s'", $args));
}
}
}
if (!empty($context->data)) {
$context = context_unpack_context($context);
$cache[$context->cid] = $context;
return $context;
}
return false;
}
function context_save_context($context) {
if (!isset($context->cid)) {
$existing = context_load_context($context, TRUE);
if ($existing && $existing->cid) {
return false;
}
else {
$context = context_pack_context($context);
drupal_write_record('context', $context);
}
}
else {
$context = context_pack_context($context);
drupal_write_record('context', $context, 'cid');
}
cache_clear_all('context', 'cache');
return TRUE;
}
function context_delete_context($context) {
if ($context = context_load_context($context, TRUE)) {
db_query("DELETE FROM {context} WHERE cid = %d", $context->cid);
cache_clear_all('context', 'cache');
return true;
}
return false;
}
function context_pack_context($context) {
$packed = new StdClass();
$packed->cid = $context->cid;
$packed->namespace = $context->namespace;
$packed->attribute = $context->attribute;
$packed->value = $context->value;
unset($context->cid);
unset($context->namespace);
unset($context->attribute);
unset($context->value);
unset($context->type);
unset($context->status);
$context = (array) $context;
$packed->data = serialize($context);
return $packed;
}
function context_unpack_context($context) {
if (!empty($context->data)) {
$data = unserialize($context->data);
foreach ($data as $k => $v) {
$context->{$k} = $v;
}
unset($context->data);
}
return $context;
}
function context_contexts($reset = FALSE) {
static $contexts;
if (!$contexts || $reset) {
$contexts = array();
foreach (module_implements('context_default_contexts') as $module) {
$function = $module . '_context_default_contexts';
$c = call_user_func($function);
if (!empty($c)) {
foreach ($c as $context) {
$context = (object) $context;
$context->type = CONTEXT_STORAGE_DEFAULT;
$identifier = "{$context->namespace}-{$context->attribute}-{$context->value}";
$contexts[$identifier] = $context;
}
}
}
drupal_alter('context_default_contexts', $contexts);
$result = db_query("SELECT * FROM {context} ORDER BY namespace ASC, attribute ASC, value ASC");
while ($context = db_fetch_object($result)) {
$context = context_unpack_context($context);
$key = "{$context->namespace}-{$context->attribute}-{$context->value}";
if (isset($contexts[$key])) {
$contexts[$key] = $context;
$contexts[$key]->type = CONTEXT_STORAGE_OVERRIDDEN;
}
else {
$contexts[$key] = $context;
$contexts[$key]->type = CONTEXT_STORAGE_NORMAL;
}
}
foreach ($contexts as $key => $context) {
$contexts[$key]->status = context_status($context);
}
}
return $contexts;
}
function context_cache_get($key, $reset = FALSE) {
static $cache;
if (!isset($cache) || $reset) {
$cache = cache_get('context', 'cache');
$cache = $cache ? $cache->data : array();
}
return !empty($cache[$key]) ? $cache[$key] : FALSE;
}
function context_cache_set($key, $value) {
$cache = cache_get('context', 'cache');
$cache = $cache ? $cache->data : array();
$cache[$key] = $value;
cache_set('context', $cache);
}
function context_enabled_contexts($namespace = NULL, $reset = FALSE) {
static $enabled;
static $namespaces;
if (!isset($enabled) || $reset) {
$enabled = array();
$cache = context_cache_get('enabled');
if ($cache && !$reset) {
$enabled = $cache;
}
else {
$contexts = context_contexts(TRUE);
foreach ($contexts as $context) {
if (context_status($context) == CONTEXT_STATUS_ENABLED) {
$identifier = "{$context->namespace}-{$context->attribute}-{$context->value}";
$enabled[$identifier] = $context;
}
}
context_cache_set('enabled', $enabled);
}
foreach ($enabled as $identifier => $context) {
if (!isset($namespaces[$context->namespace])) {
$namespaces[$context->namespace] = array();
}
$namespaces[$context->namespace][$identifier] = $context;
}
}
if (!empty($namespace)) {
return !empty($namespaces[$namespace]) ? $namespaces[$namespace] : array();
}
return $enabled;
}
function context_active_contexts($reset = FALSE) {
static $contexts;
if (!isset($contexts) || $reset) {
$contexts = array();
$cache = context_enabled_contexts();
foreach ($cache as $context) {
if (context_get($context->namespace, $context->attribute) == $context->value) {
$identifier = "{$context->namespace}-{$context->attribute}-{$context->value}";
$contexts[$identifier] = $context;
}
}
drupal_alter('context_active_contexts', $contexts);
}
return $contexts;
}
function context_condition_map($reset = FALSE) {
static $condition_map;
if (!isset($condition_map) || $reset) {
$cache = context_cache_get('condition_map');
if ($cache && !$reset) {
$condition_map = $cache;
}
else {
$enabled = context_enabled_contexts();
foreach (array_keys(context_conditions()) as $condition) {
$condition_map[$condition] = array();
foreach ($enabled as $identifier => $context) {
if (!empty($context->{$condition})) {
if (is_array($context->{$condition})) {
foreach ($context->{$condition} as $value) {
if (!isset($condition_map[$condition][$value])) {
$condition_map[$condition][$value] = array();
}
$condition_map[$condition][$value][] = $identifier;
}
}
else {
if (is_string($context->{$condition})) {
$value = $context->{$condition};
if (!isset($condition_map[$condition][$value])) {
$condition_map[$condition][$value] = array();
}
$condition_map[$condition][$value][] = $identifier;
}
}
}
}
}
context_cache_set('condition_map', $condition_map);
}
}
return $condition_map;
}
function context_active_values($type = NULL, $reset = FALSE) {
static $value_map;
if (!isset($value_map) || $reset) {
$value_map = array();
$keys = array_merge(array_keys(context_reactions()), array_keys(context_conditions()));
$keys[] = 'block';
foreach ($keys as $key) {
foreach (context_active_contexts() as $identifier => $context) {
if (!empty($context->{$key})) {
if (!isset($value_map[$key])) {
$value_map[$key] = array();
}
if (is_array($context->{$key})) {
$value_map[$key] = array_merge($value_map[$key], $context->{$key});
}
else {
$value_map[$key][] = $context->{$key};
}
}
}
}
}
if (!empty($type)) {
return !empty($value_map[$type]) ? $value_map[$type] : array();
}
return $value_map;
}
function context_status($context) {
$status = variable_get('context_status', array());
$identifier = "{$context->namespace}-{$context->attribute}-{$context->value}";
if (isset($status[$identifier]) && !$status[$identifier]) {
return CONTEXT_STATUS_DISABLED;
}
return CONTEXT_STATUS_ENABLED;
}
function context_invalidate_cache() {
cache_clear_all('context', 'cache');
}
function context_conditions($reset = FALSE) {
static $conditions;
if (!isset($conditions) || $reset) {
$cache = context_cache_get('conditions');
if ($cache && !$reset) {
$conditions = $cache;
}
else {
$conditions = module_invoke_all('context_conditions');
context_cache_set('conditions', $conditions);
}
}
return $conditions;
}
function context_reactions($reset = FALSE) {
static $reactions;
if (!isset($reactions) || $reset) {
$cache = context_cache_get('reactions');
if ($cache && !$reset) {
$reactions = $cache;
}
else {
$reactions = module_invoke_all('context_reactions');
context_cache_set('reactions', $reactions);
}
}
return $reactions;
}
function context_set_by_condition($type, $id, $force = FALSE) {
$map = context_condition_map();
$set = FALSE;
if (!empty($map[$type]) && !empty($map[$type][$id])) {
$contexts = context_enabled_contexts();
$identifiers = $map[$type][$id];
foreach ($identifiers as $identifier) {
$context = !empty($contexts[$identifier]) ? $contexts[$identifier] : FALSE;
if ($context) {
if (!context_isset($context->namespace, $context->attribute) || $force) {
context_set($context->namespace, $context->attribute, $context->value);
$set = TRUE;
}
}
}
}
return $set;
}
function context_empty($element) {
$empty = TRUE;
if (is_array($element)) {
foreach ($element as $child) {
$empty = $empty && context_empty($child);
}
}
else {
$empty = $empty && empty($element);
}
return $empty;
}
function context_var_export($var, $prefix = '', $multiple = TRUE) {
if (is_array($var)) {
if (empty($var)) {
$output = 'array()';
}
else {
$output = "array(\n";
foreach ($var as $key => $value) {
$output .= " '{$key}' => " . context_var_export($value, $prefix . ($multiple ? '' : ' ')) . ",\n";
}
$output .= ')';
}
}
else {
if (is_bool($var)) {
$output = $var ? 'TRUE' : 'FALSE';
}
else {
$output = var_export($var, TRUE);
}
}
if ($prefix) {
$output = str_replace("\n", "\n{$prefix}", $output);
}
return $output;
}