tripal_entities.admin.inc 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <?php
  2. /**
  3. * Provide a data listing for tripal entites (ie: biological data).
  4. *
  5. * This function is a callback in a menu item which is set in the
  6. * TripalEntityUIController class.
  7. */
  8. function tripal_entities_content_view() {
  9. // Retrieve our data listing form and render it.
  10. $form = drupal_get_form('tripal_entities_content_overview_form');
  11. $output = drupal_render($form);
  12. // Set the breadcrumb.
  13. $breadcrumb = array();
  14. $breadcrumb[] = l('Home', '<front>');
  15. $breadcrumb[] = l('Administration', 'admin');
  16. drupal_set_breadcrumb($breadcrumb);
  17. return $output;
  18. }
  19. /**
  20. * Display a listing of Tripal entities.
  21. *
  22. * @TODO Filters and bulk operations needed to be added to this form.
  23. *
  24. * @param array $form
  25. * @param array $form_state
  26. * @return
  27. * A form array describing this listing to the Form API.
  28. */
  29. function tripal_entities_content_overview_form($form, &$form_state) {
  30. // Set the title to be informative (defaults to content for some reason).
  31. drupal_set_title('Biological Data');
  32. // Retrieve a pages list of all tripal entitles (ie: biological data).
  33. // This will return the 25 most recently created entities.
  34. $entities = db_select('tripal_entity', 'td')
  35. ->fields('td')
  36. ->orderBy('created', 'DESC')
  37. ->range(0,25)
  38. ->execute();
  39. $headers = array('Title', 'Vocabulary', 'Term', 'Author', 'Status', 'Updated', 'Operations');
  40. $rows = array();
  41. // For each entity retrieved add a row to the data listing.
  42. while ($entity = $entities->fetchObject()) {
  43. // Retrieve details about the term this entity is based on.
  44. $cvterm = tripal_get_cvterm(array('cvterm_id' => $entity->cvterm_id));
  45. // Retrieve details about the user who created this data.
  46. $author = user_load($entity->uid);
  47. // Add information to the table.
  48. $rows[] = array(
  49. l($entity->title, 'bio-data/' . $entity->id),
  50. $cvterm->cv_id->name . ' (' . $cvterm->dbxref_id->db_id->name . ')',
  51. $cvterm->name,
  52. l($author->name, 'user/' . $entity->uid),
  53. $entity->status == 1 ? 'published' : 'unpublished',
  54. format_date($entity->changed, 'short'),
  55. l('edit', 'bio-data/' . $entity->id . '/edit') . '&nbsp;&nbsp;' .
  56. l('delete', 'bio-data/' . $entity->id . '/delete')
  57. );
  58. }
  59. // If there are no entites created yet then add a message to the table to
  60. // provide guidance to administrators.
  61. if (empty($rows)) {
  62. $rows[] = array(
  63. array(
  64. 'data' => t('No biological data available.'),
  65. 'colspan' => 7
  66. )
  67. );
  68. }
  69. // Render the data listing.
  70. $table_vars = array(
  71. 'header' => $headers,
  72. 'rows' => $rows,
  73. 'attributes' => array(),
  74. 'sticky' => TRUE,
  75. 'caption' => '',
  76. 'colgroups' => array(),
  77. 'empty' => '',
  78. );
  79. $form['results'] = array(
  80. '#type' => 'markup',
  81. '#markup' => theme('table', $table_vars),
  82. );
  83. return $form;
  84. }
  85. /**
  86. * Form for creating tripal data types.
  87. *
  88. * This form is available on the menu at Admin >> Structure >> Biological Data
  89. * Types. It requires that a module implmennt the vocabulary storage. Tripal
  90. * knows which vocabulary storage methods are available when a module
  91. * implements the hook_vocab_storage_info() hook.
  92. *
  93. */
  94. function tripal_entities_admin_add_type_form($form, &$form_state) {
  95. // TODO: we need some sort of administrative interface that lets the user
  96. // switch to the desired vocabulary type. For now, we'll just use the
  97. // first one in the list.
  98. $stores = module_invoke_all('vocab_storage_info');
  99. if (is_array($stores) and count($stores) > 0) {
  100. $keys = array_keys($stores);
  101. $module = $stores[$keys[0]]['module'];
  102. $function = $module . '_vocab_select_term_form';
  103. if (function_exists($function)) {
  104. $form = $function($form, $form_state);
  105. }
  106. }
  107. else {
  108. tripal_set_message('A storage backend is not enabled for managing
  109. the vocabulary terms used to create content. Please enable
  110. a module that supports storage of vocabualary terms (e.g. tripal_chado)
  111. and return to create new Tripal content types.', TRIPAL_NOTICE);
  112. }
  113. return $form;
  114. }
  115. /**
  116. * Implements hook_validate() for the tripal_entities_admin_add_type_form.
  117. *
  118. */
  119. function tripal_entities_admin_add_type_form_validate($form, &$form_state) {
  120. // TODO: we need some sort of administrative interface that lets the user
  121. // switch to the desired vocabulary type. For now, we'll just use the
  122. // first one in the list.
  123. $stores = module_invoke_all('vocab_storage_info');
  124. if (is_array($stores) and count($stores) > 0) {
  125. $keys = array_keys($stores);
  126. $module = $stores[$keys[0]]['module'];
  127. $function = $module . '_vocab_select_term_form_validate';
  128. if (function_exists($function)) {
  129. $function($form, $form_state);
  130. }
  131. }
  132. }
  133. /**
  134. * Implements hook_submit() for the tripal_entities_admin_add_type_form.
  135. *
  136. * The storage backend must set the
  137. *
  138. */
  139. function tripal_entities_admin_add_type_form_submit($form, &$form_state) {
  140. $namespace = '';
  141. $term_id = '';
  142. if (array_key_exists('storage', $form_state)) {
  143. $storage = $form_state['storage'];
  144. $namespace = array_key_exists('namespace', $storage) ? $storage['namespace'] : '';
  145. $term_id = array_key_exists('term_id', $storage) ? $storage['term_id'] : '';
  146. $term_name = array_key_exists('term_name', $storage) ? $storage['term_name'] : '';
  147. // Before we try to add this type, check to see if it already exists
  148. // as a bundle.
  149. $term = tripal_load_term_entity($namespace, $term_id);
  150. if (!$term) {
  151. $error = '';
  152. $success = tripal_create_bundle($namespace, $term_id, $term_name, $error);
  153. if (!$success) {
  154. drupal_set_message($error, 'error');
  155. $form_state['redirect'] = "admin/structure/bio-data";
  156. }
  157. else {
  158. drupal_set_message('New biological data type created. Fields are added automatically to this type.');
  159. $form_state['redirect'] = "admin/structure/bio-data";
  160. }
  161. }
  162. else {
  163. drupal_set_message('This type already exists.', 'warning');
  164. }
  165. }
  166. }