tripal_cv.cv_defaults.inc 11 KB

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