tripal_entities.admin.inc 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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')//ORDER BY created
  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
  90. *
  91. * @param array $form
  92. * @param array $form_state
  93. *
  94. * @return
  95. * An array describing this form to the Form API.
  96. */
  97. function tripal_entities_admin_add_type_form($form, &$form_state) {
  98. $term_name = '';
  99. $num_terms = 0;
  100. $cv_id = '';
  101. // Set defaults using the form state.
  102. if (array_key_exists('input', $form_state)) {
  103. if (array_key_exists('term_name', $form_state['input'])) {
  104. $term_name = $form_state['input']['term_name'];
  105. }
  106. if (array_key_exists('cv_id', $form_state['input'])) {
  107. $cv_id = $form_state['input']['cv_id'];
  108. }
  109. // If a term and cv_id are provided then we can look for the term using
  110. // both and we should find a unique term. If only ther term is provided
  111. // we can still look for a unique term but there must only be one.
  112. if ($term_name and !$cv_id) {
  113. $match = array(
  114. 'name' => $term_name,
  115. );
  116. }
  117. else {
  118. $match = array(
  119. 'name' => $term_name,
  120. 'cv_id' => $cv_id,
  121. );
  122. }
  123. $terms = tripal_get_cvterm($match, array('return_array' => TRUE));
  124. $num_terms = count($terms);
  125. }
  126. // If no term has been selected yet then provide the auto complete field.
  127. if ($num_terms == 0) {
  128. $form['term_name'] = array(
  129. '#title' => t('Content Type'),
  130. '#type' => 'textfield',
  131. '#description' => t("The content type must be the name of a term in
  132. a controlled vocabulary and the controlled vocabulary should
  133. already be loaded into Tripal. For example, to create a content
  134. type for storing 'genes', use the 'gene' term from the
  135. Sequence Ontology (SO)."),
  136. '#required' => TRUE,
  137. '#default_value' => $term_name,
  138. '#autocomplete_path' => "admin/tripal/chado/tripal_cv/cvterm/auto_name/$cv_id",
  139. );
  140. }
  141. else {
  142. $form['term_name'] = array(
  143. '#type' => 'hidden',
  144. '#value' => $term_name,
  145. );
  146. }
  147. // If the term belongs to more than one vocabulary then add additional fields
  148. // to let the user select the vocabulary.
  149. if ($num_terms > 1) {
  150. $cvs = array();
  151. foreach ($terms as $term) {
  152. $cvs[$term->cv_id->cv_id] = 'Vocabulary: <b>' . $term->cv_id->name . '</b> (' . $term->cv_id->definition . ')<br>' . $term->name . ': ' . $term->definition;
  153. }
  154. $form['cv_id'] = array(
  155. '#type' => 'radios',
  156. '#title' => t('Select the appropriate vocabulary'),
  157. '#options' => $cvs,
  158. );
  159. }
  160. // Add in the button for the cases of no terms or too many.
  161. $form['select_button'] = array(
  162. '#type' => 'submit',
  163. '#value' => t('Use this term'),
  164. '#name' => 'select_cvterm'
  165. );
  166. return $form;
  167. }
  168. /**
  169. * Implements hook_validate() for the tripal_entities_admin_publish_form.
  170. *
  171. */
  172. function tripal_entities_admin_add_type_form_validate($form, &$form_state) {
  173. if (array_key_exists('clicked_button', $form_state) and
  174. $form_state['clicked_button']['#name'] =='select_cvterm') {
  175. // First, make sure the term is unique. If not then we can't check it.
  176. $term_name = NULL;
  177. $cv_id = NULL;
  178. $cvterm = NULL;
  179. if (array_key_exists('term_name', $form_state['values'])) {
  180. $term_name = $form_state['input']['term_name'];
  181. }
  182. if (array_key_exists('cv_id', $form_state['input'])) {
  183. $cv_id = $form_state['input']['cv_id'];
  184. }
  185. // If a term and cv_id are provided then we can look for the term using
  186. // both and we should find a unique term. If only ther term is provided
  187. // we can still look for a unique term but there must only be one.
  188. if ($term_name and !$cv_id) {
  189. $match = array(
  190. 'name' => $term_name,
  191. );
  192. }
  193. else {
  194. $match = array(
  195. 'name' => $term_name,
  196. 'cv_id' => $cv_id,
  197. );
  198. }
  199. $terms = tripal_get_cvterm($match, array('return_array' => TRUE));
  200. $form_state['storage']['terms'] = $terms;
  201. // If we do not have any terms then the term provided by the user does not
  202. // exists and we need to provide an error message.
  203. if (count($terms) == 0) {
  204. form_set_error('term_name', t('The term does not exist in this database.'));
  205. }
  206. // If we have more than one term then we need to set an error so that the
  207. // form can provide a list of vocabularies to select from.
  208. if (count($terms) > 1) {
  209. form_set_error('term_name', t('The term is not unique. A list of vocabularies
  210. that contain this term. Please select the most appropriate vocabulary.'));
  211. }
  212. }
  213. }
  214. /**
  215. * Implements hook_submit() for the tripal_entities_admin_publish_form.
  216. *
  217. */
  218. function tripal_entities_admin_add_type_form_submit($form, &$form_state) {
  219. if ($form_state['clicked_button']['#name'] =='select_cvterm') {
  220. $cvterm = $form_state['storage']['terms'][0];
  221. $bundle_id = 'bio-data_' . $cvterm->cvterm_id;
  222. // Before we try to add this type, check to see if it already exists
  223. // as a bundle.
  224. $einfo = entity_get_info('TripalEntity');
  225. if (!in_array($bundle_id, array_keys($einfo['bundles']))) {
  226. $error = '';
  227. $success = tripal_create_entity_type($cvterm, $error);
  228. if (!$success) {
  229. drupal_set_message($error, 'error');
  230. $form_state['redirect'] = "admin/structure/bio-data";
  231. }
  232. else {
  233. drupal_set_message('New biological data type created. Fields are added automatically to this type.');
  234. $form_state['redirect'] = "admin/structure/bio-data";
  235. }
  236. }
  237. else {
  238. drupal_set_message('This type already exists.', 'warning');
  239. }
  240. }
  241. }