HtmlExportCsv.php 2.52 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
<?php
/*
 * ExportCsvAction
 * 
 * To use this action you must register it under the controller
 * actions
 * 
 * public function actions()
 *  {
 *      return array(
           'export'=>array(
  				'class'=>'ext.htmltableui.actions.HtmlExportCsv',
  				'path'=>'/csv/',
  			),
 *      );
 *  }
 * 
 */
class HtmlExportCsv extends CAction
{
	public $path; 
	
	public function run(){
		if(isset($_POST)){
            // Check if the POST variable is an array
            if(is_array($_POST)){
				$secondsoffset = 2*intval($_POST['timezone'])*60*60;
				$timestamp = mktime(date("h"))+$secondsoffset;

				$filepath = Yii::getPathOfAlias("webroot").$this->path;
				$filename = preg_replace('/\s+/', '', $_POST['title'])."_".date("Ymdhisa",$timestamp);
				$file = $filepath.$filename.".csv";
				if(file_exists($file)){
					if(is_writable($file)){
						unlink($file); //delete file!
						$fh = fopen($file,"x");//create and open for writing	
					}
				}else{
					$fh = fopen($file,"x");//create and open for writing
				}
				
				if(isset($_POST["title"])){
					fwrite($fh,"\"".$_POST["title"]."\""."\n");
				}
				if(isset($_POST["subtitle"])){
					fwrite($fh,"\"".$_POST["subtitle"]."\""."\n");
				}
				if(isset($_POST["extra"])){
					fwrite($fh,"\"".$_POST["extra"]."\""."\n");
				}

				// Write the column headers and the table rows
				// Note: This code depends on the order in which
				// the column headers array and the rows array
				// were added in the javascript routine. 
				// Column headers must be added first so they are
				// printed before the rows of data.
				unset($rows);
				$rowIndex = 0;
				foreach($_POST as $key=>$row){
					$rows[]=$row;
					
					if(is_array($row)){
						$columnIndex = 0;
						$totalColumns = count($rows[$rowIndex]);
						foreach($rows[$rowIndex] as $rkey=>$rvalue){
							fwrite($fh,"\"".$rvalue."\"");
							if($columnIndex<$totalColumns-1){
								fwrite($fh,",");
							}else{
								fwrite($fh,"\n");
							}
							$columnIndex = $columnIndex + 1;
						}
					}
					$rowIndex = $rowIndex + 1;
				}	 

				// Write csv table footer
				if(isset($_POST["footer"])){
					fwrite($fh,"\"".$_POST["footer"]."\""."\n");
				}
						
				// 3. Close the file and send the URL path of the file to 
				// the jQuery script that generates the XHRequest. 
				if(fclose($fh)){
					echo Yii::app()->baseUrl.$this->path.$filename.".csv";//"Export Successful!";				
				}
            }else{
	            //print_r($_POST);
	            echo "Error Exporting";        	
            }			
        }
	}	
}
?>