You are here

function nodeorder_taxonomy_form_vocabulary_submit in Node Order 6

Same name and namespace in other branches
  1. 5 nodeorder.module \nodeorder_taxonomy_form_vocabulary_submit()
  2. 7 nodeorder.module \nodeorder_taxonomy_form_vocabulary_submit()
1 string reference to 'nodeorder_taxonomy_form_vocabulary_submit'
nodeorder_form_alter in ./nodeorder.module
Implementation of hook_form_alter().

File

./nodeorder.module, line 59
Nodeorder module.

Code

function nodeorder_taxonomy_form_vocabulary_submit($form, &$form_state) {
  $vid = $form_state['values']['vid'];
  if ($form_state['values']['orderable']) {
    if ($form_state['values']['module'] != 'nodeorder') {

      // Switching from non-orderable to orderable...
      cache_clear_all('nodeorder:', 'cache', TRUE);

      // Set weight_in_tid to nid for all rows in term_node where
      // the tid is in this vocabulary...
      $tree = taxonomy_get_tree($vid);
      $tids = array();
      foreach ($tree as $term) {
        $tids[] = $term->tid;
      }
      if (count($tids) > 0) {
        $order = 'n.sticky DESC, tn0.weight_in_tid';
        $sql_max = "SELECT MAX(weight_in_tid) FROM {term_node} WHERE tid = %d";
        $sql_update = "UPDATE {term_node} SET weight_in_tid = %d WHERE tid = %d AND nid = %d";
        foreach ($tids as $i => $tid) {

          //select *current* nodes for the current term
          $result = nodeorder_select_nodes(array(
            $tid,
          ), 'and', 0, FALSE, $order, 0);
          while ($node = db_fetch_object($result)) {
            db_lock_table('term_node');
            $max = db_result(db_query($sql_max, $tid));
            db_query($sql_update, $max + 1, $tid, $node->nid);
            db_unlock_tables();
          }
        }
      }
      db_query("UPDATE {vocabulary} SET module = '%s' WHERE vid = %d", 'nodeorder', $vid);
      drupal_set_message(t('You may now order nodes within this vocabulary.'));
    }
  }
  else {
    if ($form_state['values']['module'] == 'nodeorder') {

      // Switching from orderable to non-orderable...
      cache_clear_all('nodeorder:', 'cache', TRUE);
      db_query("UPDATE {vocabulary} SET module = '%s' WHERE vid = %d", 'taxonomy', $vid);

      // Set weight_in_tid to 0 for all rows in term_node where
      // the tid is in this vocabulary...
      $tree = taxonomy_get_tree($vid);
      $tids = array();
      foreach ($tree as $term) {
        $tids[] = $term->tid;
      }
      if (count($tids) > 0) {
        db_query("UPDATE {term_node} SET weight_in_tid = 0 WHERE tid IN (" . implode(',', $tids) . ")");
      }
      drupal_set_message(t('You may no longer order nodes within this vocabulary.'));
    }
  }
}