form_elements.inc 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. fclose($fh);
  100. }
  101. // add a new 'items_array' element that contains the array of
  102. // submitted items from both the textbox and the input file
  103. $form['values'][$element['#name']]['items_array'] = $values;
  104. }
  105. /**
  106. * Form element description
  107. */
  108. function expand_sequence_combo($element, $edit, $form_state, $complete_form) {
  109. // set the default values for each field
  110. if (empty($element['#value'])) {
  111. $element['#value'] = array(
  112. 'upstream' => '',
  113. 'downstream' => '',
  114. );
  115. }
  116. $element['#tree'] = TRUE;
  117. // add the upstream box
  118. $parents = $element['#parents'];
  119. $parents[] = 'upstream';
  120. $element['upstream'] = array(
  121. '#type' => 'textfield',
  122. '#title' => t('Get Upstream Bases'),
  123. '#description' => t('Specify the number of upstream bases to include in the sequnce'),
  124. '#default_value' => $element['#value']['upstream'],
  125. );
  126. // add the downstream box
  127. $parents = $element['#parents'];
  128. $parents[] = 'downstream';
  129. $element['downstream'] = array(
  130. '#type' => 'textfield',
  131. '#prefix' => '<br>',
  132. '#title' => t('Get Downstream Bases'),
  133. '#description' => t('Specify the number of downstream bases to include in the sequnce'),
  134. '#default_value' => $element['#value']['downstream'],
  135. );
  136. return $element;
  137. }
  138. /**
  139. * Validate all content passed into the sequence combo form element
  140. */
  141. function sequence_combo_validate($element, &$form) {
  142. $upstream = $form['values'][$element['#name']]['upstream'];
  143. $downstream = $form['values'][$element['#name']]['downstream'];
  144. if ($upstream < 0) {
  145. form_set_error($element['#name'], 'Please provide a positive number for upstream bases');
  146. }
  147. if ($upstream and !preg_match('/^\d+$/', $upstream)) {
  148. form_set_error($element['#name'], 'Please provide a decimal number for upstream bases');
  149. }
  150. if ($downstream < 0) {
  151. form_set_error($element['#name'], 'Please provide a positive number for downstream bases');
  152. }
  153. if ($downstream and !preg_match('/^\d+$/', $downstream)) {
  154. form_set_error($element['#name'], 'Please provide a decimal number for downstream bases');
  155. }
  156. }
  157. /**
  158. * Theme the file sequence form element
  159. */
  160. function theme_sequence_combo($element) {
  161. return theme('form_element', $element, '<div class="container-inline">' . $element['#children'] . '</div>');
  162. }