Gentics Portal.Node PHP API
 All Classes Namespaces Functions Variables Pages
Public Member Functions | Static Public Member Functions | Public Attributes | Protected Member Functions | Protected Attributes | List of all members
ECSVExport Class Reference

Public Member Functions

 __construct ($dataProvider, $exportFull=true, $includeColumnHeaders=true, $delimiter=null, $enclosure=null)
 getDataProvider ()
 dontConvertProvider ()
 setToAppend ()
 setDelimiter ($delimiter)
 getDelimiter ()
 setEnclosure ($enclosure)
 getEnclosure ()
 setOutputFile ($filename)
 getOutputFile ()
 setCallback ($callback)
 getCallback ()
 setHeaders (array $headers)
 getHeaders ()
 setHeader ($key, $value)
 setExclude ($noshow)
 getExclude ()
 getModelRelations ()
 setModelRelations (array $relations)
 exportCurrentPageOnly ()
 toCSV ($outputFile=null, $delimiter=null, $enclosure=null, $includeHeaders=true)
 _loopRows (&$dp)
 _writeRow ($row)

Static Public Member Functions

static lambdaFail (&$value, $key)
static stripSlashes (&$value, $key)

Public Attributes

 $includeColumnHeaders = true
 $stripNewLines = true
 $exportFull = true
 $convertActiveDataProvider = true

Protected Member Functions

 _writeData ()
 _writeHeaders ($row)

Protected Attributes

 $_outputFile
 $_filePointer
 $_dataProvider
 $_callback
 $_headers = array()
 $_exclude = array()
 $_delimiter = ","
 $_enclosure = '"'
 $_appendCsv = false
 $_modelRelations = array()

Detailed Description

export a csv file (string) from given CSqlDataProvider usage in your controller: Yii::import('ext.CSVExport'); $provider = YourClass::model()->createCSqlProvider() or $provider = Yii::app()->db->creatCommand(...)->queryAll(); $csv = new ECSVExport($provider); $content = $csv->toCSV(); Yii::app()->getRequest()->sendFile($filename, $content, "text/csv", false); exit();

You can also do this now: $cmd = Yii::app()->db->createCommand("SELECT * FROM track_test LIMIT 10"); $csv = new ECSVExport($cmd); $csv->setOutputFile($outputFile); $csv->toCSV();

Author
Kenrick Buchanan
Version
0.6.8

Definition at line 25 of file ECSVExport.php.

Constructor & Destructor Documentation

ECSVExport::__construct (   $dataProvider,
  $exportFull = true,
  $includeColumnHeaders = true,
  $delimiter = null,
  $enclosure = null 
)
Parameters
mixed$dataProviderarray|CSqlDataProvider|CActiveDataProvider|CDbCommand
boolean$exportFull
boolean$includeColumnHeaders
string$delimiter
string$enclosure

Definition at line 124 of file ECSVExport.php.

References $exportFull, and $includeColumnHeaders.

{
$this->_dataProvider = $dataProvider;
$this->exportFull = (bool) $exportFull;
$this->includeColumnHeaders = (bool) $includeColumnHeaders;
if($delimiter) $this->_delimiter = $delimiter;
if($enclosure) $this->_enclosure = $enclosure;
}

Member Function Documentation

ECSVExport::_loopRows ( $dp)

loop through result set

Parameters
mixedCDbDataReader|array $dp

Definition at line 511 of file ECSVExport.php.

{
$firstTimeThrough = true;
if($dp instanceof CDbDataReader) {
while(($row = $dp->read()) !== false) {
if($firstTimeThrough) {
$this->_writeHeaders($row);
$firstTimeThrough = false;
}
$this->_writeRow($row);
}
} else {
$total = count($dp);
for($i=0; $i<$total; $i++) {
if($firstTimeThrough) {
$this->_writeHeaders($dp[$i]);
$firstTimeThrough = false;
}
$this->_writeRow($dp[$i]);
}
}
}
ECSVExport::_writeData ( )
protected

where the magic happens. depending on type of dataProvider, it uses different methods to get the data efficiently and to write to file pointer in memory. the most effecient is either a small array or by just passing in a CDbCommand instance

Exceptions
Exception- no data found

Definition at line 411 of file ECSVExport.php.

References _writeHeaders(), and _writeRow().

Referenced by toCSV().

{
$firstTimeThrough = true;
if($this->_dataProvider instanceof CActiveDataProvider) {
if($this->exportFull) {
// set pagination to off
$this->_dataProvider->setPagination(false);
}
if($this->convertActiveDataProvider) {
$criteria = $this->_dataProvider->getCriteria();
$model = $this->_dataProvider->model;
$criteria = $model->getCommandBuilder()
->createCriteria($criteria,array());
$this->_dataProvider = $model->getCommandBuilder()
->createFindCommand($model->getTableSchema(),
$criteria);
unset($model, $criteria);
} else {
// suggested implementation from marcovtwout
$models = $this->_dataProvider->getData();
$dataReader = array();
$attributes = $this->_dataProvider->model->getMetaData()->columns;
// since we are already looping through results, don't bother
// passing results to _loopRow, just write it here.
foreach ($models as &$model) {
$row = array();
foreach ($attributes as $attribute => $col) {
$row[$attribute] = $model->{$attribute};
}
// check model relations
if(count($this->_modelRelations)) {
foreach($this->_modelRelations as $relation=>$value) {
if(is_array($value)) {
foreach($value as $subvalue) {
if(isset($model->$relation->$subvalue) && $model->$relation->$subvalue)
$row[$relation.'['.$subvalue.']'] = $model->$relation->$subvalue;
}
} else {
if(isset($model->$relation->$value) && $model->$relation->$value)
$row[$relation.'['.$value.']'] = $model->$relation->$value;
}
}
}
if($firstTimeThrough) {
$this->_writeHeaders($row);
$firstTimeThrough = false;
}
$this->_writeRow($row);
}
unset($models, $attributes);
return;
}
}
if($this->_dataProvider instanceof CSqlDataProvider) {
if($this->exportFull) {
$this->_dataProvider->setId('csvexport');
$this->_dataProvider->getPagination()->setItemCount($this->_dataProvider->getTotalItemCount());
$pageVar = $this->_dataProvider->getPagination()->pageVar;
$_GET[$pageVar] = 0;
$totalPages = $this->_dataProvider->getPagination()->getPageCount();
$this->setToAppend();
for($i=1; $i<=$totalPages; $i++) {
$_GET[$pageVar] = $i;
$this->_dataProvider->getPagination()->setCurrentPage($i);
$_getData = $this->_dataProvider->getData();
$this->_loopRows($_getData);
$this->includeColumnHeaders = !(bool) $i;
}
} else {
$this->_loopRows($this->_dataProvider->getData());
}
return;
}
if($this->_dataProvider instanceof CDbCommand) {
$dataReader = $this->_dataProvider->query();
$this->_loopRows($dataReader);
return;
}
if(is_array($this->_dataProvider)) {
$this->_loopRows($this->_dataProvider);
return;
}
// if program made it this far something happened
throw new Exception('Data source failed to retrieve data, are you sure you passed something useable?');
}
ECSVExport::_writeHeaders (   $row)
protected

Write headers to csv file, taking into account string replacements and exclusions

Parameters
array$row
Returns
void

Definition at line 539 of file ECSVExport.php.

Referenced by _writeData().

{
if(!$this->includeColumnHeaders) {
return;
}
if($row instanceof CActiveRecord) {
$headers = array_keys($row->getAttributes());
} else {
$headers = array_keys($row);
}
// remove excluded
if(count($this->_exclude) > 0) {
foreach($this->_exclude as $e) {
$key = array_search($e, $headers);
if($key !== false) {
unset($headers[$key]);
}
}
}
if(count($this->_headers) > 0) {
foreach($headers as &$header) {
if(array_key_exists($header, $this->_headers)) {
$header = $this->_headers[$header];
}
}
}
fputcsv($this->_filePointer, $headers, $this->_delimiter, $this->_enclosure);
}
ECSVExport::_writeRow (   $row)

Write array row to current {$this->_filePointer}, taking into account exclusions

Parameters
array$row
Returns
void

Definition at line 577 of file ECSVExport.php.

Referenced by _writeData().

{
if($row instanceof CActiveRecord) {
$row = $row->getAttributes();
}
// remove excluded
if(count($this->_exclude) > 0) {
foreach($this->_exclude as $e) {
if(array_key_exists($e, $row)) {
unset($row[$e]);
}
}
}
if($this->stripNewLines) {
array_walk($row, array('ECSVExport','lambdaFail'));
}
array_walk($row, array('ECSVExport','stripSlashes'));
if(isset($this->_callback) && $this->_callback) {
fputcsv($this->_filePointer, call_user_func($this->_callback, $row), $this->_delimiter, $this->_enclosure);
} else {
fputcsv($this->_filePointer, $row, $this->_delimiter, $this->_enclosure);
}
unset($row);
}
ECSVExport::dontConvertProvider ( )

call this function to not force CActiveDataProvider to be converted to command for speed and memory at the expense of losing the with() functionality

Returns

Definition at line 148 of file ECSVExport.php.

{
$this->convertActiveDataProvider = false;
return $this;
}
ECSVExport::exportCurrentPageOnly ( )

turn off going through whole resultset, taking current page into account

Returns

Definition at line 331 of file ECSVExport.php.

{
$this->exportFull = false;
return $this;
}
ECSVExport::getCallback ( )

get per row function

Returns
mixed $this->_callback

Definition at line 247 of file ECSVExport.php.

References $_callback.

{
}
ECSVExport::getDataProvider ( )

get data provider

Returns
mixed $this->_dataProvider

Definition at line 137 of file ECSVExport.php.

References $_dataProvider.

{
}
ECSVExport::getDelimiter ( )

get current delimiter

Returns
type

Definition at line 180 of file ECSVExport.php.

References $_delimiter.

{
}
ECSVExport::getEnclosure ( )
Returns
string

Definition at line 201 of file ECSVExport.php.

References $_enclosure.

{
}
ECSVExport::getExclude ( )

get excluded fields

Returns
array $this->_exclude

Definition at line 303 of file ECSVExport.php.

References $_exclude.

{
}
ECSVExport::getHeaders ( )

get current csv headers

Returns
array $this->_headers

Definition at line 267 of file ECSVExport.php.

References $_headers.

{
}
ECSVExport::getModelRelations ( )

get the set model relations return array $this->_modelRelations

Definition at line 312 of file ECSVExport.php.

{
return $this->_modelRelations;
}
ECSVExport::getOutputFile ( )

get output file

Returns
string

Definition at line 222 of file ECSVExport.php.

References $_outputFile.

{
}
ECSVExport::setCallback (   $callback)

function to be called for each row in set

Parameters
mixedcallable|array $callback
Returns
Exceptions
Exceptionon uncallable variable

Definition at line 233 of file ECSVExport.php.

{
if(is_callable($callback)) {
$this->_callback = $callback;
return $this;
} else {
throw new Exception('Callback must be callable. Duh.');
}
}
ECSVExport::setDelimiter (   $delimiter)

set csv delimiter, defaults to ,

Parameters
type$delimiter
Returns

Definition at line 170 of file ECSVExport.php.

{
$this->_delimiter = $delimiter;
return $this;
}
ECSVExport::setEnclosure (   $enclosure)

set csv enclosure, defaults to "

Parameters
type$enclosure
Returns

Definition at line 191 of file ECSVExport.php.

{
$this->_enclosure = $enclosure;
return $this;
}
ECSVExport::setExclude (   $noshow)
Parameters
type$noshow
Returns

Definition at line 289 of file ECSVExport.php.

{
if(is_array($noshow)) {
$this->_exclude = $noshow;
return $this;
} else {
$this->_exclude[] = (string) $noshow;
}
}
ECSVExport::setHeader (   $key,
  $value 
)
Parameters
string$key
string$value
Returns

Definition at line 278 of file ECSVExport.php.

{
$this->_headers[$key] = $value;
return $this;
}
ECSVExport::setHeaders ( array  $headers)

existing column names remapped to other strings resultcolumn=>new name

Parameters
array$headers
Returns

Definition at line 257 of file ECSVExport.php.

{
$this->_headers = $headers;
return $this;
}
ECSVExport::setModelRelations ( array  $relations)

set relations to include on output that will be interpolated via model crap needs to be array of arrays

Parameters
array$relations

Definition at line 322 of file ECSVExport.php.

{
$this->_modelRelations = $relations;
}
ECSVExport::setOutputFile (   $filename)

set filename of csv file you want to create

Parameters
type$filename
Returns

Definition at line 212 of file ECSVExport.php.

Referenced by toCSV().

{
$this->_outputFile = $filename;
return $this;
}
ECSVExport::setToAppend ( )

sets flag to have it append to file instead of just overwriting it

Returns
void

Definition at line 158 of file ECSVExport.php.

{
$this->_appendCsv = true;
return $this;
}
ECSVExport::toCSV (   $outputFile = null,
  $delimiter = null,
  $enclosure = null,
  $includeHeaders = true 
)

create a csv string, or file if $outputFile is set

Parameters
string$outputFile
string$delimiter
string$enclosure
boolean$includeHeaders
Returns
mixed string|boolean|integer csv string when no outputFile is specified boolean if the writing failed, or integer of num bytes written to file

Definition at line 347 of file ECSVExport.php.

References _writeData(), and setOutputFile().

{
// check that data provider is something useful
$isGood = false;
if($this->_dataProvider instanceof CActiveDataProvider) {
$isGood = true;
}
if($this->_dataProvider instanceof CSqlDataProvider) {
$isGood = true;
}
if($this->_dataProvider instanceof CDbCommand) {
$isGood = true;
}
if(is_array($this->_dataProvider)) {
$isGood = true;
}
if(!$isGood) {
throw new Exception('Bad data provider given as source to '.__CLASS__);
}
if($outputFile !== null) {
$this->setOutputFile($outputFile);
}
if(!$includeHeaders) {
$this->includeColumnHeaders = false;
}
if($delimiter !== null) {
$this->_delimiter = $delimiter;
}
if($enclosure !== null) {
$this->_enclosure = $enclosure;
}
// create file pointer
$this->_filePointer = fopen("php://temp", 'w');
$this->_writeData();
rewind($this->_filePointer);
// make sure you can write to file!
if($this->_outputFile !== null) {
// write stream to file
return $this->_appendCsv ? file_put_contents($this->_outputFile, $this->_filePointer, FILE_APPEND | LOCK_EX)
: file_put_contents($this->_outputFile, $this->_filePointer, LOCK_EX);
} else {
return stream_get_contents($this->_filePointer);
}
}

Member Data Documentation

callable ECSVExport::$_callback
protected

per row callable function

Definition at line 74 of file ECSVExport.php.

Referenced by getCallback().

mixed CSqlDataProvider Array CActiveDataProvider CDbCommand ECSVExport::$_dataProvider
protected

data provider that will generate or contain resultset

Definition at line 68 of file ECSVExport.php.

Referenced by getDataProvider().

string ECSVExport::$_delimiter = ","
protected

column delimiter

Definition at line 93 of file ECSVExport.php.

Referenced by getDelimiter().

string ECSVExport::$_enclosure = '"'
protected

string to enclose fields when delimiter is found in field

Definition at line 99 of file ECSVExport.php.

Referenced by getEnclosure().

array ECSVExport::$_exclude = array()
protected

columns to exclude from final file

Definition at line 87 of file ECSVExport.php.

Referenced by getExclude().

resource ECSVExport::$_filePointer
protected

stream pointer

Definition at line 62 of file ECSVExport.php.

array ECSVExport::$_headers = array()
protected

csv headers

Definition at line 80 of file ECSVExport.php.

Referenced by getHeaders().

string ECSVExport::$_outputFile
protected

string filename

Definition at line 56 of file ECSVExport.php.

Referenced by getOutputFile().

boolean ECSVExport::$convertActiveDataProvider = true

convert activedata provider to a cdbcommand for speed

Definition at line 49 of file ECSVExport.php.

boolean ECSVExport::$exportFull = true

run through whole resultset, appending to output stream, using paging (if on)

Definition at line 43 of file ECSVExport.php.

Referenced by __construct().

boolean ECSVExport::$includeColumnHeaders = true

show column headers in csv file

Definition at line 31 of file ECSVExport.php.

Referenced by __construct().


The documentation for this class was generated from the following file: