tripal.importer.inc 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. );
  33. }
  34. if (array_key_exists('file_local', $class::$methods) and $class::$methods['file_local'] == TRUE) {
  35. $form['file']['file_local']= array(
  36. '#title' => t('Server path'),
  37. '#type' => 'textfield',
  38. '#maxlength' => 5120,
  39. '#description' => t('If the file is local to the Tripal server please provide the full path here.'),
  40. );
  41. }
  42. if (array_key_exists('file_remote', $class::$methods) and $class::$methods['file_remote'] == TRUE) {
  43. $form['file']['file_remote']= array(
  44. '#title' => t('Remote path'),
  45. '#type' => 'textfield',
  46. '#maxlength' => 5102,
  47. '#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.'),
  48. );
  49. }
  50. if ($class::$use_analysis) {
  51. // get the list of analyses
  52. $sql = "SELECT * FROM {analysis} ORDER BY name";
  53. $org_rset = chado_query($sql);
  54. $analyses = array();
  55. $analyses[''] = '';
  56. while ($analysis = $org_rset->fetchObject()) {
  57. $analyses[$analysis->analysis_id] = "$analysis->name ($analysis->program $analysis->programversion, $analysis->sourcename)";
  58. }
  59. $form['analysis_id'] = array(
  60. '#title' => t('Analysis'),
  61. '#type' => t('select'),
  62. '#description' => t('Choose the analysis to which the uploaded data will be associated. ' .
  63. 'Why specify an analysis for a data load? All data comes from some place, even if ' .
  64. 'downloaded from a website. By specifying analysis details for all data imports it ' .
  65. 'provides provenance and helps end user to reproduce the data set if needed. At ' .
  66. 'a minimum it indicates the source of the data.'),
  67. '#required' => $class::$require_analysis,
  68. '#options' => $analyses,
  69. );
  70. }
  71. $importer = new $class();
  72. $element = array();
  73. $form['class_elements'] = $importer->form($element, $form_state);
  74. $form['button'] = array(
  75. '#type' => 'submit',
  76. '#value' => t($class::$button_text),
  77. '#weight' => 10,
  78. );
  79. return $form;
  80. }
  81. /**
  82. * Validate function for the tripal_get_importer_form form().
  83. */
  84. function tripal_get_importer_form_validate($form, &$form_state) {
  85. $class = $form_state['values']['importer_class'];
  86. tripal_load_include_importer_class($class);
  87. $file_local = NULL;
  88. $file_upload = NULL;
  89. $file_remote = NULL;
  90. // Get the form values for the file.
  91. if (array_key_exists('file_local', $class::$methods) and $class::$methods['file_local'] == TRUE) {
  92. $file_local = trim($form_state['values']['file_local']);
  93. // If the file is local make sure it exists on the local filesystem.
  94. if ($file_local) {
  95. // check to see if the file is located local to Drupal
  96. $file_local = trim($file_local);
  97. $dfile = $_SERVER['DOCUMENT_ROOT'] . base_path() . $file_local;
  98. if (!file_exists($dfile)) {
  99. // if not local to Drupal, the file must be someplace else, just use
  100. // the full path provided
  101. $dfile = $file_local;
  102. }
  103. if (!file_exists($dfile)) {
  104. 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."));
  105. }
  106. }
  107. }
  108. if (array_key_exists('file_upload', $class::$methods) and $class::$methods['file_upload'] == TRUE) {
  109. $file_upload = trim($form_state['values']['file_upload']);
  110. }
  111. if (array_key_exists('file_remote', $class::$methods) and $class::$methods['file_remote'] == TRUE) {
  112. $file_remote = trim($form_state['values']['file_remote']);
  113. }
  114. // The user must provide at least an uploaded file or a local file path.
  115. if ($class::$file_required == TRUE and !$file_upload and !$file_local and !$file_remote) {
  116. form_set_error('file_local', t("You must provide a file."));
  117. }
  118. // Now allow the loader to do validation of it's form additions.
  119. $importer = new $class();
  120. $importer->formValidate($form, $form_state);
  121. }
  122. /**
  123. * Submit function for the tripal_get_importer_form form().
  124. */
  125. function tripal_get_importer_form_submit($form, &$form_state) {
  126. global $user;
  127. $run_args = $form_state['values'];
  128. $class = $form_state['values']['importer_class'];
  129. tripal_load_include_importer_class($class);
  130. // Remove the file_local and file_upload args. We'll add in a new
  131. // full file path and the fid instead.
  132. unset($run_args['file_local']);
  133. unset($run_args['file_upload']);
  134. unset($run_args['form_build_id']);
  135. unset($run_args['form_token']);
  136. unset($run_args['form_id']);
  137. unset($run_args['op']);
  138. unset($run_args['button']);
  139. $file_local = NULL;
  140. $file_upload = NULL;
  141. $file_remote = NULL;
  142. // Get the form values for the file.
  143. if (array_key_exists('file_local', $class::$methods) and $class::$methods['file_local'] == TRUE) {
  144. $file_local = trim($form_state['values']['file_local']);
  145. }
  146. if (array_key_exists('file_upload', $class::$methods) and $class::$methods['file_upload'] == TRUE) {
  147. $file_upload = trim($form_state['values']['file_upload']);
  148. }
  149. if (array_key_exists('file_remote', $class::$methods) and $class::$methods['file_remote'] == TRUE) {
  150. $file_remote = trim($form_state['values']['file_remote']);
  151. }
  152. // Sumbit a job for this loader.
  153. $fname = '';
  154. $fid = NULL;
  155. $file_details = array();
  156. if ($file_local) {
  157. $fname = preg_replace("/.*\/(.*)/", "$1", $file_local);
  158. $file_details['file_local'] = $file_local;
  159. }
  160. if ($file_upload) {
  161. $file_details['fid'] = $file_upload;
  162. }
  163. if ($file_remote) {
  164. $file_details['file_remote'] = $file_remote;
  165. }
  166. try {
  167. // Now allow the loader to do it's own submit if needed.
  168. $importer = new $class();
  169. $importer->formSubmit($form, $form_state);
  170. // If the importer wants to rebuild the form for some reason then let's
  171. // not add a job.
  172. if (array_key_exists('rebuild', $form_state) and $form_state['rebuild'] == TRUE) {
  173. return;
  174. }
  175. $importer->create($run_args, $file_details);
  176. $importer->submitJob();
  177. }
  178. catch (Exception $e) {
  179. drupal_set_message('Cannot submit import: ' . $e->getMessage(), 'error');
  180. }
  181. }