TripalTabDownloader.inc 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. class TripalTabDownloader extends TripalFieldDownloader {
  3. /**
  4. * Sets the label shown to the user describing this formatter. It
  5. * should be a short identifier. Use the $full_label for a more
  6. * descriptive label.
  7. */
  8. static public $label = 'TAB';
  9. /**
  10. * A more verbose label that better describes the formatter.
  11. */
  12. static public $full_label = 'Tab delimeted';
  13. /**
  14. * Indicates the default extension for the outputfile.
  15. */
  16. static public $default_extension = 'txt';
  17. /**
  18. * @see TripalFieldDownloader::isFieldSupported()
  19. */
  20. public function isFieldSupported($field, $instance) {
  21. $is_supported = parent::isFieldSupported($field, $instance);
  22. // For now all fields are supported.
  23. return TRUE;
  24. }
  25. /**
  26. * @see TripalFieldDownloader::formatEntity()
  27. */
  28. protected function formatEntity($entity) {
  29. $bundle_name = $entity->bundle;
  30. $site = !property_exists($entity, 'site_id') ? 'local' : $entity->site_id;
  31. $row = array();
  32. // Iterate through all of the printable fields and add the value to
  33. // the row.
  34. foreach ($this->printable_fields as $accession => $label) {
  35. // If this field is not present for this entity then add an empty
  36. // element and move on.
  37. if (!array_key_exists($accession, $this->fields2terms[$site][$bundle_name]['by_accession'])) {
  38. $row[] = '';
  39. continue;
  40. }
  41. // Get the field from the class variables.
  42. $field_id = $this->fields2terms[$site][$bundle_name]['by_accession'][$accession];
  43. $field = $this->fields[$site][$bundle_name][$field_id]['field'];
  44. $instance = $this->fields[$site][$bundle_name][$field_id]['instance'];
  45. $field_name = $field['field_name'];
  46. // If we only have one item for this value then add it.
  47. if (count($entity->{$field_name}['und']) == 1) {
  48. $value = $entity->{$field_name}['und'][0]['value'];
  49. // If the single element is not an array then this is good.
  50. if (!is_array($value)) {
  51. $row[] = $value;
  52. }
  53. else {
  54. if (array_key_exists('rdfs:label', $entity->{$field_name}['und'][0]['value'])) {
  55. $row[] = $tabs . strip_tags($entity->{$field_name}['und'][0]['value']['rdfs:label']);
  56. }
  57. else {
  58. $row[] = '';
  59. }
  60. // TODO: What to do with fields that are arrays?
  61. }
  62. }
  63. // If we have multiple items then deal with that.
  64. else {
  65. $row[] = '';
  66. // TODO: What to do with fields that have multiple values?
  67. }
  68. }
  69. return array(implode("\t", $row));
  70. }
  71. /**
  72. * @see TripalFieldDownloader::getHeader()
  73. */
  74. protected function getHeader() {
  75. $row = array();
  76. foreach ($this->printable_fields as $accession => $label) {
  77. $row[] = $label;
  78. }
  79. return array(implode("\t", $row));
  80. }
  81. }