TripalCSVDownloader.inc

File

tripal/includes/TripalFieldDownloaders/TripalCSVDownloader.inc
View source
  1. <?php
  2. class TripalCSVDownloader 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 = 'CSV';
  9. /**
  10. * A more verbose label that better describes the formatter.
  11. */
  12. static public $full_label = 'Comma separated';
  13. /**
  14. * Indicates the default extension for the outputfile.
  15. */
  16. static public $default_extension = 'csv';
  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::format()
  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. if (is_numeric($value) or !$value) {
  52. $col[] = $value;
  53. }
  54. else {
  55. $col[] = '"' . $value . '"';
  56. }
  57. }
  58. else {
  59. if (array_key_exists('rdfs:label', $value)) {
  60. $col[] = '"' . strip_tags($entity->{$field_name}['und'][0]['value']['rdfs:label']) . '"';
  61. }
  62. elseif (array_key_exists('schema:description', $value)) {
  63. $label = $entity->{$field_name}['und'][0]['value']['schema:description'];
  64. $col[] = strip_tags($label);
  65. }
  66. else {
  67. $col[] = '';
  68. }
  69. // TODO: What to do with fields that are arrays?
  70. }
  71. }
  72. // If we have multiple items then deal with that.
  73. else {
  74. $col[] = '';
  75. // TODO: What to do with fields that have multiple values?
  76. }
  77. }
  78. return array(implode(",", $col));
  79. }
  80. /**
  81. * @see TripalFieldDownloader::getHeader()
  82. */
  83. protected function getHeader() {
  84. $row = array();
  85. foreach ($this->printable_fields as $accession => $label) {
  86. $row[] = $label;
  87. }
  88. return array(implode(",", $row));
  89. }
  90. }