You are here

context_contrib.module in Context 6

Same filename and directory in other branches
  1. 6.2 context_contrib/context_contrib.module

File

context_contrib/context_contrib.module
View source
<?php

/**
 * Implementation of hook_context_conditions().
 */
function context_contrib_context_conditions() {
  $items = array();

  // Views
  if (module_exists('views')) {
    $items['views'] = array(
      '#title' => t('Views'),
      '#description' => t('Set this context when displaying the page of one of these views.'),
      '#options' => _context_contrib_get_views(),
      '#type' => 'checkboxes',
    );
  }

  // Nodequeue
  if (module_exists('nodequeue')) {
    $result = db_query("SELECT qid, title FROM {nodequeue_queue}");
    $options = array();
    while ($nq = db_fetch_object($result)) {
      $options[$nq->qid] = $nq->title;
    }
    $items['nodequeue'] = array(
      '#title' => t('Nodequeue'),
      '#description' => t('Set this context when a node in the selected nodequeue(s) is viewed.'),
      '#options' => $options,
      '#type' => 'checkboxes',
    );
  }
  return $items;
}

/**
 * Implementation of hook_context_reactions().
 */
function context_contrib_context_reactions() {
  $items = array();

  // CSS Injector
  if (module_exists('css_injector')) {
    $items['css_injector'] = array(
      '#title' => t('CSS injector'),
      '#description' => t('Inject the selected css when this context is set.'),
      '#options' => _context_contrib_get_css_injector(),
      '#type' => 'checkboxes',
    );
  }
  return $items;
}

/**
 * Implementation of hook_context_reaction().
 */
function context_contrib_context_reaction($context) {
  if (module_exists('css_injector')) {
    _context_contrib_css_injector_response($context);
  }
}

/**
 * Implementation of hook_nodeapi().
 */
function context_contrib_nodeapi(&$node, $op, $teaser, $page) {
  if ($op == 'view' && $page && arg(0) == 'node') {

    // Implementation of context for nodequeue.
    if (module_exists('nodequeue')) {
      $result = db_query("SELECT qid FROM {nodequeue_nodes} WHERE nid = %d", $node->nid);
      while ($qid = db_fetch_object($result)) {
        context_set_by_condition('nodequeue', $qid->qid);
      }
    }
  }
}

/**
 * Implementation of hook_views_pre_view().
 */
function context_contrib_views_pre_view($view, $args) {
  switch ($view->display_handler->display->display_plugin) {
    case 'page':
    case 'calendar':
      context_set_by_condition('views', $view->name);

      // Set any contexts associated with the current display
      if (!empty($view->current_display)) {
        context_set_by_condition('views', "{$view->name}:{$view->current_display}");
      }
      break;
  }
}

/**
 * Helper function to generate a list of database and module provided views.
 */
function _context_contrib_get_views() {
  $enabled_views = array();
  $views = views_get_all_views();
  foreach ($views as $view) {
    if (!isset($views[$view->name]->disabled) || !$views[$view->name]->disabled) {
      $enabled_views[$view->name] = $view->name;

      // Provide more granular options for each page display
      $displays = array();
      foreach ($view->display as $id => $display) {
        if ($display->display_plugin == 'page') {
          $displays[$view->name . ":" . $id] = "-- {$display->display_title}";
        }
      }
      if (count($displays) > 1) {
        $enabled_views += $displays;
      }
    }
  }
  ksort($enabled_views);
  return $enabled_views;
}

/**
 * Helper function to generate a list of css_injector files.
 */
function _context_contrib_get_css_injector() {
  $list = array();
  foreach (_css_injector_load_rule() as $css_rule) {
    $list[$css_rule['crid']] = $css_rule['title'];
  }
  ksort($list);
  return $list;
}

/**
 * Getter response function for css injector
 */
function _context_contrib_css_injector_response($context) {
  if (is_array($context->css_injector)) {
    foreach ($context->css_injector as $crid) {
      if ($css_rule = _css_injector_load_rule($crid)) {
        drupal_add_css(file_create_path($css_rule['file_path']), 'module', $css_rule['media'], $css_rule['preprocess']);
      }
    }
  }
  else {
    return;
  }
}

Functions

Namesort descending Description
context_contrib_context_conditions Implementation of hook_context_conditions().
context_contrib_context_reaction Implementation of hook_context_reaction().
context_contrib_context_reactions Implementation of hook_context_reactions().
context_contrib_nodeapi Implementation of hook_nodeapi().
context_contrib_views_pre_view Implementation of hook_views_pre_view().
_context_contrib_css_injector_response Getter response function for css injector
_context_contrib_get_css_injector Helper function to generate a list of css_injector files.
_context_contrib_get_views Helper function to generate a list of database and module provided views.