Tripal v1.0 (6.x-1.0)
tripal_stock-db_references.inc
Go to the documentation of this file.
00001 <?php
00020 function tripal_stock_add_ALL_dbreferences_page($node) {
00021   $output = '';
00022 
00023   $output .= tripal_stock_add_chado_properties_progress('db_references') . '<br />';
00024   $output .= '<b>All Database References should strictly pertain to THE CURRENT Individual</b><br />';
00025   $output .= '<br />';
00026   $output .= theme('tripal_stock_references', $node);
00027   $output .= '<br /><br />';
00028   $output .= drupal_get_form('tripal_stock_add_ONE_dbreference_form', $node);
00029   $output .= '<br />';
00030   $output .= drupal_get_form('tripal_stock_add_chado_properties_navigate', 'db_references', $node->nid);
00031   return $output;
00032 }
00033 
00034 
00048 function tripal_stock_add_ONE_dbreference_form($form_state, $node) {
00049 
00050   $stock_id = $node->stock->stock_id;
00051 
00052   $form['db_nid'] = array(
00053     '#type' => 'hidden',
00054     '#value' => $node->nid
00055   );
00056 
00057   $form['add_dbreference'] = array(
00058     '#type' => 'fieldset',
00059     '#title' => t('Add Database References') . '<span class="form-optional" title="This field is optional">(optional)</span>',
00060   );
00061 
00062   $form['add_properties']['db_stock_id'] = array(
00063     '#type' => 'value',
00064     '#value' => $stock_id,
00065     '#required' => TRUE
00066 
00067   );
00068 
00069   $form['add_dbreference']['accession'] = array(
00070     '#type' => 'textfield',
00071     '#title' => t('Accession Number'),
00072   );
00073 
00074   $form['add_dbreference']['db_description'] = array(
00075     '#type' => 'textarea',
00076     '#title' => t('Description of Database Reference') . '<span class="form-optional" title="This field is optional">+</span>',
00077     '#description' => t('Optionally enter a description about the database accession.')
00078   );
00079 
00080   $db_options = tripal_db_get_db_options();
00081   $db_options[0] = 'Select a Database';
00082   ksort($db_options);
00083   $form['add_dbreference']['database'] = array(
00084     '#type' => 'select',
00085     '#title' => t('Database'),
00086     '#options' => $db_options,
00087   );
00088 
00089   $form['add_dbreference']['submit'] = array(
00090     '#type' => 'submit',
00091     '#value' => t('Add Database Reference')
00092   );
00093 
00094   return $form;
00095 }
00096 
00107 function tripal_stock_add_ONE_dbreference_form_validate($form, &$form_state) {
00108 
00109   // Only ensure db reference valid if adding
00110   if ($form_state['clicked_button']['#value'] == t('Add Database Reference') ) {
00111     //Do work of required validators
00112     if ($form_state['values']['accession'] == '') {
00113       form_set_error('accession', 'Accession field is Required.');
00114     }
00115 
00116     // Check database is valid db_id in chado
00117     if ( $form_state['values']['database'] > 0) {
00118       $tmp_obj = db_fetch_object(chado_query("SELECT count(*) as count FROM {db} WHERE db_id=%d", $form_state['values']['database']));
00119 
00120       if ($tmp_obj->count != 1) {
00121         form_set_error('database', 'The database you selected is not valid. Please choose another one.');
00122       }
00123     }
00124     else {
00125       form_set_error('database', 'Please select a database');
00126     }
00127 
00128     // Check Accession is unique for database
00129     $tmp_obj = db_fetch_object(chado_query("SELECT count(*) as count FROM {dbxref} WHERE accession='%s'", $form_state['values']['accession']));
00130 
00131     if ($tmp_obj->count > 0) {
00132       form_set_error('accession', 'This accession has already been assigned to another stock.');
00133     }
00134 
00135   } //end of if adding
00136 
00137 }
00138 
00149 function tripal_stock_add_ONE_dbreference_form_submit($form, &$form_state) {
00150 
00151   // FIX: Sometimes on programatic submission of form (drupal_execute) values in the form state get lost
00152   //     however, the post values always seem to be correct
00153   if (empty($form_state['values']['db_stock_id'])) {
00154   $form_state['values']['db_stock_id'] = $form_state['clicked_button']['#post']['db_stock_id']; }
00155 
00156   // Only Create if valid
00157   if ($form_state['values']['database'] > 0) {
00158 
00159 
00160     // create dbxref
00161     chado_query(
00162       "INSERT INTO {dbxref} (db_id, accession, description) VALUES (%d, '%s', '%s')",
00163       $form_state['values']['database'],
00164       $form_state['values']['accession'],
00165       $form_state['values']['db_description']
00166     );
00167 
00168     //create stock_dbxref
00169     $dbxref = tripal_db_get_dbxref_by_accession($form_state['values']['accession'], $form_state['values']['database']);
00170     if (!empty($dbxref->dbxref_id)) {
00171       chado_query(
00172         "INSERT INTO {stock_dbxref} (stock_id, dbxref_id) VALUES (%d, %d)",
00173         $form_state['values']['db_stock_id'],
00174         $dbxref->dbxref_id
00175       );
00176 
00177       drupal_set_message(t('Successfully Added Database Reference'));
00178     }
00179     else {
00180       drupal_set_message(t('Database reference NOT successfully created...'), 'error');
00181     } //end of if dbxref was created successfully
00182   } //end of if valid db reference
00183 
00184 }
00185 
00199 function tripal_stock_edit_ALL_dbreferences_page($node) {
00200   $output = '';
00201 
00202   $output .= drupal_get_form('tripal_stock_edit_ALL_db_references_form', $node);
00203   $output .= '<br />';
00204   $output .= drupal_get_form('tripal_stock_add_ONE_dbreference_form', $node);
00205   $output .= '<br />';
00206   $output .= drupal_get_form('tripal_stock_back_to_stock_button', $node->nid);
00207 
00208   return $output;
00209 }
00210 
00226 function tripal_stock_edit_ALL_db_references_form($form_state, $node) {
00227   $form = array();
00228 
00229   // Add database references to the node
00230   $node = tripal_core_expand_chado_vars($node, 'table', 'stock_dbxref');
00231 
00232   $form['nid'] = array(
00233     '#type' => 'hidden',
00234     '#value' => $node->nid
00235   );
00236 
00237   $i=0;
00238   if (!$node->stock->stock_dbxref) {
00239     $node->stock->stock_dbxref = array();
00240   }
00241   elseif (!is_array($node->stock->stock_dbxref)) {
00242     $node->stock->stock_dbxref = array($node->stock->stock_dbxref);
00243   }
00244   if (sizeof($node->stock->stock_dbxref) != 0) {
00245   foreach ($node->stock->stock_dbxref as $ref) {
00246     $i++;
00247     $form["num-$i"] = array(
00248       '#type' => 'item',
00249       '#value' => $i . '.'
00250     );
00251 
00252     $form["accession-$i"] = array(
00253       '#type' => 'textfield',
00254       //'#title' => t('Accession'),
00255       '#size' => 30,
00256       '#required' => TRUE,
00257       '#default_value' => $ref->dbxref_id->accession
00258     );
00259 
00260     $db_options = tripal_db_get_db_options();
00261     $db_options[0] = 'Select a Database';
00262     ksort($db_options);
00263     $form["database-$i"] = array(
00264       '#type' => 'select',
00265       //'#title' => t('Database'),
00266       '#options' => $db_options,
00267       '#default_value' => $ref->dbxref_id->db_id->db_id
00268     );
00269 
00270 
00271     $form["id-$i"] = array(
00272       '#type' => 'hidden',
00273       '#value' => $ref->dbxref_id->dbxref_id
00274     );
00275 
00276     $form["submit-$i"] = array(
00277       '#type' => 'submit',
00278       '#value' => t("Delete #$i")
00279     );
00280 
00281   }} //end of foreach db ref
00282 
00283   $form['num_db_references'] = array(
00284     '#type' => 'hidden',
00285     '#value' => $i
00286   );
00287 
00288   $form["submit-edits"] = array(
00289     '#type' => 'submit',
00290     '#value' => t('Update DB References')
00291   );
00292 
00293   return $form;
00294 }
00295 
00306 function tripal_stock_edit_ALL_db_references_form_submit($form, &$form_state) {
00307 
00308   if ($form_state['clicked_button']['#value'] == t('Update DB References') ) {
00309 
00310        //Update all
00311     for ($i=1; $i<=$form_state['values']['num_db_references']; $i++) {
00312       tripal_stock_update_db_reference(
00313         $form_state['values']["id-$i"],
00314         $form_state['values']["database-$i"],
00315         $form_state['values']["accession-$i"]
00316       );
00317     }
00318       drupal_set_message(t("Updated all Database References"));
00319       drupal_goto('node/' . $form_state['values']['nid']);
00320 
00321   }
00322   elseif ( preg_match('/Delete #(\d+)/', $form_state['clicked_button']['#value'], $matches) ) {
00323 
00324     $i = $matches[1];
00325     tripal_stock_delete_db_reference($form_state['values']["id-$i"]);
00326     drupal_set_message(t("Deleted Database Reference"));
00327 
00328   }
00329   else {
00330     drupal_set_message(t("Unrecognized Button Pressed"), 'error');
00331 
00332   }
00333 }
00334 
00349 function tripal_stock_update_db_reference($dbxref_id, $database_id, $accession) {
00350 
00351   chado_query(
00352     "UPDATE {dbxref} SET db_id=%d, accession='%s' WHERE dbxref_id=%d",
00353     $database_id,
00354     $accession,
00355     $dbxref_id
00356   );
00357 
00358 }
00359 
00371 function tripal_stock_delete_db_reference($dbxref_id) {
00372 
00373   chado_query(
00374     "DELETE FROM {dbxref} WHERE dbxref_id=%d",
00375     $dbxref_id
00376   );
00377 
00378   chado_query(
00379     "DELETE FROM {stock_dbxref} WHERE dbxref_id=%d",
00380     $dbxref_id
00381   );
00382 
00383 }
00384 
00396 function theme_tripal_stock_edit_ALL_db_references_form($form) {
00397   $output = '';
00398 
00399   $output .= '<br /><fieldset>';
00400   $output .= '<legend>Edit Existing Database References<span class="form-optional" title="This field is optional">(optional)</span></legend>';
00401   $output .= '<p>Below is a list of already existing database references, one per line. When entering a database reference, the accession '
00402          .'is a unique identifier for this stock in the specified database.</p>';
00403   $output .= '<table>';
00404   $output .= '<tr><th>#</th><th>Database</th><th>Accession</th><th></th></tr>';
00405 
00406   for ($i=1; $i<=$form['num_db_references']['#value']; $i++) {
00407     $output .= '<tr><td>' . drupal_render($form["num-$i"]) . '</td><td>'
00408              . drupal_render($form["database-$i"]) . '</td><td>'
00409          . drupal_render($form["accession-$i"]) . '</td><td>'
00410          . drupal_render($form["submit-$i"]) . '</td></tr>';
00411   }
00412 
00413   $output .= '</table><br />';
00414   $output .= drupal_render($form);
00415   $output .= '</fieldset>';
00416 
00417   return $output;
00418 }
00419 
00420 
00434 function tripal_stock_list_dbreferences_for_node($db_references) {
00435 
00436   if (!empty($db_references) ) {
00437     $output = '<table>';
00438     $output .= '<tr><th>Database</th><th>Accession</th></tr>';
00439 
00440     foreach ($db_references as $db) {
00441         $output .= '<tr><td>' . $db->db_name . '</td><td>' . $db->accession . '</td></tr>';
00442     } // end of foreach db reference
00443 
00444     $output .= '</table>';
00445 
00446   }
00447   else {
00448     $output = 'No Database References Added to the Current Stock';
00449   }
00450 
00451   return $output;
00452 }
 All Classes Files Functions Variables