You are here

function batch_add_terms_form_submit in Batch add terms 7

Batch add terms.

File

./batch_add_terms.admin.inc, line 36

Code

function batch_add_terms_form_submit($form, &$form_state) {
  $vocabulary = $form_state['build_info']['args'][0];
  $terms_names = trim($form_state['values']['terms']);
  $terms_names_array = explode("\n", $terms_names);
  $parent_terms = array();
  $weight = 0;
  foreach ($terms_names_array as $term_name) {
    $term_depth = 0;
    if (preg_match('#^(-+)(.+)#', $term_name, $matches)) {
      $term_depth = drupal_strlen($matches[1]);
      $term_name = $matches[2];
    }
    $term_name = trim($term_name);
    if ($form_state['values']['check_duplicates'] && taxonomy_get_term_by_name($term_name, $vocabulary->machine_name)) {
      continue;
    }
    $term = (object) array(
      'vid' => $vocabulary->vid,
      'name' => $term_name,
      'parent' => $term_depth ? $parent_terms[$term_depth - 1] : 0,
      'weight' => $weight++,
    );
    taxonomy_term_save($term);
    $parent_terms[$term_depth] = $term->tid;
  }
  drupal_set_message(t('Added @count new terms', array(
    '@count' => count($terms_names_array),
  )));
  $form_state['redirect'] = 'admin/structure/taxonomy/' . $vocabulary->machine_name;
}