You are here

function nodeorder_nodeapi in Node Order 5

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

Implementation of hook_nodeapi().

File

./nodeorder.module, line 590

Code

function nodeorder_nodeapi($node, $op, $arg = 0) {
  if (nodeorder_can_be_ordered($node)) {
    switch ($op) {
      case 'load':

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

      // Set the initial weight_in_tid to the node's nid...  Since this is a
      // new node, it is fine to set all the nid/tid combinations' weight_in_tid
      // to the same initial value for tids that are in orderable vocabularies.
      //
      // 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...
        //
        // We start out by setting all the weight_in_tid values to the nid
        // because there may have been some additional tids for this node,
        // and their weight_in_tid values won't have been saved in $node->nodeorder...
        //
        // Note that we only want to set the weight_in_tid for tids that
        // are in orderable vocabularies...
        $tids = nodeorder_orderable_tids($node);
        if (count($tids) > 0) {
          $sql = "UPDATE {term_node} SET weight_in_tid = %d WHERE nid = %d AND tid IN (" . implode(',', $tids) . ")";
          db_query($sql, $node->nid, $node->nid);
        }

        // New nodes won't have any saved weight_in_tid values...
        if ($node->nodeorder) {

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