37 public $stripNewLines =
true;
106 protected $_appendCsv =
false;
114 protected $_modelRelations = array();
126 $this->_dataProvider = $dataProvider;
129 if($delimiter) $this->_delimiter = $delimiter;
130 if($enclosure) $this->_enclosure = $enclosure;
150 $this->convertActiveDataProvider =
false;
160 $this->_appendCsv =
true;
172 $this->_delimiter = $delimiter;
193 $this->_enclosure = $enclosure;
214 $this->_outputFile = $filename;
235 if(is_callable($callback)) {
236 $this->_callback = $callback;
239 throw new Exception(
'Callback must be callable. Duh.');
259 $this->_headers = $headers;
280 $this->_headers[$key] = $value;
291 if(is_array($noshow)) {
292 $this->_exclude = $noshow;
295 $this->_exclude[] = (string) $noshow;
314 return $this->_modelRelations;
324 $this->_modelRelations = $relations;
333 $this->exportFull =
false;
347 public function toCSV($outputFile=null, $delimiter=null, $enclosure=null, $includeHeaders=
true)
352 if($this->_dataProvider instanceof CActiveDataProvider) {
356 if($this->_dataProvider instanceof CSqlDataProvider) {
360 if($this->_dataProvider instanceof CDbCommand) {
364 if(is_array($this->_dataProvider)) {
369 throw new Exception(
'Bad data provider given as source to '.__CLASS__);
372 if($outputFile !== null) {
376 if(!$includeHeaders) {
377 $this->includeColumnHeaders =
false;
380 if($delimiter !== null) {
381 $this->_delimiter = $delimiter;
384 if($enclosure !== null) {
385 $this->_enclosure = $enclosure;
389 $this->_filePointer = fopen(
"php://temp",
'w');
391 rewind($this->_filePointer);
394 if($this->_outputFile !== null) {
396 return $this->_appendCsv ? file_put_contents($this->_outputFile, $this->_filePointer, FILE_APPEND | LOCK_EX)
397 : file_put_contents($this->_outputFile, $this->_filePointer, LOCK_EX);
400 return stream_get_contents($this->_filePointer);
413 $firstTimeThrough =
true;
414 if($this->_dataProvider instanceof CActiveDataProvider) {
415 if($this->exportFull) {
417 $this->_dataProvider->setPagination(
false);
419 if($this->convertActiveDataProvider) {
420 $criteria = $this->_dataProvider->getCriteria();
421 $model = $this->_dataProvider->model;
422 $criteria = $model->getCommandBuilder()
423 ->createCriteria($criteria,array());
424 $this->_dataProvider = $model->getCommandBuilder()
425 ->createFindCommand($model->getTableSchema(),
427 unset($model, $criteria);
430 $models = $this->_dataProvider->getData();
431 $dataReader = array();
432 $attributes = $this->_dataProvider->model->getMetaData()->columns;
437 foreach ($models as &$model) {
440 foreach ($attributes as $attribute => $col) {
441 $row[$attribute] = $model->{$attribute};
445 if(count($this->_modelRelations)) {
446 foreach($this->_modelRelations as $relation=>$value) {
447 if(is_array($value)) {
448 foreach($value as $subvalue) {
449 if(isset($model->$relation->$subvalue) && $model->$relation->$subvalue)
450 $row[$relation.
'['.$subvalue.
']'] = $model->$relation->$subvalue;
453 if(isset($model->$relation->$value) && $model->$relation->$value)
454 $row[$relation.
'['.$value.
']'] = $model->$relation->$value;
459 if($firstTimeThrough) {
461 $firstTimeThrough =
false;
465 unset($models, $attributes);
470 if($this->_dataProvider instanceof CSqlDataProvider) {
471 if($this->exportFull) {
472 $this->_dataProvider->setId(
'csvexport');
473 $this->_dataProvider->getPagination()->setItemCount($this->_dataProvider->getTotalItemCount());
474 $pageVar = $this->_dataProvider->getPagination()->pageVar;
476 $totalPages = $this->_dataProvider->getPagination()->getPageCount();
478 for($i=1; $i<=$totalPages; $i++) {
479 $_GET[$pageVar] = $i;
480 $this->_dataProvider->getPagination()->setCurrentPage($i);
481 $_getData = $this->_dataProvider->getData();
483 $this->includeColumnHeaders = !(bool) $i;
486 $this->
_loopRows($this->_dataProvider->getData());
492 if($this->_dataProvider instanceof CDbCommand) {
493 $dataReader = $this->_dataProvider->query();
498 if(is_array($this->_dataProvider)) {
504 throw new Exception(
'Data source failed to retrieve data, are you sure you passed something useable?');
511 public function _loopRows(&$dp)
513 $firstTimeThrough =
true;
514 if($dp instanceof CDbDataReader) {
515 while(($row = $dp->read()) !==
false) {
516 if($firstTimeThrough) {
517 $this->_writeHeaders($row);
518 $firstTimeThrough =
false;
520 $this->_writeRow($row);
524 for($i=0; $i<$total; $i++) {
525 if($firstTimeThrough) {
526 $this->_writeHeaders($dp[$i]);
527 $firstTimeThrough =
false;
529 $this->_writeRow($dp[$i]);
539 protected function _writeHeaders($row)
541 if(!$this->includeColumnHeaders) {
545 if($row instanceof CActiveRecord) {
546 $headers = array_keys($row->getAttributes());
548 $headers = array_keys($row);
552 if(count($this->_exclude) > 0) {
553 foreach($this->_exclude as $e) {
554 $key = array_search($e, $headers);
556 unset($headers[$key]);
561 if(count($this->_headers) > 0) {
562 foreach($headers as &$header) {
563 if(array_key_exists($header, $this->_headers)) {
564 $header = $this->_headers[$header];
569 fputcsv($this->_filePointer, $headers, $this->_delimiter, $this->_enclosure);
577 public function _writeRow($row)
579 if($row instanceof CActiveRecord) {
580 $row = $row->getAttributes();
583 if(count($this->_exclude) > 0) {
584 foreach($this->_exclude as $e) {
585 if(array_key_exists($e, $row)) {
591 if($this->stripNewLines) {
592 array_walk($row, array(
'ECSVExport',
'lambdaFail'));
595 array_walk($row, array(
'ECSVExport',
'stripSlashes'));
597 if(isset($this->_callback) && $this->_callback) {
598 fputcsv($this->_filePointer, call_user_func($this->_callback, $row), $this->_delimiter, $this->_enclosure);
600 fputcsv($this->_filePointer, $row, $this->_delimiter, $this->_enclosure);
605 public static function lambdaFail(&$value, $key)
607 $value = str_replace(
"\r\n",
" ", $value);
610 public static function stripSlashes(&$value, $key)
612 $value = stripslashes($value);
613 $value = str_replace(
'\"',
'"', $value);