You are here

context_node.module in Context Node 6

Same filename and directory in other branches
  1. 7 context_node.module

File

context_node.module
View source
<?php

/**
 * Implements hook_permission()
 */
function context_node_perm() {
  return array(
    'set context on nodes',
  );
}

/**
 * Implements hook_form_alter()
 */
function context_node_form_alter(&$form, $form_state, $form_id) {
  if ($form_id == 'node_type_form') {
    $form['context'] = array(
      '#type' => 'fieldset',
      '#title' => t("Allowed node contexts"),
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
      '#weight' => 10,
    );
    $form['context']['context_node'] = array(
      '#type' => 'checkboxes',
      '#title' => t('Select allowed contents'),
      '#options' => _context_node_get_contexts(),
      '#description' => t('Select all contexts that will be available for this content type'),
    );
    if (variable_get('context_node_' . $form['#node_type']->type, '') != NULL) {
      $form['context']['context_node']['#default_value'] = variable_get('context_node_' . $form['#node_type']->type, '');
    }
    $default = variable_get('context_node_default_' . $form['#node_type']->type, FALSE);
    $form['context']['context_node_default'] = array(
      '#type' => 'radios',
      '#title' => t('Select the default context'),
      '#default_value' => isset($default) ? $default : "none",
      '#options' => _context_node_get_default_contexts(),
      '#description' => t('Select the default context. If you select "Disabled" this functionality will be disabled for this content type. If you select "Default" the functionality will be enabled but no context will be enabled by default'),
    );
  }

  // Node Form
  if (isset($form['type']) && $form['type']['#value'] . '_node_form' == $form_id) {
    $node = $form['#node'];

    // Check if this content type is enabled to use 'context node'
    $option = variable_get("context_node_default_" . $node->type, '');
    if ($option == "none") {
      return;
    }
    if (!empty($option)) {
      $form['context_node'] = array(
        '#type' => 'fieldset',
        '#title' => t('Context'),
        '#collapsible' => TRUE,
        '#collapsed' => TRUE,
        '#access' => user_access('set context on nodes'),
        '#weight' => 130,
      );
      $options = _context_node_get_contexts_node_type($node->type);
      $default = variable_get("context_node_default_" . $node->type, '');
      $form['context_node']['context'] = array(
        '#type' => 'radios',
        '#title' => t('Context'),
        '#description' => t('Select a context from the list to change the layout and configuration of this !type', array(
          '!type' => $node->type,
        )),
        '#default_value' => isset($node->context) ? $node->context : $default,
        '#options' => $options,
        '#access' => user_access('set context on nodes'),
        '#submit' => array(
          'context_node_form_submit',
        ),
      );
    }
  }
}

/**
 * Submit callback
 */
function context_node_form_submit($form, &$form_state) {
  $form_state['node']->context = $form_state['values']['context'];
  $form_state['rebuild'] = TRUE;
}

/**
 * Return a formatted list of all contexts
 */
function _context_node_get_contexts() {
  $contexts = context_enabled_contexts();
  ksort($contexts);
  $con = array();
  foreach ($contexts as $context) {
    $con[$context->name] = $context->name;
    $cons[] = $con;
  }
  return $con;
}

/**
 * Return a formatted list of all contexts
 */
function _context_node_get_default_contexts() {
  $contexts = context_enabled_contexts();
  ksort($contexts);
  $con = array();
  $con["none"] = "Disabled";
  $con["default"] = "Default";
  foreach ($contexts as $context) {
    $con[$context->name] = $context->name;
    $cons[] = $con;
  }
  return $con;
}

/**
 * Return a formatted list of all availables context for a given content type
 * to use it when creating/updating a node
 */
function _context_node_get_contexts_node_type($type) {
  $contexts = variable_get("context_node_" . $type, '');
  $con = array();
  $con["default"] = "Default";
  foreach ($contexts as $context) {
    $con[$context] = $context;
    $cons[] = $con;
  }
  return $con;
}

/**
 * Implements hook_nodeapi()
 */
function context_node_nodeapi(&$node, $op, $teaser, $page) {

  // Check if this content type is enabled to use 'context node'
  $option = variable_get("context_node_default_" . $node->type, '');
  if ($option == "none" || empty($option)) {
    return;
  }
  switch ($op) {
    case 'load':
      $node->context = db_result(db_query('SELECT context FROM {context_node} WHERE vid = %d', $node->vid));
      return $node->context;
      break;
    case 'insert':
      db_query("INSERT INTO {context_node} (nid, vid, context) VALUES (%d, %d, '%s')", $node->nid, $node->vid, $node->context);
      break;
    case 'update':

      // Check for a new revision
      if ($node->revision) {
        db_query("INSERT INTO {context_node} (nid, vid, context) VALUES (%d, %d, '%s')", $node->nid, $node->vid, $node->context);
      }
      elseif (_context_node_check_for_context($node)) {
        db_query("UPDATE {context_node} SET context = '%s' WHERE vid = %d", $node->context, $node->vid);
      }
      else {
        db_query("INSERT INTO {context_node} (nid, vid, context) VALUES (%d, %d, '%s')", $node->nid, $node->vid, $node->context);
      }
      break;
    case 'delete':
      db_query('DELETE FROM {context_node} WHERE nid = %d', $node->nid);
      break;
    case 'delete revision':

      // Notice that we're matching a single revision based on the node's vid.
      db_query('DELETE FROM {node_example} WHERE vid = %d', $node->vid);
      break;
    case 'view':

      // Get the context in $node->context
      $name = $node->context;
      if ($name == "none" || empty($name) || $name == "default") {
        return;
      }

      // Load the context
      $context = context_load($name);

      // Set the context
      context_set('context', "context_node", $context);
      break;
  }
}

/**
 * Check if a node has a context in the {context_node} table
 *
 * @param $node The node to check
 */
function _context_node_check_for_context($node) {
  $result = db_result(db_query("SELECT nid FROM {context_node} WHERE nid = %d", $node->nid));
  if ($result) {
    return TRUE;
  }
  else {
    return FALSE;
  }
}

/**
* Implementation of hook_content_extra_fields().
*
* Provide integration with multistep
*/
function context_node_content_extra_fields($type_name) {
  $fields['context_node'] = array(
    'label' => t('Context Node'),
    'description' => t('Select a context'),
    'weight' => 0,
  );
  return $fields;
}

Functions

Namesort descending Description
context_node_content_extra_fields Implementation of hook_content_extra_fields().
context_node_form_alter Implements hook_form_alter()
context_node_form_submit Submit callback
context_node_nodeapi Implements hook_nodeapi()
context_node_perm Implements hook_permission()
_context_node_check_for_context Check if a node has a context in the {context_node} table
_context_node_get_contexts Return a formatted list of all contexts
_context_node_get_contexts_node_type Return a formatted list of all availables context for a given content type to use it when creating/updating a node
_context_node_get_default_contexts Return a formatted list of all contexts