TripalFieldDownloader.inc 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <?php
  2. abstract class TripalFieldDownloader {
  3. /**
  4. * Sets the label shown to the user describing this formatter.
  5. */
  6. static public $label = 'Generic';
  7. /**
  8. * Indicates the default extension for the outputfile.
  9. */
  10. static public $default_extension = 'txt';
  11. /**
  12. * The bundle name.
  13. */
  14. protected $bundle_name = '';
  15. /**
  16. * A set of entity IDs. The entities must all be of the same bundle type.
  17. */
  18. protected $entity_ids = array();
  19. /**
  20. * The set of fields
  21. */
  22. protected $fields = array();
  23. /**
  24. * The output file URI.
  25. */
  26. protected $outfile = '';
  27. /**
  28. * Constructs a new instance of the TripalFieldDownloader class.
  29. * @param $bundle_name
  30. * The name of the bundle to which the IDs in the $id argument belong.
  31. * @param $ids
  32. * An array of entity IDs. The order of the IDs will be the order that
  33. * output is generated.
  34. * @param $fields
  35. * An array of numeric field IDs to use when constructing the download. If
  36. * no fields are provided then all fields that are appropriate for the
  37. * given type will be used.
  38. * @param $outfile_name
  39. * The name of the output file to create. The name should not include
  40. * a path.
  41. */
  42. public function __construct($bundle_name, $ids, $fields = array(),
  43. $outfile_name, $uid) {
  44. $user = user_load($uid);
  45. if (!$user) {
  46. throw new Exception(t("The provided user ID does not reference a real user: '@uid'.", array('@uid' => $uid)));
  47. }
  48. if (!$outfile_name) {
  49. throw new Exception("Please provide an outputfilename");
  50. }
  51. $this->bundle_name = $bundle_name;
  52. $this->entity_ids = $ids;
  53. $this->fields = $fields;
  54. // Make sure the user directory exists
  55. $user_dir = 'public://tripal/users/' . $user->uid;
  56. if (!file_prepare_directory($user_dir, FILE_CREATE_DIRECTORY)) {
  57. $message = 'Could not access the directory on the server for storing this file.';
  58. watchdog('tripal', $message, array(), WATCHDOG_ERROR);
  59. /*drupal_json_output(array(
  60. 'status' => 'failed',
  61. 'message' => $message,
  62. 'file_id' => '',
  63. ));*/
  64. return;
  65. }
  66. $this->outfile = $user_dir. '/' . $outfile_name;
  67. }
  68. /**
  69. * Retrieves the URL for the downloadable file.
  70. */
  71. public function getURL() {
  72. return $this->outfile;
  73. }
  74. /**
  75. * Removes the downloadable file.
  76. */
  77. public function delete() {
  78. $fid = db_select('file_managed', 'fm')
  79. ->fields('fm', array('fid'))
  80. ->condition('uri', $this->outfile)
  81. ->execute()
  82. ->fetchField();
  83. if ($fid) {
  84. $file = file_load($fid);
  85. file_usage_delete($file, 'tripal', 'data-collection');
  86. file_delete($file, TRUE);
  87. }
  88. }
  89. /**
  90. * Creates the downloadable file.
  91. */
  92. public function write() {
  93. global $user;
  94. $fh = fopen(drupal_realpath($this->outfile), "w");
  95. if (!$fh) {
  96. throw new Exception("Cannout open collection file: " . $this->outfile);
  97. }
  98. $headers = $this->getHeader();
  99. if ($headers) {
  100. foreach ($headers as $line) {
  101. fwrite($fh, $line . "\r\n");
  102. }
  103. }
  104. foreach ($this->entity_ids as $entity_id) {
  105. $result = tripal_load_entity('TripalEntity', array($entity_id), FALSE, $this->fields);
  106. reset($result);
  107. $entity = $result[$entity_id];
  108. $lines = $this->formatEntity($entity);
  109. foreach ($lines as $line) {
  110. fwrite($fh, $line . "\r\n");
  111. }
  112. }
  113. fclose($fh);
  114. $file = new stdClass();
  115. $file->uri = $this->outfile;
  116. $file->filename = basename($this->outfile);
  117. $file->filemime = file_get_mimetype($this->outfile);
  118. $file->uid = $user->uid;
  119. $file->status = FILE_STATUS_PERMANENT;
  120. $file = file_save($file);
  121. $fid = $file->fid;
  122. $file = file_load($fid);
  123. // We use the fid for the last argument because these files
  124. // aren't really associated with any entity, but we need a value.
  125. file_usage_add($file, 'tripal', 'data-collection', $fid);
  126. }
  127. /**
  128. * Setups a download stream for the file.
  129. */
  130. public function download() {
  131. }
  132. /**
  133. * Formats the entity and the specified fields for utput.
  134. *
  135. * This function should be implemented by a child class. It should iterate
  136. * over the fields for the entity and return the appropriate format. It may
  137. * return multiple lines of output if necessary.
  138. *
  139. * @param $entity
  140. * The entity object. The fields that should be formatted are already
  141. * loaded.
  142. *
  143. * @return
  144. * An array of strings (one per line of output.
  145. */
  146. abstract protected function formatEntity($entity);
  147. /**
  148. * Retreives header lines
  149. *
  150. * This function should be implemented by a child class. It should return
  151. * the header lines for an output file.
  152. */
  153. abstract protected function getHeader();
  154. }