tripal_chado.stock.api.inc 7.0 KB

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