You are here

function taxonomy_manager_double_tree_edit_move in Taxonomy Manager 6.2

1 call to taxonomy_manager_double_tree_edit_move()
taxonomy_manager_double_tree_edit in ./taxonomy_manager.admin.inc
AJAX Callback for Double Tree operations

File

./taxonomy_manager.admin.inc, line 1829
Taxonomy Manager Admin

Code

function taxonomy_manager_double_tree_edit_move($params, &$msg, &$is_error_msg) {
  $selected_terms = $params['selected_terms'];
  $selected_parents = $params['selected_parents'];
  if (!is_array($selected_terms) || !count($selected_terms)) {
    $msg = t("No terms selected.");
    $is_error_msg = TRUE;
    return;
  }
  $selected_terms_names = array();
  foreach ($selected_terms as $tid) {
    $term = taxonomy_get_term($tid);
    $vid = $term->vid;
    $selected_terms_names[] = $term->name;
  }
  if (is_array($selected_parents) && count($selected_parents)) {
    $p_array = array();
    foreach ($selected_parents as $parent) {
      $p_array[$parent]['tid'] = $parent;
    }
    if (!taxonomy_manager_check_language($vid, $selected_terms, $p_array)) {
      $msg = t("Terms must be of the same language.");
      $is_error_msg = TRUE;
      return;
    }
    else {
      if (!taxonomy_manager_check_circular_hierarchy($selected_terms, $selected_parents)) {
        $msg = t('Invalid parent. The resulting hierarchy would contain circles, which is not allowed. A term cannot be a parent of itself.');
        $is_error_msg = TRUE;
        return;
      }
    }
  }
  foreach ($selected_terms as $tid) {

    //reset all parents, except the direct parent in the tree
    $term_parents = taxonomy_get_parents($tid);
    $term_parents_array = array();
    $direct_parent = is_numeric($params['selected_terms_parent'][$tid]) ? $params['selected_terms_parent'][$tid] : 0;
    foreach ($term_parents as $term_parent) {
      if ($direct_parent != $term_parent->tid) {
        $term_parents_array[$term_parent->tid] = $term_parent->tid;
      }
    }
    $selected_parent_names = array();
    if (count($selected_parents)) {
      foreach ($selected_parents as $parent) {
        $term = taxonomy_get_term($parent);
        $selected_parent_names[] = $term->name;
        $term_parents_array[$term->tid] = $term->tid;
      }
    }
    if (count($term_parents_array) == 0) {
      $term_parents_array[0] = 0;
    }
    taxonomy_manager_move($term_parents_array, array(
      $tid,
    ), array(
      'keep_old_parents' => FALSE,
    ));
    taxonomy_manager_update_voc($vid, $term_parents_array);
  }
  $term_names = implode(', ', $selected_terms_names);
  if (count($selected_parents) == 0) {
    $msg = t("Removed current parent form terms %terms.", array(
      '%terms' => $term_names,
    ));
  }
  else {
    $msg = t("Terms %terms moved to parents %parents.", array(
      '%terms' => $term_names,
      '%parents' => implode(', ', $selected_parent_names),
    ));
  }
  $is_error_msg = FALSE;
}