function tripal_bulk_loader_set_constants_form

2.x tripal_bulk_loader.constants.inc tripal_bulk_loader_set_constants_form($form, &$form_state, $node)
3.x tripal_bulk_loader.constants.inc tripal_bulk_loader_set_constants_form($form, &$form_state, $node)
1.x tripal_bulk_loader.constants.inc tripal_bulk_loader_set_constants_form($form_state, $node)

Set constants (exposed fields in template)

Parameters

$form_state: The current state of the form

$node: The node to set constants for

Return value

A form array to be rendered by drupal_get_form()

Related topics

1 string reference to 'tripal_bulk_loader_set_constants_form'

File

tripal_bulk_loader/includes/tripal_bulk_loader.constants.inc, line 126
Manages the constants form added to the tripal bulk loader node form

Code

function tripal_bulk_loader_set_constants_form($form, &$form_state, $node) {
  $form = array();

  $form['nid'] = array(
    '#type' => 'hidden',
    '#value' => $node->nid
  );

  $form['#attached']['css'] = array(
    drupal_get_path('module', 'tripal_bulk_loader') . '/theme/tripal_bulk_loader.css',
  );

  $form_state['node'] = $node;

  if (!tripal_bulk_loader_has_exposed_fields($node)) {
    return $form;
  }

  // Check to see if the template has changed since this node was last updated.
  // If so we need to remove the constant set since it's no longer valid.
  if ($node->template->changed >= $node->changed AND !empty($node->constants)) {

    // Save all constants for display to the user.
    $form['old_constants'] = array(
      '#type' => 'fieldset',
      '#title' => 'Previous Constant Set',
      '#description' => 'This constant set is no longer valid due to changes in
        the bulk loading template. As such you will need to re-enter it below.
        <strong>Please copy this information somewhere before leaving this page
        (including entering a constant set) because it will NOT BE SAVED.</strong>',
      '#prefix' => '<div id="expired-constants">',
      '#suffix' => '</div>'
    );

    $form['old_constants']['table'] = array(
      '#type' => 'markup',
      '#theme' => 'tripal_bulk_loader_constant_set',
      '#nid' => $node->nid,
      '#constants' => $node->constants,
      '#template' => $node->template,
      '#options' => array('display_operations' => FALSE),
    );

    drupal_set_message('Template has been changed since the constant set was added; therefore,
      your constants are no longer valid. Please re-enter them before loading.', 
    'warning'
    );

    // Remove all constants for this node.
    db_delete('tripal_bulk_loader_constants')
      ->condition('nid', $node->nid)
      ->execute();
    $node->constants = array();
  }

  $form['exposed_array'] = array(
    '#type' => 'hidden',
    '#value' => serialize($node->exposed_fields),
  );

  $form['exposed_fields'] = array(
    '#type' => 'fieldset',
    '#title' => t('Constant Values'),
    '#collapsible' => TRUE,
    '#collapsed' => ($node->template_id) ? FALSE : TRUE,
    '#prefix' => '<div id="set-constants">',
    '#suffix' => '</div>',
  );

  // Display table of already added constant sets with the ability to re-arrange and delete
  $first_constant = reset($node->constants);
  if (sizeof($node->constants) > 0) {
    $form['exposed_fields']['explanation-1'] = array(
      '#type' => 'item',
      '#markup' => t('You have already added constants to this bulk loading job. Each '
        . 'row in the following table represents a set of constants. Each set will be used '
        . 'to load your data file with the specified template resulting in the each record '
        . 'in the template to be loaded x number of times where there are x sets of '
        . 'constants (rows in the following table).')
    );

    $form['exposed_fields']['existing'] = array(
      '#type' => 'markup',
      '#theme' => 'tripal_bulk_loader_constant_set',
      '#nid' => $node->nid,
      '#constants' => $node->constants,
      '#template' => $node->template,
    );
  }

  $form['exposed_fields']['new'] = array(
    '#type' => 'fieldset',
    '#title' => t('New set of Constants'),
  );

  $form['exposed_fields']['new']['explanation-2'] = array(
    '#type' => 'item',
    '#markup' => t('The following fields are constants in the selected template that you need to set values for.')
  );

  // Add textifelds for exposed fields of the current template
  $exposed_fields = FALSE;
  $indexes = array();
  if (tripal_bulk_loader_has_exposed_fields($node)) {
    foreach ($node->exposed_fields as $exposed_id => $exposed_index) {

      $record_id = $exposed_index['record_id'];
      $field_id = $exposed_index['field_id'];
      $field = $node->template->template_array[$record_id]['fields'][$field_id];

      if ($field['exposed']) {
        $exposed_fields = TRUE;
        $indexes[$record_id][] = $field_id;

        $default_value = '';
        if (isset($node->constants[$record_id])) {
          if (isset($node->constants[$record_id][$field_id])) {
            $default_value = $node->constants[$exposed_id][$record_id][$field_id]['value'];
          }
        }
        elseif (isset($field['constant value'])) {
          $default_value = $field['constant value'];
        }
        elseif (isset($field['spreadsheet column'])) {
          $default_value = $field['spreadsheet column'];
        }

        switch ($field['type']) {
          case 'table field':
            $form['exposed_fields']['new'][$record_id . '-' . $field_id] = array(
              '#type' => 'textfield',
              '#title' => t('@title', array('@title' => $field['title'])),
              '#description' => t('%exposed_description', array('%exposed_description' => $field['exposed_description'])),
              '#default_value' => $default_value,
            );
            break;
          case 'constant':
            $form['exposed_fields']['new'][$record_id . '-' . $field_id] = array(
              '#type' => 'textfield',
              '#title' => t('@title', array('@title' => $field['title'])),
              '#description' => t('Enter the case-sensitive value of this constant for your data file'),
              '#default_value' => $default_value,
            );
            break;
        }

        $form['exposed_fields']['new'][$record_id . '-' . $field_id . '-table'] = array(
          '#type' => 'hidden',
          '#value' => $node->template->template_array[$record_id]['table'],
        );
        $form['exposed_fields']['new'][$record_id . '-' . $field_id . '-field'] = array(
          '#type' => 'hidden',
          '#value' => $field['field'],
        );
        $form['exposed_fields']['new'][$record_id . '-' . $field_id . '-type'] = array(
          '#type' => 'hidden',
          '#value' => $field['type'],
        );

      }
    }
  }
  $form['template'] = array(
    '#type' => 'hidden',
    '#value' => serialize($node->template->template_array)
  );

  $form['exposed_fields']['new']['indexes'] = array(
    '#type' => 'hidden',
    '#value' => serialize($indexes),
  );

  if (!$exposed_fields) {
    $form['exposed_fields']['new']['explanation'] = array(
      '#type' => 'item',
      '#value' => t('There are no exposed fields for this template.')
    );
  }

  $form['exposed_fields']['new']['submit-2'] = array(
    '#type' => 'submit',
    '#name' => 'add_constant',
    '#value' => t('Add Constant Set')
  );

  return $form;
}