tripal.collections.inc 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. <?php
  2. /**
  3. *
  4. */
  5. function tripal_user_collections_page() {
  6. global $user;
  7. $headers = array('Name', 'Description', 'Create Date', 'Download Formats',
  8. 'Generate File', 'Actions');
  9. $rows = array();
  10. $collections = db_select('tripal_collection', 'tc')
  11. ->fields('tc', array('collection_id'))
  12. ->condition('uid', $user->uid)
  13. ->orderBy('tc.collection_name')
  14. ->execute();
  15. while ($collection_id = $collections->fetchField()) {
  16. $collection = new TripalEntityCollection();
  17. $collection->load($collection_id);
  18. $downloads = array();
  19. $formatters = $collection->getDownloadFormatters();
  20. foreach ($formatters as $class_name => $label) {
  21. $outfile = $collection->getOutfilePath($class_name);
  22. if (file_exists($outfile)) {
  23. $outfileURL = file_create_url($outfile);
  24. $downloads[] = l($label, $outfileURL);
  25. }
  26. else {
  27. $downloads[] = 'Waiting on ' . $label . '...';
  28. }
  29. }
  30. $download_list = theme_item_list(array(
  31. 'items' => $downloads,
  32. 'title' => '',
  33. 'type' => 'ul',
  34. 'attributes' => array(),
  35. ));
  36. $rows[] = array(
  37. 'data' => array(
  38. $collection->getName(),
  39. $collection->getDescription(),
  40. $collection->getCreateDate(),
  41. $download_list,
  42. l('Generate File for Download', 'user/' . $user->uid . '/data-collections/generate/' . $collection_id),
  43. l('View', 'user/' . $user->uid . '/data-collections/' . $collection_id . '/view') . ' ' .
  44. l('Delete', 'user/' . $user->uid . '/data-collections/' . $collection_id . '/delete'),
  45. ),
  46. );
  47. }
  48. $content['instructions'] = array(
  49. '#type' => 'markup',
  50. '#markup' => '<p>' . t('Data collections allow you to store sets of data
  51. for download or later use by other tools on this site. Typically data
  52. collections are created using search tools. The following data
  53. collections are available to you. Some files take time to generate
  54. before they can be downloaded.') . '</p>',
  55. );
  56. $content['files_table'] = array(
  57. '#type' => 'item',
  58. '#title' => 'Data Collections',
  59. '#markup' => theme_table(array(
  60. 'header' => $headers,
  61. 'rows' => $rows,
  62. 'attributes' => array(),
  63. 'caption' => '',
  64. 'colgroups' => array(),
  65. 'sticky' => TRUE,
  66. 'empty' => t('You currently have no data collections.')
  67. )),
  68. );
  69. return $content;
  70. }
  71. /**
  72. * Renders a view of a data collection
  73. */
  74. function tripal_user_collections_view_page($uid, $collection_id) {
  75. $user = user_load($uid);
  76. // set the breadcrumb
  77. $breadcrumb = array();
  78. $breadcrumb[] = l('Home', '<front>');
  79. $breadcrumb[] = l($user->name, 'user/' . $uid);
  80. $breadcrumb[] = l('Data Collections', 'user/' . $uid . '/data-collections');
  81. drupal_set_breadcrumb($breadcrumb);
  82. $collection = new TripalEntityCollection();
  83. $collection->load($collection_id);
  84. drupal_set_title('Data Collection: ' . $collection->getName());
  85. // Get the content types for this data collection.
  86. $bundles = $collection->getBundles();
  87. $content_types = array();
  88. $field_list = array();
  89. $formatters_list = array();
  90. $entities_list = array();
  91. foreach ($bundles as $bundle) {
  92. $bundle = tripal_load_bundle_entity(array('name' => $bundle->bundle_name));
  93. $content_types[] = $bundle->label;
  94. $fields = $collection->getFields($bundle->name);
  95. foreach ($fields as $index => $flist) {
  96. foreach ($flist as $field_id) {
  97. $field = field_info_field_by_id($field_id);
  98. $instance = field_info_instance('TripalEntity', $field['field_name'], $bundle->name);
  99. $field_list[] = $instance['label'];
  100. $field_formatters = tripal_get_field_field_formatters($field, $instance);
  101. foreach ($field_formatters as $class_name => $label) {
  102. tripal_load_include_downloader_class($class_name);
  103. $formatters_list[] = $label . ' (' . $class_name::$full_label . ')';
  104. }
  105. }
  106. }
  107. $eids = $collection->getEntityIDs($bundle->name);
  108. foreach ($eids as $index => $elist) {
  109. foreach ($elist as $eid) {
  110. $entities_list[] = $eid;
  111. }
  112. }
  113. }
  114. $contents['content_types'] = array(
  115. '#type' => 'item',
  116. '#title' => 'Content types',
  117. '#description' => t('The content types in this data collection'),
  118. '#markup' => join(', ', $content_types),
  119. );
  120. $contents['field_list'] = array(
  121. '#type' => 'item',
  122. '#title' => 'Fields',
  123. '#description' => t('The fields listed in this data collection'),
  124. '#markup' => join(', ', array_unique($field_list)),
  125. );
  126. $contents['formatters'] = array(
  127. '#type' => 'item',
  128. '#title' => 'Supported File Types',
  129. '#description' => t('The fields in your data collection appear to support the above file formats. Please note that not all fields are compatible with all formats and only those that are compatible will appear in the format. Some file formats require certain fields and if all fields are not present in the collection they will not be complete.'),
  130. '#markup' => join(', ', array_unique($formatters_list)),
  131. );
  132. $contents['ecount'] = array(
  133. '#type' => 'item',
  134. '#title' => 'Records',
  135. '#description' => t('The number of records contained in this data collection.'),
  136. '#markup' => count($entities_list),
  137. );
  138. $contents['peek'] = array(
  139. '#type' => 'item',
  140. '#title' => 'Data View',
  141. '#markup' => 'Coming soon...',
  142. );
  143. return $contents;
  144. }
  145. /**
  146. * Provides the confirm form for deleting a data collection.
  147. */
  148. function tripal_user_collections_delete_form($form, &$form_state, $uid, $collection_id) {
  149. $form_state['collection_id'] = $collection_id;
  150. $form['#submit'][] = 'tripal_user_collections_delete_form_submit';
  151. $collection = new TripalEntityCollection();
  152. $collection->load($collection_id);
  153. $form = confirm_form($form,
  154. t('Click the delete button below to confirm deletion of the collection titled: %title',
  155. array('%title' => $collection->getName())), 'user/' . $uid . '/data-collections',
  156. '<p>' .t('This action cannot be undone.') .'</p>', t('Delete'), t('Cancel'), 'confirm');
  157. return $form;
  158. }
  159. /**
  160. * Deletes a user's collection.
  161. *
  162. * @param $collection_id
  163. * The ID of the collection to delete.
  164. */
  165. function tripal_user_collections_delete_form_submit($form, &$form_state) {
  166. global $user;
  167. $collection_id = $form_state['collection_id'];
  168. $collection = new TripalEntityCollection();
  169. $collection->load($collection_id);
  170. if ($collection->getUserID() == $user->uid) {
  171. try {
  172. $collection->delete();
  173. drupal_set_message('The data collection has been deleted.');
  174. }
  175. catch (Exception $e) {
  176. drupal_set_message('There was a problem deleting the data collection please contact the site to report the error.', 'error');
  177. }
  178. }
  179. drupal_goto('user/' . $user->uid . '/data-collections');
  180. }
  181. function tripal_user_collections_generate_file_form($form, &$form_state, $uid, $collection_id) {
  182. $form_state['collection_id'] = $collection_id;
  183. $form['#submit'][] = 'tripal_user_collections_generate_file_form_submit';
  184. $collection = new TripalEntityCollection();
  185. $collection->load($collection_id);
  186. $form = confirm_form($form,
  187. t('Confirm creation of files for the data collection named: "%title"',
  188. array('%title' => $collection->getName())), 'user/' . $uid . '/data-collections',
  189. '<p>' .t('It may take some time for the file(s) to be generated. An email will be sent when files are ready.') .'</p>', t('Generate File'), t('Cancel'), 'confirm');
  190. return $form;
  191. }
  192. /**
  193. * Deletes a user's collection.
  194. *
  195. * @param $collection_id
  196. * The ID of the collection to delete.
  197. */
  198. function tripal_user_collections_generate_file_form_submit($form, &$form_state) {
  199. global $user;
  200. $collection_id = $form_state['collection_id'];
  201. $collection = new TripalEntityCollection();
  202. $collection->load($collection_id);
  203. if ($collection->getUserID() == $user->uid) {
  204. try {
  205. // Add the job to write the collection download files.
  206. $args = array($collection_id);
  207. tripal_add_job('Create data collection files for ' . $user->name, 'tripal',
  208. 'tripal_create_collection_files', $args, $user->uid, 10, array());
  209. }
  210. catch (Exception $e) {
  211. drupal_set_message('There was a problem creating the file for the data collection please contact the site to report the error.', 'error');
  212. }
  213. }
  214. drupal_goto('user/' . $user->uid . '/data-collections');
  215. }