protected function TripalCSVDownloader::formatEntity

3.x TripalCSVDownloader.inc protected TripalCSVDownloader::formatEntity($entity)

Overrides TripalFieldDownloader::formatEntity

See also

TripalFieldDownloader::format()

File

tripal/includes/TripalFieldDownloaders/TripalCSVDownloader.inc, line 34

Class

TripalCSVDownloader

Code

protected function formatEntity($entity) {
  $bundle_name = $entity->bundle;
  $site = !property_exists($entity, 'site_id') ? 'local' : $entity->site_id;
  $col = array();

  // Iterate through all of the printable fields and add the value to
  // the row.
  foreach ($this->printable_fields as $accession => $label) {

    // If this field is not present for this entity then add an empty
    // element and move on.
    if (!array_key_exists($accession, $this->fields2terms[$site][$bundle_name]['by_accession'])) {
      $col[] = '';
      continue;
    }

    // Get the field from the class variables.
    $field_id = $this->fields2terms[$site][$bundle_name]['by_accession'][$accession];
    $field = $this->fields[$site][$bundle_name][$field_id]['field'];
    $instance = $this->fields[$site][$bundle_name][$field_id]['instance'];
    $field_name = $field['field_name'];

    // If we only have one item for this value then add it.
    if (count($entity->{$field_name}['und']) == 1) {
      $value = $entity->{$field_name}['und'][0]['value'];

      // If the single element is not an array then this is good.
      if (!is_array($value)) {
        if (is_numeric($value) or !$value) {
          $col[] = $value;
        }
        else {
          $col[] = '"' . $value . '"';
        }
      }
      else {
        if (array_key_exists('rdfs:label', $value)) {
          $col[] = '"' . strip_tags($entity->{$field_name}['und'][0]['value']['rdfs:label']) . '"';
        }
        elseif (array_key_exists('schema:description', $value)) {
          $label = $entity->{$field_name}['und'][0]['value']['schema:description'];
          $col[] = strip_tags($label);
        }
        else {
          $col[] = '';
        }
        // TODO: What to do with fields that are arrays?
      }
    }
    // If we have multiple items then deal with that.
    else {
      $col[] = '';
      // TODO: What to do with fields that have multiple values?
    }
  }
  return array(implode(",", $col));
}