tripal.upload.api.inc

Provides an application programming interface (API) for working with file uploads.

File

tripal/api/tripal.upload.api.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * Provides an application programming interface (API) for working with
  5. * file uploads.
  6. */
  7. /**
  8. * @defgroup tripal_upload_api File Upload
  9. * @ingroup tripal_api
  10. * @{
  11. * Tripal provides a convenient HTML5 Javascript uploader. It is automatically
  12. * embedded into the TripalImporter class. This application programing
  13. * interface (API) provides support for working with uploaded files.
  14. *
  15. * If you want to use the TripalUploader JavaScript in your own form the
  16. * following must be performed:
  17. *
  18. * 1) Add a Drupal form to your code that contains the following:
  19. * * A Drupal-style table with 4 or 8 columns. See the initialize
  20. * function in this class for a description of the columns.
  21. * * A button for submitting a file for upload.
  22. *
  23. * @code
  24. * $headers = array(
  25. * array('data' => 'Sequence File'),
  26. * array('data' => 'Size', 'width' => '10%'),
  27. * array('data' => 'Upload Progress', 'width' => '20%'),
  28. * array('data' => 'Action', 'width' => '10%')
  29. * );
  30. * $rows = array();
  31. * $table_vars = array(
  32. * 'header' => $headers,
  33. * 'rows' => $rows,
  34. * 'attributes' => array('id' => 'sequence-file-upload-table'),
  35. * 'sticky' => TRUE,
  36. * 'colgroups' => array(),
  37. * 'empty' => t('There are currently no files added.'),
  38. * );
  39. * $form['upload']['sequence_file'] = array(
  40. * '#markup' => theme('table', $table_vars)
  41. * );
  42. * $form['upload']['sequence_fid'] = array(
  43. * '#type' => 'hidden',
  44. * '#value' => 0,
  45. * '#attributes' => array('id' => 'sequence-fid')
  46. * );
  47. * $form['upload']['sequence_file_submit'] = array(
  48. * '#type' => 'submit',
  49. * '#value' => 'Upload Sequence File',
  50. * '#name' => 'sequence_file_submit',
  51. * // We don't want this button to submit as the file upload
  52. * // is handled by the JavaScript code.
  53. * '#attributes' => array('onclick' => 'return (false);')
  54. * );
  55. * @endcode
  56. *
  57. *
  58. * 2) Edit the theme/js/[module_name].js and in the "Drupal.behaviors.[module]"
  59. * section add a JQuery show function to the form that converts the table
  60. * created in the Drupal form to a TripalUploader table. The 'table_id' must be
  61. * the same as the 'id' attribute set for the table in the Drupal code above.
  62. * The 'submit_id' must be the id of the upload button added in the Drupal
  63. * code above. The 'category' for the files. This is the category that
  64. * will be saved in Tripal for the file. See the addUploadTable function
  65. * for additional options. Include a 'cardinality' setting to indicate
  66. * the number of allowed files per upload, and set the 'target_id' to the
  67. * name of the field that will contain the file ID (fid) after uploading.
  68. *
  69. * @code
  70. * // The TripalUploader object used for uploading of files using the
  71. * // HTML5 File API. Large files are uploaded as chunks and a progress
  72. * // bar is provided.
  73. * var uploader = new TripalUploader();
  74. *
  75. * $('#tripal-sequences-panel-form').show(function() {
  76. * uploader.addUploadTable('sequence_file', {
  77. * 'table_id' : '#sequence-file-upload-table',
  78. * 'submit_id': '#edit-sequence-file-submit',
  79. * 'category' : ['sequence_file'],
  80. * 'cardinality' : 1,
  81. * 'target_id' : 'sequence-fid',
  82. * });
  83. * });
  84. * @endcode
  85. *
  86. *
  87. * 3) Files are uploaded automatically to Tripal. Files are saved in the
  88. * Tripal user's directory. You can retreive information about the
  89. * file by querying for the file category for the current project.
  90. *
  91. * @code
  92. * $seq_files = TripalFeature::getFilesByTypes($user->uid,
  93. * array('sequence_file'), $project_id);
  94. * @endcode
  95. *
  96. * 4) If the 'target_id' was used in array for step #2 above, then the
  97. * file ID can be retrieved in the hook_validate() and hook_submit() functions
  98. * via the $form_state['input'] array (not the $form_state['values'] array.
  99. *
  100. * @}
  101. */
  102. /**
  103. * Allows a module to interact with the Tripal file uploader during upload.
  104. *
  105. * This function is called prior to an 'action' aoccuring and allows the
  106. * module that is responsible for the file upload to halt an upload if
  107. * needed.
  108. *
  109. * @param $action
  110. * The current action that is being peformed during the upload process. The
  111. * actions are: 'save', 'check' and 'merge'.
  112. * @param $details
  113. * An associative array containing details about the upload process. Valid
  114. * keys include:
  115. * - filename: The name of the file.
  116. * - file_size: The total size of the file.
  117. * - chunk_size: The size of the chunk.
  118. * - fid: The file ID of the file.
  119. * @param $message
  120. * An error message to report to the user if the function returns FALSE.
  121. *
  122. * @return
  123. * TRUE if the upload should continue. FALSE if a problem occurs and the
  124. * upload should be terminated.
  125. *
  126. * @ingroup tripal_upload_api
  127. */
  128. function hook_file_upload_check($action, $details, &$message){
  129. switch ($action) {
  130. case 'save':
  131. // Place code here when chunks are being saved.
  132. break;
  133. case 'check':
  134. // Place code here when a chunck is being checked if the upload
  135. // completed successfully.
  136. break;
  137. case 'merge':
  138. // Place code here when all chunks will be merged.
  139. break;
  140. }
  141. return TRUE;
  142. }