tripal_cv.admin.inc 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. <?php
  2. /**
  3. * @file
  4. * Provides administration of controlled vocabularies & their terms.
  5. */
  6. /**
  7. * Provide landing page to the new admin pages
  8. *
  9. * @ingroup tripal_cv
  10. */
  11. function tripal_cv_admin_cv_listing() {
  12. $output = '';
  13. // set the breadcrumb
  14. $breadcrumb = array();
  15. $breadcrumb[] = l('Home', '<front>');
  16. $breadcrumb[] = l('Administration', 'admin');
  17. $breadcrumb[] = l('Tripal', 'admin/tripal');
  18. $breadcrumb[] = l('Chado Modules', 'admin/tripal/chado');
  19. $breadcrumb[] = l('Vocabularies', 'admin/tripal/chado/tripal_cv');
  20. drupal_set_breadcrumb($breadcrumb);
  21. // Add the view
  22. $cvs_view = views_embed_view('tripal_cv_admin_cvs','default');
  23. $cvterms_view = views_embed_view('tripal_cv_admin_cvterms','default');
  24. if (isset($cvs_view) && isset($cvterms_view)) {
  25. $output .= $cvs_view;
  26. }
  27. else {
  28. $output .= '<p>The Tripal Controlled Vocabulary module uses primarily views to provide an '
  29. . 'administrative interface. Currently one or more views needed for this '
  30. . 'administrative interface are disabled. <strong>Click each of the following links to '
  31. . 'enable the pertinent views</strong>:</p>';
  32. $output .= '<ul>';
  33. if (!isset($cvs_view)) {
  34. $output .= '<li>'.l('Tripal Vocabularies', 'admin/tripal/chado/tripal_cv/views/cvs/enable').'</li>';
  35. }
  36. if (!isset($cvterm_view)) {
  37. $output .= '<li>'.l('Tripal Vocabulary Terms', 'admin/tripal/chado/tripal_cv/views/cvterms/enable').'</li>';
  38. }
  39. $output .= '</ul>';
  40. }
  41. return $output;
  42. }
  43. /**
  44. *
  45. */
  46. function tripal_cv_admin_set_defaults_form($form, $form_state) {
  47. $form['instructions'] = array(
  48. '#markup' => t('Much of the data housed in Chado is typed, meaning that a ' .
  49. 'controlled vocabulary describes what type of data the record is. For example, '.
  50. 'a feature must have a "type" which is typically a term from ' .
  51. 'the Sequence Ontology. Record properties typically have a type as well. '.
  52. 'Tripal allows the administrator to set a default type for each table in '.
  53. 'Chado that requires a type from a vocabulary. By default, autocomplete fields, '.
  54. 'type select boxes and type validation occur using the default vocabularies set below. '),
  55. );
  56. // get the list of all tables that use the cvterm table as an FK
  57. $cvterm_schema = chado_get_schema('cvterm');
  58. $referring_tables = $cvterm_schema['referring_tables'];
  59. // get the list of tables that already have default set
  60. $cv_defaults = db_select('tripal_cv_defaults', 'TCD')
  61. ->fields('TCD', array('cv_default_id', 'table_name', 'field_name', 'cv_id'))
  62. ->orderBy('table_name', 'ASC')
  63. ->execute();
  64. // get the list of vocabularies
  65. $cvs = tripal_get_cv_select_options();
  66. $form['settings'] = array(
  67. '#type' => 'fieldset',
  68. '#title' => t('Configured Defaults'),
  69. '#description' => t('The following tables have a default vocabulary'),
  70. '#tree' => TRUE,
  71. );
  72. foreach ($cv_defaults as $cv_default) {
  73. $cv_default_id = $cv_default->cv_default_id;
  74. $cv = tripal_get_cv(array('cv_id' => $cv_default->cv_id));
  75. $form['settings']['existing'][$cv_default_id]["id"] = array(
  76. '#type' => 'hidden',
  77. '#value' => $cv_default_id,
  78. );
  79. // Display
  80. $form['settings']['existing'][$cv_default_id]["table_name-display"] = array(
  81. '#type' => 'markup',
  82. '#markup' => $cv_default->table_name
  83. );
  84. $form['settings']['existing'][$cv_default_id]["field_name-display"] = array(
  85. '#type' => 'markup',
  86. '#markup' => $cv_default->field_name
  87. );
  88. // Save for use in submit
  89. $form['settings']['existing'][$cv_default_id]["table_name"] = array(
  90. '#type' => 'hidden',
  91. '#value' => $cv_default->table_name
  92. );
  93. $form['settings']['existing'][$cv_default_id]["field_name"] = array(
  94. '#type' => 'hidden',
  95. '#value' => $cv_default->field_name
  96. );
  97. // Selectbox to set the vocabulary
  98. if (!empty($cv)) {
  99. $default_cv = $cv_default->cv_id;
  100. }
  101. else {
  102. $cvs[0] = 'NONE SET';
  103. $default_cv = 0;
  104. }
  105. $form['settings']['existing'][$cv_default_id]["vocabulary"] = array(
  106. '#type' => 'select',
  107. '#options' => $cvs,
  108. '#default_value' => $default_cv,
  109. );
  110. // Actions
  111. $view_terms = l('New Vocabulary', 'admin/tripal/chado/tripal_cv/cv/add');
  112. $add_term = '';
  113. if (!empty($cv)) {
  114. $view_terms = l(
  115. 'View Terms',
  116. 'admin/tripal/chado/tripal_cv/cvterms',
  117. array('query' => array('cv' => $cv->name))
  118. );
  119. $add_term = l(
  120. 'Add Term',
  121. 'admin/tripal/chado/tripal_cv/cv/' . $cv->cv_id . '/cvterm/add'
  122. );
  123. }
  124. $form['settings']['existing'][$cv_default_id]["view-terms"] = array(
  125. '#type' => 'markup',
  126. '#markup' => $view_terms
  127. );
  128. $form['settings']['existing'][$cv_default_id]["add-new-term"] = array(
  129. '#type' => 'markup',
  130. '#markup' => $add_term
  131. );
  132. }
  133. $form['submit'] = array(
  134. '#type' => 'submit',
  135. '#value' => 'Update Defaults'
  136. );
  137. return $form;
  138. }
  139. function tripal_cv_admin_set_defaults_form_submit($form, $form_state) {
  140. foreach ($form_state['values']['settings']['existing'] as $default_cv) {
  141. if (!empty($default_cv['vocabulary'])) {
  142. tripal_set_default_cv(
  143. $default_cv['table_name'],
  144. $default_cv['field_name'],
  145. '', // We are passing in the cv_id so we don't need the name
  146. $default_cv['vocabulary']
  147. );
  148. }
  149. }
  150. }
  151. /**
  152. *
  153. * @param unknown $variables
  154. */
  155. function theme_tripal_cv_admin_set_defaults_form($variables) {
  156. $element = $variables['element'];
  157. $header = array(
  158. 'table_name' => array('data' => t('Table Name'), 'width' => '20%'),
  159. 'field_name' => array('data' => t('Field Name'), 'width' => '20%'),
  160. 'vocabulary' => array('data' => t('Default Vocabulary'), 'width' => '30%'),
  161. 'actions' => array('data' => t('Actions'), 'width' => '30%'),
  162. );
  163. $rows = array();
  164. foreach ($element['settings']['existing'] as $key => $value) {
  165. if (is_numeric($key)) {
  166. $action_links = '<ul class="links inline">';
  167. if (!empty($value['view-terms'])) {
  168. $action_links .= '<li>' . drupal_render($value['view-terms']) . '</li>';
  169. }
  170. if (!empty($value['add-new-term'])) {
  171. $action_links .= '<li>' . drupal_render($value['add-new-term']) . '</li>';
  172. }
  173. $action_links .= '</li></ul>';
  174. $rows[] = array(
  175. drupal_render($value['table_name-display']),
  176. drupal_render($value['field_name-display']),
  177. drupal_render($value['vocabulary']),
  178. $action_links
  179. );
  180. }
  181. }
  182. $settings_table = theme('table', array(
  183. 'header' => $header,
  184. 'rows' => $rows
  185. ));
  186. $element['settings']['existing'] = array(
  187. '#type' => 'markup',
  188. '#markup' => $settings_table,
  189. );
  190. return drupal_render_children($element);
  191. }