You are here

function taxonomy_tools_copier_destination in Taxonomy Tools 8

Same name and namespace in other branches
  1. 7 taxonomy_tools_copier/taxonomy_tools_copier.admin.inc \taxonomy_tools_copier_destination()

Builds hierarchical destination selector.

1 call to taxonomy_tools_copier_destination()
taxonomy_tools_copier_copy_form in taxonomy_tools_copier/taxonomy_tools_copier.admin.inc
Builds taxonomy term branch copying form.

File

taxonomy_tools_copier/taxonomy_tools_copier.admin.inc, line 110
Administrative page callbacks for the Taxonomy Copier module.

Code

function taxonomy_tools_copier_destination(&$form_state) {
  $content['destination-select'] = array(
    '#type' => 'container',
    '#attributes' => array(
      'id' => 'destination-select',
      'class' => array(
        'destination-select',
      ),
      'name' => 'destination-select',
    ),
  );

  // Hierarchical destination selector.
  // Vocabulary selector.
  $vocabularies = taxonomy_get_vocabularies();
  $options = array(
    'none' => t('Select'),
  );
  foreach ($vocabularies as $vocabulary) {
    $options[$vocabulary->vid] = $vocabulary->name;
  }
  $content['destination-select']['destination-vocabulary'] = array(
    '#type' => 'select',
    '#options' => $options,
    '#title' => t('Vocabulary'),
    '#ajax' => array(
      'callback' => 'taxonomy_tools_copier_destination_ajax',
      'wrapper' => 'destination-select',
    ),
    '#default_value' => isset($form_state['input']['destination-vocabulary']) ? $form_state['input']['destination-vocabulary'] : 'none',
  );

  // Show term selection if vocabulary is selected.
  if (isset($form_state['input']['destination-vocabulary']) && is_numeric($form_state['input']['destination-vocabulary'])) {

    // Determine if vocabulary has been changed.
    $vid_changed = FALSE;
    $vid = $form_state['input']['destination-vocabulary'];
    if (isset($form_state['current_vid']) && $vid != $form_state['current_vid']) {
      $vid_changed = TRUE;
    }
    $form_state['current_vid'] = $vid;
    $tree = taxonomy_get_tree($vid, 0, 1);

    // Show term selectors only if vocabulary has any terms.
    if (!empty($tree)) {

      // Determine how many term selectors need to be generated.
      $count = 1;

      // Continue only when vocabulary is not changed.
      while (!$vid_changed && isset($form_state['input']['destination-level-' . $count])) {
        if (is_numeric($form_state['input']['destination-level-' . $count])) {
          $children = taxonomy_get_children($form_state['input']['destination-level-' . $count]);
          if (!empty($children)) {
            if (isset($form_state['input']['destination-level-' . ($count + 1)]) && !isset($children[$form_state['input']['destination-level-' . ($count + 1)]])) {
              $count++;
              break;
            }
            else {
              $count++;
            }
          }
          else {
            break;
          }
        }
        else {
          break;
        }
      }
    }
    else {
      $count = 0;
    }

    // Term selection.
    $level = 1;
    while ($level <= $count) {
      if ($level == 1) {
        $tid = 0;
      }
      else {
        $tid = $form_state['input']['destination-level-' . ($level - 1)];
      }
      $options = array(
        'none' => t('Select'),
      );
      $tree = taxonomy_get_tree($vid, $tid, 1);
      foreach ($tree as $term) {
        $options[$term->tid] = $term->name;
      }
      $content['destination-select']['destination-level-' . $level] = array(
        '#type' => 'select',
        '#title' => t('Level') . '&nbsp;' . $level,
        '#ajax' => array(
          'callback' => 'taxonomy_tools_copier_destination_ajax',
          'wrapper' => 'destination-select',
        ),
        '#options' => $options,
        '#default_value' => isset($form_state['input']['destination-level-' . $level]) ? $form_state['input']['destination-level-' . $level] : 'none',
      );
      $level++;
    }
  }
  return $content;
}