Tripal v1.0 (6.x-1.0)
tripal_db.admin.inc
Go to the documentation of this file.
00001 <?php
00007 function tripal_db_admin_page() {
00008   $add_url = url("admin/tripal/tripal_db/add_db");
00009   $output = "<a href=\"$add_url\">Add a new external database</a>";
00010   $output .= drupal_get_form('tripal_db_select_form');
00011   $output .= '<div id="db-edit-div">Please select a database above to view or edit</div>';
00012   return $output;
00013 }
00019 function tripal_db_select_form() {
00020   return $form;
00021 }
00026 function tripal_ajax_db_edit() {
00027   $status = TRUE;
00028 
00029   // prepare and render the form
00030   $form = tripal_core_ahah_prepare_form();   
00031   $data = drupal_render($form);  
00032 
00033   // bind javascript events to the new objects that will be returned 
00034   // so that AHAH enabled elements will work.
00035   $settings = tripal_core_ahah_bind_events();
00036 
00037   // return the updated JSON
00038   drupal_json(
00039     array(
00040       'status'   => $status, 
00041       'data'     => $data,
00042       'settings' => $settings,
00043     )  
00044   );
00045 }
00046 
00051 function tripal_db_form(&$form_state = NULL, $action = 'Update') {
00052   
00053   $dbid = $form_state['values']['dbid'];
00054   
00055   
00056   if (strcmp($action,'Update')==0) {
00057     // get a list of db from chado for user to choose
00058     $sql = "SELECT * FROM {db} WHERE NOT name = 'tripal' ORDER BY name ";
00059     $results = chado_query($sql);
00060   
00061     $dbs = array();
00062     $dbs[] = '';
00063     while ($db = db_fetch_object($results)) {
00064       $dbs[$db->db_id] = $db->name;
00065     }
00066   
00067     $form['dbid'] = array(
00068       '#title' => t('External Database Name'),
00069       '#type' => 'select',
00070       '#options' => $dbs,
00071       '#ahah' => array(
00072         'path' => 'admin/tripal/tripal_db/edit/js',
00073         'wrapper' => 'db-edit-div',
00074         'effect' => 'fade',
00075         'event' => 'change',
00076         'method' => 'replace',
00077       ),
00078       '#prefix' => '<div id="db-edit-div">',
00079       '#suffix' => '</div>',
00080       '#default_value' => $dbid,
00081       '#description' => t('Please select a database to edit'),
00082     );
00083   }   
00084   else {
00085     $default_db = $form_state['values']['name'];
00086     $default_desc = $form_state['values']['description'];
00087     $default_url = $form_state['values']['url'];
00088     $default_urlprefix = $form_state['values']['urlprefix']; 
00089   }
00090   
00091   // get this requested database
00092   if ($dbid) {
00093     $values = array('db_id' => $dbid);
00094     $result = tripal_core_chado_select('db', array('*'), $values);
00095     $db = $result[0];
00096     $prev_dbid = $form_state['values']['prev_dbid'];
00097     // if the database has changed then repopulate the fields with the databaes values
00098     if ($prev_dbid != $dbid) {
00099       $default_db        = $db->name;
00100       $default_desc      = $db->description;
00101       $default_url       = $db->url;
00102       $default_urlprefix = $db->urlprefix;
00103     }
00104     // if the database did not change then keep the values in the form values
00105     else {
00106       $default_db = $form_state['values']['name'];
00107       $default_desc = $form_state['values']['description'];
00108       $default_url = $form_state['values']['url'];
00109       $default_urlprefix = $form_state['values']['urlprefix'];      
00110     }
00111   }
00112   
00113   $form['form_action'] = array(
00114     '#type' => 'hidden',
00115     '#value' => $action, 
00116   );  
00117 
00118   // we need to distinguish between edits in a field that may have failed
00119   // and when the user selects a different database from the list.  
00120   $form['prev_dbid'] = array(
00121     '#type' => 'hidden',
00122     '#value' => $dbid, 
00123   );  
00124   
00125   // if we want to update a database but the user has not
00126   // yet selected a database then return so we don't show the other fields
00127   // the rest of the fields will be added with the AHAH callback. 
00128   if (strcmp($action,'Update')==0 and !$dbid) {
00129     return $form;
00130   }
00131 
00132   $form['name']= array(
00133     '#type'          => 'textfield',
00134     '#title'         => t("Database Name"),
00135     '#description'   => t('Please enter the name for this external database.'),
00136     '#required'      => TRUE,
00137     '#default_value' => $default_db,
00138     '#weight'        => 1
00139   );
00140 
00141   $form['description']= array(
00142     '#type'          => 'textarea',
00143     '#title'         => t('Description'),
00144     '#description'   => t('Please enter a description for this database'),
00145     '#default_value' => $default_desc,
00146     '#weight'        => 2
00147   );
00148   $form['url']= array(
00149     '#type'          => 'textfield',
00150     '#title'         => t('URL'),
00151     '#description'   => t('Please enter the web address for this database.'),
00152     '#default_value' => $default_url,
00153     '#weight'        => 3
00154   );
00155   $form['urlprefix']= array(
00156     '#type'          => 'textfield',
00157     '#title'         => t('URL prefix'),
00158     '#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.'),
00159     '#default_value' => $default_urlprefix,
00160     '#weight'        => 4
00161   );
00162 
00163 
00164   if (strcmp($action, 'Update')==0) {
00165     $form['update'] = array(
00166       '#type'         => 'submit',
00167       '#value'        => t('Update'),
00168       '#weight'       => 5,
00169       '#executes_submit_callback' => TRUE,
00170     );
00171     $form['delete'] = array(
00172       '#type'         => 'submit',
00173       '#value'        => t('Delete'),
00174       '#weight'       => 6,
00175       '#executes_submit_callback' => TRUE,
00176     );
00177   }
00178   else {
00179     $form['add'] = array(
00180       '#type'         => 'submit',
00181       '#value'        => t('Add'),
00182       '#weight'       => 5,
00183       '#executes_submit_callback' => TRUE,
00184     );
00185   }
00186 
00187   return $form;
00188 }
00193 function tripal_db_form_validate($form, &$form_state) {
00194   $name =  trim($form_state['values']['name']);
00195   $desc =  trim($form_state['values']['description']);
00196   $url  =  trim($form_state['values']['url']);
00197   $urlp =  trim($form_state['values']['urlprefix']);
00198   $dbid =  trim($form_state['values']['dbid']);
00199   $op   =  trim($form_state['values']['op']);
00200   $action =  $form_state['values']['form_action'];
00201  
00202   // make sure the database name is unique
00203   $values = array('name' => $name);
00204   $results = tripal_core_chado_select('db', array('db_id'), $values);   
00205   if (count($results) > 0 and $results[0]->db_id != $dbid) {
00206     form_set_error('name', 'The database name must be unique');
00207   }
00208 }
00213 function tripal_db_form_submit($form, &$form_state) {
00214 
00215   $name =  trim($form_state['values']['name']);
00216   $desc =  trim($form_state['values']['description']);
00217   $url  =  trim($form_state['values']['url']);
00218   $urlp =  trim($form_state['values']['urlprefix']);
00219   $dbid =  trim($form_state['values']['dbid']);
00220   $op   =  trim($form_state['values']['op']);
00221 
00222   $values = array(
00223     'name' => $name,
00224     'description' => $desc,
00225     'url' => $url,
00226     'urlprefix' => $urlp,
00227   );
00228   if ($dbid) {
00229     if (strcmp($op, 'Update')==0) {      
00230       $match = array('db_id' => $dbid);
00231       $success = tripal_core_chado_update('db', $match, $values);
00232       if ($success) {
00233         drupal_set_message(t("External database updated"));
00234       }
00235       else {
00236         drupal_set_message(t("Failed to update external database."));
00237       }
00238     }
00239     if (strcmp($op, 'Delete')==0) {
00240       $match = array('db_id' => $dbid);
00241       $success = tripal_core_chado_delete('db', $match);
00242       if ($success) {
00243         drupal_set_message(t("External database deleted"));
00244       }
00245       else {
00246         drupal_set_message(t("Failed to delete external database."));
00247       }
00248     }
00249   }
00250   else {
00251     $success = tripal_core_chado_insert('db', $values);
00252     if ($success) {
00253       drupal_set_message(t("External database added"));
00254     }
00255     else {
00256       drupal_set_message(t("Failed to add external database."));
00257     }
00258   }
00259 }
 All Classes Files Functions Variables