tripal_cv.api.inc 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. <?php
  2. /**
  3. * @file
  4. * Provides legacy application programming interface (API) to manage controlled
  5. * vocabularies in the Chado database.
  6. */
  7. /**
  8. * @defgroup tripal_legacy_chado_cv_api Legacy Chado CV
  9. * @ingroup tripal_legacy_api
  10. * @{
  11. * Provides an application programming interface (API) to manage entities
  12. * that use Chado as their base data.
  13. * @}
  14. */
  15. /**
  16. * Retrieves the default vocabulary for a given table and field.
  17. *
  18. * Each table in Chado that has a 'type_id' (or foreign key constraint to
  19. * the cvterm table) will have a default vocabulary assigned. This indicates to
  20. * Tripal that terms in that vocabulary are used to set the type_id for that
  21. * table. An example where this is used is the
  22. * tripal_get_cvterm_select_options() function which generates a list of options
  23. * for a select box used in a Drupal form. The select box will list the terms
  24. * from the default vocabulary in the drop down.
  25. *
  26. * This function uses the Chado table and field name (e.g. 'type_id') to
  27. * retreive the vocabulary assgined.
  28. *
  29. * @param $table
  30. * The name of the table that contains a field with a foreign key
  31. * relationship to the cvterm table
  32. * @param $field
  33. * The table field name that has the foreign key relationship to the
  34. * cvterm table for which the default vocabulary will be set
  35. *
  36. * @return
  37. * The cv object of the default vocabulary or an empty array if not
  38. * available.
  39. *
  40. * @ingroup tripal_legacy_chado_cv_api
  41. */
  42. function tripal_get_default_cv($table, $field) {
  43. $sql = "
  44. SELECT cv_id
  45. FROM {tripal_cv_defaults}
  46. WHERE table_name = :table and field_name = :field
  47. ";
  48. $cv_id = db_query($sql, array(':table' => $table, ':field' => $field))->fetchField();
  49. return tripal_get_cv(array('cv_id' => $cv_id));
  50. }
  51. /**
  52. * Retrieves the Chado table to which a vocbulary is set as default.
  53. *
  54. * Each table in Chado that has a 'type_id' (or foreign key constraint to
  55. * the cvterm table) will have a default vocabulary assigned. This indicates to
  56. * Tripal that terms in that vocabulary are used to set the type_id for that
  57. * table. An example where this is used is the
  58. * tripal_get_cvterm_select_options() function which generates a list of options
  59. * for a select box used in a Drupal form. The select box will list the terms
  60. * from the default vocabulary in the drop down.
  61. *
  62. * This function uses the vocabulary ID to get the Chado table to which it
  63. * is assigned.
  64. *
  65. * @param $cv_id
  66. * The ID of the vocabulary.
  67. *
  68. * @return
  69. * If an assignment is present, an object containing the 'table_name' and
  70. * 'field_name' is returned.
  71. *
  72. * @ingroup tripal_legacy_chado_cv_api
  73. */
  74. function tripal_get_default_cv_table($cv_id) {
  75. $default = db_select('tripal_cv_defaults', 't')
  76. ->fields('t', array('table_name', 'field_name'))
  77. ->condition('cv_id', $cv_id)
  78. ->execute()
  79. ->fetchObject();
  80. return $default;
  81. }
  82. /**
  83. * Create an options array to be used in a form element
  84. * which provides a list of all chado cvterms. Unlike the
  85. * tripal_get_cvterm_select_option, this function retreives the cvterms using
  86. * the default vocabulary set for a given table and field. It will also
  87. * notify the administrative user if a default vocabulary is missing for the
  88. * field and if the vocabulary is empty.
  89. *
  90. * @param $table
  91. * The name of the table that contains the field with a foreign key
  92. * relationship to the cvterm table
  93. * @param $field
  94. * The table field name that has the foreign key relationship to the
  95. * cvterm table for which the default vocabulary will be set
  96. * @param $field_desc
  97. * A human readable descriptive name for the field
  98. *
  99. * @return
  100. * An array(cvterm_id => name)
  101. * for each cvterm in the chado cvterm table where cv_id=that supplied
  102. *
  103. * @ingroup tripal_legacy_chado_cv_api
  104. */
  105. function tripal_get_cvterm_default_select_options($table, $field, $field_desc) {
  106. $default_cv = tripal_get_default_cv($table, $field);
  107. $options = array();
  108. if ($default_cv) {
  109. $options = tripal_get_cvterm_select_options($default_cv->cv_id);
  110. if (count($options) == 0) {
  111. tripal_set_message('There are no ' . $field_desc . '. Please ' .
  112. l('add terms',
  113. 'admin/tripal/loaders/chado_vocabs/chado_cv/' .$default_cv->cv_id. '/cvterm/add',
  114. array('attributes' => array('target' => '_blank'))) . ' to the ' .
  115. $default_cv->name .' vocabulary.',
  116. TRIPAL_WARNING);
  117. }
  118. }
  119. else {
  120. tripal_set_message('There is not a default vocabulary set for ' . $field_desc . '. '.
  121. 'Please set one using the ' .
  122. l('vocabulary defaults configuration page',
  123. 'admin/tripal/vocab/defaults',
  124. array('attributes' => array('target' => '_blank'))) . '.',
  125. TRIPAL_WARNING);
  126. }
  127. return $options;
  128. }
  129. /**
  130. * This function sets the default vocabulary for a given table and field.
  131. *
  132. * @param $table
  133. * The name of the table that contains a field with a foreign key
  134. * relationship to the cvterm table
  135. * @param $field
  136. * The table field name that has the foreign key relationship to the
  137. * cvterm table for which the default vocabulary will be set
  138. * @param $cv_name
  139. * The name of the vocabulary
  140. *
  141. * @return
  142. * TRUE if set, FALSE if an error occured
  143. *
  144. * @ingroup tripal_legacy_chado_cv_api
  145. */
  146. function tripal_set_default_cv($table, $field, $cv_name, $cv_id = FALSE) {
  147. // Get the CV object
  148. if ($cv_id) {
  149. $cv = tripal_get_cv(array('cv_id' => $cv_id));
  150. }
  151. else {
  152. $cv = tripal_get_cv(array('name' => $cv_name));
  153. }
  154. if ($cv) {
  155. // first delete any entries for this table and field
  156. $num_deleted = db_delete('tripal_cv_defaults')
  157. ->condition('table_name', $table)
  158. ->condition('field_name', $field)
  159. ->execute();
  160. // now add the default value
  161. $cv_default_id = db_insert('tripal_cv_defaults')
  162. ->fields(array(
  163. 'table_name' => $table,
  164. 'field_name' => $field,
  165. 'cv_id' => $cv->cv_id,
  166. ))
  167. ->execute();
  168. if (!$cv_default_id) {
  169. tripal_report_error('tripal_chado', TRIPAL_WARNING,
  170. "Cannot set default vocabulary for %table.%field. Check the error logs.",
  171. array('%table' => $table, '%field' => $field));
  172. return FALSE;
  173. }
  174. }
  175. else {
  176. tripal_report_error('tripal_chado', TRIPAL_WARNING,
  177. "Cannot set default vocabulary for %table.%field. The vocabulary name, '%cvname', doesn't exist.",
  178. array('%table' => $table, '%field' => $field, '%cvname' => $cv_name));
  179. return FALSE;
  180. }
  181. }