You are here

function _prev_next_add in Previous/Next API 7

Same name and namespace in other branches
  1. 6 prev_next.module \_prev_next_add()
  2. 7.2 prev_next.module \_prev_next_add()

Create or update the prev_next records.

4 calls to _prev_next_add()
prev_next_cron in ./prev_next.module
Implements hook_cron().
prev_next_node_insert in ./prev_next.module
Implements hook_node_insert().
_prev_next_modify in ./prev_next.module
Update the prev_next records.
_prev_next_modify_pointing_nodes in ./prev_next.module
Update other nodes pointing to a particular node

File

./prev_next.module, line 414

Code

function _prev_next_add($nid) {
  $node_type = db_query_range("SELECT type FROM {node} WHERE nid = :nid", 0, 1, array(
    ':nid' => $nid,
  ))
    ->fetchField();
  $search_criteria = variable_get(PREV_NEXT_NODE_TYPE . $node_type . '_indexing_criteria', PREV_NEXT_INDEXING_CRITERIA_DEFAULT);
  $cond = _prev_next_node_types_sql($node_type);
  if ($search_criteria != 'nid') {
    $criteria_value = db_query_range("SELECT {$search_criteria} FROM {node} WHERE nid = :nid", 0, 1, array(
      ':nid' => $nid,
    ))
      ->fetchField();
    $next_nid = db_query_range("SELECT nid FROM {node} WHERE (({$search_criteria} = :value AND nid > :nid) OR {$search_criteria} > :value) AND status = 1 {$cond} ORDER BY {$search_criteria} ASC,nid ASC", 0, 1, array(
      ':value' => $criteria_value,
      ':nid' => $nid,
    ))
      ->fetchField();
    $prev_nid = db_query_range("SELECT nid FROM {node} WHERE (({$search_criteria} = :value AND nid < :nid) OR {$search_criteria} < :value) AND status = 1 {$cond} ORDER BY {$search_criteria} DESC,nid DESC", 0, 1, array(
      ':value' => $criteria_value,
      ':nid' => $nid,
    ))
      ->fetchField();
  }
  else {
    $next_nid = db_query_range("SELECT nid FROM {node} WHERE nid > :nid AND status = 1 {$cond} ORDER BY nid ASC", 0, 1, array(
      ':nid' => $nid,
    ))
      ->fetchField();
    $prev_nid = db_query_range("SELECT nid FROM {node} WHERE nid < :nid AND status = 1 {$cond} ORDER BY nid DESC", 0, 1, array(
      ':nid' => $nid,
    ))
      ->fetchField();
  }

  // Update the node-level data
  $exists = (bool) db_query_range('SELECT 1 FROM {prev_next_node} WHERE nid = :nid', 0, 1, array(
    ':nid' => $nid,
  ))
    ->fetchField();
  if ($exists) {
    db_update('prev_next_node')
      ->fields(array(
      'prev_nid' => $prev_nid ? $prev_nid : 0,
      'next_nid' => $next_nid ? $next_nid : 0,
      'changed' => REQUEST_TIME,
    ))
      ->condition('nid', $nid)
      ->execute();
  }
  else {
    $id = db_insert('prev_next_node')
      ->fields(array(
      'prev_nid' => $prev_nid ? $prev_nid : 0,
      'next_nid' => $next_nid ? $next_nid : 0,
      'changed' => REQUEST_TIME,
      'nid' => $nid,
    ))
      ->execute();
  }

  // Update the other nodes pointing to this node
  foreach (node_type_get_types() as $type => $name) {
    if (variable_get(PREV_NEXT_NODE_TYPE . $type, 0)) {
      $search_criteria = variable_get(PREV_NEXT_NODE_TYPE . $type . '_indexing_criteria', PREV_NEXT_INDEXING_CRITERIA_DEFAULT);
      $cond = _prev_next_node_types_sql($node_type);
      if ($search_criteria != 'nid') {
        $criteria_value = db_query_range("SELECT {$search_criteria} FROM {node} WHERE nid = :nid", 0, 1, array(
          ':nid' => $nid,
        ))
          ->fetchField();
        $prev_nid = db_query_range("SELECT nid FROM {node} WHERE (({$search_criteria} = :value AND nid > :nid) OR {$search_criteria} > :value) AND status = 1 {$cond} ORDER BY {$search_criteria} ASC,nid ASC", 0, 1, array(
          ':value' => $criteria_value,
          ':nid' => $nid,
        ))
          ->fetchField();
        $next_nid = db_query_range("SELECT nid FROM {node} WHERE (({$search_criteria} = :value AND nid < :nid) OR {$search_criteria} < :value) AND status = 1 {$cond} ORDER BY {$search_criteria} DESC,nid DESC", 0, 1, array(
          ':value' => $criteria_value,
          ':nid' => $nid,
        ))
          ->fetchField();
      }
      else {
        $prev_nid = db_query_range("SELECT nid FROM {node} WHERE nid > :nid AND status = 1 {$cond} ORDER BY nid ASC", 0, 1, array(
          ':nid' => $nid,
        ))
          ->fetchField();
        $next_nid = db_query_range("SELECT nid FROM {node} WHERE nid < :nid AND status = 1 {$cond} ORDER BY nid DESC", 0, 1, array(
          ':nid' => $nid,
        ))
          ->fetchField();
      }
      if ($next_nid) {
        db_update('prev_next_node')
          ->fields(array(
          'next_nid' => $nid,
        ))
          ->condition('nid', $next_nid)
          ->execute();
      }
      if ($prev_nid) {
        db_update('prev_next_node')
          ->fields(array(
          'prev_nid' => $nid,
        ))
          ->condition('nid', $prev_nid)
          ->execute();
      }
    }
  }
}