tripal.importer.inc 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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'] = [
  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'] = [
  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'] = [
  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'] = [
  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'] = [
  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 = [];
  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'] = [
  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 = [];
  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)) {
  96. $element_form = array();
  97. }
  98. // Merge the custom form with our default one.
  99. // This way, the custom TripalImporter can use the #weight property
  100. // to change the order of their elements in reference to the default ones.
  101. $form = array_merge($form, $element_form);
  102. if ($class::$use_button == TRUE) {
  103. $form['button'] = [
  104. '#type' => 'submit',
  105. '#value' => t($class::$button_text),
  106. '#weight' => 10,
  107. ];
  108. }
  109. return $form;
  110. }
  111. /**
  112. * Validate function for the tripal_get_importer_form form().
  113. */
  114. function tripal_get_importer_form_validate($form, &$form_state) {
  115. $class = $form_state['values']['importer_class'];
  116. tripal_load_include_importer_class($class);
  117. $file_local = NULL;
  118. $file_upload = NULL;
  119. $file_remote = NULL;
  120. $file_existing = NULL;
  121. // Get the form values for the file.
  122. if (array_key_exists('file_local', $class::$methods) and $class::$methods['file_local'] == TRUE) {
  123. $file_local = trim($form_state['values']['file_local']);
  124. // If the file is local make sure it exists on the local filesystem.
  125. if ($file_local) {
  126. // check to see if the file is located local to Drupal
  127. $file_local = trim($file_local);
  128. $dfile = $_SERVER['DOCUMENT_ROOT'] . base_path() . $file_local;
  129. if (!file_exists($dfile)) {
  130. // if not local to Drupal, the file must be someplace else, just use
  131. // the full path provided
  132. $dfile = $file_local;
  133. }
  134. if (!file_exists($dfile)) {
  135. 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."));
  136. }
  137. }
  138. }
  139. if (array_key_exists('file_upload', $class::$methods) and $class::$methods['file_upload'] == TRUE) {
  140. $file_upload = trim($form_state['values']['file_upload']);
  141. if (array_key_exists('file_upload_existing', $form_state['values']) and $form_state['values']['file_upload_existing']) {
  142. $file_existing = $form_state['values']['file_upload_existing'];
  143. }
  144. }
  145. if (array_key_exists('file_remote', $class::$methods) and $class::$methods['file_remote'] == TRUE) {
  146. $file_remote = trim($form_state['values']['file_remote']);
  147. }
  148. // The user must provide at least an uploaded file or a local file path.
  149. if ($class::$file_required == TRUE and !$file_upload and !$file_local and !$file_remote and !$file_existing) {
  150. form_set_error('file_local', t("You must provide a file."));
  151. }
  152. // Now allow the loader to do validation of it's form additions.
  153. $importer = new $class();
  154. $importer->formValidate($form, $form_state);
  155. }
  156. /**
  157. * Submit function for the tripal_get_importer_form form().
  158. */
  159. function tripal_get_importer_form_submit($form, &$form_state) {
  160. global $user;
  161. $run_args = $form_state['values'];
  162. $class = $form_state['values']['importer_class'];
  163. tripal_load_include_importer_class($class);
  164. // Remove the file_local and file_upload args. We'll add in a new
  165. // full file path and the fid instead.
  166. unset($run_args['file_local']);
  167. unset($run_args['file_upload']);
  168. unset($run_args['file_upload_existing']);
  169. unset($run_args['form_build_id']);
  170. unset($run_args['form_token']);
  171. unset($run_args['form_id']);
  172. unset($run_args['op']);
  173. unset($run_args['button']);
  174. $file_local = NULL;
  175. $file_upload = NULL;
  176. $file_remote = NULL;
  177. $file_existing = NULL;
  178. // Get the form values for the file.
  179. if (array_key_exists('file_local', $class::$methods) and $class::$methods['file_local'] == TRUE) {
  180. $file_local = trim($form_state['values']['file_local']);
  181. }
  182. if (array_key_exists('file_upload', $class::$methods) and $class::$methods['file_upload'] == TRUE) {
  183. $file_upload = trim($form_state['values']['file_upload']);
  184. if (array_key_exists('file_upload_existing', $form_state['values']) and $form_state['values']['file_upload_existing']) {
  185. $file_existing = trim($form_state['values']['file_upload_existing']);
  186. }
  187. }
  188. if (array_key_exists('file_remote', $class::$methods) and $class::$methods['file_remote'] == TRUE) {
  189. $file_remote = trim($form_state['values']['file_remote']);
  190. }
  191. // Sumbit a job for this loader.
  192. $fname = '';
  193. $fid = NULL;
  194. $file_details = [];
  195. if ($file_existing) {
  196. $file_details['fid'] = $file_existing;
  197. }
  198. elseif ($file_local) {
  199. $fname = preg_replace("/.*\/(.*)/", "$1", $file_local);
  200. $file_details['file_local'] = $file_local;
  201. }
  202. elseif ($file_upload) {
  203. $file_details['fid'] = $file_upload;
  204. }
  205. elseif ($file_remote) {
  206. $file_details['file_remote'] = $file_remote;
  207. }
  208. try {
  209. // Now allow the loader to do its own submit if needed.
  210. $importer = new $class();
  211. $importer->formSubmit($form, $form_state);
  212. // If the formSubmit made changes to the $form_state we need to update the
  213. // $run_args info.
  214. if ($run_args !== $form_state['values']) {
  215. $run_args = $form_state['values'];
  216. }
  217. // If the importer wants to rebuild the form for some reason then let's
  218. // not add a job.
  219. if (array_key_exists('rebuild', $form_state) and $form_state['rebuild'] == TRUE) {
  220. return;
  221. }
  222. $importer->create($run_args, $file_details);
  223. $importer->submitJob();
  224. } catch (Exception $e) {
  225. drupal_set_message('Cannot submit import: ' . $e->getMessage(), 'error');
  226. }
  227. }