tripal_stock.api.inc 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. <?php
  2. /**
  3. * @file
  4. * Provides an application programming interface (API) to manage stocks
  5. */
  6. /**
  7. * @defgroup tripal_stock_api Stock Module API
  8. * @ingroup tripal_api
  9. * @{
  10. * Provides an application programming interface (API) to manage stocks
  11. * @}
  12. */
  13. /**
  14. * Retrieves a chado stock object
  15. *
  16. * @param $identifier
  17. * An array with the key stating what the identifier is. Supported keys (only one of the
  18. * following unique keys is required):
  19. * - stock_id: the chado stock.stock_id primary key
  20. * - nid: the drupal nid of the stock
  21. * There are also some specially handled keys. They are:
  22. * - property: An array/object describing the property to select records for. It
  23. * should at least have either a type_name (if unique across cvs) or type_id. Other
  24. * supported keys include: cv_id/cv_name (of the type), value and rank
  25. * @param $options
  26. * An array of options. Supported keys include:
  27. * - Any keys supported by chado_generate_var(). See that function definition for
  28. * additional details.
  29. *
  30. * NOTE: the $identifier parameter can really be any array similar to $values passed into
  31. * chado_select_record(). It should fully specify the stock record to be returned.
  32. *
  33. * @return
  34. * If unique values were passed in as an identifier then an object describing the stock
  35. * will be returned (will be a chado variable from chado_generate_var()). Otherwise,
  36. * FALSE will be returned.
  37. *
  38. * @ingroup tripal_stock_api
  39. */
  40. function chado_get_stock($identifiers, $options = array()) {
  41. // Error Checking of parameters
  42. if (!is_array($identifiers)) {
  43. tripal_report_error(
  44. 'tripal_stock_api',
  45. TRIPAL_ERROR,
  46. "chado_get_stock: The identifier passed in is expected to be an array with the key
  47. matching a column name in the stock table (ie: stock_id or name). You passed in %identifier.",
  48. array(
  49. '%identifier'=> print_r($identifiers, TRUE)
  50. )
  51. );
  52. }
  53. elseif (empty($identifiers)) {
  54. tripal_report_error(
  55. 'tripal_stock_api',
  56. TRIPAL_ERROR,
  57. "chado_get_stock: You did not pass in anything to identify the stock you want. The identifier
  58. is expected to be an array with the key matching a column name in the stock table
  59. (ie: stock_id or name). You passed in %identifier.",
  60. array(
  61. '%identifier'=> print_r($identifiers, TRUE)
  62. )
  63. );
  64. }
  65. // If one of the identifiers is property then use chado_get_record_with_property()
  66. if (isset($identifiers['property'])) {
  67. $property = $identifiers['property'];
  68. unset($identifiers['property']);
  69. $stock = chado_get_record_with_property('stock', $property, $identifiers, $options);
  70. }
  71. // Else we have a simple case and we can just use chado_generate_var to get the stock
  72. else {
  73. // Try to get the stock
  74. $stock = chado_generate_var(
  75. 'stock',
  76. $identifiers,
  77. $options
  78. );
  79. }
  80. // Ensure the stock is singular. If it's an array then it is not singular
  81. if (is_array($stock)) {
  82. tripal_report_error(
  83. 'tripal_stock_api',
  84. TRIPAL_ERROR,
  85. "chado_get_stock: The identifiers you passed in were not unique. You passed in %identifier.",
  86. array(
  87. '%identifier'=> print_r($identifiers, TRUE)
  88. )
  89. );
  90. }
  91. // Report an error if $stock is FALSE since then chado_generate_var has failed
  92. elseif ($stock === FALSE) {
  93. tripal_report_error(
  94. 'tripal_stock_api',
  95. TRIPAL_ERROR,
  96. "chado_get_stock: chado_generate_var() failed to return a stock based on the identifiers
  97. you passed in. You should check that your identifiers are correct, as well as, look
  98. for a chado_generate_var error for additional clues. You passed in %identifier.",
  99. array(
  100. '%identifier'=> print_r($identifiers, TRUE)
  101. )
  102. );
  103. }
  104. // Else, as far we know, everything is fine so give them their stock :)
  105. else {
  106. return $stock;
  107. }
  108. }
  109. /**
  110. * Retrieves a chado stock object
  111. *
  112. * @param $identifier
  113. * An array with the key stating what the identifier is. Supported keys include any
  114. * field in the stock table. See the chado_select_record() $values parameter for
  115. * additional details including an example.
  116. * @param $options
  117. * An array of options. Supported keys include:
  118. * - Any keys supported by chado_generate_var(). See that function definition for
  119. * additional details.
  120. *
  121. * @return
  122. * An array of stock objects matching the criteria.
  123. *
  124. * @ingroup tripal_stock_api
  125. */
  126. function chado_get_multiple_stocks($identifiers, $options = array()) {
  127. // Error Checking of parameters
  128. if (!is_array($identifiers)) {
  129. tripal_report_error(
  130. 'tripal_stock_api',
  131. TRIPAL_ERROR,
  132. "chado_get_stock: The identifier passed in is expected to be an array with the key
  133. matching a column name in the stock table (ie: stock_id or name). You passed in %identifier.",
  134. array(
  135. '%identifier'=> print_r($identifiers, TRUE)
  136. )
  137. );
  138. }
  139. elseif (empty($identifiers)) {
  140. tripal_report_error(
  141. 'tripal_stock_api',
  142. TRIPAL_ERROR,
  143. "chado_get_stock: You did not pass in anything to identify the stock you want. The identifier
  144. is expected to be an array with the key matching a column name in the stock table
  145. (ie: stock_id or name). You passed in %identifier.",
  146. array(
  147. '%identifier'=> print_r($identifiers, TRUE)
  148. )
  149. );
  150. }
  151. // If one of the identifiers is property then use chado_get_record_with_property()
  152. if (isset($identifiers['property'])) {
  153. $property = $identifiers['property'];
  154. unset($identifiers['property']);
  155. $stock = chado_get_record_with_property('stock', $property, $identifiers, $options);
  156. }
  157. // Else we have a simple case and we can just use chado_generate_var to get the stock
  158. else {
  159. // Try to get the stock
  160. $stock = chado_generate_var(
  161. 'stock',
  162. $identifiers,
  163. $options
  164. );
  165. }
  166. // Report an error if $stock is FALSE since then chado_generate_var has failed
  167. if ($stock === FALSE) {
  168. tripal_report_error(
  169. 'tripal_stock_api',
  170. TRIPAL_ERROR,
  171. "chado_get_stock: chado_generate_var() failed to return a stock based on the identifiers
  172. you passed in. You should check that your identifiers are correct, as well as, look
  173. for a chado_generate_var error for additional clues. You passed in %identifier.",
  174. array(
  175. '%identifier'=> print_r($identifiers, TRUE)
  176. )
  177. );
  178. }
  179. // Else, as far we know, everything is fine so give them their stock :)
  180. else {
  181. return $stock;
  182. }
  183. }