View source
<?php
function context_theme() {
$items = array();
$items['context_links'] = array(
'arguments' => array(),
);
return $items;
}
function context_theme_registry_alter(&$theme_registry) {
if (!in_array('context_preprocess_page', $theme_registry['page']['preprocess functions'])) {
$theme_registry['page']['preprocess functions'][] = 'context_preprocess_page';
}
if (!in_array('context_page_alter', $theme_registry['page']['preprocess functions'])) {
array_unshift($theme_registry['page']['preprocess functions'], 'context_page_alter');
}
if (!isset($theme_registry['blocks']['type']) || !in_array($theme_registry['blocks']['type'], array(
'base_theme_engine',
'theme_engine',
)) && !isset($theme_registry['blocks']['file'])) {
unset($theme_registry['blocks']['preprocess functions']);
$theme_registry['blocks']['function'] = 'context_blocks';
}
}
function context_context_conditions() {
$items = array();
$nodetypes = array();
foreach (node_get_types() as $type) {
$nodetypes[$type->type] = t(drupal_ucfirst($type->name));
}
$items['node'] = array(
'#title' => t('Node pages'),
'#description' => t('Set this context when viewing a node page or using the add/edit form of one of these content types.'),
'#options' => $nodetypes,
'#type' => 'checkboxes',
'#help_topic' => 'node-condition',
'#help_module' => 'context_ui',
);
$items['user'] = array(
'#title' => t('User pages'),
'#description' => t('Set this context when a user with selected role(s) is viewed'),
'#options' => user_roles(true),
'#type' => 'checkboxes',
);
if (module_exists('book')) {
$options = array();
foreach (book_get_books() as $book) {
$options[$book['menu_name']] = $book['title'];
}
$items['book'] = array(
'#title' => t('Book'),
'#description' => t('Set this context when a node in the selected book is viewed.'),
'#options' => $options,
'#type' => 'checkboxes',
);
}
$items['sitewide'] = array(
'#title' => t('Sitewide context'),
'#type' => 'radios',
'#options' => array(
0 => t('False'),
1 => t('True'),
),
'#description' => theme('advanced_help_topic', 'context_ui', 'default-contexts') . t('Should this context always be set? If <strong>true</strong>, this context will be active across your entire site.'),
'#help_topic' => 'default-contexts',
'#help_module' => 'context_ui',
);
$items['default'] = array(
'#title' => t('Default context'),
'#type' => 'radios',
'#options' => array(
0 => t('False'),
1 => t('True'),
),
'#description' => theme('advanced_help_topic', 'context_ui', 'default-context') . t('If no other context is activated in this namespace and attribute, should this one be activated?'),
'#help_topic' => 'default-context',
'#help_module' => 'context_ui',
);
$items['path'] = array(
'#title' => t('Path'),
'#description' => t('Set this context when any of the paths above match the page path. Put each path on a separate line. You can use the "*" character as a wildcard and <front> for the front page.'),
'#type' => 'textarea',
'#element_validate' => array(
'context_condition_text_validate',
),
);
if (module_exists('menu')) {
$menus = menu_parent_options(array_reverse(menu_get_menus()), NULL);
$root_menus = array();
foreach ($menus as $key => $name) {
$id = explode(':', $key);
if ($id[1] == '0') {
$root_menus[$id[0]] = check_plain($name);
}
else {
$link = menu_link_load($id[1]);
$root_menu = $root_menus[$id[0]];
$menus[$root_menu][$link['link_path']] = $name;
}
unset($menus[$key]);
}
array_unshift($menus, "-- " . t('None') . " --");
$items['menu_trail'] = array(
'#title' => t('Menu trail'),
'#description' => t('Set this context when any of the selected menu items belong to the current active menu trail.'),
'#options' => $menus,
'#type' => 'select',
'#multiple' => TRUE,
);
}
else {
$items['menu_trail'] = array(
'#type' => 'value',
);
}
if (module_exists('taxonomy')) {
$taxonomy = array();
$context_taxonomies = variable_get('context_taxonomy_vocabularies', array());
foreach (taxonomy_get_vocabularies() as $vid => $vocab) {
if (in_array($vid, $context_taxonomies)) {
$taxonomy[$vid . ':0'] = '<' . $vocab->name . '>';
foreach (taxonomy_get_tree($vid) as $term) {
$taxonomy[$vid . ':' . $term->tid] = str_repeat('-', $term->depth + 1) . ' ' . $vocab->name . ': ' . $term->name;
}
}
}
$items['taxonomy'] = array(
'#title' => t('Taxonomy Terms'),
'#description' => t('Set this context when a node with a selected term is viewed. Select the vocabulary to set the context when a node contains any term in the vocabulary. Vocabularies and their terms will only appear in this list if you have explicity included them on their settings page.'),
'#type' => 'select',
'#options' => $taxonomy,
'#multiple' => TRUE,
'#help_topic' => 'taxonomy-condition',
'#help_module' => 'context_ui',
);
}
return $items;
}
function context_condition_text_validate($element, &$form_state) {
if (!empty($element['#value']) && in_array($element['#type'], array(
'textfield',
'textarea',
))) {
switch ($element['#type']) {
case 'textfield':
$delimiter = ',';
break;
case 'textarea':
$delimiter = "\n";
break;
}
$items = $element['#value'];
$items = explode($delimiter, $items);
if (!empty($items)) {
$values = array();
foreach ($items as $k => $v) {
$v = trim($v);
if (!empty($v)) {
$values[$v] = $v;
}
}
$id = end($element['#parents']);
$form_state['values']['items'][$id] = $values;
}
}
}
function context_context_reactions() {
$items = array();
if (module_exists('menu')) {
$menus = menu_parent_options(array_reverse(menu_get_menus()), NULL);
$root_menus = array();
foreach ($menus as $key => $name) {
$id = explode(':', $key);
if ($id[1] == '0') {
$root_menus[$id[0]] = check_plain($name);
}
else {
$link = menu_link_load($id[1]);
$root_menu = $root_menus[$id[0]];
$menus[$root_menu][$link['link_path']] = $name;
}
unset($menus[$key]);
}
array_unshift($menus, "-- " . t('None') . " --");
$items['menu'] = array(
'#title' => t('Active menu'),
'#description' => t('Display the selected menu item as active in the <strong>$primary_links</strong> or <strong>$secondary_links</strong> of page.tpl.php when this context is set.'),
'#options' => $menus,
'#type' => 'select',
'#size' => 20,
'#help_topic' => 'active-menus',
'#help_module' => 'context_ui',
);
}
else {
$items['menu'] = array(
'#type' => 'value',
);
}
$items['theme_section'] = array(
'#tree' => TRUE,
'#title' => t('Theme variables'),
'title' => array(
'#title' => t('Section title'),
'#description' => t('Provides this text as a <strong>$section_title</strong> variable for display in page.tpl.php when this context is active.'),
'#type' => 'textfield',
'#maxlength' => 255,
),
'subtitle' => array(
'#title' => t('Section subtitle'),
'#description' => t('Provides this text as a <strong>$section_subtitle</strong> variable for display in page.tpl.php when this context is active.'),
'#type' => 'textfield',
'#maxlength' => 255,
),
'class' => array(
'#title' => t('Section class'),
'#description' => t('Provides this text as an additional body class (in <strong>$body_classes</strong> in page.tpl.php) when this section is active.'),
'#type' => 'textfield',
'#maxlength' => 64,
),
);
$theme_key = variable_get('theme_default', 'garland');
$regions = system_region_list($theme_key);
$items['theme_regiontoggle'] = array(
'#title' => t('Disabled regions'),
'#type' => 'checkboxes',
'#options' => $regions,
);
return $items;
}
function context_nodeapi(&$node, $op, $teaser, $page) {
if ($op == 'view' && $page && ($menu_ob = menu_get_object()) && $menu_ob->nid == $node->nid) {
context_node_condition($node, $op);
}
}
function context_ctools_render_alter($info, $page, $args, $contexts, $task, $subtask) {
if ($page && in_array($task['name'], array(
'node_view',
'node_edit',
), TRUE)) {
foreach ($contexts as $ctools_context) {
if ($ctools_context->type === 'node' && !empty($ctools_context->data)) {
context_node_condition($ctools_context->data, $task['name'] === 'node_view' ? 'view' : 'form');
break;
}
}
}
}
function context_node_condition(&$node, $op) {
context_set_by_condition('node', $node->type);
if (module_exists('book') && isset($node->book)) {
if ($node->book['menu_name']) {
context_set_by_condition('book', $node->book['menu_name']);
}
}
if (module_exists('taxonomy') && is_array($node->taxonomy)) {
foreach ($node->taxonomy as $term) {
context_set_by_condition('taxonomy', $term->vid . ':' . $term->tid);
context_set_by_condition('taxonomy', $term->vid . ':0');
}
}
}
function context_form_alter(&$form, $form_state, $form_id) {
if ($form['#id'] === 'node-form' && arg(0) != 'admin') {
context_node_condition($form['#node'], 'form');
$form['#validate'][] = 'context_form_alter_node_validate';
}
if ($form_id == 'system_modules') {
context_invalidate_cache();
}
if ($form_id == 'taxonomy_form_vocabulary') {
$vid = isset($form['vid']['#value']) ? $form['vid']['#value'] : 0;
$form['settings']['context'] = array(
'#type' => 'checkbox',
'#title' => t('Include as context condition'),
'#description' => t('Select to make this vocabulary available for use as a context condition.'),
'#default_value' => in_array($vid, variable_get('context_taxonomy_vocabularies', array())),
);
$form['#submit'][] = 'content_taxonomy_form_vocabulary_submit';
}
}
function content_taxonomy_form_vocabulary_submit($form, &$form_state) {
$context_vocabularies = variable_get('context_taxonomy_vocabularies', array());
$vid = $form_state['values']['vid'];
if ($form_state['values']['context']) {
$context_vocabularies[$vid] = $vid;
}
else {
unset($context_vocabularies[$vid]);
}
variable_set('context_taxonomy_vocabularies', $context_vocabularies);
}
function context_form_alter_node_validate($form, &$form_state) {
context_node_condition($form['#node'], 'form');
}
function context_form_comment_form_alter(&$form, $form_state) {
if ($nid = $form['nid']['#value']) {
$node = node_load($nid);
context_set_by_condition('node', $node->type);
}
}
function context_user($op, &$edit, &$account, $category = NULL) {
if ($op == 'view' && !empty($account->roles)) {
foreach (array_keys($account->roles) as $rid) {
context_set_by_condition('user', $rid);
}
}
}
function context_blocks($region) {
$disabled_regions = context_active_values('theme_regiontoggle');
if (!empty($disabled_regions) && in_array($region, $disabled_regions)) {
return '';
}
$output = "";
if ($list = context_block_list($region)) {
foreach ($list as $key => $block) {
$output .= theme("block", $block);
}
}
return $output . drupal_get_content($region);
}
function context_block_list($region) {
static $blocks;
static $context_blocks;
if (!isset($context_blocks)) {
$blocks = array();
$context_blocks = array();
$context_blocks = array();
foreach (context_active_values('block') as $block) {
$block = (object) $block;
$context_blocks["{$block->module}-{$block->delta}"] = $block;
}
global $user, $theme_key;
$rids = array_keys($user->roles);
$result = db_query(db_rewrite_sql("SELECT DISTINCT b.* FROM {blocks} b LEFT JOIN {blocks_roles} r ON b.module = r.module AND b.delta = r.delta WHERE b.theme = '%s' AND (r.rid IN (" . db_placeholders($rids) . ") OR r.rid IS NULL) ORDER BY b.region, b.weight, b.module", 'b', 'bid'), array_merge(array(
$theme_key,
), $rids));
while ($block = db_fetch_object($result)) {
$bid = "{$block->module}-{$block->delta}";
if (!empty($context_blocks[$bid])) {
$block->region = $context_blocks[$bid]->region;
$block->weight = $context_blocks[$bid]->weight;
unset($context_blocks[$bid]);
$enabled = TRUE;
}
else {
if (!$block->status) {
continue;
}
}
if (!isset($blocks[$block->region])) {
$blocks[$block->region] = array();
}
if ($block->custom != 0) {
if ($user->uid && isset($user->block[$block->module][$block->delta])) {
$enabled = $user->block[$block->module][$block->delta];
}
else {
$enabled = $block->custom == 1;
}
}
else {
$enabled = TRUE;
}
if ($block->pages) {
if ($block->visibility < 2) {
$path = drupal_get_path_alias($_GET['q']);
$page_match = drupal_match_path($path, $block->pages);
if ($path != $_GET['q']) {
$page_match = $page_match || drupal_match_path($_GET['q'], $block->pages);
}
$page_match = !($block->visibility xor $page_match);
}
else {
$page_match = drupal_eval($block->pages);
}
}
else {
$page_match = TRUE;
}
$block->enabled = $enabled;
$block->page_match = $page_match;
$blocks[$block->region]["{$block->module}_{$block->delta}"] = $block;
}
if (!empty($context_blocks)) {
foreach ($context_blocks as $block) {
$block = (object) $block;
$block->status = 1;
$block->enabled = TRUE;
$block->page_match = TRUE;
$block->throttle = FALSE;
$block->title = '';
$blocks[$block->region]["{$block->module}_{$block->delta}"] = $block;
}
}
foreach ($blocks as $r => $dummy) {
uasort($blocks[$r], '_context_block_sort');
}
}
if (!isset($blocks[$region])) {
$blocks[$region] = array();
}
foreach ($blocks[$region] as $key => $block) {
if (!isset($block->content)) {
unset($blocks[$region][$key]);
if ($block->enabled && $block->page_match) {
if (!($block->throttle && module_invoke('throttle', 'status') > 0)) {
if (!count(module_implements('node_grants')) && $_SERVER['REQUEST_METHOD'] == 'GET' && ($cid = _block_get_cache_id($block)) && ($cache = cache_get($cid, 'cache_block'))) {
$array = $cache->data;
}
else {
$array = module_invoke($block->module, 'block', 'view', $block->delta);
if (isset($cid)) {
cache_set($cid, $array, 'cache_block', CACHE_TEMPORARY);
}
}
if (isset($array) && is_array($array)) {
foreach ($array as $k => $v) {
$block->{$k} = $v;
}
}
}
if (isset($block->content) && $block->content) {
if ($block->title) {
$block->subject = $block->title == '<none>' ? '' : check_plain($block->title);
}
if (!isset($block->subject)) {
$block->subject = '';
}
$blocks[$block->region]["{$block->module}_{$block->delta}"] = $block;
}
}
}
}
return $blocks[$region];
}
function _context_block_sort($a, $b) {
return $a->weight - $b->weight;
}
function theme_context_links($links) {
$output = '';
foreach ($links as $link) {
$options = $link;
$options['attributes']['class'] = isset($link['attributes']['class']) ? $link['attributes']['class'] : 'button';
if (!empty($link['custom'])) {
$output .= l($link['title'], $link['href'], $options);
}
else {
$output .= l('+ ' . t('Add !type', array(
'!type' => $link['title'],
)), $link['href'], $options);
}
}
return $output;
}
function context_links($reset = FALSE) {
static $links;
if (!$links || $reset) {
$links = array();
$active_types = context_active_values('node');
if (!empty($active_types)) {
$types = node_get_types();
foreach ($active_types as $type) {
$add_url = 'node/add/' . str_replace('_', '-', $type);
$item = menu_get_item($add_url);
if ($item && $item['access'] && strpos($_GET['q'], $add_url) !== 0) {
$links[$type] = array(
'title' => $types[$type]->name,
'href' => $add_url,
);
}
}
}
drupal_alter('context_links', $links);
}
return $links;
}
function context_page_alter(&$vars) {
module_invoke_all('context_page_condition');
context_set_by_condition('default', 1);
module_invoke_all('context_page_reaction');
}
function context_context_page_condition() {
$map = context_condition_map();
if (!empty($map['menu_trail'])) {
$trail = menu_get_active_trail();
foreach ($trail as $item) {
if (!empty($item['href'])) {
context_set_by_condition('menu_trail', $item['href']);
}
}
}
}
function context_preprocess_page(&$vars) {
$info = context_active_values('theme_section');
$vars['section_title'] = !empty($info['title']) ? check_plain($info['title']) : '';
$vars['section_subtitle'] = !empty($info['subtitle']) ? check_plain($info['subtitle']) : '';
$classes = array();
foreach (context_active_contexts() as $context) {
if (!empty($context->theme_section['class'])) {
$classes[$context->theme_section['class']] = $context->theme_section['class'];
}
}
$vars['body_classes'] .= !empty($classes) ? ' ' . check_plain(implode(' ', $classes)) : '';
if (variable_get('menu_primary_links_source', 'primary-links') == variable_get('menu_secondary_links_source', 'secondary-links')) {
$vars['primary_links'] = theme_get_setting('toggle_primary_links') ? context_menu_navigation_links(variable_get('menu_primary_links_source', 'primary-links')) : $vars['primary_links'];
$vars['secondary_links'] = theme_get_setting('toggle_secondary_links') ? context_menu_navigation_links(variable_get('menu_secondary_links_source', 'secondary-links'), 1) : $vars['secondary_links'];
}
$vars['primary_links'] = theme_get_setting('toggle_primary_links') ? context_menu_set_active($vars['primary_links']) : $vars['primary_links'];
$vars['secondary_links'] = theme_get_setting('toggle_secondary_links') ? context_menu_set_active($vars['secondary_links']) : $vars['secondary_links'];
if ($context_links = context_links()) {
$vars['context_links'] = theme('context_links', $context_links);
}
}
function context_menu_set_active($links = array(), $reset = FALSE) {
$active_paths = context_active_values('menu');
$new_links = array();
if (!empty($links)) {
foreach ($links as $key => $link) {
if (!empty($link['href']) && in_array($link['href'], $active_paths)) {
if (isset($links['attributes']['class'])) {
$link['attributes']['class'] .= ' active';
}
else {
$link['attributes']['class'] = 'active';
}
if (strpos(' active', $key) === FALSE) {
$new_links[$key . ' active'] = $link;
}
}
else {
$new_links[$key] = $link;
}
}
}
return $new_links;
}
function context_menu_navigation_links($menu_name, $level = 0) {
$original_path = $_GET['q'];
$active_paths = context_active_values('menu');
if (!empty($active_paths)) {
$path = current($active_paths);
if (menu_get_item($path)) {
menu_set_active_item($path);
}
}
$links = menu_navigation_links($menu_name, $level);
menu_set_active_item($original_path);
return $links;
}