form_elements.inc 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. <?php
  2. /**
  3. * @file
  4. * Form elements used for tripal views
  5. */
  6. /**
  7. * Register form elements
  8. */
  9. function tripal_core_element_info() {
  10. $elements = array();
  11. $elements['file_upload_combo'] = array(
  12. '#input' => TRUE,
  13. '#process' => array('expand_file_upload_combo'),
  14. '#value_callback' =>'file_upload_combo_value_callback',
  15. '#theme' => 'theme_file_upload_combo',
  16. '#theme_wrappers' => array('form_element'),
  17. );
  18. $elements['sequence_combo'] = array(
  19. '#input' => TRUE,
  20. '#process' => array('expand_sequence_combo'),
  21. '#value_callback' => 'sequence_combo_value_callback',
  22. '#theme' => 'theme_sequence_combo',
  23. '#theme_wrappers' => array('form_element'),
  24. '#tree' => TRUE,
  25. );
  26. return $elements;
  27. }
  28. /**
  29. * Upload File and keep track of previously uploaded files
  30. * Form element description
  31. */
  32. function expand_file_upload_combo($element, $form_state, $complete_form) {
  33. // set the default values for each field
  34. if (empty($element['#value'])) {
  35. $element['#value'] = array(
  36. 'items' => '',
  37. 'items_file' => '',
  38. 'file_path' => '',
  39. );
  40. }
  41. $element['#tree'] = TRUE;
  42. // add items text area element
  43. $parents = $element['#parents'];
  44. $parents[] = 'items';
  45. $element['items'] = array(
  46. '#type' => 'textarea',
  47. '#default_value' => (isset($element['#value']['items'])) ? $element['#value']['items'] : '',
  48. );
  49. // add file upload element
  50. $parents = $element['#parents'];
  51. $parents[] = 'items_file';
  52. $element['items_file'] = array(
  53. '#type' => 'file',
  54. '#title' => 'File upload',
  55. '#default_value' => (isset($element['#value']['items_file'])) ? $element['#value']['items_file'] : '',
  56. );
  57. // add hidden elelment
  58. $parents = $element['#parents'];
  59. $parents[] = 'file_path';
  60. $element['file_path'] = array(
  61. '#type' => 'hidden',
  62. '#default_value' => (isset($element['#value']['file_path'])) ? $element['#value']['file_path'] : '',
  63. );
  64. return $element;
  65. }
  66. /**
  67. * Theme the file upload combo form element
  68. */
  69. function theme_file_upload_combo($variables) {
  70. $element = $variables['element'];
  71. $output = '';
  72. $output .= drupal_render($element['items']);
  73. $output .= " "; // This space forces our fields to have a little room in between.
  74. $output .= drupal_render($element['items_file']);
  75. $output .= " "; // This space forces our fields to have a little room in between.
  76. $output .= drupal_render($element['file_path']);
  77. return $output;
  78. }
  79. /**
  80. * Validate all content passed into the file upload combo form element
  81. */
  82. function file_upload_combo_value_callback($element, $input = FALSE, &$form_state) {
  83. $values = array();
  84. if ($input == FALSE) {
  85. if (!empty($element['#default_value'])) {
  86. return $element['#default_value'];
  87. }
  88. else {
  89. return;
  90. }
  91. }
  92. // get the items in the textbox
  93. $items = $input['items'];
  94. if ($items) {
  95. // split on new line or comma
  96. $vals = preg_split("/[\n,]+/", $items);
  97. // iterate through the values and trim surrounding space
  98. foreach ($vals as $i => $value) {
  99. $values[] = trim($value);
  100. }
  101. }
  102. // merge any items from the file upload
  103. $file = file_save_upload($element['#name'], array());
  104. if ($file) {
  105. $file_path = $file->uri;
  106. $input['file_path'] = $file_path;
  107. // we need to add our file path to the $_GET element as if it were
  108. // submitted along with the rest of the form
  109. $_GET[$element['#name']]['file_path'] = $file_path;
  110. $fh = fopen($file_path, 'r');
  111. while ($line = fgets($fh)) {
  112. $items = trim($line);
  113. // split on new line or comma
  114. $vals = preg_split("/[\n,]+/", $items);
  115. // iterate through the values and trim surrounding space
  116. foreach ($vals as $i => $value) {
  117. $values[] = trim($value);
  118. }
  119. }
  120. fclose($fh);
  121. }
  122. // add a new 'items_array' element that contains the array of
  123. // submitted items from both the textbox and the input file
  124. $input['items_array'] = $values;
  125. return $input;
  126. }
  127. /**
  128. * Form element description
  129. */
  130. function expand_sequence_combo($element, $form_state, $complete_form) {
  131. // set the default values for each field
  132. if (empty($element['#value'])) {
  133. $element['#value'] = array(
  134. 'upstream' => '',
  135. 'downstream' => '',
  136. );
  137. }
  138. $element['#tree'] = TRUE;
  139. // add the upstream box
  140. $parents = $element['#parents'];
  141. $parents[] = 'upstream';
  142. $element['upstream'] = array(
  143. '#type' => 'textfield',
  144. '#title' => t('Get Upstream Bases'),
  145. '#description' => t('Specify the number of upstream bases to include in the sequnce'),
  146. '#default_value' => $element['#value']['upstream'],
  147. );
  148. // add the downstream box
  149. $parents = $element['#parents'];
  150. $parents[] = 'downstream';
  151. $element['downstream'] = array(
  152. '#type' => 'textfield',
  153. '#prefix' => '<br>',
  154. '#title' => t('Get Downstream Bases'),
  155. '#description' => t('Specify the number of downstream bases to include in the sequnce'),
  156. '#default_value' => $element['#value']['downstream'],
  157. );
  158. return $element;
  159. }
  160. /**
  161. * Validate all content passed into the sequence combo form element
  162. * D7 @todo: test/fix this callback
  163. */
  164. function sequence_combo_value_callback($element, $input = FALSE, &$form_state) {
  165. $upstream = $form['values'][$element['#name']]['upstream'];
  166. $downstream = $form['values'][$element['#name']]['downstream'];
  167. if ($upstream < 0) {
  168. form_set_error($element['#name'], 'Please provide a positive number for upstream bases');
  169. }
  170. if ($upstream and !preg_match('/^\d+$/', $upstream)) {
  171. form_set_error($element['#name'], 'Please provide a decimal number for upstream bases');
  172. }
  173. if ($downstream < 0) {
  174. form_set_error($element['#name'], 'Please provide a positive number for downstream bases');
  175. }
  176. if ($downstream and !preg_match('/^\d+$/', $downstream)) {
  177. form_set_error($element['#name'], 'Please provide a decimal number for downstream bases');
  178. }
  179. }
  180. /**
  181. * Theme the file sequence form element
  182. */
  183. function theme_sequence_combo($variables) {
  184. $element = $variables['element'];
  185. $output = '';
  186. $output .= drupal_render($element['upstream']);
  187. $output .= " "; // This space forces our fields to have a little room in between.
  188. $output .= drupal_render($element['downstream']);
  189. return $output;
  190. }