You are here

function nodeorder_nodeapi in Node Order 6

Same name and namespace in other branches
  1. 5 nodeorder.module \nodeorder_nodeapi()

Implementation of hook_nodeapi().

File

./nodeorder.module, line 680
Nodeorder module.

Code

function nodeorder_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
  switch ($op) {
    case 'presave':
      if (nodeorder_can_be_ordered($node)) {
        if (!isset($node->nodeorder)) {
          $node->nodeorder = array();

          // When a node gets loaded, store an element called 'nodeorder' that contains
          // an associative array of tid to weight_in_tid...
          $result = db_query('SELECT tid, weight_in_tid FROM {term_node} WHERE nid = %d', $node->nid);
          while ($term_node = db_fetch_object($result)) {
            $node->nodeorder[$term_node->tid] = $term_node->weight_in_tid;
          }
        }
      }
      break;
    case 'delete':

      // make sure the weight cache is invalidated
      if (nodeorder_can_be_ordered($node)) {
        $tids = nodeorder_orderable_tids($node);
        if (count($tids) > 0) {
          foreach ($tids as $i => $tid) {
            nodeorder_get_term_min_max($tid, TRUE);

            // reinitialize the cache
          }
        }
      }
      break;
    case 'insert':

    // Set the initial weight_in_tid to max+1... This makes sure that the weight
    // will be unique for each nid/tid combination
    //
    // NOTE - fall through to 'update' since we do mostly the same thing there.
    case 'update':

      // Set the weight_in_tid -- taxonomy probably stomped it because
      // we added the weight_in_tid column to term_node, and taxonomy
      // just wants to delete and re-insert rows when things change...
      // Note that we only want to set the weight_in_tid for tids that
      // are in orderable vocabularies...
      if (nodeorder_can_be_ordered($node)) {
        $tids = nodeorder_orderable_tids($node);
        if (count($tids) > 0) {
          $sql = "UPDATE {term_node} SET weight_in_tid = %d WHERE tid = %d AND nid = %d";
          foreach ($tids as $i => $tid) {
            db_lock_table('term_node');
            $weights = nodeorder_get_term_min_max($tid, FALSE);

            // get the cached weights
            db_query($sql, $weights["max"] + 1, $tid, $node->nid);
            nodeorder_get_term_min_max($tid, TRUE);

            // reinitialize the cache
            db_unlock_tables();
          }
        }

        // New nodes won't have any saved weight_in_tid values so this array will be empty...
        if ($node->nodeorder) {

          // Restore any saved weight_in_tid values...
          $sql = "UPDATE {term_node} SET weight_in_tid = %d WHERE nid = %d AND tid = %d";
          foreach ($node->nodeorder as $tid => $weight_in_tid) {

            // weight_in_tid cannot be 0
            if ($weight_in_tid != 0) {
              db_query($sql, $weight_in_tid, $node->nid, $tid);
            }
          }
        }
      }
      break;
  }
}