tripal.user.inc

File

tripal/includes/tripal.user.inc
View source
  1. <?php
  2. /**
  3. * Provides the page with a list of files uploaded by the user.
  4. *
  5. * @param $uid
  6. * The user ID.
  7. *
  8. * @return
  9. * A Drupal render array.
  10. */
  11. function tripal_user_files_page($uid) {
  12. // Get all of the files that have been uploaded by the user.
  13. // TODO: we should make this a paged query in case a user has a huge
  14. // numbef of uploaded files.
  15. $sql = "
  16. SELECT FM.fid, FM.filename, TGEF.expiration_date
  17. FROM {file_managed} FM
  18. INNER JOIN {file_usage} FU on FM.fid = FU.fid and FM.uid = :user_id
  19. LEFT JOIN {tripal_expiration_files} TGEF on TGEF.fid = FU.fid
  20. WHERE FU.module = 'tripal'
  21. GROUP BY FM.fid, TGEF.expiration_date
  22. ORDER BY FM.filename
  23. ";
  24. $files = db_query($sql, array(':user_id' => $uid));
  25. $rows = array();
  26. While ($entry = $files->fetchObject()) {
  27. $file = file_load($entry->fid);
  28. // Don't list files that don't exist on the file system.
  29. if (!file_exists($file->uri)) {
  30. continue;
  31. }
  32. $date_uploaded = date('Y-m-d H:i:s', $file->timestamp);
  33. $expiration = $entry->expiration_date ? date('Y-m-d H:i:s', $entry->expiration_date) : '';
  34. $actions = l('Delete', "user/$uid/files/$file->fid/delete") . ' | ' .
  35. l('Renew', "user/$uid/files/$file->fid/renew");
  36. $rows[] = array(
  37. $entry->fid,
  38. l($file->filename,"/user/$uid/files/$file->fid"),
  39. $date_uploaded,
  40. $expiration,
  41. tripal_format_bytes($file->filesize),
  42. $actions,
  43. );
  44. }
  45. $header = array('ID', 'File Name', 'Upload Date', 'Expiration', 'Size', 'Actions');
  46. // Get the user quota settings.
  47. $quota = tripal_get_user_quota($uid);
  48. $usage = tripal_get_user_usage($uid);
  49. $content = array(
  50. 'page_title' => array(
  51. '#type' => 'markup',
  52. '#markup' => '<h2>Your Uploaded Files</h2>',
  53. ),
  54. 'page_description' => array(
  55. '#type' => 'markup',
  56. '#markup' => '<p>' . t('Each user is allowed to consume a limited amount of space with uploaded files. This page provides details about your current usage, your limits and files you\'ve uploaded.') . '</p>',
  57. ),
  58. 'usage' => array(
  59. '#type' => 'item',
  60. '#title' => 'Current Usage',
  61. '#markup' => tripal_format_bytes($usage),
  62. '#description' => t('The total number of bytes you currently consume.'),
  63. ),
  64. 'quota' => array(
  65. '#type' => 'item',
  66. '#title' => 'Current Quota',
  67. '#markup' => tripal_format_bytes($quota->custom_quota),
  68. '#description' => t('The maximum number of bytes of files you can upload.')
  69. ),
  70. 'expiration' => array(
  71. '#type' => 'item',
  72. '#title' => 'Current Days to Expire',
  73. '#markup' => $quota->custom_expiration,
  74. '#description' => t('The number of days a file will remain on the server before deletion. The expiration of date of a file can be renewed using the "Renew" link in the table below.')
  75. ),
  76. 'file_list' => array(
  77. '#type' => 'item',
  78. '#title' => 'Uploaded Files',
  79. '#markup' => theme_table(array(
  80. 'header' => $header,
  81. 'rows' => $rows,
  82. 'attributes' => array(),
  83. 'caption' => t('Click a file name for more details.'),
  84. 'colgroups' => array(),
  85. 'sticky' => TRUE,
  86. 'empty' => 'You currently have no uploaded files.',
  87. )),
  88. )
  89. );
  90. if ($usage < $quota->custom_quota) {
  91. drupal_set_message('Your file usage is currently below the file quota limit.');
  92. }
  93. else {
  94. drupal_set_message('Your file usage is currently over your file quota limit. Please remove some files before uploading more', 'warning');
  95. }
  96. return $content;
  97. }
  98. /**
  99. * User action to renew the expiration of a file.
  100. *
  101. * Adds the current time and the expiration date (either from default or if
  102. * the user has a custom expiration date) to tripal_expiration_files
  103. * table.
  104. *
  105. **/
  106. function tripal_renew_file($fid) {
  107. $file = file_load($fid);
  108. $success = tripal_reset_file_expiration($fid);
  109. if ($success) {
  110. drupal_set_message('Successfully updated expiration date.');
  111. }
  112. drupal_goto('user/' . $file->uid . '/files/');
  113. }
  114. /**
  115. * Downloads a file.
  116. *
  117. * @param $fid
  118. * The File ID of the file to be downloaded.
  119. */
  120. function tripal_download_file($fid) {
  121. $file = file_load($fid);
  122. if (file_exists($file->uri)) {
  123. $headers = array();
  124. $headers['Content-Type'] = $file->filemime;
  125. $headers['Content-Disposition'] = 'attachment; filename=' . $file->filename;
  126. $headers['Content-Length'] = $file->filesize;
  127. file_transfer($file->uri, $headers);
  128. }
  129. else {
  130. drupal_set_message('Can not download. The file no longer exists on the server.', 'error');
  131. drupal_goto('user/' . $file->uid . '/files/');
  132. }
  133. }
  134. /**
  135. * Provides a confirmation form for deleting a galaxy workflow uploaded file.
  136. */
  137. function tripal_delete_file_form($form, $form_state, $uid, $fid) {
  138. $form = array();
  139. $file = file_load($fid);
  140. $form['uid'] = array(
  141. '#type' => 'value',
  142. '#value' => $uid,
  143. );
  144. $form['fid'] = array(
  145. '#type' => 'value',
  146. '#value' => $fid,
  147. );
  148. return confirm_form($form,
  149. t('Confirm deletion of the file named "' . $file->filename . '"?'),
  150. 'user/' . $uid . '/files/',
  151. t('Warning. If this file is intended to be used with a analysis workflow submission that has not yet started then the workflow will fail. Once deleted, the file can no longer be used for new workflow submissions without uploading again.')
  152. );
  153. }
  154. /**
  155. * Implements a form submit for deleting a galaxy workflow uploaded file.
  156. */
  157. function tripal_delete_file_form_submit($form, &$form_state) {
  158. $fid = $form_state['values']['fid'];
  159. $uid = $form_state['values']['uid'];
  160. $file = file_load($fid);
  161. // Remove the file from the file_usage table for all entries that link
  162. // to the tripal module.
  163. file_usage_delete($file, 'tripal', NULL, NULL, 0);
  164. // Get any remaining usage for other modules
  165. $file_usage = file_usage_list($file);
  166. // If this file is still used by the tripal module then something
  167. // didn't work right.
  168. if (in_array('tripal', $file_usage)) {
  169. drupal_set_message('The file could not be removed. Please contact the site administrator.', 'error');
  170. }
  171. // If there is no other usage of this file from other modules then delete it.
  172. if (count(array_keys($file_usage)) == 0) {
  173. if (file_unmanaged_delete($file->uri)) {
  174. // Also remove the md5 checksum.
  175. if (file_exists(file_unmanaged_delete($file->uri . '.md5'))) {
  176. file_unmanaged_delete($file->uri . '.md5');
  177. }
  178. drupal_set_message('The file has been fully removed.');
  179. }
  180. else {
  181. drupal_set_message('The file has removed from this list and does not count against your quota, but other components of this site rely on this file. Thus it has not been fully removed.');
  182. }
  183. }
  184. drupal_goto('user/' . $file->uid . '/files/');
  185. }
  186. /**
  187. * Provides details about a file.
  188. */
  189. function tripal_view_file($uid, $fid) {
  190. $file = file_load($fid);
  191. $headers = array();
  192. $rows = array();
  193. $actions = l('Delete', "user/$uid/files/$file->fid/delete") . '<br>' .
  194. l('Download', "user/$uid/files/$file->fid/download");
  195. // Name row
  196. $rows[] = array(
  197. array(
  198. 'data' => 'File Name',
  199. 'header' => TRUE,
  200. 'width' => '20%',
  201. ),
  202. $file->filename
  203. );
  204. $date_uploaded = date('Y-m-d H:i:s', $file->timestamp);
  205. $rows[] = array(
  206. array(
  207. 'data' => 'Upload Date',
  208. 'header' => TRUE,
  209. 'width' => '20%',
  210. ),
  211. $date_uploaded
  212. );
  213. $expiration_date = db_select('tripal_expiration_files', 'tgef')
  214. ->fields('tgef', array('expiration_date'))
  215. ->condition('fid', $fid)
  216. ->execute()
  217. ->fetchField();
  218. $expiration = $expiration_date ? date('Y-m-d H:i:s', $expiration_date) : '';
  219. $rows[] = array(
  220. array(
  221. 'data' => 'Expiration Date',
  222. 'header' => TRUE,
  223. 'width' => '20%',
  224. ),
  225. $expiration
  226. );
  227. // Find which workflow submissions are using this file.
  228. $usage = file_usage_list($file);
  229. $usage = $usage['tripal'];
  230. $workflow_nids = array();
  231. foreach ($usage as $step => $nid) {
  232. $nid = array_keys($nid)[0];
  233. if (!in_array($nid, $workflow_nids)) {
  234. $workflow_nids[] = $nid;
  235. }
  236. }
  237. $wf_links = array();
  238. foreach ($workflow_nids as $i => $nid) {
  239. $query = db_select('tripal_workflow', 'tgw');
  240. $query->fields('tgw', array('workflow_name'));
  241. $query->innerJoin('tripal_workflow_submission', 'tgws', 'tgw.galaxy_workflow_id = tgws.galaxy_workflow_id');
  242. $query->fields('tgws', array('sid'));
  243. $query->condition('tgw.nid', $nid, '=');
  244. $results = $query->execute();
  245. $workflow = $results->fetchObject();
  246. if ($workflow) {
  247. $wf_links[] = l($workflow->workflow_name . ' (submission ID: ' . $workflow->sid . ')', "user/$uid/galaxy-jobs/$workflow->sid");
  248. }
  249. }
  250. $rows[] = array(
  251. array(
  252. 'data' => 'Usage',
  253. 'header' => TRUE,
  254. 'width' => '20%',
  255. ),
  256. theme_item_list(array(
  257. 'items' => $wf_links,
  258. 'title' => '',
  259. 'type' => 'ul',
  260. 'attributes' => array(),
  261. )),
  262. );
  263. $rows[] = array(
  264. array(
  265. 'data' => 'Actions',
  266. 'header' => TRUE,
  267. 'width' => '20%',
  268. ),
  269. $actions
  270. );
  271. $content = array(
  272. 'description' => array(
  273. '#type' => 'markup',
  274. '#markup' => '<p>' . t('The following file has been uploaded for use in an analytical workflow.') . '</p>',
  275. ),
  276. 'return' => array(
  277. '#type' => 'markup',
  278. '#markup' => '<p>' . l('View all Uploaded Files', "user/$uid/files") . '</p>',
  279. ),
  280. 'file_details' => array(
  281. '#type' => 'markup',
  282. '#markup' => theme_table(array(
  283. 'header' => $headers,
  284. 'rows' => $rows,
  285. 'attributes' => array(),
  286. 'sticky' => FALSE,
  287. 'caption' => '',
  288. 'colgroups' => array(),
  289. 'empty' => '',
  290. )),
  291. ),
  292. );
  293. return $content;
  294. }