function tripal_bulk_loader_modify_template_base_form

2.x tripal_bulk_loader.admin.templates.inc tripal_bulk_loader_modify_template_base_form($form, &$form_state = NULL, $mode)
3.x tripal_bulk_loader.admin.templates.inc tripal_bulk_loader_modify_template_base_form($form, &$form_state = NULL, $mode)
1.x tripal_bulk_loader.admin.templates.inc tripal_bulk_loader_modify_template_base_form($form_state = NULL, $mode)

The main form reached at admin/tripal/tripal_bulk_loader/create and /edit

Related topics

1 string reference to 'tripal_bulk_loader_modify_template_base_form'
tripal_bulk_loader_menu in tripal_bulk_loader/tripal_bulk_loader.module
Implements hook_menu().

File

tripal_bulk_loader/includes/tripal_bulk_loader.admin.templates.inc, line 22
All functions in this file pertain to administrative management of bulk loader templates

Code

function tripal_bulk_loader_modify_template_base_form($form, &$form_state = NULL, $mode) {

  // set the breadcrumb
  $breadcrumb = array();
  $breadcrumb[] = l('Home', '<front>');
  $breadcrumb[] = l('Administration', 'admin');
  $breadcrumb[] = l('Tripal', 'admin/tripal');
  $breadcrumb[] = l('Chado Data Loaders', 'admin/tripal/loaders');
  $breadcrumb[] = l('Bulk Loader', 'admin/tripal/loaders/bulk');
  $breadcrumb[] = l('Templates', 'admin/tripal/loaders/bulk/templates');
  drupal_set_breadcrumb($breadcrumb);

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

  // get template id from path and rebuild form
  if (isset($form_state['build_info']['args'][1])) {
    $mode = 'edit';
    if (preg_match('/^\d+$/', $form_state['build_info']['args'][1])) {
      $form_state['storage']['template_id'] = $form_state['build_info']['args'][1];

      $template_id = $form_state['storage']['template_id'];

      // add the template and template name to the $form_state['storage'] array
      $sql = "SELECT * FROM {tripal_bulk_loader_template} WHERE template_id=:template";
      $result = db_query($sql, array(':template' => $template_id))->fetchObject();
      $form_state['storage']['template'] = unserialize($result->template_array);
      if (!$form_state['storage']['template']) {
        $form_state['storage']['template'] = array();
      }
      $form_state['storage']['template_name'] = $result->name;

      // build the recrod2priority entry for the $form_state['storage'] array. This simply
      // maps the records to their respective orders (priorities).
      $form_state['storage']['record2priority'] = array();
      foreach ($form_state['storage']['template'] as $priority => $record_array) {
        if (!is_array($record_array)) {
          continue;
        }
        $form_state['storage']['record2priority'][$record_array['record_id']] = $priority;
      }
    }
    else {
      $form_state['storage']['template_id'] = FALSE;
      $form['records']['no_records']['#value'] = TRUE;
      $form['fields']['total_fields']['#value'] = 0;
      tripal_set_message('Unable to determine the template_id from the path.', TRIPAL_ERROR);
    }
  }
  else {
    $mode = 'create';
    $form_state['storage']['template_id'] = FALSE;
    $form['records']['no_records']['#value'] = TRUE;
    $form['fields']['total_fields']['#value'] = 0;
  }

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

  if ($form_state['storage']['template_id']) {
    $form['template_name'] = array(
      '#type' => 'item',
      '#title' => 'Template',
      '#markup' => $form_state['storage']['template_name'],
      '#weight' => 1,
    );
  }
  else {
    if (preg_match('/create/', $mode)) {
      $form['create_desc'] = array(
        '#type' => 'item',
        '#prefix' => '<div class="tripal-bulk-loader" "instructions" style="font-weight: bold;">',
        '#suffix' => '</div>',
        '#markup' => 'The first step to creating a Tripal Bulk Loading Template is to
          choose a short, unique name to identify it. After this is done you will be able
          to add record and field description through the edit template form (you should
          be redirected after you click the "Create Template" button below).'
      );

      $form['new_template_name'] = array(
        '#type' => 'textfield',
        '#title' => 'Template Name',
        '#required' => TRUE,
        '#description' => 'The template name should be unique, short and descriptive. It '
          . 'will be shown to your users in the Bulk Loading Job creation form so it '
          . 'should be friendly and easy to determine which template to choose.',
        '#weight' => 1,
      );
    }
    elseif (preg_match('/edit/', $mode)) {
      $sql = "SELECT * FROM {tripal_bulk_loader_template}";
      $resource = db_query($sql);
      $templates = array();
      $templates[''] = 'Select a Template';
      while ($r = $resource->fetchObject()) {
        $templates[$r->template_id] = $r->name;
      }

      $form['template_id'] = array(
        '#title' => t('Template'),
        '#description' => t('Please select the template you would like to edit.'),
        '#type' => 'select',
        '#options' => $templates,
        '#default_value' => $form_state['storage']['template_id'],
        '#weight' => 0,
        '#required' => TRUE,
        '#weight' => 1,
      );
    }
  }

  $mode_title = (preg_match('/create/', $mode)) ? 'Create Template' : 'Edit Template';
  $value = ($form_state['storage']['template_id']) ? 'Save Template' : $mode_title;
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => $value,
    '#weight' => 10,
  );

  if (preg_match('/edit/', $mode) && !$form_state['storage']['template_id']) {
    return $form;
  }
  elseif (preg_match('/create/', $mode)) {
    return $form;
  }

  $form['records'] = array(
    '#type' => (isset($form_state['storage']['template_id'])) ? 'fieldset' : 'hidden',
    '#title' => t('Current Records'),
    '#collapsible' => TRUE,
    '#weight' => 2,
    '#description' => t('A "Record" is an entry in a database table. Each of the records '
      . 'added to the bulk loader will be added per constant set and per line of you '
      . 'input file when a Tripal Bulk Loading Job is created using this template. '
      . 'Furthermore, if you add two records for the same table (i.e. feature table) then '
      . 'two distinct entries will be created per line (i.e. if recordA specifies a parent '
      . 'feature and recordB specifies the child using different columns in the input file '
      . 'then you will have one entry in the feature table specifying the parent and '
      . 'another one for the child.')
  );

  $form['records']['description'] = array(
    '#type' => 'item',
    '#markup' => 'Records will be inserted into the chado database in the order listed below. To '
      . 'change this order: <ul><li>Drag the rows into the correct order <br/>(If you don\'t have javascript enabled then enter '
      . 'the numbers 1 and up in the Order textboxes to indicate the correct order).</li></ul>',
  );

  $form['records']['records-data'] = array(
    '#prefix' => '<div id="tripal-bulk-loader-template-records">',
    '#suffix' => '</div>',
    '#tree' => TRUE,
    '#theme' => 'tripal_bulk_loader_modify_template_base_form_records'
  );

  $form['records']['no_records'] = array(
    '#type' => 'hidden',
    '#value' => TRUE,
  );

  $form['records']['submit-new_record'] = array(
    '#type' => 'submit',
    '#value' => 'New Record/Field',
  );

  $form['records']['submit-reorder'] = array(
    '#type' => 'submit',
    '#value' => 'Save Order',
  );

  $form['fields'] = array(
    '#type' => (isset($form_state['storage']['template_id'])) ? 'fieldset' : 'hidden',
    '#title' => t('Current Fields'),
    '#collapsible' => TRUE,
    '#weight' => 3,
    '#description' => t('A "Field" is a single column in a database entry. As such a field '
      . 'is always associated with a database entry (record).')
  );

  $form['fields']['fields-data'] = array(
    '#prefix' => '<div id="tripal-bulk-loader-template-fields">',
    '#suffix' => '</div>',
    '#tree' => TRUE,
    '#theme' => 'tripal_bulk_loader_modify_template_base_form_fields'
  );

  if (array_key_exists('template', $form_state['storage'])) {

    // List Current Fields -------------------------------------------------------------
    $num_fields = 0;

    // pre-create the URLs for dealing with records & fields. We will substitute values as needed
    $record_href_template = 'admin/tripal/loaders/bulk/template/%template/%action/%priority';
    $field_href_template = 'admin/tripal/loaders/bulk/template/%template/%action/%priority/%field';

    foreach ($form_state['storage']['template'] as $priority => $table_array) {
      if (!is_array($table_array)) {
        continue;
      }

      $form['records']['no_records']['#value'] = FALSE;

      $mode_value = '';
      $table_array['optional'] = (!isset($table_array['optional'])) ? FALSE : $table_array['optional'];
      if ($table_array['optional']) {
        $mode_value .= 'optional ';
      }

      // for backwards compatibility we want to convert insert_unique to be 'insert'
      // and optional to 'insert'
      $table_array['mode'] = (!isset($table_array['mode'])) ? 'insert' : $table_array['mode'];
      if (strcmp($table_array['mode'], 'insert_unique') == 0) {
        $mode_value .= 'insert or select if duplicate';
      }
      elseif (strcmp($table_array['mode'], 'optional') == 0) {
        $mode_value .= 'optional insert';
      }
      elseif (strcmp($table_array['mode'], 'insert_once') == 0) {
        $mode_value .= 'insert once';
      }
      elseif (strcmp($table_array['mode'], 'select_once') == 0) {
        $mode_value .= 'select once';
      }
      elseif ($table_array['mode']) {
        $mode_value .= $table_array['mode'];
      }
      else {
        $mode_value .= 'insert';
      }

      // add in the select if duplicate
      if (isset($table_array['select_if_duplicate'])) {
        if ($table_array['select_if_duplicate']) {
          $mode_value .= ' or select if duplicate';
        }
      }
      if (isset($table_array['select_optional'])) {
        if ($table_array['select_optional']) {
          $mode_value .= ' (no fail)';
        }
      }
      if (isset($table_array['update_if_duplicate'])) {
        if ($table_array['update_if_duplicate']) {
          $mode_value .= ' or update if duplicate';
        }
      }


      // add in the disabled
      if (isset($table_array['disable'])) {
        if ($table_array['disable']) {
          $mode_value .= '. <font color="Red">DISABLED</font>';
        }
      }

      // if either the template_id or priority are 0 then we need to make the link
      // point to the letter o instead (drupal menu can't have 0)
      $path_template_id = ($template_id > 0) ? $template_id : 'O';
      $path_priority = ($priority > 0) ? $priority : 'O';
      $form['records']['records-data'][$priority] = array(
        'title' => array(
          '#type' => 'item',
          '#markup' => filter_xss($priority . ". " . $table_array['record_id']),
          '#prefix' => "<a name=\"record_$priority\"></a>",
        ),
        'view-fields-link' => array(
          '#type' => 'markup',
          '#markup' => "<a href=\"#fields_$priority\"> View Fields </a>",
        ),
        'chado_table' => array(
          '#type' => 'item',
          '#markup' => filter_xss($table_array['table']),
        ),
        'mode' => array(
          '#type' => 'item',
          '#markup' => $mode_value,
        ),
        'new_priority' => array(
          '#type' => 'select',
          '#options' => range(1, sizeof($form_state['storage']['template'])),
          '#default_value' => $priority,
        ),
        'old_priority' => array(
          '#type' => 'hidden',
          '#value' => $priority,
        ),
        'id' => array(
          '#type' => 'hidden',
          '#value' => $priority,
        ),
        'submit-edit_record' => array(
          '#type' => 'link',
          '#href' => str_replace(array('%template', '%priority', '%action'), array($path_template_id, $path_priority, 'edit_record'), $record_href_template),
          '#title' => 'Edit',
        ),
        'submit-delete_record' => array(
          '#type' => 'link',
          '#href' => str_replace(array('%template', '%priority', '%action'), array($path_template_id, $path_priority, 'delete_record'), $record_href_template),
          '#options' => array(
            'query' => array(
              'record_name' => $table_array['record_id'],
              'chado_table' => $table_array['table'],
              'record_mode' => $mode_value
            )
          ),
          '#title' => 'Delete',
        ),
        'submit-duplicate_record' => array(
          '#type' => 'link',
          '#href' => str_replace(array('%template', '%priority', '%action'), array($path_template_id, $path_priority, 'duplicate_record'), $record_href_template),
          '#options' => array(
            'query' => array(
              'record_name' => $table_array['record_id'],
              'chado_table' => $table_array['table'],
              'record_mode' => $mode_value
            )
          ),
          '#title' => 'Duplicate'
        ),
        'submit-add_field' => array(
          '#type' => 'link',
          '#href' => str_replace(array('%template', '%priority', '%action'), array($path_template_id, $path_priority, 'add_field'), $record_href_template),
          '#title' => 'Add Field',
        ),
      );

      foreach ($table_array['fields'] as $field_index => $field) {
        $i = $num_fields;
        $fk_value = '';
        if (isset($field['foreign key'])) {
          if (isset($field['foreign field'])) {
            $fk_value = $field['foreign key'] . " (" . $field['foreign field'] . ")";
          }
          else {
            // for backwards compatibility we need to get the FK relationship to find
            // out what field we're joining on.  For templates created using a
            // previous version this information isn't stored in the template
            // so we need to get it.
            $fk_priority = $form_state['storage']['record2priority'][$field['foreign key']];
            $fk_table = $form_state['storage']['template'][$fk_priority]['table'];
            $tbl_description = chado_get_schema($table_array['table']);
            foreach ($tbl_description['foreign keys'] as $key_table => $key_array) {
              foreach ($key_array['columns'] as $left_field => $right_field) {
                if ($key_table == $fk_table and $left_field == $field['field']) {
                  $fk_value = $field['foreign key'] . " ($right_field)";
                }
              }
            }
          }
        }

        // if either the template_id or priority are 0 then we need to make the link
        // point to the letter o instead (drupal menu can't have 0)
        $path_template_id = ($template_id > 0) ? $template_id : 'O';
        $path_priority = ($priority > 0) ? $priority : 'O';
        $path_field_index = ($field_index > 0) ? $field_index : 'O';
        $form['fields']['fields-data'][$i] = array(
          'record_id' => array(
            '#type' => 'item',
            '#markup' => $table_array['record_id'] . '<br /><em>(' . ucwords($mode_value) . ')</em>',
            '#prefix' => "<a name=\"fields_$priority\"></a>",
          ),
          'view-record-link' => array(
            '#type' => 'markup',
            '#markup' => "<a href=\"#record_$priority\"> View Record</a>",
          ),
          'priority_hidden' => array(
            '#type' => 'hidden',
            '#value' => $priority,
          ),
          'field_name' => array(
            '#type' => 'item',
            '#markup' => $field['title'],
          ),
          'chado_table_name' => array(
            '#type' => 'item',
            '#markup' => $table_array['table'],
          ),
          'chado_table_hidden' => array(
            '#type' => 'hidden',
            '#value' => $table_array['table'],
          ),
          'chado_field_name' => array(
            '#type' => 'item',
            '#markup' => $field['field'],
          ),
          'column_num' => array(
            '#type' => 'item',
            '#markup' => (isset($field['spreadsheet column'])) ? $field['spreadsheet column'] : '',
          ),
          'constant_value' => array(
            '#type' => 'item',
            '#markup' => (isset($field['constant value'])) ? $field['constant value'] : '',
          ),
          'field_index' => array(
            '#type' => 'hidden',
            '#value' => $field_index
          ),
          'foreign_record_id' => array(
            '#type' => 'item',
            '#markup' => $fk_value,
          ),
          'edit_submit' => array(
            '#type' => 'link',
            '#href' => str_replace(array('%template', '%priority', '%field', '%action'), array($path_template_id, $path_priority, $path_field_index, 'edit_field'), $field_href_template),
            '#title' => "Edit",
          ),
          'delete_submit' => array(
            '#type' => 'link',
            '#href' => str_replace(array('%template', '%priority', '%field', '%action'), array($path_template_id, $path_priority, $path_field_index, 'delete_field'), $field_href_template),
            '#options' => array(
              'query' => array(
                'record_name' => $table_array['record_id'],
                'field_name' => $field['title'],
                'chado_table' => $table_array['table'],
                'chado_field' => $field['field'],
                'data_column' => (isset($field['spreadsheet column'])) ? $field['spreadsheet column'] : '',
                'constant_value' => (isset($field['constant value'])) ? $field['constant value'] : '',
                'foreign_record' => $fk_value
              )
            ),
            '#title' => "Delete",
          ),
        );
        $num_fields++;
      }
    }
    $form['fields']['total_fields'] = array(
      '#type' => 'hidden',
      '#value' => $num_fields,
    );

  }

  if ($form['records']['no_records']['#value']) {
    $form['records']['description'] = array(
      '#type' => 'item',
      '#markup' => 'There are currently no records. Click on the "New Record/Field" button below to add a record and the first field associated with it.',
    );
    unset($form['records']['submit-reorder']);

    $form['fields']['description'] = array(
      '#type' => 'item',
      '#markup' => 'There are currently no fields. Each field must be associated with a record, so, first, create a new record above.',
    );

  }

  return $form;
}