tripal_stock.api.inc 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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 variable
  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 stock_retrieve($identifiers, $options = array()) {
  41. // Set Defaults
  42. if (!isset($options['include_fk'])) {
  43. // Tells chado_generate_var to only expand 1 level
  44. $options['include_fk'] = array('type_id' => TRUE, 'dbxref_id' => TRUE);
  45. }
  46. // Error Checking of parameters
  47. if (!is_array($identifiers)) {
  48. tripal_report_error(
  49. 'tripal_stock_api',
  50. TRIPAL_ERROR,
  51. "stock_retrieve: The identifier passed in is expected to be an array with the key
  52. matching a column name in the stock table (ie: stock_id or name). You passed in %identifier.",
  53. array(
  54. '%identifier'=> print_r($identifiers, TRUE)
  55. )
  56. );
  57. }
  58. elseif (empty($identifiers)) {
  59. tripal_report_error(
  60. 'tripal_stock_api',
  61. TRIPAL_ERROR,
  62. "stock_retrieve: You did not pass in anything to identify the stock you want. The identifier
  63. is expected to be an array with the key matching a column name in the stock table
  64. (ie: stock_id or name). You passed in %identifier.",
  65. array(
  66. '%identifier'=> print_r($identifiers, TRUE)
  67. )
  68. );
  69. }
  70. // If one of the identifiers is property then use chado_get_record_with_property()
  71. if (isset($identifiers['property'])) {
  72. $property = $identifiers['property'];
  73. unset($identifiers['property']);
  74. $stock = chado_get_record_with_property('stock', $property, $identifiers, $options);
  75. }
  76. // Else we have a simple case and we can just use chado_generate_var to get the stock
  77. else {
  78. // Try to get the stock
  79. $stock = chado_generate_var(
  80. 'stock',
  81. $identifiers,
  82. $options
  83. );
  84. }
  85. // Ensure the stock is singular. If it's an array then it is not singular
  86. if (is_array($stock)) {
  87. tripal_report_error(
  88. 'tripal_stock_api',
  89. TRIPAL_ERROR,
  90. "stock_retrieve: The identifiers you passed in were not unique. You passed in %identifier.",
  91. array(
  92. '%identifier'=> print_r($identifiers, TRUE)
  93. )
  94. );
  95. }
  96. // Report an error if $stock is FALSE since then chado_generate_var has failed
  97. elseif ($stock === FALSE) {
  98. tripal_report_error(
  99. 'tripal_stock_api',
  100. TRIPAL_ERROR,
  101. "stock_retrieve: chado_generate_var() failed to return a stock based on the identifiers
  102. you passed in. You should check that your identifiers are correct, as well as, look
  103. for a chado_generate_var error for additional clues. You passed in %identifier.",
  104. array(
  105. '%identifier'=> print_r($identifiers, TRUE)
  106. )
  107. );
  108. }
  109. // Else, as far we know, everything is fine so give them their stock :)
  110. else {
  111. return $stock;
  112. }
  113. }
  114. /**
  115. * Retrieves a chado stock variable
  116. *
  117. * @param $identifier
  118. * An array with the key stating what the identifier is. Supported keys include any
  119. * field in the stock table. See the chado_select_record() $values parameter for
  120. * additional details including an example.
  121. * @param $options
  122. * An array of options. Supported keys include:
  123. * - Any keys supported by chado_generate_var(). See that function definition for
  124. * additional details.
  125. *
  126. * @return
  127. * An array of stock objects matching the criteria.
  128. *
  129. * @ingroup tripal_stock_api
  130. */
  131. function stock_retrieve_multiple($identifiers, $options = array()) {
  132. // Set Defaults
  133. if (!isset($options['include_fk'])) {
  134. // Tells chado_generate_var to only expand 1 level
  135. $options['include_fk'] = array('type_id' => TRUE, 'dbxref_id' => TRUE);
  136. }
  137. // Error Checking of parameters
  138. if (!is_array($identifiers)) {
  139. tripal_report_error(
  140. 'tripal_stock_api',
  141. TRIPAL_ERROR,
  142. "stock_retrieve: The identifier passed in is expected to be an array with the key
  143. matching a column name in the stock table (ie: stock_id or name). You passed in %identifier.",
  144. array(
  145. '%identifier'=> print_r($identifiers, TRUE)
  146. )
  147. );
  148. }
  149. elseif (empty($identifiers)) {
  150. tripal_report_error(
  151. 'tripal_stock_api',
  152. TRIPAL_ERROR,
  153. "stock_retrieve: You did not pass in anything to identify the stock you want. The identifier
  154. is expected to be an array with the key matching a column name in the stock table
  155. (ie: stock_id or name). You passed in %identifier.",
  156. array(
  157. '%identifier'=> print_r($identifiers, TRUE)
  158. )
  159. );
  160. }
  161. // If one of the identifiers is property then use chado_get_record_with_property()
  162. if (isset($identifiers['property'])) {
  163. $property = $identifiers['property'];
  164. unset($identifiers['property']);
  165. $stock = chado_get_record_with_property('stock', $property, $identifiers, $options);
  166. }
  167. // Else we have a simple case and we can just use chado_generate_var to get the stock
  168. else {
  169. // Try to get the stock
  170. $stock = chado_generate_var(
  171. 'stock',
  172. $identifiers,
  173. $options
  174. );
  175. }
  176. // Report an error if $stock is FALSE since then chado_generate_var has failed
  177. if ($stock === FALSE) {
  178. tripal_report_error(
  179. 'tripal_stock_api',
  180. TRIPAL_ERROR,
  181. "stock_retrieve: chado_generate_var() failed to return a stock based on the identifiers
  182. you passed in. You should check that your identifiers are correct, as well as, look
  183. for a chado_generate_var error for additional clues. You passed in %identifier.",
  184. array(
  185. '%identifier'=> print_r($identifiers, TRUE)
  186. )
  187. );
  188. }
  189. // Else, as far we know, everything is fine so give them their stock :)
  190. else {
  191. return $stock;
  192. }
  193. }