tripal.importer.inc 9.3 KB

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