tripal_organism.api.inc 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. /**
  3. * @defgroup tripal_organism_api Organism API
  4. * @ingroup tripal_api
  5. * @{
  6. * Provides an application programming interface (API) to manage organisms
  7. * @}
  8. */
  9. /**
  10. * Purpose: Return a given organism object using the nid
  11. *
  12. * @return organism object created by node load
  13. *
  14. * @ingroup tripal_organism_api
  15. */
  16. function tripal_organism_get_organism_by_nid($nid) {
  17. return node_load($nid);
  18. }
  19. /**
  20. * Purpose: Return a given organism object using the organism id
  21. *
  22. * @return organism object created by node load
  23. *
  24. * @ingroup tripal_organism_api
  25. */
  26. function tripal_organism_get_organism_by_organism_id($organism_id) {
  27. $sql = "SELECT nid FROM {chado_organism} WHERE organism_id = :organism_id";
  28. $r = db_query($sql, array(':organism_id' => $organism_id))->fetchObject();
  29. if (!empty($r->nid)) {
  30. return node_load($r->nid);
  31. }
  32. else {
  33. drupal_set_message(t("Function: tripal_organism_get_organism_by_organism_id() -no organism with that organism id sync'd with drupal"), 'error');
  34. }
  35. return 0;
  36. }
  37. /**
  38. * Returns a list of organisms that are currently synced with Drupal
  39. * @ingroup tripal_organism_api
  40. */
  41. function tripal_organism_get_synced() {
  42. // use this SQL for getting synced organisms
  43. $dsql = "SELECT * FROM {chado_organism}";
  44. $orgs = db_query($dsql);
  45. // use this SQL statement for getting the organisms
  46. $csql = "SELECT * FROM {Organism} " .
  47. "WHERE organism_id = :organism_id";
  48. $org_list = array();
  49. // iterate through the organisms and build an array of those that are synced
  50. foreach ($orgs as $org) {
  51. $args = array(':organism_id' => $org->organism_id);
  52. $org = chado_query($csql, $args)->fetchObject();
  53. $org_list[] = $org;
  54. }
  55. return $org_list;
  56. }
  57. /**
  58. *
  59. * @param $organism
  60. * @param $nid
  61. */
  62. function tripal_organism_get_image_url($organism, $nid = NULL) {
  63. $url = '';
  64. // first look for an image with the genus/species name. This is old-style tripal
  65. // and we keep it for backwards compatibility. If we don't find that file
  66. // then look for the image with the node ID in the name. If we don't find that then
  67. // no image tag is generated
  68. $base_path = realpath('.');
  69. $image_dir = tripal_get_files_dir('tripal_organism') . "/images";
  70. $image_name = $organism->genus . "_" . $organism->species . ".jpg";
  71. if (file_exists("$base_path/$image_dir/$image_name")) {
  72. $url = file_create_url("$image_dir/$image_name");
  73. }
  74. else {
  75. $image_name = $nid . ".jpg";
  76. if (file_exists("$base_path/$image_dir/$image_name")) {
  77. $url = file_create_url("$image_dir/$image_name");
  78. }
  79. }
  80. return $url;
  81. }