tripal_cv.api.inc 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. /**
  3. * Retreives the default vocabulary for a given table and field.
  4. *
  5. * Each table in Chado that has a 'type_id' (or foreign key constraint to
  6. * the cvterm table) will have a default vocabulary assigned. This indicates to
  7. * Tripal that terms in that vocabulary are used to set the type_id for that
  8. * table. An example where this is used is the
  9. * tripal_get_cvterm_select_options() function which generates a list of options
  10. * for a select box used in a Drupal form. The select box will list the terms
  11. * from the default vocabulary in the drop down.
  12. *
  13. * This function uses the Chado table and field name (e.g. 'type_id') to
  14. * retreive the vocabulary assgined.
  15. *
  16. * @param $table
  17. * The name of the table that contains a field with a foreign key
  18. * relationship to the cvterm table
  19. * @param $field
  20. * The table field name that has the foreign key relationship to the
  21. * cvterm table for which the default vocabulary will be set
  22. *
  23. * @return
  24. * The cv object of the default vocabulary or an empty array if not
  25. * available.
  26. */
  27. function tripal_get_default_cv($table, $field) {
  28. $sql = "
  29. SELECT cv_id
  30. FROM {tripal_cv_defaults}
  31. WHERE table_name = :table and field_name = :field
  32. ";
  33. $cv_id = db_query($sql, array(':table' => $table, ':field' => $field))->fetchField();
  34. return tripal_get_cv(array('cv_id' => $cv_id));
  35. }
  36. /**
  37. * Retrieves the Chado table to which a vocbulary is set as default.
  38. *
  39. * Each table in Chado that has a 'type_id' (or foreign key constraint to
  40. * the cvterm table) will have a default vocabulary assigned. This indicates to
  41. * Tripal that terms in that vocabulary are used to set the type_id for that
  42. * table. An example where this is used is the
  43. * tripal_get_cvterm_select_options() function which generates a list of options
  44. * for a select box used in a Drupal form. The select box will list the terms
  45. * from the default vocabulary in the drop down.
  46. *
  47. * This function uses the vocabulary ID to get the Chado table to which it
  48. * is assigned.
  49. *
  50. * @param $cv_id
  51. * The ID of the vocabulary.
  52. *
  53. * @return
  54. * If an assignment is present, an object containing the 'table_name' and
  55. * 'field_name' is returned.
  56. */
  57. function tripal_get_default_cv_table($cv_id) {
  58. $default = db_select('tripal_cv_defaults', 't')
  59. ->fields('t', array('table_name', 'field_name'))
  60. ->condition('cv_id', $cv_id)
  61. ->execute()
  62. ->fetchObject();
  63. return $default;
  64. }
  65. /**
  66. * Create an options array to be used in a form element
  67. * which provides a list of all chado cvterms. Unlike the
  68. * tripal_get_cvterm_select_option, this function retreives the cvterms using
  69. * the default vocabulary set for a given table and field. It will also
  70. * notify the administrative user if a default vocabulary is missing for the
  71. * field and if the vocabulary is empty.
  72. *
  73. * @param $table
  74. * The name of the table that contains the field with a foreign key
  75. * relationship to the cvterm table
  76. * @param $field
  77. * The table field name that has the foreign key relationship to the
  78. * cvterm table for which the default vocabulary will be set
  79. * @param $field_desc
  80. * A human readable descriptive name for the field
  81. *
  82. * @return
  83. * An array(cvterm_id => name)
  84. * for each cvterm in the chado cvterm table where cv_id=that supplied
  85. */
  86. function tripal_get_cvterm_default_select_options($table, $field, $field_desc) {
  87. $default_cv = tripal_get_default_cv($table, $field);
  88. $options = array();
  89. if ($default_cv) {
  90. $options = tripal_get_cvterm_select_options($default_cv->cv_id);
  91. if (count($options) == 0) {
  92. tripal_set_message('There are no ' . $field_desc . '. Please ' .
  93. l('add terms',
  94. 'admin/tripal/vocab/cv/' .$default_cv->cv_id. '/cvterm/add',
  95. array('attributes' => array('target' => '_blank'))) . ' to the ' .
  96. $default_cv->name .' vocabulary.',
  97. TRIPAL_WARNING);
  98. }
  99. }
  100. else {
  101. tripal_set_message('There is not a default vocabulary set for ' . $field_desc . '. '.
  102. 'Please set one using the ' .
  103. l('vocabulary defaults configuration page',
  104. 'admin/tripal/vocab/defaults',
  105. array('attributes' => array('target' => '_blank'))) . '.',
  106. TRIPAL_WARNING);
  107. }
  108. return $options;
  109. }