You are here

class CSVExporter in Loft Data Grids 6.2

Same name and namespace in other branches
  1. 7.2 vendor/aklump/loft_data_grids/src/AKlump/LoftDataGrids/CSVExporter.php \AKlump\LoftDataGrids\CSVExporter

Class CSVExporter

Hierarchy

Expanded class hierarchy of CSVExporter

File

vendor/aklump/loft_data_grids/src/AKlump/LoftDataGrids/CSVExporter.php, line 7

Namespace

AKlump\LoftDataGrids
View source
class CSVExporter extends Exporter implements ExporterInterface {
  protected $extension = '.csv';
  protected $format;
  protected $page = NULL;

  /**
   * Constructor
   */
  public function __construct(ExportDataInterface $data = NULL, $filename = '') {
    parent::__construct($data, $filename);
    $this->format = new \stdClass();
    $this->format->bol = '';
    $this->format->eol = "\r\n";
    $this->format->left = '"';
    $this->format->right = '"';
    $this->format->sep = ',';
    $this->format->escape = '"';
    $this->format->html = FALSE;
  }
  public function getInfo() {
    $info = parent::getInfo();
    $info = array(
      'name' => 'Comma-separated Values Format',
      'shortname' => 'CSV',
      'description' => 'Export data in the .csv file format.  Fields are wrapped with double quotes, separated by commas.  Lines are separated by \\r\\n',
    ) + $info;
    return $info;
  }
  public function compile($page_id = NULL) {
    $pages = $this
      ->getData()
      ->get();
    if ($page_id === NULL && count($pages) > 1 || !array_key_exists($page_id, $pages)) {
      reset($pages);
      $page_id = key($pages);
    }
    $data = $this
      ->getData()
      ->getPage($page_id);
    $this->output = '';
    $this->output .= $this
      ->collapseRow($this
      ->getHeader($page_id));

    // Format the rows:
    foreach ($data as $row) {
      $this->output .= $this
        ->collapseRow($row);
    }
  }

  /**
   * Collapse a row
   */
  protected function collapseRow($row) {
    $output = '';

    // Check if we're dealing with a simple or complex row
    if (isset($row['data'])) {
      foreach ($row as $key => $value) {
        if ($key == 'data') {
          $cells = $value;
        }
      }
    }
    else {
      $cells = $row;
    }
    $output = array();
    if (count($cells)) {
      foreach ($cells as $column => $cell) {
        $output[] = $this
          ->collapseCell($cell, $column);
      }
    }
    $output = $this->format->bol . implode($this->format->sep, $output) . $this->format->eol;
    return $output;
  }

  /**
   * Collapse a single cell in a row.
   *
   * @param  array $cell
   *
   * @return string
   */
  protected function collapseCell($cell, $column) {

    //compress a complex cell
    if (is_array($cell)) {
      $cell = isset($cell['data']) ? $cell['data'] : '';
    }
    if (!$this->format->html) {
      $cell = strip_tags($cell);
    }

    // Escape chars that conflice with delimiters
    if (!empty($this->format->escape)) {
      $escapeables = array(
        $this->format->left,
        $this->format->right,
      );
      $escapeables = array_filter(array_unique($escapeables));
      foreach ($escapeables as $find) {
        $cell = str_replace($find, $this->format->escape . $find, $cell);
      }
    }
    return $this->format->left . $cell . $this->format->right;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CSVExporter::$extension protected property 3
CSVExporter::$format protected property 2
CSVExporter::$page protected property
CSVExporter::collapseCell protected function Collapse a single cell in a row. 1
CSVExporter::collapseRow protected function Collapse a row 1
CSVExporter::compile public function Build $this->output in prep for export/save Overrides Exporter::compile 2
CSVExporter::getInfo public function Return info about this class Overrides Exporter::getInfo 3
CSVExporter::__construct public function Constructor Overrides Exporter::__construct 3
Exporter::$export_data protected property
Exporter::$header protected property
Exporter::$settings protected property
Exporter::addSetting public function Adds/Updates a single setting by name. Overrides ExporterInterface::addSetting
Exporter::cssSafe protected function
Exporter::export public function Export data as a string Overrides ExporterInterface::export 1
Exporter::filenameSafe protected function Return a string as a safe filename
Exporter::formatColumn public function Format a single column with format by string Overrides ExporterInterface::formatColumn 1
Exporter::getData public function Return the ExportDataInterface object Overrides ExporterInterface::getData
Exporter::getFilename public function Get the filename Overrides ExporterInterface::getFilename
Exporter::getHeader public function Return an array containing the header row values for a page Overrides ExporterInterface::getHeader
Exporter::getSettings public function Return the settings object. Overrides ExporterInterface::getSettings
Exporter::getShowPageIds public function Return the showPageIds. Overrides ExporterInterface::getShowPageIds
Exporter::getTitle public function Overrides ExporterInterface::getTitle
Exporter::hidePageIds public function Set the exporter to hide page ids. Overrides ExporterInterface::hidePageIds
Exporter::save public function Save as a file to the server Overrides ExporterInterface::save 1
Exporter::setData public function Set the export data object Overrides ExporterInterface::setData
Exporter::setFilename public function Getter/Setter for the filename Overrides ExporterInterface::setFilename
Exporter::setSettings public function Set the settings object. Overrides ExporterInterface::setSettings
Exporter::setSettingsDefault protected function Setup default values on object data. 1
Exporter::setTitle public function Set a title for the exported document Overrides ExporterInterface::setTitle 1
Exporter::showPageIds public function Set the exporter to display page ids. Overrides ExporterInterface::showPageIds