tripal_core.form_elements.inc

  1. 2.x tripal_core/includes/tripal_core.form_elements.inc
  2. 3.x legacy/tripal_core/includes/tripal_core.form_elements.inc

Form elements used Various places in Tripal

File

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