TripalTabDownloader.inc 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. $row = array();
  31. // Iterate through all of the printable fields and add the value to
  32. // the row.
  33. foreach ($this->printable_fields as $accession => $label) {
  34. // If this field is not present for this entity then add an empty
  35. // element and move on.
  36. if (!array_key_exists($accession, $this->fields2terms[$bundle_name]['by_accession'])) {
  37. $row[] = '';
  38. continue;
  39. }
  40. // Get the field from the class variables.
  41. $field_id = $this->fields2terms[$bundle_name]['by_accession'][$accession];
  42. $field = $this->fields[$bundle_name][$field_id]['field'];
  43. $instance = $this->fields[$bundle_name][$field_id]['instance'];
  44. $field_name = $field['field_name'];
  45. // If we only have one item for this value then add it.
  46. if (count($entity->{$field_name}['und']) == 1) {
  47. $value = $entity->{$field_name}['und'][0]['value'];
  48. // If the single element is not an array then this is good.
  49. if (!is_array($value)) {
  50. $row[] = $value;
  51. }
  52. else {
  53. if (array_key_exists('rdfs:label', $entity->{$field_name}['und'][0]['value'])) {
  54. $row[] = $tabs . strip_tags($entity->{$field_name}['und'][0]['value']['rdfs:label']);
  55. }
  56. else {
  57. $row[] = '';
  58. }
  59. // TODO: What to do with fields that are arrays?
  60. }
  61. }
  62. // If we have multiple items then deal with that.
  63. else {
  64. $row[] = '';
  65. // TODO: What to do with fields that have multiple values?
  66. }
  67. }
  68. return array(implode("\t", $row));
  69. }
  70. /**
  71. * @see TripalFieldDownloader::getHeader()
  72. */
  73. protected function getHeader() {
  74. $row = array();
  75. foreach ($this->printable_fields as $accession => $label) {
  76. $row[] = $label;
  77. }
  78. return array(implode("\t", $row));
  79. }
  80. }