TripalTabDownloader.inc

File

tripal/includes/TripalFieldDownloaders/TripalTabDownloader.inc
View source
  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. $col = 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. $col[] = '';
  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. $col[] = $value;
  52. }
  53. else {
  54. if (array_key_exists('rdfs:label', $value)) {
  55. $label = $entity->{$field_name}['und'][0]['value']['rdfs:label'];
  56. $col[] = strip_tags($label);
  57. }
  58. elseif (array_key_exists('schema:description', $value)) {
  59. $label = $entity->{$field_name}['und'][0]['value']['schema:description'];
  60. $col[] = strip_tags($label);
  61. }
  62. else {
  63. $col[] = '';
  64. }
  65. // TODO: What to do with fields that are arrays?
  66. }
  67. }
  68. // If we have multiple items then deal with that.
  69. else {
  70. $col[] = '';
  71. // TODO: What to do with fields that have multiple values?
  72. }
  73. }
  74. return array(implode("\t", $col));
  75. }
  76. /**
  77. * @see TripalFieldDownloader::getHeader()
  78. */
  79. protected function getHeader() {
  80. $row = array();
  81. foreach ($this->printable_fields as $accession => $label) {
  82. $row[] = $label;
  83. }
  84. return array(implode("\t", $row));
  85. }
  86. }