tripal_organism.api.inc 3.1 KB

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