You are here

tca_node.module in Token Content Access 7

Main module file for Token Content Access nodes module.

This module will add the Token Content Access functionality to nodes.

File

tca_node/tca_node.module
View source
<?php

/**
 * @file
 * Main module file for Token Content Access nodes module.
 *
 * This module will add the Token Content Access functionality to nodes.
 */

/**
 * Implements hook_tca().
 */
function tca_node_tca() {
  return array(
    'tca_node' => array(
      'entity type' => 'node',
      'base table' => 'node',
      'view path' => 'node/%/view',
    ),
  );
}

/**
 * Implements hook_form_FORM_ID_alter().
 *
 * This will add Token Content Access options to the node type form. These
 * settings will be used as default for every node of this node type.
 */
function tca_node_form_node_type_form_alter(&$form, $form_state) {

  // Add the Token Content Access form, and add an extra javascript file that's
  // needed for the fieldset summary.
  tca_form($form, $form_state, 'node', $form['#node_type']->type, 'tca_node');
  $form['#attached']['js'][] = drupal_get_path('module', 'tca_node') . '/tca_node.js';
}

/**
 * Implements hook_form_FORM_ID_alter().
 *
 * This will add Token Content Access options to the node form. The user will be
 * able to override the default Token Content Access options.
 */
function tca_node_form_node_form_alter(&$form, $form_state) {

  // Add the Token Content Access form, and add an extra javascript file that's
  // needed for the fieldset summary.
  tca_form($form, $form_state, 'node', $form['#node']->type, 'tca_node', $form['#node']);
  $form['#attached']['js'][] = drupal_get_path('module', 'tca_node') . '/tca_node.js';

  // Add a custom submit function. This is used to disable the redirect to
  // node/123 if Token Content Access is enabled and the user doesn't have the
  // bypass tca_node permission.
  if (!user_access('bypass tca_node')) {
    $form['actions']['submit']['#submit'][] = 'tca_node_node_form_submit';
  }
}

/**
 * Custom submit function for the node form.
 *
 * This will fire after the regular submit function, and it's purpose is to make
 * sure that the user doesn't get redirected to node/123 after saving the node,
 * if any Token Content Access action is enabled. This works by redirecting the
 * user to node/123/edit, if a destination parameter hasn't been set.
 *
 * @see node_form_submit()
 */
function tca_node_node_form_submit($form, &$form_state) {

  // If the action says anything else than to display the content, make sure
  // that the user doesn't land on the node view page. We'll check if a custom
  // redirect path has been set, otherwise, we'll redirect the user to the edit
  // page again.
  $active = isset($form_state['values']['tca_active']) && $form_state['values']['tca_active'] != 0 || !isset($form_state['values']['tca_active']) && tca_get_active_entity('node', $form['#node']) ? TRUE : FALSE;
  if ($active && $form_state['redirect'] == 'node/' . $form_state['values']['nid']) {
    $form_state['redirect'] = 'node/' . $form_state['values']['nid'] . '/edit';
  }
}

/**
 * Implements hook_node_view().
 */
function tca_node_node_view($node, $view_mode, $langcode) {

  // Execute Token Content Access, if the node is being viewed at its own page
  // using the full view mode, and the current user isn't able to override Token
  // Content Access.
  if ($view_mode == 'full' && node_is_page($node) && !user_access('bypass tca_node')) {
    tca_execute('node', $node);
  }
}

/**
 * Implements hook_ctools_render_alter().
 *
 * This is done since hook_node_view never gets called if the node is rendered
 * by Ctools.
 */
function tca_node_ctools_render_alter($info, $page, $context) {
  if ($page) {
    if (isset($context['handler']) && $context['handler']->task == 'node_view') {
      $node = $context['contexts'][$context['handler']->conf['context']]->data;
      if (node_is_page($node) && !user_access('bypass tca_node')) {
        tca_execute('node', $node);
      }
    }
  }
}

/**
 * Implements hook_node_insert().
 */
function tca_node_node_insert($node) {
  if (tca_get_active_entity('node', $node)) {
    $node->tca_token = tca_get_token('node', $node, $value = '');
    db_update('node')
      ->fields(array(
      'tca_token' => $node->tca_token,
    ))
      ->condition('nid', $node->nid)
      ->execute();
    if (user_access('administer tca_node')) {
      $token_url = url('node/' . $node->nid, array(
        'absolute' => TRUE,
        'query' => array(
          'tca' => $node->tca_token,
        ),
      ));
      drupal_set_message(t('URL to bypass Token Access Control for this item: @token', array(
        '@token' => $token_url,
      )));
    }
  }
}

/**
 * Implements hook_node_update().
 */
function tca_node_node_update($node) {
  if (tca_get_active_entity('node', $node)) {
    if (user_access('administer tca_node')) {
      $token_url = url('node/' . $node->nid, array(
        'absolute' => TRUE,
        'query' => array(
          'tca' => $node->tca_token,
        ),
      ));
      drupal_set_message(t('URL to bypass Token Access Control for this item: @token', array(
        '@token' => $token_url,
      )));
    }
  }
}

/**
 * Implements hook_node_type_delete().
 */
function tca_node_node_type_delete($type) {

  // Delete variables connected to this content type.
  tca_delete_variables('node', $type->type);
}

/**
 * Implements hook_views_query_alter().
 */
function tca_views_query_alter(&$view, &$query) {

  // Alter all node queries to remove TCA protected nodes if user does not have
  // bypass access.
  if ($query->base_table == 'node' && !user_access('bypass tca_node')) {
    $query
      ->add_field('node', 'tca_active', 'tca_active');
    $query
      ->add_where_expression(0, "node.tca_active = '0' OR node.tca_active IS NULL");
  }
}

/**
 * Implements hook_apachesolr_exclude().
 */
function tca_node_apachesolr_exclude($entity_id, $entity_type, $row, $env_id) {
  if ($entity_type == 'node') {

    // If TCA is active for this node, then exclude from the search.
    $exclude_search = db_query('SELECT tca_active FROM {node} WHERE nid = :nid AND tca_active = 1', array(
      ':nid' => $entity_id,
    ))
      ->fetchColumn();

    // Remove from index.
    if ($exclude_search) {
      return TRUE;
    }
    return FALSE;
  }
}