You are here

function nodeorder_move_in_category in Node Order 6

Same name and namespace in other branches
  1. 5 nodeorder.module \nodeorder_move_in_category()
  2. 7 nodeorder.module \nodeorder_move_in_category()

Move a node up or down in its category...

1 string reference to 'nodeorder_move_in_category'
nodeorder_menu in ./nodeorder.module
Implementation of hook_menu().

File

./nodeorder.module, line 491
Nodeorder module.

Code

function nodeorder_move_in_category($direction, $nid, $tid) {

  // Note that it would be nice to wrap this in a transaction...
  $up = $direction == 'moveup';
  $node = node_load($nid);
  $destination = isset($_REQUEST['destination']) ? $_REQUEST['destination'] : $_GET['q'];

  // Get the current weight for the node
  $weight = db_result(db_query("SELECT weight_in_tid FROM {term_node} WHERE nid = %d AND tid = %d", $node->nid, $tid));
  if ($up) {
    $weights = nodeorder_get_term_min_max($tid, FALSE);
    if ($weight == $weights["min"]) {
      drupal_set_message(t('%title cannot be moved up as it already is at the top.', array(
        '%title' => $node->title,
      )), 'error');
      drupal_goto($destination);
      return;
    }
    $sql = 'SELECT DISTINCT(n.nid), n.vid, tn.weight_in_tid FROM {node} n INNER JOIN {term_node} tn ON n.vid = tn.vid WHERE tn.tid = %d AND n.status = 1 AND tn.weight_in_tid <= %d ORDER BY tn.weight_in_tid DESC';
    $direction = 'up';
  }
  else {
    $weights = nodeorder_get_term_min_max($tid, FALSE);
    if ($weight == $weights["max"]) {
      drupal_set_message(t('%title cannot be moved down as it already is at the bottom.', array(
        '%title' => $node->title,
      )), 'error');
      drupal_goto($destination);
      return;
    }
    $sql = 'SELECT DISTINCT(n.nid), n.vid, tn.weight_in_tid FROM {node} n INNER JOIN {term_node} tn ON n.vid = tn.vid WHERE tn.tid = %d AND n.status = 1 AND tn.weight_in_tid >= %d ORDER BY tn.weight_in_tid';
    $direction = 'down';
  }
  $result = db_query_range($sql, $tid, $weight, 0, 2);
  $node1 = db_fetch_object($result);
  $node2 = db_fetch_object($result);

  // Now we just need to swap the weights of the two nodes...
  if (!$node1 || !$node2) {
    drupal_set_message('There was a problem moving the node within its category.');
    drupal_access_denied();
    return;
  }
  $sql = "UPDATE {term_node} SET weight_in_tid = %d WHERE nid = %d AND tid = %d";
  db_query($sql, $node1->weight_in_tid, $node2->nid, $tid);
  db_query($sql, $node2->weight_in_tid, $node1->nid, $tid);
  $term = taxonomy_get_term($tid);
  drupal_set_message(t("<em>%title</em> was moved {$direction} in %category...", array(
    '%title' => $node->title,
    '%category' => $term->name,
  )));

  // Now send user to the page they were on before...
  drupal_goto($_GET['destination']);
}