tripal_cv.admin.inc

  1. 2.x tripal_cv/includes/tripal_cv.admin.inc
  2. 3.x legacy/tripal_cv/includes/tripal_cv.admin.inc

Provides administration of controlled vocabularies & their terms.

File

tripal_cv/includes/tripal_cv.admin.inc
View source
  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['settings']['submit'] = array(
  134. '#type' => 'submit',
  135. '#value' => 'Update Defaults'
  136. );
  137. // Adding new CV Defaults
  138. $form['new'] = array(
  139. '#type' => 'fieldset',
  140. '#title' => 'Add New Defaults',
  141. '#description' => 'You can use the form below to add a default controlled vocabulary
  142. for a table/field combination not available in the "Configured Defaults" section above.',
  143. '#collapsible' => TRUE,
  144. '#collapsed' => TRUE,
  145. '#tree' => TRUE,
  146. '#prefix' => '<div id="new-default">',
  147. '#suffix' => '</div>',
  148. );
  149. $tripal_msg = tripal_set_message(
  150. 'If you are developing a custom module and would like to use the Default Controlled
  151. Vocabulary API to flexibly set the controlled vocabulary, then it is better to set
  152. the default programatically rather than through this interface. To do this <ol>
  153. <li>Tell Tripal about the table/field you would like to set the default for. This
  154. is done by implementing hook_install() in your modules .install file and adding
  155. a call to <code>tripal_set_default_cv([table name], [field name], [cv name])</code> which
  156. will set the default for <code>[table name].[field name]</code> to the <code>[cv name]</code> controlled
  157. vocabulary. This vocabulary must already exist.</li>
  158. <li>Then everywhere in your module that you need to know the controlled vocabulary,
  159. you call <code>tripal_get_default_cv([table name], [field name])</code> which will return
  160. an object describing the set default controlled vocabulary or call
  161. <code>tripal_get_cvterm_default_select_options([table name], [field name], [field friendly name])</code>
  162. if you would like an array of options for use in a select or autocomplete form element.</li></ol>
  163. ',
  164. TRIPAL_NOTICE,
  165. array('return_html' => TRUE)
  166. );
  167. $form['new']['instructions'] = array(
  168. '#type' => 'markup',
  169. '#markup' => $tripal_msg
  170. );
  171. $chado_tables = chado_get_table_names(TRUE);
  172. $chado_tables[0] = 'Select a Table';
  173. $form['new']['table'] = array(
  174. '#type' => 'select',
  175. '#title' => 'Table Name',
  176. '#description' => 'The name of the table you would like to set a controlled vocabulary default for.',
  177. '#options' => $chado_tables,
  178. '#default_value' => 0,
  179. '#ajax' => array(
  180. 'callback' => 'tripal_cv_admin_ajax_new_default_field_callback',
  181. 'wrapper' => 'new-default',
  182. )
  183. );
  184. $table = (isset($form_state['values']['new']['table']))? $form_state['values']['new']['table'] : FALSE;
  185. $columns = array('Select a Field');
  186. if ($table) {
  187. // get the table description
  188. $table_desc = chado_get_schema($table);
  189. if (isset($table_desc['foreign keys']['cvterm'])) {
  190. foreach ($table_desc['foreign keys']['cvterm']['columns'] as $left_column => $right_column) {
  191. $columns[$left_column] = $left_column;
  192. }
  193. }
  194. }
  195. $form['new']['field'] = array(
  196. '#type' => 'select',
  197. '#title' => 'Field Name',
  198. '#description' => 'The name of the field you would like to set a controlled vocabulary default for.',
  199. '#options' => $columns,
  200. '#default_value' => 0
  201. );
  202. $cvs[0] = 'Select a Vocabulary';
  203. $form['new']['vocabulary'] = array(
  204. '#type' => 'select',
  205. '#title' => 'Vocabulary',
  206. '#description' => 'The default controlled vocabulary you would like to set for this field.',
  207. '#options' => $cvs,
  208. '#default_value' => 0
  209. );
  210. $form['new']['add_new'] = array(
  211. '#type' => 'submit',
  212. '#value' => 'Set New Default'
  213. );
  214. return $form;
  215. }
  216. function tripal_cv_admin_set_defaults_form_submit($form, $form_state) {
  217. if ($form_state['triggering_element']['#value'] == 'Update Defaults') {
  218. foreach ($form_state['values']['settings']['existing'] as $default_cv) {
  219. if (!empty($default_cv['vocabulary'])) {
  220. tripal_set_default_cv(
  221. $default_cv['table_name'],
  222. $default_cv['field_name'],
  223. '', // We are passing in the cv_id so we don't need the name
  224. $default_cv['vocabulary']
  225. );
  226. }
  227. }
  228. }
  229. if ($form_state['triggering_element']['#value'] == 'Set New Default') {
  230. if (!empty($form_state['values']['new']['vocabulary'])) {
  231. tripal_set_default_cv(
  232. $form_state['values']['new']['table'],
  233. $form_state['values']['new']['field'],
  234. '', // We are passing in the cv_id so we don't need the name
  235. $form_state['values']['new']['vocabulary']
  236. );
  237. }
  238. }
  239. }
  240. function tripal_cv_admin_ajax_new_default_field_callback($form, $form_state) {
  241. return $form['new'];
  242. }
  243. /**
  244. *
  245. * @param unknown $variables
  246. */
  247. function theme_tripal_cv_admin_set_defaults_form($variables) {
  248. $element = $variables['element'];
  249. $header = array(
  250. 'table_name' => array('data' => t('Table Name'), 'width' => '20%'),
  251. 'field_name' => array('data' => t('Field Name'), 'width' => '20%'),
  252. 'vocabulary' => array('data' => t('Default Vocabulary'), 'width' => '30%'),
  253. 'actions' => array('data' => t('Actions'), 'width' => '30%'),
  254. );
  255. $rows = array();
  256. foreach ($element['settings']['existing'] as $key => $value) {
  257. if (is_numeric($key)) {
  258. $action_links = '<ul class="links inline">';
  259. if (!empty($value['view-terms'])) {
  260. $action_links .= '<li>' . drupal_render($value['view-terms']) . '</li>';
  261. }
  262. if (!empty($value['add-new-term'])) {
  263. $action_links .= '<li>' . drupal_render($value['add-new-term']) . '</li>';
  264. }
  265. $action_links .= '</li></ul>';
  266. $rows[] = array(
  267. drupal_render($value['table_name-display']),
  268. drupal_render($value['field_name-display']),
  269. drupal_render($value['vocabulary']),
  270. $action_links
  271. );
  272. }
  273. }
  274. $settings_table = theme('table', array(
  275. 'header' => $header,
  276. 'rows' => $rows
  277. ));
  278. $element['settings']['existing'] = array(
  279. '#type' => 'markup',
  280. '#markup' => $settings_table,
  281. );
  282. // TODO: I believe rendering of the form should not happen here. But rather
  283. // the form should be returned as is. This way other modules can have access
  284. // to the form elements via the hook_form_alter. Rather, there should
  285. // be a theme function for the form where the rendering in the table
  286. // should occur. See the tripal_pub_importer_setup_form() for an exmaple.
  287. return drupal_render_children($element);
  288. }