You are here

function nodeorder_reordered in Node Order 5

AJAX callback that gets executed after every drop. Updates the database with the new order of nodes in the given category.

We should probably provide a way of sending the changes over in batch. It would be easy to do by embedding the whole page in a form. When the user hits 'submit' they would just end up here and it would all work the same way...

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

File

./nodeorder.module, line 384

Code

function nodeorder_reordered() {
  $tid = $_POST['tid'];
  $new_order = split(',', $_POST['new-values']);
  $initial_order = split(',', $_POST['initial-values']);
  $initial_weights = split(',', $_POST['initial-weights']);
  $size = count($initial_order);
  $first = -1;
  $last = -1;

  // Go through the two arrays and find the first and
  // last different values...
  if (count($new_order) == $size) {
    for ($i = 0; $i < $size; $i++) {
      if ($initial_order[$i] != $new_order[$i]) {
        $first = $i;
        break;
      }
    }
    for ($i = $size - 1; $i > $first; $i--) {
      if ($initial_order[$i] != $new_order[$i]) {
        $last = $i;
        break;
      }
    }
    if ($last > $first) {

      // Go through all the nodes between the first
      // and last affected nodes and rearrange their
      // weight_in_tid values...
      //
      // We should have a transaction around this loop...
      for ($i = $first; $i <= $last; $i++) {
        $sql = "UPDATE {term_node} SET weight_in_tid = %d WHERE tid = %d AND nid = %d";
        db_query($sql, $initial_weights[$i], $tid, $new_order[$i]);
      }
    }
  }
}