tripal.importer.inc 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. <?php
  2. /**
  3. * Build the form for a TripalImporter implementation.
  4. */
  5. function tripal_get_importer_form($form, &$form_state, $class) {
  6. tripal_load_include_importer_class($class);
  7. $form['importer_class'] = array(
  8. '#type' => 'value',
  9. '#value' => $class,
  10. );
  11. if ((array_key_exists('file_upload', $class::$methods) and $class::$methods['file_upload'] == TRUE) or
  12. (array_key_exists('file_local', $class::$methods) and $class::$methods['file_local'] == TRUE) or
  13. (array_key_exists('file_remote', $class::$methods) and $class::$methods['file_remote'] == TRUE)) {
  14. $form['file'] = array(
  15. '#type' => 'fieldset',
  16. '#title' => t($class::$upload_title),
  17. '#description' => t($class::$upload_description)
  18. );
  19. }
  20. if (array_key_exists('file_upload', $class::$methods) and $class::$methods['file_upload'] == TRUE) {
  21. $form['file']['file_upload']= array(
  22. '#type' => 'html5_file',
  23. '#title' => '',
  24. '#description' => 'Remember to click the "Upload" button below to send ' .
  25. 'your file to the server. This interface is capable of uploading very ' .
  26. 'large files. If you are disconnected you can return, reload the file and it ' .
  27. 'will resume where it left off. Once the file is uploaded the "Upload '.
  28. 'Progress" will indicate "Complete". If the file is already present on the server ' .
  29. 'then the status will quickly update to "Complete".',
  30. '#usage_type' => 'tripal_importer',
  31. '#usage_id' => 0,
  32. '#allowed_types' => $class::$file_types,
  33. '#cardinality' => $class::$cardinality,
  34. );
  35. }
  36. if (array_key_exists('file_local', $class::$methods) and $class::$methods['file_local'] == TRUE) {
  37. $form['file']['file_local']= array(
  38. '#title' => t('Server path'),
  39. '#type' => 'textfield',
  40. '#maxlength' => 5120,
  41. '#description' => t('If the file is local to the Tripal server please provide the full path here.'),
  42. );
  43. }
  44. if (array_key_exists('file_remote', $class::$methods) and $class::$methods['file_remote'] == TRUE) {
  45. $form['file']['file_remote']= array(
  46. '#title' => t('Remote path'),
  47. '#type' => 'textfield',
  48. '#maxlength' => 5102,
  49. '#description' => t('If the file is available via a remote URL please provide the full URL here. The file will be downloaded when the importer job is executed.'),
  50. );
  51. }
  52. if ($class::$use_analysis) {
  53. // get the list of analyses
  54. $sql = "SELECT * FROM {analysis} ORDER BY name";
  55. $org_rset = chado_query($sql);
  56. $analyses = array();
  57. $analyses[''] = '';
  58. while ($analysis = $org_rset->fetchObject()) {
  59. $analyses[$analysis->analysis_id] = "$analysis->name ($analysis->program $analysis->programversion, $analysis->sourcename)";
  60. }
  61. $form['analysis_id'] = array(
  62. '#title' => t('Analysis'),
  63. '#type' => t('select'),
  64. '#description' => t('Choose the analysis to which the uploaded data will be associated. ' .
  65. 'Why specify an analysis for a data load? All data comes from some place, even if ' .
  66. 'downloaded from a website. By specifying analysis details for all data imports it ' .
  67. 'provides provenance and helps end user to reproduce the data set if needed. At ' .
  68. 'a minimum it indicates the source of the data.'),
  69. '#required' => $class::$require_analysis,
  70. '#options' => $analyses,
  71. );
  72. }
  73. $importer = new $class();
  74. $element = array();
  75. $form['class_elements'] = $importer->form($element, $form_state);
  76. $form['button'] = array(
  77. '#type' => 'submit',
  78. '#value' => t($class::$button_text),
  79. '#weight' => 10,
  80. );
  81. return $form;
  82. }
  83. /**
  84. * Validate function for the tripal_get_importer_form form().
  85. */
  86. function tripal_get_importer_form_validate($form, &$form_state) {
  87. $class = $form_state['values']['importer_class'];
  88. tripal_load_include_importer_class($class);
  89. $file_local = NULL;
  90. $file_upload = NULL;
  91. $file_remote = NULL;
  92. // Get the form values for the file.
  93. if (array_key_exists('file_local', $class::$methods) and $class::$methods['file_local'] == TRUE) {
  94. $file_local = trim($form_state['values']['file_local']);
  95. // If the file is local make sure it exists on the local filesystem.
  96. if ($file_local) {
  97. // check to see if the file is located local to Drupal
  98. $file_local = trim($file_local);
  99. $dfile = $_SERVER['DOCUMENT_ROOT'] . base_path() . $file_local;
  100. if (!file_exists($dfile)) {
  101. // if not local to Drupal, the file must be someplace else, just use
  102. // the full path provided
  103. $dfile = $file_local;
  104. }
  105. if (!file_exists($dfile)) {
  106. form_set_error('file_local', t("Cannot find the file on the system. Check that the file exists or that the web server has permissions to read the file."));
  107. }
  108. }
  109. }
  110. if (array_key_exists('file_upload', $class::$methods) and $class::$methods['file_upload'] == TRUE) {
  111. $file_upload = trim($form_state['values']['file_upload']);
  112. }
  113. if (array_key_exists('file_remote', $class::$methods) and $class::$methods['file_remote'] == TRUE) {
  114. $file_remote = trim($form_state['values']['file_remote']);
  115. }
  116. // The user must provide at least an uploaded file or a local file path.
  117. if ($class::$file_required == TRUE and !$file_upload and !$file_local and !$file_remote) {
  118. form_set_error('file_local', t("You must provide a file."));
  119. }
  120. // Now allow the loader to do validation of it's form additions.
  121. $importer = new $class();
  122. $importer->formValidate($form, $form_state);
  123. }
  124. /**
  125. * Submit function for the tripal_get_importer_form form().
  126. */
  127. function tripal_get_importer_form_submit($form, &$form_state) {
  128. global $user;
  129. $run_args = $form_state['values'];
  130. $class = $form_state['values']['importer_class'];
  131. tripal_load_include_importer_class($class);
  132. // Remove the file_local and file_upload args. We'll add in a new
  133. // full file path and the fid instead.
  134. unset($run_args['file_local']);
  135. unset($run_args['file_upload']);
  136. unset($run_args['form_build_id']);
  137. unset($run_args['form_token']);
  138. unset($run_args['form_id']);
  139. unset($run_args['op']);
  140. unset($run_args['button']);
  141. $file_local = NULL;
  142. $file_upload = NULL;
  143. $file_remote = NULL;
  144. // Get the form values for the file.
  145. if (array_key_exists('file_local', $class::$methods) and $class::$methods['file_local'] == TRUE) {
  146. $file_local = trim($form_state['values']['file_local']);
  147. }
  148. if (array_key_exists('file_upload', $class::$methods) and $class::$methods['file_upload'] == TRUE) {
  149. $file_upload = trim($form_state['values']['file_upload']);
  150. }
  151. if (array_key_exists('file_remote', $class::$methods) and $class::$methods['file_remote'] == TRUE) {
  152. $file_remote = trim($form_state['values']['file_remote']);
  153. }
  154. // Sumbit a job for this loader.
  155. $fname = '';
  156. $fid = NULL;
  157. $file_details = array();
  158. if ($file_local) {
  159. $fname = preg_replace("/.*\/(.*)/", "$1", $file_local);
  160. $file_details['file_local'] = $file_local;
  161. }
  162. if ($file_upload) {
  163. $file_details['fid'] = $file_upload;
  164. }
  165. if ($file_remote) {
  166. $file_details['file_remote'] = $file_remote;
  167. }
  168. try {
  169. // Now allow the loader to do its own submit if needed.
  170. $importer = new $class();
  171. $importer->formSubmit($form, $form_state);
  172. // If the formSubmit made changes to the $form_state we need to update the
  173. // $run_args info.
  174. if ($run_args !== $form_state['values']) {
  175. $run_args = $form_state['values'];
  176. }
  177. // If the importer wants to rebuild the form for some reason then let's
  178. // not add a job.
  179. if (array_key_exists('rebuild', $form_state) and $form_state['rebuild'] == TRUE) {
  180. return;
  181. }
  182. $importer->create($run_args, $file_details);
  183. $importer->submitJob();
  184. }
  185. catch (Exception $e) {
  186. drupal_set_message('Cannot submit import: ' . $e->getMessage(), 'error');
  187. }
  188. }