You are here

function nodeorder_move_in_category in Node Order 5

Same name and namespace in other branches
  1. 6 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 500

Code

function nodeorder_move_in_category(&$node, $tid, $up) {

  // Note that it would be nice to wrap this in a transaction...
  // We rely on the fact that every node has a unique weight_in_tid
  // (initially equal to negative one times its nid)...
  $weight = db_result(db_query("SELECT weight_in_tid FROM {term_node} WHERE nid = %d AND tid = %d", $node->nid, $tid));
  if ($up) {
    $sql = "SELECT nid, weight_in_tid FROM {term_node} WHERE tid = %d AND weight_in_tid <= %d ORDER BY weight_in_tid DESC LIMIT 2";
    $direction = 'up';
  }
  else {
    $sql = "SELECT nid, weight_in_tid FROM {term_node} WHERE tid = %d AND weight_in_tid >= %d ORDER BY weight_in_tid LIMIT 2";
    $direction = 'down';
  }
  $result = db_query($sql, $tid, $weight);

  // Now we just need to swap the weights of the two nodes...
  if (db_num_rows($result) != 2) {
    drupal_set_message('There was a problem moving the node within its category.');
    drupal_access_denied();
    return;
  }
  $node1 = db_fetch_object($result);
  $node2 = db_fetch_object($result);
  $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']);
}