|
Tripal 0.3b
|
00001 <?php 00002 00003 00004 /** 00005 * 00006 * 00007 * @ingroup tripal_feature 00008 */ 00009 function tripal_feature_add_ALL_relationships_page($node) { 00010 $output = ''; 00011 00012 $output .= tripal_feature_implement_add_chado_properties_progress('relationships').'<br>'; 00013 $output .= '<b>All Relationships should include the CURRENT Individual ('.$node->uniquename.')</b><br>'; 00014 $output .= '<br><b>Current Relationships</b><br>'; 00015 $output .= list_relationships_for_node($node->uniquename, $node->subject_relationships, $node->object_relationships); 00016 $output .= '<br><br>'; 00017 $output .= drupal_get_form('tripal_feature_add_ONE_relationship_form', $node); 00018 $output .= '<br>'; 00019 $output .= drupal_get_form('tripal_feature_implement_add_chado_properties_navigate', 'relationships', $node->nid); 00020 return $output; 00021 } 00022 00023 /** 00024 * Implements Hook_form() 00025 * Handles adding of Relationships to Features 00026 * 00027 * @ingroup tripal_feature 00028 */ 00029 function tripal_feature_add_ONE_relationship_form($form_state, $node) { 00030 00031 $feature_id = $node->feature_id; 00032 $organism_id = $node->organism->organism_id; 00033 $_SESSION['organism'] = $organism_id; //needed for autocomplete enter feature to work 00034 00035 $form['rel_nid'] = array( 00036 '#type' => 'hidden', 00037 '#value' => $node->nid 00038 ); 00039 00040 $form['add_relationships'] = array( 00041 '#type' => 'fieldset', 00042 '#title' => t('Add Relationships') . '<span class="form-optional" title="This field is optional">(optional)</span>', 00043 ); 00044 00045 $form['add_relationships']['description'] = array( 00046 '#type' => 'item', 00047 '#value' => t('Relationships are specified as follows: (Subject) (Type of Relationship) (Object). For example, X part_of Y, where X & Y are genomic features.') 00048 ); 00049 00050 $form['add_relationships']['subject_id'] = array( 00051 '#type' => 'textfield', 00052 '#title' => t('Subject'), 00053 '#description' => 'The Uniquename, Name, Database Reference or Synonym of a Feature can be used here', 00054 ); 00055 00056 $cv = tripal_cv_get_cv_by_name('relationship'); 00057 $type_options = tripal_cv_get_cvterm_options($cv->cv_id); 00058 $type_options[0] = 'Select a Type'; 00059 ksort($type_options); 00060 $form['add_relationships']['type_id'] = array( 00061 '#type' => 'select', 00062 '#title' => t('Type of Relationship'), 00063 '#options' => $type_options 00064 00065 ); 00066 00067 $form['add_relationships']['object_id'] = array( 00068 '#type' => 'textfield', 00069 '#title' => t('Object'), 00070 '#description' => 'The Uniquename, Name, Database Reference or Synonym of a Feature can be used here', 00071 ); 00072 00073 $form['add_relationships']['r_description'] = array( 00074 '#type' => 'textarea', 00075 '#title' => t('Notes on the relationship') . '<span class="form-optional" title="This field is optional">+</span>', 00076 '#description' => t('Should not include Genotypes and Phenotypes'), 00077 ); 00078 00079 $form['add_relationships']['submit'] = array( 00080 '#type' => 'submit', 00081 '#value' => t('Add a Relationship') 00082 ); 00083 00084 $form['add_relationships']['r_feature_id'] = array( 00085 '#type' => 'value', 00086 '#value' => $feature_id, 00087 '#required' => TRUE 00088 00089 ); 00090 00091 $form['add_relationships']['r_feature_uniquename'] = array( 00092 '#type' => 'value', 00093 '#value' => $node->uniquename, 00094 '#required' => TRUE 00095 ); 00096 00097 return $form; 00098 00099 } 00100 00101 /** 00102 * 00103 * 00104 * @ingroup tripal_feature 00105 */ 00106 function tripal_feature_add_ONE_relationship_form_validate($form, &$form_state) { 00107 00108 //Require Validation if adding 00109 if ($form_state['clicked_button']['#value'] == t('Add a Relationship') ) { 00110 00111 // check valid feature selected for subject 00112 $criteria = array('unknown' => array('value'=> $form_state['values']['subject_id'], 00113 'columns'=>array('name','uniquename','accession','synonym') )); 00114 $subject_results = get_chado_stocks($criteria,'ANY',$_SESSION['organism']); 00115 if (sizeof($subject_results) > 1) { 00116 $links= array(); 00117 for ($i=0; $i<sizeof($subject_results); $i++) { $links[] = l($i+1, "node/".$subject_results[$i]->nid); } 00118 $message = "Too many stocks match '".$form_state['values']['subject_id']."'! " 00119 . " Please refine your input to match ONLY ONE stock. <br>" 00120 . "To aid in this process, here are the stocks that match your initial input: " 00121 .join(', ',$links); 00122 form_set_error('subject_id', $message); 00123 } elseif (sizeof($subject_results) < 1) { 00124 form_set_error('subject_id', "There are no stocks matching your input. Please check your input for typos and/or lookup the stock ".l('here', 'stocks')); 00125 } elseif (sizeof($subject_results) == 1) { 00126 $form_state['values']['subject_id'] = $subject_results[0]->stock_id; 00127 } 00128 00129 // check valid stock selected for object 00130 $criteria = array('unknown' => array('value'=> $form_state['values']['object_id'], 00131 'columns'=>array('name','uniquename','accession','synonym') )); 00132 $object_results = get_chado_stocks($criteria,'ANY',$_SESSION['organism']); 00133 if (sizeof($object_results) > 1) { 00134 $links= array(); 00135 for ($i=0; $i<sizeof($object_results); $i++) { $links[] = l($i+1, "node/".$object_results[$i]->nid); } 00136 $message = "Too many stocks match '".$form_state['values']['object_id']."'! " 00137 . "Please refine your input to match ONLY ONE stock. <br>" 00138 . "To aid in this process, here are the stocks that match your initial input: " 00139 .join(', ',$links); 00140 form_set_error('object_id', $message); 00141 } elseif (sizeof($object_results) < 1) { 00142 form_set_error('object_id', "There are no stocks matching your input. Please check your input for typos and/or lookup the stock ".l('here', 'stocks')); 00143 } elseif (sizeof($object_results) == 1) { 00144 $form_state['values']['object_id'] = $object_results[0]->stock_id; 00145 } 00146 00147 // check valid type selected 00148 if ($form_state['values']['type_id'] == 0) { 00149 form_set_error('type_id', 'Please select a type of relationship.'); 00150 } else { 00151 $previous_db = tripal_db_set_active('chado'); 00152 $tmp_obj = db_fetch_object(db_query("SELECT count(*) as count FROM cvterm WHERE cvterm_id=%d",$form_state['values']['type_id'])); 00153 tripal_db_set_active($previous_db); 00154 00155 if ($tmp_obj->count != 1) { 00156 form_set_error('type_id', 'The type you selected is not valid. Please choose another one.'); 00157 } 00158 } 00159 00160 // check either subject or object is the current stock 00161 if ( $subject_results[0]->nid != $form_state['values']['rel_nid'] ) { 00162 if ( $object_results[0]->nid != $form_state['values']['rel_nid'] ) { 00163 form_set_error('subject_id', 'Either Subject or Object must be the current stock ('.$form_state['values']['r_stock_uniquename'].').'); 00164 } 00165 } 00166 } //end of require validation if adding relationship 00167 } 00168 00169 /** 00170 * 00171 * 00172 * @ingroup tripal_feature 00173 */ 00174 function tripal_feature_add_ONE_relationship_form_submit($form, &$form_state) { 00175 00176 if ($form_state['values']['subject_id'] > 0) { 00177 $previous_db = db_set_active('chado'); 00178 db_query( 00179 "INSERT INTO stock_relationship (subject_id, type_id, object_id, value) VALUES (%d, %d, %d, '%s')", 00180 $form_state['values']['subject_id'], 00181 $form_state['values']['type_id'], 00182 $form_state['values']['object_id'], 00183 $form_state['values']['r_description'] 00184 ); 00185 db_set_active($previous_db); 00186 00187 drupal_set_message('Successfully Added Relationship.'); 00188 } //end of insert relationship 00189 00190 } 00191 00192 /** 00193 * 00194 * 00195 * @ingroup tripal_feature 00196 */ 00197 function tripal_feature_edit_ALL_relationships_page($node) { 00198 $output = ''; 00199 00200 $output .= drupal_get_form('tripal_feature_edit_ALL_relationships_form', $node); 00201 $output .= '<br>'; 00202 $output .= drupal_get_form('tripal_feature_add_ONE_relationship_form', $node); 00203 $output .= '<br>'; 00204 $output .= drupal_get_form('tripal_feature_implement_back_to_feature_button', $node->nid); 00205 00206 return $output; 00207 } 00208 00209 /** 00210 * Implements Hook_form() 00211 * Handles adding of Properties & Synonyms to Stocks 00212 * 00213 * @ingroup tripal_feature 00214 */ 00215 function tripal_feature_edit_ALL_relationships_form($form_state, $node) { 00216 $form = array(); 00217 00218 $form['nid'] = array( 00219 '#type' => 'hidden', 00220 '#value' => $node->nid 00221 ); 00222 00223 $form['r_feature_uniquename'] = array( 00224 '#type' => 'hidden', 00225 '#value' => $node->uniquename 00226 ); 00227 00228 $i=0; 00229 00230 $feature = $node->feature; 00231 $orelationships = tripal_feature_load_relationships ($feature->feature_id,'as_object'); 00232 $srelationships = tripal_feature_load_relationships ($feature->feature_id,'as_subject'); 00233 $relationships = array_merge($orelationships,$srelationships); 00234 00235 if (sizeof($relationships) != 0) { 00236 foreach ($relationships as $r) { 00237 00238 $i++; 00239 $form["num-$i"] = array( 00240 '#type' => 'fieldset', 00241 '#title' => "Relationship $i", 00242 ); 00243 00244 $form["num-$i"]["id-$i"] = array( 00245 '#type' => 'hidden', 00246 '#value' => $r->stock_relationship_id 00247 ); 00248 00249 //Enter relationship specific fields 00250 if ( !empty($r->subject_id) ) { 00251 $default = $r->subject_uniquename; 00252 $description = l($r->subject_name, 'node/'.$r->subject_nid); 00253 } else { 00254 $default = $node->uniquename; 00255 $description = "Current Feature"; 00256 } 00257 $description .= " (".$r->subject_type.")"; 00258 $form["num-$i"]["subject_id-$i"] = array( 00259 '#type' => 'textfield', 00260 //'#title' => t('Subject'), 00261 '#required' => TRUE, 00262 '#size' => 30, 00263 '#default_value' => $default, 00264 '#description' => $description, 00265 ); 00266 00267 $cv = tripal_cv_get_cv_by_name('relationship'); 00268 $type_options = tripal_cv_get_cvterm_options($cv->cv_id); 00269 ksort($type_options); 00270 $form["num-$i"]["type_id-$i"] = array( 00271 '#type' => 'select', 00272 //'#title' => t('Type of Relationship'), 00273 '#options' => $type_options, 00274 '#required' => TRUE, 00275 '#default_value' => $r->relationship_type_id 00276 ); 00277 00278 if (!empty($r->object_id) ) { 00279 $default = $r->object_uniquename; 00280 $description = l($r->object_name, 'node/'.$r->object_nid); 00281 } else { 00282 $default = $node->uniquename; 00283 $description = 'Current Feature'; 00284 } 00285 $description .= " (".$r->object_type.")"; 00286 $form["num-$i"]["object_id-$i"] = array( 00287 '#type' => 'textfield', 00288 //'#title' => t('Object'), 00289 '#required' => TRUE, 00290 '#size' => 30, 00291 '#default_value' => $default, 00292 '#description' => $description 00293 ); 00294 00295 $form["num-$i"]["delete-$i"] = array( 00296 '#type' => 'submit', 00297 '#value' => t("Delete"), 00298 '#name' => "delete-$i", 00299 ); 00300 00301 } //end of foreach relationship 00302 $form['num_relationships'] = array( 00303 '#type' => 'hidden', 00304 '#value' => $i 00305 ); 00306 00307 $form["submit-edits"] = array( 00308 '#type' => 'submit', 00309 '#value' => t('Update All Relationships') 00310 ); 00311 } else { 00312 $form["info"] = array( 00313 '#type' => 'markup', 00314 '#value' => t('No relationships currently exist for this feature.') 00315 ); 00316 } 00317 00318 00319 00320 return $form; 00321 } 00322 00323 /** 00324 * 00325 * 00326 * @ingroup tripal_feature 00327 */ 00328 function tripal_feature_edit_ALL_relationships_form_validate($form, &$form_state) { 00329 00330 // Only Require if Updating Relationships 00331 if ($form_state['clicked_button']['#value'] == t('Update All Relationships') ) { 00332 00333 for ($i=1; $i<=$form_state['values']['num_relationships']; $i++) { 00334 00335 // check valid stock selected for subject 00336 $criteria = array('unknown' => array('value'=>$form_state['values']["subject_id-$i"], 00337 'columns'=>array('name','uniquename','accession','synonym') )); 00338 $subject_results = get_chado_stocks($criteria,'ANY',$_SESSION['organism']); 00339 if (sizeof($subject_results) > 1) { 00340 $links= array(); 00341 for ($j=0; $j<sizeof($subject_results); $j++) { $links[] = l($j+1, "node/".$subject_results[$j]->nid); } 00342 $message = "Too many stocks match '".$form_state['values']["subject_id-$i"]."'! " 00343 . "Please refine your input to match ONLY ONE stock. <br>" 00344 . "To aid in this process, here are the stocks that match your initial input: " 00345 .join(', ',$links); 00346 form_set_error("subject_id-$i", $message); 00347 } elseif (sizeof($subject_results) < 1) { 00348 form_set_error("subject_id-$i", "There are no stocks matching your input. Please check your input for typos and/or lookup the stock ".l('here', 'stocks')); 00349 } elseif (sizeof($subject_results) == 1) { 00350 $form_state['values']["subject_id-$i"] = $subject_results[0]->stock_id; 00351 } 00352 00353 // check valid stock selected for object 00354 $criteria = array('unknown' => array('value'=> $form_state['values']["object_id-$i"], 00355 'columns'=>array('name','uniquename','accession','synonym') )); 00356 $object_results = get_chado_stocks($criteria,'ANY',$_SESSION['organism']); 00357 if (sizeof($object_results) > 1) { 00358 $links= array(); 00359 for ($j=0; $j<sizeof($object_results); $j++) { $links[] = l($j+1, "node/".$object_results[$j]->nid); } 00360 $message = "Too many stocks match '".$form_state['values']["object_id-$i"]."'! " 00361 . "Please refine your input to match ONLY ONE stock. <br>" 00362 . "To aid in this process, here are the stocks that match your initial input: " 00363 .join(', ',$links); 00364 form_set_error("object_id-$i", $message); 00365 } elseif (sizeof($object_results) < 1) { 00366 form_set_error("object_id-$i", "There are no stocks matching your input. Please check your input for typos and/or lookup the stock ".l('here', 'stocks')); 00367 } elseif (sizeof($object_results) == 1) { 00368 $form_state['values']["object_id-$i"] = $object_results[0]->stock_id; 00369 } 00370 00371 // check valid type selected 00372 if ($form_state['values']["type_id-$i"] == 0) { 00373 form_set_error('type_id', 'Please select a type of relationship.'); 00374 } else { 00375 $previous_db = tripal_db_set_active('chado'); 00376 $tmp_obj = db_fetch_object(db_query("SELECT count(*) as count FROM cvterm WHERE cvterm_id=%d",$form_state['values']["type_id-$i"])); 00377 tripal_db_set_active($previous_db); 00378 00379 if ($tmp_obj->count != 1) { 00380 form_set_error("type_id-$i", 'The type you selected is not valid. Please choose another one.'); 00381 } 00382 } 00383 00384 // check either subject or object is the current stock 00385 if ( $subject_results[0]->nid != $form_state['values']['nid'] ) { 00386 if ( $object_results[0]->nid != $form_state['values']['nid'] ) { 00387 form_set_error("subject_id-$i", 'Either Subject or Object must be the current stock ('.$form_state['values']['r_stock_uniquename'].').'); 00388 } 00389 } 00390 00391 } // end of for each relationship 00392 } //end of if updating relationships 00393 00394 } 00395 00396 /** 00397 * 00398 * 00399 * @ingroup tripal_feature 00400 */ 00401 function tripal_feature_edit_ALL_relationships_form_submit($form, &$form_state) { 00402 00403 if ($form_state['clicked_button']['#value'] == t('Update Relationships') ) { 00404 //Update all 00405 for ($i=1; $i<=$form_state['values']['num_relationships']; $i++) { 00406 00407 //process stock textfields 00408 tripal_feature_update_relationship( 00409 $form_state['values']["id-$i"], 00410 $form_state['values']["subject_id-$i"], 00411 $form_state['values']["type_id-$i"], 00412 $form_state['values']["object_id-$i"] 00413 ); 00414 } 00415 drupal_set_message("Updated all Relationships"); 00416 drupal_goto('node/'.$form_state['values']['nid']); 00417 00418 } elseif ( preg_match('/Delete #(\d+)/', $form_state['clicked_button']['#value'], $matches) ) { 00419 00420 $i = $matches[1]; 00421 tripal_feature_delete_relationship($form_state['values']["id-$i"]); 00422 drupal_set_message("Deleted Relationship"); 00423 00424 } elseif ($form_state['clicked_button']['#value'] == t('Back to Stock') ) { 00425 drupal_goto('node/'.$form_state['values']['nid']); 00426 } else { 00427 drupal_set_message("Unrecognized Button Pressed",'error'); 00428 } 00429 00430 } 00431 00432 /** 00433 * 00434 * 00435 * @ingroup tripal_feature 00436 */ 00437 function tripal_feature_update_relationship ($stock_relationship_id, $subject_id, $cvterm_id, $object_id) { 00438 00439 $previous_db = db_set_active('chado'); 00440 db_query( 00441 "UPDATE stock_relationship SET subject_id=%d, type_id=%d, object_id=%d WHERE stock_relationship_id=%d", 00442 $subject_id, 00443 $cvterm_id, 00444 $object_id, 00445 $stock_relationship_id 00446 ); 00447 db_set_active($previous_db); 00448 00449 } 00450 00451 /** 00452 * 00453 * 00454 * @ingroup tripal_feature 00455 */ 00456 function tripal_feature_delete_relationship ($stock_relationship_id) { 00457 00458 $previous_db = db_set_active('chado'); 00459 db_query( 00460 "DELETE FROM stock_relationship WHERE stock_relationship_id=%d", 00461 $stock_relationship_id 00462 ); 00463 db_set_active($previous_db); 00464 00465 } 00466 00467 /** 00468 * 00469 * 00470 * @ingroup tripal_feature 00471 */ 00472 function theme_tripal_feature_edit_ALL_relationships_form ($form) { 00473 $output = ''; 00474 00475 $output .= '<br><fieldset>'; 00476 $output .= '<legend>Edit Already Existing Relationships<span class="form-optional" title="This field is optional">(optional)</span></legend>'; 00477 $output .= '<p>Each relationship for this stock is listed below, one per line. The textboxes indicating ' 00478 .'the subject and object of the relationship can contain the uniquename, name, database ' 00479 .'reference or synonym of a stock of the same organism.</p>'; 00480 $output .= '<table>'; 00481 $output .= '<tr><th>#</th><th>Subject</th><th>Type</th><th>Object</th><th></th></tr>'; 00482 00483 for ($i=1; $i<=$form['num_relationships']['#value']; $i++) { 00484 $output .= '<tr><td>'.drupal_render($form["num-$i"]).'</td>'. 00485 '<td>'.drupal_render($form["subject_id-$i"]).'</td>'. 00486 '<td>'.drupal_render($form["type_id-$i"]).'</td>'. 00487 '<td>'.drupal_render($form["object_id-$i"]).'</td>'. 00488 '<td>'.drupal_render($form["submit-$i"]).'</td></tr>'; 00489 } 00490 00491 $output .= '</table><br>'; 00492 $output .= drupal_render($form); 00493 $output .= '</fieldset>'; 00494 00495 return $output; 00496 } 00497 00498 /** 00499 * 00500 * 00501 * @ingroup tripal_feature 00502 */ 00503 function tripal_feature_list_relationships_for_node($feature_name, $subject_relationships, $object_relationships) { 00504 00505 if (!empty($subject_relationships) OR !empty($object_relationships) ) { 00506 $output = '<table>'; 00507 $output .= '<tr><th>Subject</th><th>Relationship Type</th><th>Object</th></tr>'; 00508 00509 if (!empty($subject_relationships) ) { 00510 foreach ($subject_relationships as $s) { 00511 $output .= '<tr><td>'.$s->subject_name.'</td><td>'.$s->relationship_type.'</td><td>'.$feature_name.'</td></tr>'; 00512 } 00513 } 00514 00515 if (!empty($object_relationships) ) { 00516 foreach ($object_relationships as $o) { 00517 $output .= '<tr><td>'.$feature_name.'</td><td>'.$o->relationship_type.'</td><td>'.$o->object_name.'</td></tr>'; 00518 } // end of foreach property 00519 } 00520 00521 $output .= '</table>'; 00522 } else { 00523 $output = 'No Relationships For the Current Feature'; 00524 } 00525 00526 return $output; 00527 00528 }