Tripal v1.0 (6.x-1.0)
tripal_stock-relationships.inc
Go to the documentation of this file.
00001 <?php
00012 function tripal_stock_add_ALL_relationships_page($node) {
00013   $output = '';
00014 
00015   $output .= tripal_stock_add_chado_properties_progress('relationships') . '<br />';
00016   $output .= '<b>All Relationships should include the CURRENT Individual (' . $node->stock->uniquename . ')</b><br />';
00017   $output .= '<br />';
00018   $output .= theme('tripal_stock_relationships', $node);
00019   $output .= '<br /><br />';
00020   $output .= drupal_get_form('tripal_stock_add_ONE_relationship_form', $node);
00021   $output .= '<br />';
00022   $output .= drupal_get_form('tripal_stock_add_chado_properties_navigate', 'relationships', $node->nid);
00023   return $output;
00024 }
00025 
00031 function tripal_stock_add_ONE_relationship_form($form_state, $node) {
00032 
00033   $stock_id = $node->stock->stock_id;
00034   $organism_id = $node->stock->organism_id->organism_id;
00035   $_SESSION['organism'] = $organism_id; //needed for autocomplete enter stock to work
00036 
00037   $form['rel_nid'] = array(
00038     '#type' => 'hidden',
00039     '#value' => $node->nid
00040   );
00041 
00042   $form['add_relationships'] = array(
00043     '#type' => 'fieldset',
00044     '#title' => t('Add Relationships') . '<span class="form-optional" title="This field is optional">(optional)</span>',
00045   );
00046 
00047   $form['add_relationships']['description'] = array(
00048     '#type' => 'item',
00049     '#value' => t('Relationships are specified as follows: (Subject) (Type of Relationship) (Object). For example, Fred is_the_paternal_parent_of Matty, where Fred & Matty are both stocks.')
00050   );
00051 
00052   $form['add_relationships']['subject_id'] = array(
00053     '#type' => 'textfield',
00054     '#title' => t('Subject'),
00055     '#description' => 'The Uniquename, Name, Database Reference or Synonym of a Stock can be used here',
00056   );
00057 
00058   $type_options = tripal_cv_get_cvterm_options( variable_get('chado_stock_relationship_cv', 'null') );
00059   $type_options[0] = 'Select a Type';
00060   ksort($type_options);
00061   $form['add_relationships']['type_id'] = array(
00062     '#type' => 'select',
00063     '#title' => t('Type of Relationship'),
00064     '#options' => $type_options
00065 
00066   );
00067 
00068   $form['add_relationships']['object_id'] = array(
00069     '#type' => 'textfield',
00070     '#title' => t('Object'),
00071     '#description' => 'The Uniquename, Name, Database Reference or Synonym of a Stock can be used here',
00072   );
00073 
00074   $form['add_relationships']['r_description'] = array(
00075     '#type' => 'textarea',
00076     '#title' => t('Notes on the relationship') . '<span class="form-optional" title="This field is optional">+</span>',
00077     '#description' => t('Should not include Genotypes and Phenotypes'),
00078   );
00079 
00080   $form['add_relationships']['submit'] = array(
00081     '#type' => 'submit',
00082     '#value' => t('Add a Relationship')
00083   );
00084 
00085   $form['add_relationships']['r_stock_id'] = array(
00086     '#type' => 'value',
00087     '#value' => $stock_id,
00088     '#required' => TRUE
00089 
00090   );
00091 
00092   $form['add_relationships']['r_stock_uniquename'] = array(
00093     '#type' => 'value',
00094     '#value' => $node->stock->uniquename,
00095     '#required' => TRUE
00096   );
00097 
00098   return $form;
00099 
00100 }
00101 
00107 function tripal_stock_add_ONE_relationship_form_validate($form, &$form_state) {
00108 
00109   //Require Validation if adding
00110   if ($form_state['clicked_button']['#value'] == t('Add a Relationship') ) {
00111     // check valid stock selected for subject
00112     $subject_results = tripal_stock_get_stock_by_name_identifier( $form_state['values']['subject_id'], $_SESSION['organism']);
00113     if (sizeof($subject_results) > 1) {
00114       $links= array();
00115       for ($i=0; $i<sizeof($subject_results); $i++) {
00116       $links[] = l($i+1, "node/" . $subject_results[$i]->nid); }
00117       $message = "Too many stocks match '" . $form_state['values']['subject_id'] . "'! "
00118                   . " Please refine your input to match ONLY ONE stock. <br />"
00119      . "To aid in this process, here are the stocks that match your initial input: "
00120      . join(', ', $links);
00121       form_set_error('subject_id', $message);
00122     }
00123     elseif (sizeof($subject_results) < 1) {
00124       form_set_error('subject_id', t("There are no stocks matching your input. Please check your input for typos and/or lookup the stock <a href='!url'>here</a>", array('!url' => url('stocks'))));
00125     }
00126     elseif (sizeof($subject_results) == 1) {
00127       $form_state['values']['subject_id'] = $subject_results[0]->stock->stock_id;
00128     }
00129 
00130     // check valid stock selected for object
00131     $object_results = tripal_stock_get_stock_by_name_identifier( $form_state['values']['object_id'], $_SESSION['organism']);
00132     if (sizeof($object_results) > 1) {
00133       $links= array();
00134       for ($i=0; $i<sizeof($object_results); $i++) {
00135       $links[] = l($i+1, "node/" . $object_results[$i]->nid); }
00136       $message = t("Too many stocks match '%object'!  Please refine your input to match
00137         ONLY ONE stock. <br /> To aid in this process, here are the stocks that match your
00138         initial input: %stocks.",
00139         array('%object' => $form_state['values']['object_id'],
00140           '%stocks' => join(', ', $links)
00141         )
00142       );
00143       form_set_error('object_id', $message);
00144     }
00145     elseif (sizeof($object_results) < 1) {
00146       form_set_error('object_id', t("There are no stocks matching your input. Please check your input for typos and/or lookup the stock <a href='!url'>here</a>", array('!url' => url('stocks'))));
00147     }
00148     elseif (sizeof($object_results) == 1) {
00149       $form_state['values']['object_id'] = $object_results[0]->stock->stock_id;
00150     }
00151 
00152     // check valid type selected
00153     if ($form_state['values']['type_id'] == 0) {
00154       form_set_error('type_id', 'Please select a type of relationship.');
00155     }
00156     else {
00157       $tmp_obj = db_fetch_object(chado_query("SELECT count(*) as count FROM {cvterm} WHERE cvterm_id=%d", $form_state['values']['type_id']));
00158 
00159       if ($tmp_obj->count != 1) {
00160         form_set_error('type_id', 'The type you selected is not valid. Please choose another one.');
00161       }
00162     }
00163 
00164     // check either subject or object is the current stock
00165     if ( $subject_results[0]->nid != $form_state['values']['rel_nid'] ) {
00166       if ( $object_results[0]->nid != $form_state['values']['rel_nid'] ) {
00167         form_set_error('subject_id', 'Either Subject or Object must be the current stock (' . $form_state['values']['r_stock_uniquename'] . ').');
00168       }
00169     }
00170   } //end of require validation if adding relationship
00171 }
00172 
00178 function tripal_stock_add_ONE_relationship_form_submit($form, &$form_state) {
00179 
00180   if ($form_state['values']['subject_id'] > 0) {
00181     chado_query(
00182       "INSERT INTO {stock_relationship} (subject_id, type_id, object_id, value) VALUES (%d, %d, %d, '%s')",
00183       $form_state['values']['subject_id'],
00184       $form_state['values']['type_id'],
00185       $form_state['values']['object_id'],
00186       $form_state['values']['r_description']
00187     );
00188 
00189     drupal_set_message(t('Successfully Added Relationship.'));
00190   } //end of insert relationship
00191 
00192 }
00193 
00199 function tripal_stock_edit_ALL_relationships_page($node) {
00200   $output = '';
00201 
00202   $output .= drupal_get_form('tripal_stock_edit_ALL_relationships_form', $node);
00203   $output .= '<br />';
00204   $output .= drupal_get_form('tripal_stock_add_ONE_relationship_form', $node);
00205   $output .= '<br />';
00206   $output .= drupal_get_form('tripal_stock_back_to_stock_button', $node->nid);
00207 
00208   return $output;
00209 }
00210 
00216 function tripal_stock_edit_ALL_relationships_form($form_state, $node) {
00217   $form = array();
00218 
00219   // All Stock Relationships
00220   $node = tripal_core_expand_chado_vars($node, 'table', 'stock_relationship');
00221 
00222   // compile all relationships into one variable
00223   $relationships = array();
00224   if (is_array($node->stock->stock_relationship->subject_id)) {
00225     foreach ($node->stock->stock_relationship->subject_id as $r) {
00226       $relationships[$r->stock_relationship_id] = $r;
00227     }
00228   }
00229   elseif (is_object($node->stock->stock_relationship->subject_id)) {
00230     $relationships[$node->stock->stock_relationship->subject_id->stock_relationship_id] = $node->stock->stock_relationship->subject_id;
00231   }
00232 
00233   if (is_array($node->stock->stock_relationship->object_id)) {
00234     foreach ($node->stock->stock_relationship->object_id as $r) {
00235       $relationships[$r->stock_relationship_id] = $r;
00236     }
00237   }
00238   elseif (is_object($node->stock->stock_relationship->object_id)) {
00239     $relationships[$node->stock->stock_relationship->object_id->stock_relationship_id] = $node->stock->stock_relationship->object_id;
00240   }
00241 
00242   $form['nid'] = array(
00243     '#type' => 'hidden',
00244     '#value' => $node->nid
00245   );
00246 
00247   $form['r_stock_uniquename'] = array(
00248     '#type' => 'hidden',
00249     '#value' => $node->stock->uniquename
00250   );
00251 
00252   $i=0;
00253   if (sizeof($relationships) != 0) {
00254   foreach ($relationships as $r) {
00255 
00256     $i++;
00257     $form["num-$i"] = array(
00258       '#type' => 'item',
00259       '#value' => $i . '.'
00260     );
00261 
00262     $form["id-$i"] = array(
00263       '#type' => 'hidden',
00264       '#value' => $r->stock_relationship_id
00265     );
00266 
00267     //Enter relationship specific fields
00268     if ( $node->stock->stock_id != $r->subject_id->stock_id ) {
00269       $default = $r->subject_id->uniquename;
00270       $description = l($r->subject_id->name, 'node/' . $r->subject_id->nid);
00271     }
00272     else { $default = $node->stock->uniquename; $description = 'Current Stock'; }
00273     $form["subject_id-$i"] = array(
00274       '#type' => 'textfield',
00275       //'#title' => t('Subject'),
00276       '#required'   => TRUE,
00277       '#size' => 30,
00278       '#default_value' => $default,
00279       '#description' => t('%description', array('%description' => $description)),
00280     );
00281 
00282     $type_options = tripal_cv_get_cvterm_options( variable_get('chado_stock_relationship_cv', 'null') );
00283     ksort($type_options);
00284     $form["type_id-$i"] = array(
00285       '#type' => 'select',
00286       //'#title' => t('Type of Relationship'),
00287       '#options' => $type_options,
00288       '#required' => TRUE,
00289       '#default_value' => $r->type_id->cvterm_id
00290     );
00291 
00292     if ( $node->stock->stock_id != $r->object_id->stock_id ) {
00293       $default = $r->object_id->uniquename;
00294       $description = l($r->object_id->name, 'node/' . $r->object_id->nid);
00295     }
00296     else { $default = $node->stock->uniquename; $description = 'Current Stock'; }
00297     $form["object_id-$i"] = array(
00298       '#type' => 'textfield',
00299       //'#title' => t('Object'),
00300       '#required'   => TRUE,
00301       '#size' => 30,
00302       '#default_value' => $default,
00303       '#description' => $description
00304     );
00305 
00306     $form["submit-$i"] = array(
00307       '#type' => 'submit',
00308       '#value' => t("Delete #$i")
00309     );
00310 
00311   }} //end of foreach relationship
00312 
00313   $form['num_relationships'] = array(
00314     '#type' => 'hidden',
00315     '#value' => $i
00316   );
00317 
00318   $form["submit-edits"] = array(
00319     '#type' => 'submit',
00320     '#value' => t('Update Relationships')
00321   );
00322 
00323   return $form;
00324 }
00325 
00331 function tripal_stock_edit_ALL_relationships_form_validate($form, &$form_state) {
00332 
00333   // Only Require if Updating Relationships
00334   if ($form_state['clicked_button']['#value'] == t('Update Relationships') ) {
00335 
00336     for ($i=1; $i<=$form_state['values']['num_relationships']; $i++) {
00337 
00338       // check valid stock selected for subject
00339       $subject_results = tripal_stock_get_stock_by_name_identifier( $form_state['values']["subject_id-$i"], $_SESSION['organism']);
00340       if (sizeof($subject_results) > 1) {
00341         $links= array();
00342         for ($j=0; $j<sizeof($subject_results); $j++) {
00343         $links[] = l($j+1, "node/" . $subject_results[$j]->nid); }
00344         $message = t("Too many stocks match '%subject'!  Please refine your input to match
00345           ONLY ONE stock. <br /> To aid in this process, here are the stocks that match
00346           your initial input: %stocks",
00347           array('%subject' => $form_state['values']["subject_id-$i"],
00348           '%stocks' => join(', ', $links)
00349           )
00350         );
00351         form_set_error("subject_id-$i", $message);
00352       }
00353       elseif (sizeof($subject_results) < 1) {
00354         form_set_error("subject_id-$i", t("There are no stocks matching your input. Please check your input for typos and/or lookup the stock <a href='!url'>here</a>", array('!url' => url('stocks'))));
00355       }
00356       elseif (sizeof($subject_results) == 1) {
00357         $form_state['values']["subject_id-$i"] = $subject_results[0]->stock->stock_id;
00358       }
00359 
00360       // check valid stock selected for object
00361       $object_results = tripal_stock_get_stock_by_name_identifier( $form_state['values']["object_id-$i"], $_SESSION['organism']);
00362       if (sizeof($object_results) > 1) {
00363         $links= array();
00364         for ($j=0; $j<sizeof($object_results); $j++) {
00365         $links[] = l($j+1, "node/" . $object_results[$j]->nid); }
00366         $message = "Too many stocks match '" . $form_state['values']["object_id-$i"] . "'! "
00367                  . "Please refine your input to match ONLY ONE stock. <br />"
00368                  . "To aid in this process, here are the stocks that match your initial input: "
00369                  . join(', ', $links);
00370         form_set_error("object_id-$i", $message);
00371       }
00372       elseif (sizeof($object_results) < 1) {
00373         form_set_error("object_id-$i", t("There are no stocks matching your input. Please check your input for typos and/or lookup the stock <a href='!url'>here</a>", array('!url' => url('stocks'))));
00374       }
00375       elseif (sizeof($object_results) == 1) {
00376         $form_state['values']["object_id-$i"] = $object_results[0]->stock->stock_id;
00377       }
00378 
00379       // check valid type selected
00380       if ($form_state['values']["type_id-$i"] == 0) {
00381         form_set_error('type_id', 'Please select a type of relationship.');
00382       }
00383       else {
00384         $tmp_obj = db_fetch_object(chado_query("SELECT count(*) as count FROM {cvterm} WHERE cvterm_id=%d", $form_state['values']["type_id-$i"]));
00385 
00386         if ($tmp_obj->count != 1) {
00387           form_set_error("type_id-$i", 'The type you selected is not valid. Please choose another one.');
00388         }
00389       }
00390 
00391       // check either subject or object is the current stock
00392       if ( $subject_results[0]->nid != $form_state['values']['nid'] ) {
00393         if ( $object_results[0]->nid != $form_state['values']['nid'] ) {
00394           form_set_error("subject_id-$i", 'Either Subject or Object must be the current stock (' . $form_state['values']['r_stock_uniquename'] . ').');
00395         }
00396       }
00397 
00398     } // end of for each relationship
00399   } //end of if updating relationships
00400 
00401 }
00402 
00408 function tripal_stock_edit_ALL_relationships_form_submit($form, &$form_state) {
00409 
00410   if ($form_state['clicked_button']['#value'] == t('Update Relationships') ) {
00411     //Update all
00412     for ($i=1; $i<=$form_state['values']['num_relationships']; $i++) {
00413 
00414       //process stock textfields
00415       tripal_stock_update_relationship(
00416         $form_state['values']["id-$i"],
00417         $form_state['values']["subject_id-$i"],
00418         $form_state['values']["type_id-$i"],
00419         $form_state['values']["object_id-$i"]
00420       );
00421     }
00422     drupal_set_message(t("Updated all Relationships"));
00423     drupal_goto('node/' . $form_state['values']['nid']);
00424 
00425   }
00426   elseif ( preg_match('/Delete #(\d+)/', $form_state['clicked_button']['#value'], $matches) ) {
00427 
00428     $i = $matches[1];
00429     tripal_stock_delete_relationship($form_state['values']["id-$i"]);
00430     drupal_set_message(t("Deleted Relationship"));
00431 
00432   }
00433   elseif ($form_state['clicked_button']['#value'] == t('Back to Stock') ) {
00434     drupal_goto('node/' . $form_state['values']['nid']);
00435   }
00436   else {
00437     drupal_set_message(t("Unrecognized Button Pressed"), 'error');
00438   }
00439 
00440 }
00441 
00447 function tripal_stock_update_relationship($stock_relationship_id, $subject_id, $cvterm_id, $object_id) {
00448 
00449   chado_query(
00450     "UPDATE {stock_relationship} SET subject_id=%d, type_id=%d, object_id=%d WHERE stock_relationship_id=%d",
00451     $subject_id,
00452     $cvterm_id,
00453     $object_id,
00454     $stock_relationship_id
00455   );
00456 
00457 }
00458 
00464 function tripal_stock_delete_relationship($stock_relationship_id) {
00465 
00466   chado_query(
00467     "DELETE FROM {stock_relationship} WHERE stock_relationship_id=%d",
00468     $stock_relationship_id
00469   );
00470 
00471 }
00472 
00478 function theme_tripal_stock_edit_ALL_relationships_form($form) {
00479   $output = '';
00480 
00481   $output .= '<br /><fieldset>';
00482   $output .= '<legend>Edit Already Existing Relationships<span class="form-optional" title="This field is optional">(optional)</span></legend>';
00483   $output .= '<p>Each relationship for this stock is listed below, one per line. The textboxes indicating '
00484         . 'the subject and object of the relationship can contain the uniquename, name, database '
00485       . 'reference or synonym of a stock of the same organism.</p>';
00486   $output .= '<table>';
00487   $output .= '<tr><th>#</th><th>Subject</th><th>Type</th><th>Object</th><th></th></tr>';
00488 
00489   for ($i=1; $i<=$form['num_relationships']['#value']; $i++) {
00490     $output .= '<tr><td>' . drupal_render($form["num-$i"]) . '</td><td>'
00491              . drupal_render($form["subject_id-$i"]) . '</td><td>'
00492          . drupal_render($form["type_id-$i"]) . '</td><td>'
00493          . drupal_render($form["object_id-$i"]) . '</td><td>'
00494          . drupal_render($form["submit-$i"]) . '</td></tr>';
00495   }
00496 
00497   $output .= '</table><br />';
00498   $output .= drupal_render($form);
00499   $output .= '</fieldset>';
00500 
00501   return $output;
00502 }
00503 
00504 
00510 function tripal_stock_list_relationships_for_node($stock_name, $subject_relationships, $object_relationships) {
00511 
00512   if (!empty($subject_relationships) OR !empty($object_relationships) ) {
00513     $output = '<table>';
00514     $output .= '<tr><th>Subject</th><th>Relationship Type</th><th>Object</th></tr>';
00515 
00516     if (!empty($subject_relationships) ) {
00517       foreach ($subject_relationships as $s) {
00518         $output .= '<tr><td>' . $s->subject_name . '</td><td>' . $s->relationship_type . '</td><td>' . $stock_name . '</td></tr>';
00519       }
00520     }
00521 
00522     if (!empty($object_relationships) ) {
00523       foreach ($object_relationships as $o) {
00524         $output .= '<tr><td>' . $stock_name . '</td><td>' . $o->relationship_type . '</td><td>' . $o->object_name . '</td></tr>';
00525       } // end of foreach property
00526     }
00527 
00528     $output .= '</table>';
00529   }
00530   else {
00531     $output = 'No Relationships Involving the Current Stock';
00532   }
00533 
00534   return $output;
00535 
00536 }
 All Classes Files Functions Variables