You are here

function taxonomy_manager_form_merge_validate in Taxonomy Manager 6.2

Same name and namespace in other branches
  1. 6 taxonomy_manager.admin.inc \taxonomy_manager_form_merge_validate()

Validation handler for validating terms

1 string reference to 'taxonomy_manager_form_merge_validate'
taxonomy_manager_merge_form in ./taxonomy_manager.admin.inc
form for merging terms

File

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

Code

function taxonomy_manager_form_merge_validate($form, &$form_state) {
  $selected_tids = array();
  $selected_tids = $form_state['values']['taxonomy']['manager']['tree']['selected_terms'];
  $main_terms = array();
  $regexp = '%(?:^|,\\ *)("(?>[^"]*)(?>""[^"]* )*"|(?: [^",]*))%x';
  preg_match_all($regexp, $form_state['values']['merge']['main_term'], $matches);
  $main_terms = $matches[1];
  $error_msg = "";
  $typed_terms = taxonomy_manager_autocomplete_tags_get_tids($form_state['values']['merge']['main_term'], $form_state['values']['vid'], FALSE);
  if (!is_array($main_terms) || count($main_terms) == 0 || empty($main_terms[0])) {
    form_set_error('merge][main_term', t('Please enter a name into "Resulting merged term"'));
    $form_state['rebuild'] = TRUE;
  }
  else {
    if (count($main_terms) > 1) {
      form_set_error('merge][main_term', t('Please only enter single names into "Resulting merged term"'));
      $form_state['rebuild'] = TRUE;
    }
  }
  if (count($selected_tids) < 1) {
    form_set_error('merge', t("Please selected terms you want to merge"));
    $form_state['rebuild'] = TRUE;
  }
  else {
    if (count($selected_tids) > 50) {
      form_set_error('merge', t("Please select less than 50 terms to merge. Merging of too many terms in one step can cause timeouts and inconsistent database states"));
      $form_state['rebuild'] = TRUE;
    }
    else {
      if (_taxonomy_manager_check_duplicates($form_state['values']['vid'], $form_state['values']['merge']['main_term'], $error_msg)) {
        form_set_error('merge', t("Warning: Your input matches with multiple terms, because of duplicated term names. Please enter a unique term name or the term id with 'term-id:[tid]'") . " (" . $error_msg . ").");
        $form_state['rebuild'] = TRUE;
      }
      else {
        if ($form_state['values']['merge']['options']['collect_parents']) {
          $main_terms = array();
          foreach ($typed_terms as $term_info) {
            $main_terms[$term_info['tid']] = $term_info['tid'];
          }
          if (count($main_terms) == 1) {
            if (!taxonomy_manager_check_circular_hierarchy($main_terms, $selected_tids)) {
              form_set_error('merge', t('Invalid selection. The resulting hierarchy would contain circles, which is not allowed. A term cannot be a parent of itself. Unselect "Collect all parents of selected terms an add it to the merged term" or specify a different resulting term.'));
              $form_state['rebuild'] = TRUE;
            }
          }
        }
        else {
          if (!taxonomy_manager_check_language($form_state['values']['vid'], $selected_tids, $typed_terms)) {
            form_set_error('merge', t('Terms must be of the same language'));
            $form_state['rebuild'] = TRUE;
          }
        }
      }
    }
  }
}