You are here

context_node.module in Context Node 7

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

File

context_node.module
View source
<?php

/**
 * Implements hook_permission()
 */
function context_node_permission() {
  return array(
    'set context on nodes' => array(
      'title' => t('Set context on nodes'),
      'description' => t('Allow the user to set a context on each node.'),
    ),
  );
}

/**
 * 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,
      '#group' => 'additional_settings',
      '#weight' => 50,
    );
    $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'),
    );
  }
}

/**
 * Implements hook_form_node_form_alter()
 */
function context_node_form_node_form_alter(&$form, &$form_state, $form_id) {
  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'),
        '#group' => 'additional_settings',
        '#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
 */
function _context_node_get_contexts_type($type) {
  $contexts = variable_get("context_node_" . $type);
  $con = array();
  foreach ($contexts as $context) {
    $con[$context] = $context;
    $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_node_load()
 */
function context_node_node_load($nodes, $types) {
  foreach ($nodes as $node) {
    $vids[] = $node->vid;
  }
  $result = db_select('context_node', 'c')
    ->fields('c', array(
    'nid',
    'vid',
    'context',
  ))
    ->where('c.vid IN (:vids)', array(
    ':vids' => $vids,
  ))
    ->execute();
  foreach ($result as $record) {
    $nodes[$record->nid]->context = $record->context;
  }
}

/**
 * Implements hook_node_insert()
 */
function context_node_node_insert($node) {
  $option = variable_get("context_node_default_" . $node->type, '');
  if ($option == "none" || empty($option)) {
    return;
  }
  else {
    if (empty($node->context)) {
      $node->context = $option;
    }
    db_insert('context_node')
      ->fields(array(
      'nid' => $node->nid,
      'vid' => $node->vid,
      'context' => $node->context,
    ))
      ->execute();
  }
}

/**
 * Implements hook_node_update()
 */
function context_node_node_update($node) {
  $option = variable_get("context_node_default_" . $node->type, '');
  if ($option == "none" || empty($option)) {
    return;
  }
  else {
    if (empty($node->context)) {
      $node->context = $option;
    }

    // Check for a new revision
    if (!empty($node->revision)) {
      db_insert('context_node')
        ->fields(array(
        'nid' => $node->nid,
        'vid' => $node->vid,
        'context' => $node->context,
      ))
        ->execute();
    }
    elseif (_context_node_check_for_context($node)) {
      db_update('context_node')
        ->fields(array(
        'context' => $node->context,
      ))
        ->condition('vid', $node->vid)
        ->execute();
    }
    else {
      db_insert('context_node')
        ->fields(array(
        'nid' => $node->nid,
        'vid' => $node->vid,
        'context' => $node->context,
      ))
        ->execute();
    }
  }
}

/**
 * Implements hook_node_delete()
 */
function context_node_node_delete($node) {

  // We don't check if this content type's node has enabled the context node functionality
  // because once a node is deleted it doesn't matter if the context node functionality is
  // enabled or not, we just want to get rid of the node from everywhere in the database
  db_delete('context_node')
    ->condition('nid', $node->nid)
    ->execute();
}

/**
 * Implements hook_node_view()
 */
function context_node_node_view($node, $view_mode, $langcode) {
  $option = variable_get("context_node_default_" . $node->type, '');
  if ($option == "none" || empty($option)) {
    return;
  }
  else {
    if (isset($node->context)) {
      if ($node->context == "none" || empty($node->context) || $node->context == "default") {
        return;
      }

      // Load the context
      $context = context_load($node->context);

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

/**
 * 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_query("SELECT nid FROM {context_node} WHERE nid = :nid", array(
    ':nid' => $node->nid,
  ))
    ->fetchObject();
  if ($result) {
    return TRUE;
  }
  else {
    return FALSE;
  }
}

Functions

Namesort descending Description
context_node_form_alter Implements hook_form_alter()
context_node_form_node_form_alter Implements hook_form_node_form_alter()
context_node_form_submit Submit callback
context_node_node_delete Implements hook_node_delete()
context_node_node_insert Implements hook_node_insert()
context_node_node_load Implements hook_node_load()
context_node_node_update Implements hook_node_update()
context_node_node_view Implements hook_node_view()
context_node_permission 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_contexts_type Return a formatted list of all availables context for a given content type
_context_node_get_default_contexts Return a formatted list of all contexts