<?php

class ExportAction extends CAction {

    public $modelClass;
    public $criteria;

    public function run($to, $criteria = NULL, $columns) {

        $to = strtoupper($to);
        if (method_exists($this, "exportTo{$to}")) {
            call_user_func_array(array($this, "exportTo{$to}"), array($criteria, $columns));
        }
    }

    private function exportToCSV($criteria = NULL, $columns = null) {
        Yii::import('ext.export.ECSVExport');
        if (isset($criteria) && $criteria != NULL) {
            $criteria = $_SESSION[$this->modelClass . 'FilteredData'];
            if (isset($columns) && $columns != "") {
                $columns = str_replace('"', '', $columns);
                $col = explode(',', $columns);
                $criteria->select = $col;
            }
        }
        $data = $criteria == NULL ? (CActiveRecord::model($this->modelClass)->findAll()) : CActiveRecord::model($this->modelClass)->commandBuilder->createFindCommand(CActiveRecord::model($this->modelClass)->tableName(), $criteria)->queryAll();
        $csv = new ECSVExport($data);
        $content = $csv->toCSV();
        Yii::app()->getRequest()->sendFile("{$this->modelClass}.csv", $content, "text/csv", false);
        exit();
    }

    private function exportToEXCEL($criteria = NULL, $columns = null) {
        Yii::import('ext.phpexcel.JPhpExcel');
        if (isset($criteria) && $criteria != NULL) {
            $criteria = $_SESSION[$this->modelClass . 'FilteredData'];
            if (isset($columns) && $columns != "") {
                $columns = str_replace('"', '', $columns);
                $col = explode(',', $columns);
                $criteria->select = $col;
            }
        }
        $result = array();
        $data = $criteria == NULL ? (CActiveRecord::model($this->modelClass)->findAll()) : CActiveRecord::model($this->modelClass)->commandBuilder->createFindCommand(CActiveRecord::model($this->modelClass)->tableName(), $criteria)->queryAll();
        $header = array();
        if (isset($data[0]) && $data [0] != null)
            $header[] = array_keys($data[0]); //to get field names.
        array_splice($data, 0, 0, $header);
        $result = $data;

        $xls = new JPhpExcel('UTF-8', true, $this->modelClass);
        $xls->addArray($result);
        $xls->generateXML($this->modelClass);
    }

    private
            function exportToJSON($criteria = NULL, $columns = null) {
        if (isset($criteria) && $criteria != NULL) {
            $criteria = $_SESSION[$this->modelClass . 'FilteredData'];
            if (isset($columns) && $columns != "") {
                $columns = str_replace('"', '', $columns);
                $col = explode(',', $columns);
                $criteria->select = $col;
            }
        }
        $data = $criteria == NULL ? ( CActiveRecord::model($this->modelClass)->findAll()) : CActiveRecord::model($this->modelClass)->commandBuilder->createFindCommand(CActiveRecord::model($this->modelClass)->tableName(), $criteria)->queryAll();
        header('Content-type: application/json');
        echo CJSON::encode($data);
        Yii::app()->end();
    }

}
