You are here

function nodeorder_taxonomy_form_vocabulary_submit in Node Order 7

Same name and namespace in other branches
  1. 5 nodeorder.module \nodeorder_taxonomy_form_vocabulary_submit()
  2. 6 nodeorder.module \nodeorder_taxonomy_form_vocabulary_submit()

Submit handler for nodeorder_form_taxonomy_form_vocabulary_alter().

1 string reference to 'nodeorder_taxonomy_form_vocabulary_submit'
nodeorder_form_taxonomy_form_vocabulary_alter in ./nodeorder.module
Implements hook_form_FORM_ID_alter() for taxonomy_form_vocabulary().

File

./nodeorder.module, line 163
Nodeorder module.

Code

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

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

      // Set weight 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';
        $tid = 0;
        $query_max = db_select('taxonomy_index', 'ti')
          ->condition('tid', $tid);
        $query_max
          ->addExpression('MAX(weight)', 'mweight');
        foreach ($tids as $i => $tid) {

          // Select *current* nodes for the current term.
          // @todo: Replace this hacked function call.
          $result = nodeorder_select_nodes(array(
            $tid,
          ), 'and', 0, FALSE, $order, 0);
          foreach ($result as $node) {
            $max = $query_max
              ->execute()
              ->fetchField();
            $max = (int) $max + 1;
            db_update('taxonomy_index')
              ->condition('nid', $node->nid)
              ->condition('tid', $tid)
              ->fields(array(
              'weight' => $max,
            ))
              ->execute();
          }
        }
      }
      db_update('taxonomy_vocabulary')
        ->fields(array(
        'module' => 'nodeorder',
      ))
        ->condition('vid', $vid)
        ->execute();
      drupal_set_message(t('You may now order nodes within this vocabulary.'));
    }
  }
  elseif ($form_state['vocabulary']->module === 'nodeorder') {

    // Switching from orderable to non-orderable.
    cache_clear_all('nodeorder:', 'cache', TRUE);
    db_update('taxonomy_vocabulary')
      ->fields(array(
      'module' => 'taxonomy',
    ))
      ->condition('vid', $vid)
      ->execute();

    // Set weight 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_update('taxonomy_index')
        ->fields(array(
        'weight' => 0,
      ))
        ->condition('tid', $tids, 'IN')
        ->execute();
    }
    drupal_set_message(t('You may no longer order nodes within this vocabulary.'));
  }
}