You are here

apachesolr_exclude_node.module in Apache Solr Exclude Node 6

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

Module file for the Apache Solr Exclude Node module.

File

apachesolr_exclude_node.module
View source
<?php

/**
 * @file
 * Module file for the Apache Solr Exclude Node module.
 */

/**
 * Implementation of hook_perm().
 */
function apachesolr_exclude_node_perm() {
  return array(
    'exclude nodes from apache solr',
  );
}

/**
 * Implementation of hook_form_node_type_form_alter().
 */
function apachesolr_exclude_node_form_node_type_form_alter(&$form, $form_state) {
  $form['submission']['apachesolr_exclude_node_enable'] = array(
    '#type' => 'checkbox',
    '#title' => t('Enable excluding of nodes from Apache Solr'),
    '#default_value' => variable_get('apachesolr_exclude_node_enable_' . $form['#node_type']->type, 0),
    '#description' => t('Check this box to enable excluding of specific nodes from Apache Solr for this content type.'),
  );
}

/**
 * Implementation of hook_form_alter().
 */
function apachesolr_exclude_node_form_alter(&$form, $form_state, $form_id) {
  if (strpos($form_id, '_node_form') !== FALSE && user_access('exclude nodes from apache solr') && variable_get('apachesolr_exclude_node_enable_' . $form['type']['#value'], 0)) {
    $node = $form['#node'];
    $form['apachesolr_exclude_node'] = array(
      '#type' => 'fieldset',
      '#title' => t('Apache Solr exclude'),
      '#description' => t('Apache Solr exclude'),
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
      '#weight' => 30,
    );
    $form['apachesolr_exclude_node']['apachesolr_exclude_node_enabled'] = array(
      '#type' => 'checkbox',
      '#title' => t('Exclude from Apache Solr'),
      '#default_value' => isset($node->apachesolr_exclude_node_enabled) ? $node->apachesolr_exclude_node_enabled : 0,
      '#description' => t('Exclude this node from being indexing in Apache Solr.'),
    );
  }
}

/**
 * Implementation of hook_nodeapi().
 */
function apachesolr_exclude_node_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
  switch ($op) {
    case 'insert':
      if (!empty($node->apachesolr_exclude_node_enabled)) {
        $exclude = $node->apachesolr_exclude_node_enabled ? 1 : 0;
        db_query('INSERT INTO {apachesolr_exclude_node} (nid, vid, exclude) VALUES (%d, %d, %d)', $node->nid, $node->vid, $exclude);
      }
      break;
    case 'update':
      if (!empty($node->apachesolr_exclude_node_enabled)) {
        $result = db_result(db_query('SELECT vid FROM {apachesolr_exclude_node} WHERE vid = %d', $node->vid));
        if ($result) {
          $exclude = $node->apachesolr_exclude_node_enabled ? 1 : 0;
          db_query('UPDATE {apachesolr_exclude_node} SET exclude = %d WHERE vid = %d', $exclude, $node->vid);
        }
        else {
          apachesolr_exclude_node_nodeapi($node, 'insert');
        }
      }
      else {
        apachesolr_exclude_node_nodeapi($node, 'delete revision');
      }
      break;
    case 'delete':
      db_query('DELETE FROM {apachesolr_exclude_node} WHERE nid = %d', $node->nid);
      break;
    case 'delete revision':
      db_query('DELETE FROM {apachesolr_exclude_node} WHERE vid = %d', $node->vid);
      break;
    case 'load':
      $exclude = db_result(db_query('SELECT exclude FROM {apachesolr_exclude_node} WHERE vid = %d', $node->vid));
      $node->apachesolr_exclude_node_enabled = $exclude;
      break;
  }
}

/**
 * Implementation of hook_apachesolr_node_exclude()
 */
function apachesolr_exclude_node_apachesolr_node_exclude($node) {
  if (variable_get('apachesolr_exclude_node_enable_' . $node->type, 0)) {
    if (isset($node->apachesolr_exclude_node_enabled) && $node->apachesolr_exclude_node_enabled) {
      $apachesolr_path = drupal_get_path('module', 'apachesolr');
      $apachesolr_info = drupal_parse_info_file($apachesolr_path . '/apachesolr.info');
      if (strpos($apachesolr_info['version'], '6.x-3') === 0) {
        $node = node_load($node);
        apachesolr_entity_delete($node, 'node');
      }
      else {
        apachesolr_delete_node_from_index($node);
      }
      return TRUE;
    }
  }
}

Functions

Namesort descending Description
apachesolr_exclude_node_apachesolr_node_exclude Implementation of hook_apachesolr_node_exclude()
apachesolr_exclude_node_form_alter Implementation of hook_form_alter().
apachesolr_exclude_node_form_node_type_form_alter Implementation of hook_form_node_type_form_alter().
apachesolr_exclude_node_nodeapi Implementation of hook_nodeapi().
apachesolr_exclude_node_perm Implementation of hook_perm().