diff --git a/taxonomy_manager.admin.inc b/taxonomy_manager.admin.inc index d768cc9..05a62f0 100644 --- a/taxonomy_manager.admin.inc +++ b/taxonomy_manager.admin.inc @@ -1747,29 +1747,38 @@ function taxonomy_manager_term_data_form_submit_parents_add($form, &$form_state) $parent_names = $term_data['parents']['op']['add']; $typed_terms = taxonomy_manager_autocomplete_tags_get_tids($parent_names, $term->vid, TRUE); //insert terms + foreach ($typed_terms as $parent_info) { + $new_tid = $parent_info['tid']; + } if (!isset($term->parent)) { $term->parent = array(); } - foreach ($typed_terms as $parent_info) { - $term->parent[] = $parent_info['tid']; + // Get the current parents first. + $flag = FALSE; + $current_parents = taxonomy_get_parents($term->tid); + foreach ($current_parents as $key => $value) { + $term->parent[] = $key; + if ($key == $new_tid) { + $flag = TRUE; + } } - //remove root level entry - db_delete('taxonomy_term_hierarchy') - ->condition('tid', $term->tid) - ->condition('parent', 0) - ->execute(); - - taxonomy_term_save($term); //executes hooks and clears caches - taxonomy_manager_update_voc($term->vid, taxonomy_get_parents($term->tid)); // update hierarchy settings, if necessary - drupal_set_message(t('Updated hierarchy')); - $form_state['rebuild'] = TRUE; - form_set_value(array('#parents' => array('tree-update')), array('update' => TRUE), $form_state); + // Add on the new parent if it did not already exist. + if (!$flag) { + $term->parent[] = $new_tid; + taxonomy_term_save($term); //executes hooks and clears caches + taxonomy_manager_update_voc($term->vid, taxonomy_get_parents($term->tid)); // update hierarchy settings, if necessary + drupal_set_message(t('Updated hierarchy')); + $form_state['rebuild'] = TRUE; + form_set_value(array('#parents' => array('tree-update')), array('update' => TRUE), $form_state); + } + else { + form_set_error('parents][op][add', t("Parent already exists.")); + } } - function taxonomy_manager_term_data_form_validate_parents_remove($form, &$form_state) { $splits = explode('][', $form_state['triggering_element']['#name']); if (count($splits) == 3 && is_numeric($splits[1])) { @@ -1794,11 +1803,15 @@ function taxonomy_manager_term_data_form_submit_parents_remove($form, &$form_sta } // Ensure that a term has a least parent 0. - if (count($parents) == 1) { + if (count($parents) <= 1) { $term->parent[] = 0; } - - $term->parent = array_diff($term->parent, array($parent)); + else { + unset($parents[$parent]); + foreach($parents as $key => $value) { + $term->parent[] = $key; + } + } taxonomy_term_save($term); //executes hooks and clears caches drupal_set_message(t("Removed parent")); @@ -2575,7 +2588,7 @@ function taxonomy_manager_export_csv($tree, $delimiter = ";") { $array[] = '"' . $term->tid . '"'; $array[] = '"' . $term->name . '"'; $array[] = '"' . $term->description . '"'; - foreach ($term->parents as $parent) { + foreach ($term->parent as $parent) { $array[] = '"' . $parent . '"'; } $output .= implode($delimiter, $array) . "\n";