tripal_library.api.inc 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. <?php
  2. /**
  3. * Implements hook_chado_library_schema()
  4. * Purpose: To add descriptions and foreign keys to default table description
  5. * Note: This array will be merged with the array from all other implementations
  6. *
  7. * @return
  8. * Array describing the library table
  9. *
  10. * @ingroup tripal_library
  11. */
  12. function tripal_library_chado_library_schema() {
  13. $description = array();
  14. // Default table description in tripal_core.schema.api.inc: tripal_core_chado_feature_schema()
  15. $description['foreign keys']['organism'] = array(
  16. 'table' => 'organism',
  17. 'columns' => array(
  18. 'organism_id' => 'organism_id',
  19. ),
  20. );
  21. $description['foreign keys']['cvterm'] = array(
  22. 'table' => 'cvterm',
  23. 'columns' => array(
  24. 'type_id' => 'cvterm_id',
  25. ),
  26. );
  27. $referring_tables = array(
  28. 'library_cvterm',
  29. 'library_feature',
  30. 'library_pub',
  31. 'library_synonym',
  32. 'libraryprop'
  33. );
  34. $description['referring_tables'] = $referring_tables;
  35. return $description;
  36. }
  37. /**
  38. * Implements hook_chado_library_feature_schema()
  39. * Purpose: To add descriptions and foreign keys to default table description
  40. * Note: This array will be merged with the array from all other implementations
  41. *
  42. * @return
  43. * Array describing the library_feature table
  44. *
  45. * @ingroup tripal_library
  46. */
  47. function tripal_library_chado_library_feature_schema() {
  48. $description = array();
  49. // Default table description in tripal_core.schema.api.inc: tripal_core_chado_library_feature_schema()
  50. $description['foreign keys']['library'] = array(
  51. 'table' => 'library',
  52. 'columns' => array(
  53. 'library_id' => 'library_id',
  54. ),
  55. );
  56. $description['foreign keys']['feature'] = array(
  57. 'table' => 'feature',
  58. 'columns' => array(
  59. 'feature_id' => 'feature_id',
  60. ),
  61. );
  62. return $description;
  63. }
  64. /**
  65. * Implements hook_chado_libraryprop_schema()
  66. * Purpose: To add descriptions and foreign keys to default table description
  67. * Note: This array will be merged with the array from all other implementations
  68. *
  69. * @return
  70. * Array describing the libraryprop table
  71. *
  72. * @ingroup tripal_library
  73. */
  74. function tripal_library_chado_libraryprop_schema() {
  75. $description = array();
  76. // Default table description in tripal_core.schema.api.inc: tripal_core_chado_library_feature_schema()
  77. $description['foreign keys']['library'] = array(
  78. 'table' => 'library',
  79. 'columns' => array(
  80. 'library_id' => 'library_id',
  81. ),
  82. );
  83. $description['foreign keys']['cvterm'] = array(
  84. 'table' => 'cvterm',
  85. 'columns' => array(
  86. 'type_id' => 'cvterm_id',
  87. ),
  88. );
  89. return $description;
  90. }
  91. /**
  92. * Adds a single property to an existing library record.
  93. *
  94. * @ingroup tripal_api
  95. */
  96. function tripal_library_get_property($library_id,$property){
  97. // construct the array of values to be inserted
  98. $values = array (
  99. 'library_id' => $library_id,
  100. 'type_id' => array (
  101. 'cv_id' => array (
  102. 'name' => 'tripal',
  103. ),
  104. 'name' => $property,
  105. 'is_obsolete' => 0
  106. ),
  107. );
  108. $results = tripal_core_generate_chado_var('libraryprop',$values);
  109. $results = tripal_core_expand_chado_vars($results,'field','libraryprop.value');
  110. return $results;
  111. }
  112. /**
  113. * Adds a single property to an existing library record.
  114. *
  115. * @ingroup tripal_api
  116. */
  117. function tripal_library_insert_property($library_id,$property,$value,$update_if_present = 0){
  118. // first see if the property already exists, if so we can't insert
  119. $prop = tripal_library_get_property($library_id,$property);
  120. if(count($prop)>0){
  121. if($update_if_present){
  122. return tripal_library_update_property($library_id,$property,$value) ;
  123. } else {
  124. return FALSE;
  125. }
  126. }
  127. // construct the array of values to be inserted
  128. $values = array (
  129. 'library_id' => $library_id,
  130. 'type_id' => array (
  131. 'cv_id' => array (
  132. 'name' => 'tripal',
  133. ),
  134. 'name' => $property,
  135. 'is_obsolete' => 0
  136. ),
  137. 'value' => $value,
  138. 'rank' => 0,
  139. );
  140. return tripal_core_chado_insert('libraryprop',$values);
  141. }
  142. /**
  143. * Adds a single property to an existing library record.
  144. *
  145. * @ingroup tripal_api
  146. */
  147. function tripal_library_update_property($library_id,$property,$value,$insert_if_missing = 0){
  148. // first see if the property is missing (we can't update a missing property
  149. $prop = tripal_library_get_property($library_id,$property);
  150. if(count($prop)==0){
  151. if($insert_if_missing){
  152. return tripal_library_insert_property($library_id,$property,$value);
  153. } else {
  154. return FALSE;
  155. }
  156. }
  157. // construct the array that will match the exact record to update
  158. $match = array (
  159. 'library_id' => $library_id,
  160. 'type_id' => array (
  161. 'cv_id' => array (
  162. 'name' => 'tripal',
  163. ),
  164. 'name' => $property,
  165. ),
  166. );
  167. // construct the array of values to be updated
  168. $values = array (
  169. 'value' => $value,
  170. );
  171. return tripal_core_chado_update('libraryprop',$match,$values);
  172. }
  173. /**
  174. * Adds a single property to an existing library record.
  175. *
  176. * @ingroup tripal_api
  177. */
  178. function tripal_library_delete_property($library_id,$property){
  179. // construct the array that will match the exact record to update
  180. $match = array (
  181. 'library_id' => $library_id,
  182. 'type_id' => array (
  183. 'cv_id' => array (
  184. 'name' => 'tripal',
  185. ),
  186. 'name' => $property,
  187. ),
  188. );
  189. return tripal_core_chado_delete('libraryprop',$match);
  190. }