Tripal v1.0 (6.x-1.0)
tripal_feature-db_references.inc
Go to the documentation of this file.
00001 <?php
00002 
00013 function tripal_feature_add_ALL_dbreferences_page($node) {
00014   $output = '';
00015 
00016   $output .= tripal_feature_implement_add_chado_properties_progress('db_references') . '<br />';
00017   $output .= '<b>All Database References should strictly pertain to THE CURRENT Individual</b><br />';
00018   $output .= '<br /><b>Current Database References</b><br />';
00019   $output .= list_dbreferences_for_node($node->db_references);
00020   $output .= '<br /><br />';
00021   $output .= drupal_get_form('tripal_feature_add_ONE_dbreference_form', $node);
00022   $output .= '<br />';
00023   $output .= drupal_get_form('tripal_feature_implement_add_chado_properties_navigate', 'db_references', $node->nid);
00024   return $output;
00025 }
00026 
00033 function tripal_feature_add_ONE_dbreference_form($form_state, $node) {
00034 
00035   $form['nid'] = array(
00036     '#type' => 'hidden',
00037     '#value' => $node->nid
00038   );
00039 
00040   $form['feature_id'] = array(
00041     '#type' => 'hidden',
00042     '#value' => $node->feature->feature_id,
00043   );
00044 
00045   $form['add_dbreference'] = array(
00046     '#type' => 'fieldset',
00047     '#title' => t('Add Database References') . '<span class="form-optional" title="This field is optional">(optional)</span>',
00048   );
00049 
00050   $form['add_dbreference']['accession'] = array(
00051     '#type' => 'textfield',
00052     '#title' => t('Accession'),
00053     '#required' => TRUE,
00054   );
00055 
00056   $form['add_dbreference']['description'] = array(
00057     '#type' => 'textarea',
00058     '#title' => t('Description of Reference') . '<span class="form-optional" title="This field is optional">+</span>',
00059     '#description' => t('Optionally enter a description about the database accession.'),
00060   );
00061 
00062   $db_options = tripal_db_get_db_options();
00063   $db_options[0] = 'Select a Database';
00064   ksort($db_options);
00065   $form['add_dbreference']['db_id'] = array(
00066     '#type' => 'select',
00067     '#title' => t('Database'),
00068     '#options' => $db_options,
00069     '#required' => TRUE,
00070   );
00071 
00072   $form['add_dbreference']['submit'] = array(
00073     '#type' => 'submit',
00074     '#value' => t('Add Database Reference')
00075   );
00076 
00077   return $form;
00078 }
00079 
00085 function tripal_feature_add_ONE_dbreference_form_validate($form, &$form_state) {
00086 
00087   $db_id = $form_state['values']['db_id'];
00088   $accession = $form_state['values']['accession'];
00089   $description = $form_state['values']['description'];
00090   $feature_id = $form_state['values']['feature_id'];
00091   $nid = $form_state['values']['nid'];
00092 
00093   // Check database is valid db_id in chado
00094   $tmp_obj = db_fetch_object(chado_query("SELECT count(*) as count FROM {db} WHERE db_id=%d", $db_id));
00095   if ($tmp_obj->count != 1) {
00096     form_set_error('database', 'The database you selected is not valid. Please choose another one.');
00097   }
00098 
00099   // Check Accession is unique for database
00100   $sql = "SELECT count(*) as count FROM {dbxref} WHERE accession='%s' and db_id = %d";
00101   $tmp_obj = db_fetch_object(chado_query($sql, $accession, $db_id));
00102 
00103   if ($tmp_obj->count > 0) {
00104     form_set_error('accession', 'This accession has already been assigned to another feature in the selected database.');
00105   }
00106 
00107 }
00108 
00114 function tripal_feature_add_ONE_dbreference_form_submit($form, &$form_state) {
00115 
00116   $db_id = $form_state['values']['db_id'];
00117   $accession = $form_state['values']['accession'];
00118   $description = $form_state['values']['description'];
00119   $feature_id = $form_state['values']['feature_id'];
00120   $nid = $form_state['values']['nid'];
00121 
00122   // create dbxref
00123   $isql =  "INSERT INTO {dbxref} (db_id, accession, description) VALUES (%d, '%s', '%s')";
00124   chado_query($isql, $db_id, $accession, $description);
00125 
00126   //create feature_dbxref
00127   $dbxref = tripal_db_get_dbxref( array('db_id' => array('type' => 'INT', 'value' => $form_state['values']['db_id']),
00128                      'accession' => array('type' => 'STRING', 'exact' => TRUE, 'value' => $form_state['values']['accession']) ) );
00129 
00130   if (!empty($dbxref->dbxref_id)) {
00131       $isql = "INSERT INTO {feature_dbxref} (feature_id, dbxref_id) VALUES (%d, %d)";
00132       chado_query($isql, $feature_id, $dbxref->dbxref_id);
00133       drupal_set_message(t('Successfully Added Database Reference'));
00134       drupal_goto('node/' . $nid);
00135   }
00136   else {
00137     drupal_set_message(t('Database reference NOT successfully created...'), 'error');
00138   } //end of if dbxref was created successfully
00139 
00140 }
00141 
00142 
00148 function tripal_feature_edit_ALL_dbreferences_page($node) {
00149   $output = '';
00150 
00151   $output .= drupal_get_form('tripal_feature_edit_ALL_db_references_form', $node);
00152   $output .= '<br />';
00153   $output .= drupal_get_form('tripal_feature_add_ONE_dbreference_form', $node);
00154   $output .= '<br />';
00155   $output .= drupal_get_form('tripal_feature_implement_back_to_feature_button', $node->nid);
00156 
00157   return $output;
00158 }
00159 
00166 function tripal_feature_edit_ALL_db_references_form($form_state, $node) {
00167   $form = array();
00168 
00169   $form['nid'] = array(
00170     '#type' => 'hidden',
00171     '#value' => $node->nid
00172   );
00173 
00174   $i=0;
00175 
00176   $feature = $node->feature;
00177   $references = tripal_feature_load_references($feature->feature_id);
00178 
00179   // pre populate the database options
00180   $db_options = tripal_db_get_db_options();
00181   $db_options[0] = 'Select a Database';
00182   ksort($db_options);
00183 
00184   if (sizeof($references) != 0) {
00185     foreach ($references as $ref) {
00186       $i++;
00187       $form["num-$i"] = array(
00188         '#type' => 'fieldset',
00189         '#title' => t("Database Reference") . " $i"
00190       );
00191 
00192       $form["num-$i"]["accession-$i"] = array(
00193         '#type' => 'textfield',
00194         '#title' => t('Accession'),
00195         '#size' => 30,
00196         '#required' => TRUE,
00197         '#default_value' => $ref->accession
00198       );
00199 
00200       $form["num-$i"]["db_id-$i"] = array(
00201         '#type' => 'select',
00202         '#title' => t('Database'),
00203         '#options' => $db_options,
00204         '#required' => TRUE,
00205         '#default_value' => $ref->db_id
00206       );
00207 
00208 
00209       $form["num-$i"]["dbxref_id-$i"] = array(
00210         '#type' => 'hidden',
00211         '#value' => $ref->dbxref_id
00212       );
00213 
00214       $form["num-$i"]["delete-$i"] = array(
00215         '#type' => 'submit',
00216         '#value' => t("Delete"),
00217         '#name' => "delete-$i",
00218       );
00219 
00220       }
00221 
00222       $form['num_db_references'] = array(
00223         '#type' => 'hidden',
00224         '#value' => $i
00225       );
00226 
00227       $form["submit-edits"] = array(
00228         '#type' => 'submit',
00229         '#value' => t('Update All References')
00230       );
00231   } //end of foreach db ref
00232   return $form;
00233 }
00234 
00240 function tripal_feature_edit_ALL_db_references_form_submit($form, &$form_state) {
00241 
00242   $num_refs = $form_state['values']['num_db_references'];
00243   $action = $form_state['clicked_button']['#value'];
00244   $button = $form_state['clicked_button']['#name'];
00245   $nid = $form_state['values']['nid'];
00246 
00247   if (strcmp($action, 'Update All References')==0) {
00248     for ($i=1; $i<=$num_refs; $i++) {
00249     $dbxref_id = $form_state['values']["dbxref_id-$i"];
00250     $db_id = $form_state['values']["db_id-$i"];
00251     $accession = $form_state['values']["accession-$i"];
00252       tripal_feature_update_db_reference($dbxref_id, $db_id, $accession);
00253     }
00254     drupal_set_message(t("Updated all Database References"));
00255     drupal_goto('node/' . $nid);
00256   }
00257   elseif (strcmp($action, 'Delete')==0) {
00258     if (preg_match('/delete-(\d+)/', $button, $matches) ) {
00259       $i = $matches[1];
00260       $dbxref_id = $form_state['values']["dbxref_id-$i"];
00261       tripal_feature_delete_db_reference($dbxref_id);
00262       drupal_set_message(t("Deleted Database Reference"));
00263       drupal_goto('node/' . $nid);
00264     }
00265     else {
00266       drupal_set_message(t("Could not remove database reference:"), 'error');
00267     }
00268     }
00269   else {
00270     drupal_set_message(t("Unrecognized Button Pressed"), 'error');
00271   }
00272 
00273 }
00274 
00280 function tripal_feature_update_db_reference($dbxref_id, $db_id, $accession) {
00281 
00282   $sql =  "UPDATE {dbxref} SET db_id=%d, accession='%s' WHERE dbxref_id=%d";
00283   chado_query($sql, $db_id, $accession, $dbxref_id);
00284 
00285 }
00286 
00292 function tripal_feature_delete_db_reference($dbxref_id) {
00293 
00294   chado_query(
00295     "DELETE FROM {dbxref} WHERE dbxref_id=%d",
00296     $dbxref_id
00297   );
00298 
00299   chado_query(
00300     "DELETE FROM {feature_dbxref} WHERE dbxref_id=%d",
00301     $dbxref_id
00302   );
00303 }
00304 
00310 function theme_tripal_feature_edit_ALL_db_references_form($form) {
00311   $output = '';
00312 
00313   $output .= '<br /><fieldset>';
00314   $output .= '<legend>Edit Existing Database References<span class="form-optional" title="This field is optional">(optional)</span></legend>';
00315   $output .= '<p>Below is a list of already existing database references, one per line. When entering a database reference, the accession '
00316          .'is a unique identifier for this feature in the specified database.</p>';
00317   $output .= '<table>';
00318   $output .= '<tr><th>#</th><th>Database</th><th>Accession</th><th></th></tr>';
00319 
00320   for ($i=1; $i<=$form['num_db_references']['#value']; $i++) {
00321     $output .= '<tr><td>' . drupal_render($form["num-$i"]) . '</td><td>'
00322              . drupal_render($form["database-$i"]) . '</td><td>'
00323          . drupal_render($form["accession-$i"]) . '</td><td>'
00324          . drupal_render($form["submit-$i"]) . '</td></tr>';
00325   }
00326 
00327   $output .= '</table><br />';
00328   $output .= drupal_render($form);
00329   $output .= '</fieldset>';
00330 
00331   return $output;
00332 }
00333 
00339 function list_dbreferences_for_node($db_references) {
00340 
00341   if (!empty($db_references) ) {
00342     $output = '<table>';
00343     $output .= '<tr><th>Database</th><th>Accession</th></tr>';
00344 
00345     foreach ($db_references as $db) {
00346         $output .= '<tr><td>' . $db->db_name . '</td><td>' . $db->accession . '</td></tr>';
00347     } // end of foreach db reference
00348 
00349     $output .= '</table>';
00350 
00351   }
00352   else {
00353     $output = 'No Database References Added to the Current Feature';
00354   }
00355 
00356   return $output;
00357 }
 All Classes Files Functions Variables