tripal.collections.api.inc

Provides the API for working with Data collections. This includes creation, experiation, and retreival.

File

tripal/api/tripal.collections.api.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. *
  5. * Provides the API for working with Data collections. This includes creation,
  6. * experiation, and retreival.
  7. */
  8. /**
  9. * @defgroup tripal_data_collections_api Data Collections
  10. * @ingroup tripal_api
  11. * @{
  12. * New with Tripal v3 are data collections. A data collection can be compared
  13. * to a "shopping cart" system where site users can collect data of interest
  14. * to them, and use that data later for other purposes. By default, Tripal
  15. * offers data collections with the default search interface provided by views.
  16. * This interface allows end-users to create data collections after searching
  17. * for items using the default search. Once in a collection users can then
  18. * generate files for downloading or use by other tools that support data
  19. * collections.
  20. * @}
  21. */
  22. /**
  23. * Creates a collection of entities for a given user.
  24. *
  25. * Use this function if you want to create a new collection with an bundle.
  26. * Otherwise, a new colleciton can also be created by creating an new instance
  27. * of a TripalEntityCollection object.
  28. *
  29. * @param $details
  30. * An association array containing the details for a collection. The
  31. * details must include the following key/value pairs:
  32. * - uid: The ID of the user that owns the collection
  33. * - collection_name: The name of the collection
  34. * - bundle_name: The name of the TripalEntity content type.
  35. * - ids: An array of the entity IDs that form the collection.
  36. * - fields: An array of the field IDs that the collection is limited to.
  37. * - description: A user supplied description for the collection.
  38. *
  39. * @return TripalEntityCollection
  40. * An instance of a TripalEntityCollection class or FALSE on failure.
  41. *
  42. * @ingroup tripal_data_collections_api
  43. */
  44. function tripal_create_collection($details) {
  45. global $user;
  46. try {
  47. $collection = new TripalEntityCollection();
  48. $collection->create($details);
  49. $collection_id = $collection->getCollectionID();
  50. $collection->addBundle($details);
  51. drupal_set_message(t("Collection '%name' created with %num_recs record(s). Check the !view to generate file links.",
  52. array(
  53. '%name' => $details['collection_name'],
  54. '%num_recs' => count($details['ids']),
  55. '!view' => l('data collections page', 'user/' . $user->uid . '/data-collections'),
  56. ))
  57. );
  58. return $collection;
  59. }
  60. catch (Exception $e) {
  61. drupal_set_message(t("Failed to create the collection '%name': " . $e->getMessage(), array('%name' => $details['collection_name'])), 'error');
  62. return FALSE;
  63. }
  64. }
  65. /**
  66. * Retrieves an array of collections for a user.
  67. *
  68. * @param $uid
  69. * The User ID
  70. *
  71. * @return
  72. * An array of TripalEntityCollection objects.
  73. *
  74. * @ingroup tripal_data_collections_api
  75. */
  76. function tripal_get_user_collections($uid) {
  77. if (!$uid) {
  78. throw new Exception('tripal_get_user_collections: Missing the $uid argument.');
  79. }
  80. $user = user_load($uid);
  81. if (!$user) {
  82. throw new Exception('tripal_get_user_collections: Invalid $uid provided.');
  83. }
  84. $collections = array();
  85. $results = db_select('tripal_collection', 'tc')
  86. ->fields('tc', array('collection_id'))
  87. ->condition('uid', $uid)
  88. ->execute();
  89. while ($collection_id = $results->fetchField()) {
  90. $collection = new TripalEntityCollection();
  91. $collection->load($collection_id);
  92. $collections[] = $collection;
  93. }
  94. return $collections;
  95. }
  96. /**
  97. * Deletes all collections that have surpassed their lifespan
  98. *
  99. * @ingroup tripal_data_collections_api
  100. */
  101. function tripal_expire_collections() {
  102. $max_days = variable_get('tripal_data_collections_lifespan', 30);
  103. $ctime = time();
  104. $query = db_select('tripal_collection', 'tc');
  105. $query->fields('tc', array('collection_id'));
  106. $query->where("((($ctime - create_date) / 60) / 60) / 24 >= $max_days");
  107. $results = $query->execute();
  108. while ($collection_id = $results->fetchField()) {
  109. $collection = new TripalEntityCollection();
  110. $collection->load($collection_id);
  111. $collections[] = $collection;
  112. $collection->delete();
  113. }
  114. return $collections;
  115. }
  116. /**
  117. * Retrieve a collection using the collection ID
  118. *
  119. * @param $values
  120. * An array of key/value pairs to uniquely identify a collection. The
  121. * following keys can be used:
  122. * - collection_id: The numeric value for the collection.
  123. * - uid: The ID of the user that owns the collection. This key must
  124. * always be used with the 'name' key.
  125. * - name: The name of the collection. This key must always be used
  126. * with the 'uid' key.
  127. *
  128. * @return
  129. * An instance of a TripalEntityCollection class or FALSE on failure.
  130. *
  131. * @ingroup tripal_data_collections_api
  132. */
  133. function tripal_get_collection($values) {
  134. $collection_id = array_key_exists('collection_id', $values) ? $values['collection_id'] : NULL;
  135. $uid = array_key_exists('uid', $values) ? $values['uid'] : NULL;
  136. $name = array_key_exists('name', $values) ? $values['name'] : NULL;
  137. if ($uid and !$name) {
  138. throw new Exception('tripal_get_collection: Missing the collection name when specifying the User ID.');
  139. }
  140. if (!$uid and $name) {
  141. throw new Exception('tripal_get_collection: Missing the User ID when specifying the collection name.');
  142. }
  143. if (!$collection_id and !$uid and !$name) {
  144. throw new Exception('tripal_get_collection: Missing a valid key.');
  145. }
  146. if ($name and $uid) {
  147. $collection_id = db_select('tripal_collection', 'tc')
  148. ->fields('tc', array('collection_id'))
  149. ->condition('uid', $uid)
  150. ->condition('collection_name', $name)
  151. ->execute()
  152. ->fetchField();
  153. if (!$collection_id) {
  154. throw new Exception('tripal_get_collection: The collection could not be found with the given uid and name.');
  155. }
  156. }
  157. try {
  158. $collection = new TripalEntityCollection();
  159. $collection->load($collection_id);
  160. return $collection;
  161. }
  162. catch (Exception $e) {
  163. drupal_set_message(t("Failed to load the collection with id '%id': " . $e->getMessage(), array('%id' => $collection_id)), 'error');
  164. return FALSE;
  165. }
  166. }
  167. /**
  168. * Generates the downloadable files for a Collection
  169. *
  170. * @param TripalEntityCollection $collection
  171. *
  172. * @ingroup tripal_data_collections_api
  173. */
  174. function tripal_create_collection_files($collection_id, TripalJob $job = NULL) {
  175. $collection = new TripalEntityCollection();
  176. $collection->load($collection_id);
  177. $collection->write(NULL, $job);
  178. }