tripal_cv.cv_defaults.inc 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. <?php
  2. /**
  3. *
  4. */
  5. function tripal_cv_admin_set_defaults_form($form, &$form_state) {
  6. $form['instructions'] = [
  7. '#markup' => t('Much of the data housed in Chado is typed, meaning that a ' .
  8. 'controlled vocabulary describes what type of data the record is. For example, ' .
  9. 'a feature must have a "type" which is typically a term from ' .
  10. 'the Sequence Ontology. Record properties typically have a type as well. ' .
  11. 'Tripal allows the administrator to set a default type for each table in ' .
  12. 'Chado that requires a type from a vocabulary. By default, autocomplete fields, ' .
  13. 'type select boxes and type validation occur using the default vocabularies set below. '),
  14. ];
  15. // get the list of all tables that use the cvterm table as an FK
  16. $cvterm_schema = chado_get_schema('cvterm');
  17. $referring_tables = $cvterm_schema['referring_tables'];
  18. // get the list of tables that already have default set
  19. $cv_defaults = db_select('tripal_cv_defaults', 'TCD')
  20. ->fields('TCD', ['cv_default_id', 'table_name', 'field_name', 'cv_id'])
  21. ->orderBy('table_name', 'ASC')
  22. ->execute();
  23. // get the list of vocabularies
  24. $cvs = tripal_get_cv_select_options();
  25. $form['settings'] = [
  26. '#type' => 'fieldset',
  27. '#title' => t('Configured Defaults'),
  28. '#description' => t('The following tables have a default vocabulary'),
  29. '#tree' => TRUE,
  30. ];
  31. foreach ($cv_defaults as $cv_default) {
  32. $cv_default_id = $cv_default->cv_default_id;
  33. $cv = tripal_get_cv(['cv_id' => $cv_default->cv_id]);
  34. $form['settings']['existing'][$cv_default_id]["id"] = [
  35. '#type' => 'hidden',
  36. '#value' => $cv_default_id,
  37. ];
  38. // Display
  39. $form['settings']['existing'][$cv_default_id]["table_name-display"] = [
  40. '#type' => 'markup',
  41. '#markup' => $cv_default->table_name,
  42. ];
  43. $form['settings']['existing'][$cv_default_id]["field_name-display"] = [
  44. '#type' => 'markup',
  45. '#markup' => $cv_default->field_name,
  46. ];
  47. // Save for use in submit
  48. $form['settings']['existing'][$cv_default_id]["table_name"] = [
  49. '#type' => 'hidden',
  50. '#value' => $cv_default->table_name,
  51. ];
  52. $form['settings']['existing'][$cv_default_id]["field_name"] = [
  53. '#type' => 'hidden',
  54. '#value' => $cv_default->field_name,
  55. ];
  56. // Selectbox to set the vocabulary
  57. if (!empty($cv)) {
  58. $default_cv = $cv_default->cv_id;
  59. }
  60. else {
  61. $cvs[0] = 'NONE SET';
  62. $default_cv = 0;
  63. }
  64. $form['settings']['existing'][$cv_default_id]["vocabulary"] = [
  65. '#type' => 'select',
  66. '#options' => $cvs,
  67. '#default_value' => $default_cv,
  68. ];
  69. // Actions
  70. $view_terms = l('New Vocabulary', 'admin/tripal/loaders/chado_vocabs/chado_cv/add');
  71. $add_term = '';
  72. if (!empty($cv)) {
  73. $view_terms = l(
  74. 'View Terms',
  75. 'admin/tripal/loaders/chado_vocabs/chado_cvterms',
  76. ['query' => ['cv' => $cv->name]]
  77. );
  78. $add_term = l(
  79. 'Add Term',
  80. 'admin/tripal/loaders/chado_vocabs/chado_cv/' . $cv->cv_id . '/cvterm/add'
  81. );
  82. }
  83. $form['settings']['existing'][$cv_default_id]["view-terms"] = [
  84. '#type' => 'markup',
  85. '#markup' => $view_terms,
  86. ];
  87. $form['settings']['existing'][$cv_default_id]["add-new-term"] = [
  88. '#type' => 'markup',
  89. '#markup' => $add_term,
  90. ];
  91. }
  92. $form['settings']['submit'] = [
  93. '#type' => 'submit',
  94. '#value' => 'Update Defaults',
  95. ];
  96. // Adding new CV Defaults
  97. $form['new'] = [
  98. '#type' => 'fieldset',
  99. '#title' => 'Add New Defaults',
  100. '#description' => 'You can use the form below to add a default controlled vocabulary
  101. for a table/field combination not available in the "Configured Defaults" section above.',
  102. '#collapsible' => TRUE,
  103. '#collapsed' => TRUE,
  104. '#tree' => TRUE,
  105. '#prefix' => '<div id="new-default">',
  106. '#suffix' => '</div>',
  107. ];
  108. $tripal_msg = tripal_set_message(
  109. 'If you are developing a custom module and would like to use the Default Controlled
  110. Vocabulary API to flexibly set the controlled vocabulary, then it is better to set
  111. the default programatically rather than through this interface. To do this <ol>
  112. <li>Tell Tripal about the table/field you would like to set the default for. This
  113. is done by implementing hook_install() in your modules .install file and adding
  114. a call to <code>tripal_set_default_cv([table name], [field name], [cv name])</code> which
  115. will set the default for <code>[table name].[field name]</code> to the <code>[cv name]</code> controlled
  116. vocabulary. This vocabulary must already exist.</li>
  117. <li>Then everywhere in your module that you need to know the controlled vocabulary,
  118. you call <code>tripal_get_default_cv([table name], [field name])</code> which will return
  119. an object describing the set default controlled vocabulary or call
  120. <code>tripal_get_cvterm_default_select_options([table name], [field name], [field friendly name])</code>
  121. if you would like an array of options for use in a select or autocomplete form element.</li></ol>
  122. ',
  123. TRIPAL_NOTICE,
  124. ['return_html' => TRUE]
  125. );
  126. $form['new']['instructions'] = [
  127. '#type' => 'markup',
  128. '#markup' => $tripal_msg,
  129. ];
  130. $chado_tables = chado_get_table_names(TRUE);
  131. $chado_tables[0] = 'Select a Table';
  132. $form['new']['table'] = [
  133. '#type' => 'select',
  134. '#title' => 'Table Name',
  135. '#description' => 'The name of the table you would like to set a controlled vocabulary default for.',
  136. '#options' => $chado_tables,
  137. '#default_value' => 0,
  138. '#ajax' => [
  139. 'callback' => 'tripal_cv_admin_ajax_new_default_field_callback',
  140. 'wrapper' => 'new-default',
  141. ],
  142. ];
  143. $table = (isset($form_state['values']['new']['table'])) ? $form_state['values']['new']['table'] : FALSE;
  144. $columns = ['Select a Field'];
  145. if ($table) {
  146. // get the table description
  147. $table_desc = chado_get_schema($table);
  148. if (isset($table_desc['foreign keys']['cvterm'])) {
  149. foreach ($table_desc['foreign keys']['cvterm']['columns'] as $left_column => $right_column) {
  150. $columns[$left_column] = $left_column;
  151. }
  152. }
  153. }
  154. $form['new']['field'] = [
  155. '#type' => 'select',
  156. '#title' => 'Field Name',
  157. '#description' => 'The name of the field you would like to set a controlled vocabulary default for.',
  158. '#options' => $columns,
  159. '#default_value' => 0,
  160. ];
  161. $cvs[0] = 'Select a Vocabulary';
  162. $form['new']['vocabulary'] = [
  163. '#type' => 'select',
  164. '#title' => 'Vocabulary',
  165. '#description' => 'The default controlled vocabulary you would like to set for this field.',
  166. '#options' => $cvs,
  167. '#default_value' => 0,
  168. ];
  169. $form['new']['add_new'] = [
  170. '#type' => 'submit',
  171. '#value' => 'Set New Default',
  172. ];
  173. return $form;
  174. }
  175. function tripal_cv_admin_set_defaults_form_submit($form, $form_state) {
  176. if ($form_state['triggering_element']['#value'] == 'Update Defaults') {
  177. foreach ($form_state['values']['settings']['existing'] as $default_cv) {
  178. if (!empty($default_cv['vocabulary'])) {
  179. tripal_set_default_cv(
  180. $default_cv['table_name'],
  181. $default_cv['field_name'],
  182. '', // We are passing in the cv_id so we don't need the name
  183. $default_cv['vocabulary']
  184. );
  185. }
  186. }
  187. }
  188. if ($form_state['triggering_element']['#value'] == 'Set New Default') {
  189. if (!empty($form_state['values']['new']['vocabulary'])) {
  190. tripal_set_default_cv(
  191. $form_state['values']['new']['table'],
  192. $form_state['values']['new']['field'],
  193. '', // We are passing in the cv_id so we don't need the name
  194. $form_state['values']['new']['vocabulary']
  195. );
  196. }
  197. }
  198. }
  199. function tripal_cv_admin_ajax_new_default_field_callback($form, $form_state) {
  200. return $form['new'];
  201. }
  202. /**
  203. *
  204. * @param unknown $variables
  205. */
  206. function theme_tripal_cv_admin_set_defaults_form($variables) {
  207. $element = $variables['element'];
  208. $header = [
  209. 'table_name' => ['data' => t('Table Name'), 'width' => '20%'],
  210. 'field_name' => ['data' => t('Field Name'), 'width' => '20%'],
  211. 'vocabulary' => ['data' => t('Default Vocabulary'), 'width' => '30%'],
  212. 'actions' => ['data' => t('Actions'), 'width' => '30%'],
  213. ];
  214. $rows = [];
  215. foreach ($element['settings']['existing'] as $key => $value) {
  216. if (is_numeric($key)) {
  217. // $action_links = '<ul class="links inline">';
  218. // if (!empty($value['view-terms'])) {
  219. // $action_links .= '<li>' . drupal_render($value['view-terms']) . '</li>';
  220. // }
  221. // if (!empty($value['add-new-term'])) {
  222. // $action_links .= '<li>' . drupal_render($value['add-new-term']) . '</li>';
  223. // }
  224. // $action_links .= '</li></ul>';
  225. $rows[] = [
  226. drupal_render($value['table_name-display']),
  227. drupal_render($value['field_name-display']),
  228. drupal_render($value['vocabulary']),
  229. $action_links,
  230. ];
  231. }
  232. }
  233. $settings_table = theme('table', [
  234. 'header' => $header,
  235. 'rows' => $rows,
  236. ]);
  237. $element['settings']['existing'] = [
  238. '#type' => 'markup',
  239. '#markup' => $settings_table,
  240. ];
  241. // TODO: I believe rendering of the form should not happen here. But rather
  242. // the form should be returned as is. This way other modules can have access
  243. // to the form elements via the hook_form_alter. Rather, there should
  244. // be a theme function for the form where the rendering in the table
  245. // should occur. See the tripal_pub_importer_setup_form() for an exmaple.
  246. return drupal_render_children($element);
  247. }