function tripal_db_form

1.x tripal_db.admin.inc tripal_db_form(&$form_state = NULL, $action = 'Update')

Related topics

1 string reference to 'tripal_db_form'

File

tripal_db/includes/tripal_db.admin.inc, line 51

Code

function tripal_db_form(&$form_state = NULL, $action = 'Update') {

  $dbid = $form_state['values']['dbid'];


  if (strcmp($action, 'Update') == 0) {
    // get a list of db from chado for user to choose
    $sql = "SELECT * FROM {db} WHERE NOT name = 'tripal' ORDER BY name ";
    $results = chado_query($sql);

    $dbs = array();
    $dbs[] = '';
    while ($db = db_fetch_object($results)) {
      $dbs[$db->db_id] = $db->name;
    }

    $form['dbid'] = array(
      '#title' => t('External Database Name'),
      '#type' => 'select',
      '#options' => $dbs,
      '#ahah' => array(
        'path' => 'admin/tripal/tripal_db/edit/js',
        'wrapper' => 'db-edit-div',
        'effect' => 'fade',
        'event' => 'change',
        'method' => 'replace',
      ),
      '#prefix' => '<div id="db-edit-div">',
      '#suffix' => '</div>',
      '#default_value' => $dbid,
      '#description' => t('Please select a database to edit'),
    );
  }
  else {
    $default_db = $form_state['values']['name'];
    $default_desc = $form_state['values']['description'];
    $default_url = $form_state['values']['url'];
    $default_urlprefix = $form_state['values']['urlprefix'];
  }

  // get this requested database
  if ($dbid) {
    $values = array('db_id' => $dbid);
    $result = tripal_core_chado_select('db', array('*'), $values);
    $db = $result[0];
    $prev_dbid = $form_state['values']['prev_dbid'];
    // if the database has changed then repopulate the fields with the databaes values
    if ($prev_dbid != $dbid) {
      $default_db = $db->name;
      $default_desc = $db->description;
      $default_url = $db->url;
      $default_urlprefix = $db->urlprefix;
    }
    // if the database did not change then keep the values in the form values
    else {
      $default_db = $form_state['values']['name'];
      $default_desc = $form_state['values']['description'];
      $default_url = $form_state['values']['url'];
      $default_urlprefix = $form_state['values']['urlprefix'];
    }
  }

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

  // we need to distinguish between edits in a field that may have failed
  // and when the user selects a different database from the list.  
  $form['prev_dbid'] = array(
    '#type' => 'hidden',
    '#value' => $dbid,
  );

  // if we want to update a database but the user has not
  // yet selected a database then return so we don't show the other fields
  // the rest of the fields will be added with the AHAH callback. 
  if (strcmp($action, 'Update') == 0 and !$dbid) {
    return $form;
  }

  $form['name'] = array(
    '#type' => 'textfield',
    '#title' => t("Database Name"),
    '#description' => t('Please enter the name for this external database.'),
    '#required' => TRUE,
    '#default_value' => $default_db,
    '#weight' => 1,
    '#maxlength' => 255,
  );

  $form['description'] = array(
    '#type' => 'textarea',
    '#title' => t('Description'),
    '#description' => t('Please enter a description for this database'),
    '#default_value' => $default_desc,
    '#weight' => 2,
    '#maxlength' => 255,
  );
  $form['url'] = array(
    '#type' => 'textfield',
    '#title' => t('URL'),
    '#description' => t('Please enter the web address for this database.'),
    '#default_value' => $default_url,
    '#weight' => 3,
    '#maxlength' => 255,
  );
  $form['urlprefix'] = array(
    '#type' => 'textfield',
    '#title' => t('URL prefix'),
    '#description' => t('Tripal can provide links to external databases when accession numbers or unique identifiers are known.  Typically, a database will provide a unique web address for each accession and the accession usually is the last component of the page address.  Please enter the web address, minus the accession number for this database.  When an accession number is present, Tripal will combine this web address with the accession and provide a link to the external site.'),
    '#default_value' => $default_urlprefix,
    '#weight' => 4,
    '#maxlength' => 255,
  );


  if (strcmp($action, 'Update') == 0) {
    $form['update'] = array(
      '#type' => 'submit',
      '#value' => t('Update'),
      '#weight' => 5,
      '#executes_submit_callback' => TRUE,
    );
    $form['delete'] = array(
      '#type' => 'submit',
      '#value' => t('Delete'),
      '#weight' => 6,
      '#executes_submit_callback' => TRUE,
    );
  }
  else {
    $form['add'] = array(
      '#type' => 'submit',
      '#value' => t('Add'),
      '#weight' => 5,
      '#executes_submit_callback' => TRUE,
    );
  }

  return $form;
}