tripal_stock.api.inc

  1. 2.x tripal_stock/api/tripal_stock.api.inc
  2. 1.x tripal_stock/api/tripal_stock.api.inc

Provides an application programming interface (API) to manage stocks

File

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