tripal_chado.stock.api.inc 6.9 KB

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