You are here

function theme_vbo_export_csv in VBO export 7

Csv content builder function.

File

./vbo_export.theme.inc, line 11
Contains module theme functions.

Code

function theme_vbo_export_csv($variables) {

  // Sanitize data.
  foreach ($variables['header'] as $key => $item) {
    $variables['header'][$key] = strtr($item, array(
      $variables['separator'] => ' ',
    ));
  }
  $content_replacements = array(
    "\r\n" => ' ',
    "\n\r" => ' ',
    "\r" => ' ',
    "\n" => ' ',
    "\t" => ' ',
  );
  foreach ($variables['rows'] as $row_key => $row) {
    foreach ($row as $cell_key => $cell) {
      $variables['rows'][$row_key][$cell_key] = strtr($cell, $content_replacements);

      // Handle separator occurrences in cells.
      if (strpos($cell, $variables['separator']) !== FALSE) {
        $variables['rows'][$row_key][$cell_key] = '"' . strtr($variables['rows'][$row_key][$cell_key], array(
          '"' => '""',
        )) . '"';
      }
    }
  }

  // Generate output.
  $csv_rows = array();
  $csv_rows[] = implode($variables['separator'], $variables['header']);
  foreach ($variables['rows'] as $row) {
    $csv_rows[] = implode($variables['separator'], $row);
  }
  $output = implode(PHP_EOL, $csv_rows);

  // Add BOM for better Excel reading.
  $output = chr(0xef) . chr(0xbb) . chr(0xbf) . $output;
  return $output;
}