You are here

function taxonomy_edge_reorder_submit in Taxonomy Edge 8

Same name and namespace in other branches
  1. 7.2 taxonomy_edge.module \taxonomy_edge_reorder_submit()
  2. 7 taxonomy_edge.module \taxonomy_edge_reorder_submit()

Copy/paste from core taxonomy module. This is the penalty for not having a proper abstraction layer! And for not invoking update hook on terms when changing their parents!

_state

Parameters

type $form:

Return value

type

1 string reference to 'taxonomy_edge_reorder_submit'
taxonomy_edge_form_taxonomy_overview_terms_alter in ./taxonomy_edge.module
Hook into the drag'n'drop interface of terms in order to update tree when terms are reordered.

File

./taxonomy_edge.module, line 321
Selecting all children of a given taxonomy term can be a pain. This module makes it easier to do this, by maintaining a complete list of edges for each term using the adjecency matrix graph theory.

Code

function taxonomy_edge_reorder_submit($form, &$form_state) {
  $vocabulary = $form['#vocabulary'];
  $hierarchy = 0;

  // Update the current hierarchy type as we go.
  $changed_terms = array();
  $tree = taxonomy_get_tree($vocabulary->vid);
  if (empty($tree)) {
    return;
  }

  // Build a list of all terms that need to be updated on previous pages.
  $weight = 0;
  $term = (array) $tree[0];
  while ($term['tid'] != $form['#first_tid']) {
    if ($term['parents'][0] == 0 && $term['weight'] != $weight) {
      $term['parent'] = $term['parents'][0];
      $term['weight'] = $weight;
      $changed_terms[$term['tid']] = $term;
    }
    $weight++;
    $hierarchy = $term['parents'][0] != 0 ? 1 : $hierarchy;
    $term = (array) $tree[$weight];
  }

  // Renumber the current page weights and assign any new parents.
  $level_weights = array();
  foreach ($form_state['values'] as $tid => $values) {
    if (isset($form[$tid]['#term'])) {
      $term = $form[$tid]['#term'];

      // Give terms at the root level a weight in sequence with terms on previous pages.
      if ($values['parent'] == 0 && $term['weight'] != $weight) {
        $term['weight'] = $weight;
        $changed_terms[$term['tid']] = $term;
      }
      elseif ($values['parent'] > 0) {
        $level_weights[$values['parent']] = isset($level_weights[$values['parent']]) ? $level_weights[$values['parent']] + 1 : 0;
        if ($level_weights[$values['parent']] != $term['weight']) {
          $term['weight'] = $level_weights[$values['parent']];
          $changed_terms[$term['tid']] = $term;
        }
      }

      // Update any changed parents.
      if ($values['parent'] != $term['parent']) {
        $term['parent'] = $values['parent'];
        $changed_terms[$term['tid']] = $term;
      }
      $hierarchy = $term['parent'] != 0 ? 1 : $hierarchy;
      $weight++;
    }
  }

  // Build a list of all terms that need to be updated on following pages.
  for ($weight; $weight < count($tree); $weight++) {
    $term = (array) $tree[$weight];
    if ($term['parents'][0] == 0 && $term['weight'] != $weight) {
      $term['parent'] = $term['parents'][0];
      $term['weight'] = $weight;
      $changed_terms[$term['tid']] = $term;
    }
    $hierarchy = $term['parents'][0] != 0 ? 1 : $hierarchy;
  }

  // Save all updated terms.
  foreach ($changed_terms as $changed) {
    $term = (object) $changed;
    $term->parent = array(
      $term->parent,
    );
    _taxonomy_edge_taxonomy_term_update($term);
  }
}