tripal_cv.api.inc 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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, [
  49. ':table' => $table,
  50. ':field' => $field,
  51. ])->fetchField();
  52. return tripal_get_cv(['cv_id' => $cv_id]);
  53. }
  54. /**
  55. * Retrieves the Chado table to which a vocbulary is set as default.
  56. *
  57. * Each table in Chado that has a 'type_id' (or foreign key constraint to
  58. * the cvterm table) will have a default vocabulary assigned. This indicates to
  59. * Tripal that terms in that vocabulary are used to set the type_id for that
  60. * table. An example where this is used is the
  61. * tripal_get_cvterm_select_options() function which generates a list of options
  62. * for a select box used in a Drupal form. The select box will list the terms
  63. * from the default vocabulary in the drop down.
  64. *
  65. * This function uses the vocabulary ID to get the Chado table to which it
  66. * is assigned.
  67. *
  68. * @param $cv_id
  69. * The ID of the vocabulary.
  70. *
  71. * @return
  72. * If an assignment is present, an object containing the 'table_name' and
  73. * 'field_name' is returned.
  74. *
  75. * @ingroup tripal_legacy_chado_cv_api
  76. */
  77. function tripal_get_default_cv_table($cv_id) {
  78. $default = db_select('tripal_cv_defaults', 't')
  79. ->fields('t', ['table_name', 'field_name'])
  80. ->condition('cv_id', $cv_id)
  81. ->execute()
  82. ->fetchObject();
  83. return $default;
  84. }
  85. /**
  86. * Create an options array to be used in a form element
  87. * which provides a list of all chado cvterms. Unlike the
  88. * tripal_get_cvterm_select_option, this function retreives the cvterms using
  89. * the default vocabulary set for a given table and field. It will also
  90. * notify the administrative user if a default vocabulary is missing for the
  91. * field and if the vocabulary is empty.
  92. *
  93. * @param $table
  94. * The name of the table that contains the field with a foreign key
  95. * relationship to the cvterm table
  96. * @param $field
  97. * The table field name that has the foreign key relationship to the
  98. * cvterm table for which the default vocabulary will be set
  99. * @param $field_desc
  100. * A human readable descriptive name for the field
  101. *
  102. * @return
  103. * An array(cvterm_id => name)
  104. * for each cvterm in the chado cvterm table where cv_id=that supplied
  105. *
  106. * @ingroup tripal_legacy_chado_cv_api
  107. */
  108. function tripal_get_cvterm_default_select_options($table, $field, $field_desc) {
  109. $default_cv = tripal_get_default_cv($table, $field);
  110. $options = [];
  111. if ($default_cv) {
  112. $options = tripal_get_cvterm_select_options($default_cv->cv_id);
  113. if (count($options) == 0) {
  114. tripal_set_message('There are no ' . $field_desc . '. Please ' .
  115. l('add terms',
  116. 'admin/tripal/loaders/chado_vocabs/chado_cv/' . $default_cv->cv_id . '/cvterm/add',
  117. ['attributes' => ['target' => '_blank']]) . ' to the ' .
  118. $default_cv->name . ' vocabulary.',
  119. TRIPAL_WARNING);
  120. }
  121. }
  122. else {
  123. tripal_set_message('There is not a default vocabulary set for ' . $field_desc . '. ' .
  124. 'Please set one using the ' .
  125. l('vocabulary defaults configuration page',
  126. 'admin/tripal/vocab/defaults',
  127. ['attributes' => ['target' => '_blank']]) . '.',
  128. TRIPAL_WARNING);
  129. }
  130. return $options;
  131. }
  132. /**
  133. * This function sets the default vocabulary for a given table and field.
  134. *
  135. * @param $table
  136. * The name of the table that contains a field with a foreign key
  137. * relationship to the cvterm table
  138. * @param $field
  139. * The table field name that has the foreign key relationship to the
  140. * cvterm table for which the default vocabulary will be set
  141. * @param $cv_name
  142. * The name of the vocabulary
  143. *
  144. * @return
  145. * TRUE if set, FALSE if an error occured
  146. *
  147. * @ingroup tripal_legacy_chado_cv_api
  148. */
  149. function tripal_set_default_cv($table, $field, $cv_name, $cv_id = FALSE) {
  150. // Get the CV object
  151. if ($cv_id) {
  152. $cv = tripal_get_cv(['cv_id' => $cv_id]);
  153. }
  154. else {
  155. $cv = tripal_get_cv(['name' => $cv_name]);
  156. }
  157. if ($cv) {
  158. // first delete any entries for this table and field
  159. $num_deleted = db_delete('tripal_cv_defaults')
  160. ->condition('table_name', $table)
  161. ->condition('field_name', $field)
  162. ->execute();
  163. // now add the default value
  164. $cv_default_id = db_insert('tripal_cv_defaults')
  165. ->fields([
  166. 'table_name' => $table,
  167. 'field_name' => $field,
  168. 'cv_id' => $cv->cv_id,
  169. ])
  170. ->execute();
  171. if (!$cv_default_id) {
  172. tripal_report_error('tripal_chado', TRIPAL_WARNING,
  173. "Cannot set default vocabulary for %table.%field. Check the error logs.",
  174. ['%table' => $table, '%field' => $field]);
  175. return FALSE;
  176. }
  177. }
  178. else {
  179. tripal_report_error('tripal_chado', TRIPAL_WARNING,
  180. "Cannot set default vocabulary for %table.%field. The vocabulary name, '%cvname', doesn't exist.",
  181. ['%table' => $table, '%field' => $field, '%cvname' => $cv_name]);
  182. return FALSE;
  183. }
  184. }