form_elements.inc 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <?php
  2. /**
  3. * @file
  4. * Form elements used for tripal views
  5. */
  6. /**
  7. * Register form elements
  8. */
  9. function tripal_core_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. * Theme the file upload combo form element
  62. */
  63. function theme_file_upload_combo($element) {
  64. return theme('form_element', $element, '<div class="container-inline">' . $element['#children'] . '</div>');
  65. }
  66. /**
  67. * Validate all content passed into the file upload combo form element
  68. */
  69. function file_upload_combo_validate($element, &$form) {
  70. $values = array();
  71. // get the items in the textbox
  72. $items = $form['values'][$element['#name']]['items'];
  73. if ($items) {
  74. // split on new line or comma
  75. $vals = preg_split("/[\n,]+/", $items);
  76. // iterate through the values and trim surrounding space
  77. foreach ($vals as $i => $value) {
  78. $values[] = trim($value);
  79. }
  80. }
  81. // merge any items from the file upload
  82. $file = file_save_upload($element['#name'], array());
  83. if ($file) {
  84. $file_path = $file->filepath;
  85. $form['values'][$element['#name']]['file_path'] = $file_path;
  86. // we need to add our file path to the $_GET element as if it were
  87. // submitted along with the rest of the form
  88. $_GET[$element['#name']]['file_path'] = $file_path;
  89. $fh = fopen($file_path, 'r');
  90. while ($line = fgets($fh)) {
  91. $items = trim($line);
  92. // split on new line or comma
  93. $vals = preg_split("/[\n,]+/", $items);
  94. // iterate through the values and trim surrounding space
  95. foreach ($vals as $i => $value) {
  96. $values[] = trim($value);
  97. }
  98. }
  99. }
  100. // add a new 'items_array' element that contains the array of
  101. // submitted items from both the textbox and the input file
  102. $form['values'][$element['#name']]['items_array'] = $values;
  103. }
  104. /**
  105. * Form element description
  106. */
  107. function expand_sequence_combo($element, $edit, $form_state, $complete_form) {
  108. // set the default values for each field
  109. if (empty($element['#value'])) {
  110. $element['#value'] = array(
  111. 'upstream' => '',
  112. 'downstream' => '',
  113. );
  114. }
  115. $element['#tree'] = TRUE;
  116. // add the upstream box
  117. $parents = $element['#parents'];
  118. $parents[] = 'upstream';
  119. $element['upstream'] = array(
  120. '#type' => 'textfield',
  121. '#title' => t('Get Upstream Bases'),
  122. '#description' => t('Specify the number of upstream bases to include in the sequnce'),
  123. '#default_value' => $element['#value']['upstream'],
  124. );
  125. // add the downstream box
  126. $parents = $element['#parents'];
  127. $parents[] = 'downstream';
  128. $element['downstream'] = array(
  129. '#type' => 'textfield',
  130. '#prefix' => '<br>',
  131. '#title' => t('Get Downstream Bases'),
  132. '#description' => t('Specify the number of downstream bases to include in the sequnce'),
  133. '#default_value' => $element['#value']['downstream'],
  134. );
  135. return $element;
  136. }
  137. /**
  138. * Validate all content passed into the sequence combo form element
  139. */
  140. function sequence_combo_validate($element, &$form) {
  141. $upstream = $form['values'][$element['#name']]['upstream'];
  142. $downstream = $form['values'][$element['#name']]['downstream'];
  143. if ($upstream < 0) {
  144. form_set_error($element['#name'], 'Please provide a positive number for upstream bases');
  145. }
  146. if ($upstream and !preg_match('/^\d+$/', $upstream)) {
  147. form_set_error($element['#name'], 'Please provide a decimal number for upstream bases');
  148. }
  149. if ($downstream < 0) {
  150. form_set_error($element['#name'], 'Please provide a positive number for downstream bases');
  151. }
  152. if ($downstream and !preg_match('/^\d+$/', $downstream)) {
  153. form_set_error($element['#name'], 'Please provide a decimal number for downstream bases');
  154. }
  155. }
  156. /**
  157. * Theme the file sequence form element
  158. */
  159. function theme_sequence_combo($element) {
  160. return theme('form_element', $element, '<div class="container-inline">' . $element['#children'] . '</div>');
  161. }