You are here

function nodeorder_node_update in Node Order 7

Same name and namespace in other branches
  1. 8 nodeorder.module \nodeorder_node_update()

Implements hook_node_update().

Handle the weights, which were reset on rebuild of the taxonomy.

File

./nodeorder.module, line 790
Nodeorder module.

Code

function nodeorder_node_update($node) {
  if (!nodeorder_can_be_ordered($node)) {
    return;
  }
  $tids = nodeorder_orderable_tids($node, TRUE);
  $old_tids = $node->nodeorder;
  foreach ($tids as $tid) {

    // Restore weight of unchanged terms, or leave as is if zero.
    if (isset($old_tids[$tid])) {
      $old_weight = $old_tids[$tid];
      unset($old_tids[$tid]);
      if (!$old_weight) {
        continue;
      }
      $old_weight = is_array($old_weight) ? $old_weight['weight'] : $old_weight;
      $query = db_update('taxonomy_index')
        ->fields(array(
        'weight' => $old_weight,
      ))
        ->condition('nid', $node->nid)
        ->condition('tid', $tid)
        ->execute();
    }
    else {
      nodeorder_add_node_to_list($node, $tid);
    }
  }

  // Handle lists in which the node is removed.
  // Note that the old tids are at this point only the ones that were not
  // updated, the others were dropped when restoring above.
  foreach ($old_tids as $tid => $weight) {
    nodeorder_handle_node_lists_decrease($tid);
  }
}