tripal_views_form_elements.inc 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. /**
  3. * @file
  4. * Form elements used for tripal views
  5. */
  6. /**
  7. * Register form elements
  8. */
  9. function tripal_views_elements() {
  10. $type['file_upload_combo'] = array(
  11. '#input' => TRUE,
  12. '#process' => array('expand_file_upload_combo'),
  13. '#element_validate' => array('file_upload_combo_validate'),
  14. );
  15. $type['sequence_combo'] = array(
  16. '#input' => TRUE,
  17. '#process' => array('expand_sequence_combo'),
  18. '#element_validate' => array('sequence_combo_validate'),
  19. );
  20. return $type;
  21. }
  22. /**
  23. * Upload File and keep track of previously uploaded files
  24. * Form element description
  25. */
  26. function expand_file_upload_combo($element, $edit, $form_state, $complete_form) {
  27. // set the default values for each field
  28. if (empty($element['#value'])) {
  29. $element['#value'] = array(
  30. 'items' => '',
  31. 'items_file' => '',
  32. 'file_path' => '',
  33. );
  34. }
  35. $element['#tree'] = TRUE;
  36. // add items text area element
  37. $parents = $element['#parents'];
  38. $parents[] = 'items';
  39. $element['items'] = array(
  40. '#type' => 'textarea',
  41. '#default_value' => $element['#value']['items'],
  42. );
  43. // add file upload element
  44. $parents = $element['#parents'];
  45. $parents[] = 'items_file';
  46. $element['items_file'] = array(
  47. '#type' => 'file',
  48. '#title' => 'File upload',
  49. '#default_value' => $element['#value']['items_file'],
  50. );
  51. // add hidden elelment
  52. $parents = $element['#parents'];
  53. $parents[] = 'file_path';
  54. $element['file_path'] = array(
  55. '#type' => 'hidden',
  56. '#default_value' => $element['#value']['file_path'],
  57. );
  58. return $element;
  59. }
  60. /**
  61. * Form element description
  62. */
  63. function expand_sequence_combo($element, $edit, $form_state, $complete_form) {
  64. // set the default values for each field
  65. if (empty($element['#value'])) {
  66. $element['#value'] = array(
  67. 'upstream' => '',
  68. 'downstream' => '',
  69. );
  70. }
  71. $element['#tree'] = TRUE;
  72. // add the upstream box
  73. $parents = $element['#parents'];
  74. $parents[] = 'upstream';
  75. $element['upstream'] = array(
  76. '#type' => 'textfield',
  77. '#title' => t('Get Upstream Bases'),
  78. '#description' => t('Specify the number of upstream bases to include in the sequnce'),
  79. '#default_value' => $element['#value']['upstream'],
  80. );
  81. // add the downstream box
  82. $parents = $element['#parents'];
  83. $parents[] = 'downstream';
  84. $element['downstream'] = array(
  85. '#type' => 'textfield',
  86. '#prefix' => '<br>',
  87. '#title' => t('Get Downstream Bases'),
  88. '#description' => t('Specify the number of downstream bases to include in the sequnce'),
  89. '#default_value' => $element['#value']['downstream'],
  90. );
  91. return $element;
  92. }
  93. /**
  94. * Theme the file upload combo form element
  95. */
  96. function theme_file_upload_combo($element) {
  97. return theme('form_element', $element, '<div class="container-inline">' . $element['#children'] . '</div>');
  98. }
  99. /**
  100. * Theme the file sequence form element
  101. */
  102. function theme_sequence_combo($element) {
  103. return theme('form_element', $element, '<div class="container-inline">' . $element['#children'] . '</div>');
  104. }
  105. /**
  106. * Validate all content passed into the file upload combo form element
  107. */
  108. function file_upload_combo_validate($element, &$form) {
  109. $file = file_save_upload($element['#name'], array());
  110. if ($file) {
  111. $form['values'][$element['#name']]['file_path'] = $file->filepath;
  112. // we need to add our file path to the $_GET element as if it were
  113. // submitted along with the rest of the form
  114. $_GET[$element['#name']]['file_path'] = $file->filepath;
  115. }
  116. }
  117. /**
  118. * Validate all content passed into the sequence combo form element
  119. */
  120. function sequence_combo_validate($element, &$form) {
  121. $upstream = $form['values'][$element['#name']]['upstream'];
  122. $downstream = $form['values'][$element['#name']]['downstream'];
  123. if ($upstream < 0) {
  124. form_set_error($element['#name'], 'Please provide a positive number for upstream bases');
  125. }
  126. if ($upstream and !preg_match('/^\d+$/',$upstream)) {
  127. form_set_error($element['#name'], 'Please provide a decimal number for upstream bases');
  128. }
  129. if ($downstream < 0) {
  130. form_set_error($element['#name'], 'Please provide a positive number for downstream bases');
  131. }
  132. if ($downstream and !preg_match('/^\d+$/',$downstream)) {
  133. form_set_error($element['#name'], 'Please provide a decimal number for downstream bases');
  134. }
  135. }