tripal.importer.inc 9.4 KB

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