TripalCSVDownloader.inc 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. class TripalCSVDownloader extends TripalFieldDownloader {
  3. /**
  4. * Sets the label shown to the user describing this formatter.
  5. */
  6. static public $label = 'CSV (comma separated)';
  7. /**
  8. * Indicates the default extension for the outputfile.
  9. */
  10. static public $default_extension = 'csv';
  11. /**
  12. * @see TripalFieldDownloader::format()
  13. */
  14. protected function formatEntity($entity) {
  15. $row = array();
  16. foreach ($this->fields as $field_id) {
  17. $field = field_info_field_by_id($field_id);
  18. $field_name = $field['field_name'];
  19. if (!property_exists($entity, $field_name)) {
  20. continue;
  21. }
  22. // If we only have one element then this is good.
  23. if (count($entity->{$field_name}['und']) == 1) {
  24. $value = $entity->{$field_name}['und'][0]['value'];
  25. // If the single element is not an array then this is good.
  26. if (!is_array($value)) {
  27. $row[] = '"' . $value . '"';
  28. }
  29. else {
  30. if (array_key_exists('rdfs:label', $entity->{$field_name}['und'][0]['value'])) {
  31. $row[] = strip_tags($entity->{$field_name}['und'][0]['value']['rdfs:label']);
  32. }
  33. else {
  34. $row[] = '';
  35. }
  36. // TODO: What to do with fields that are arrays?
  37. }
  38. }
  39. else {
  40. $row[] = '';
  41. // TODO: What to do with fields that have multiple values?
  42. }
  43. }
  44. return array(implode(',', $row));
  45. }
  46. /**
  47. * @see TripalFieldDownloader::getHeader()
  48. */
  49. protected function getHeader() {
  50. $row = array();
  51. foreach ($this->fields as $field_id) {
  52. $field = field_info_field_by_id($field_id);
  53. $field_name = $field['field_name'];
  54. $instance = field_info_instance('TripalEntity', $field_name, $this->bundle_name);
  55. $row[] = '"' . $instance['label'] . '"';
  56. }
  57. return array(implode(',', $row));
  58. }
  59. }