Commits

christiansalazar committed c61c746

preparing demo EYuiFormDataExport

Comments (0)

Files changed (2)

protected/controllers/ExampleController.php

 	{ 
 		$this->render('eyuiajaxaction'); 
 	}	
+	
+	
+	public function actionDataExport($userid=null) {
+		
+		// 'mymodelid' is referenced in: eyuidemo\protected\views\example\eyuiform2.php
+		
+		//$tmp_folder = sys_get_temp_dir();
+		$tmp_folder = "e:/code/tmp/";
+		
+		// 1. load the form structure (pages, groups and fields)
+		$exporter = EYuiFormDataExport::newModel(
+			EYuiFormEditorDb::model('mymodelid','form1'));
+
+		// 2. build the required CSV files (empty files, only column definition)
+		$exporter->prepareFiles($tmp_folder);
+		
+		if($userid == null){
+			//	3.a) export records for each user
+			foreach(TblUser::model()->findAll() as $user)
+				$exporter->insertModel(EYuiFormDb::newModel($user),'form1');
+		}
+		else{
+			//	3.b) export records for selected user
+			$user = TblUser::model()->findByPk($userid);
+			$exporter->insertModel(EYuiFormDb::newModel($user),'form1');
+		}
+		
+		// AT THIS POINT YOU MUST HAVE CSV FILES STORED IN YOUR $tmp_folder
+		// EACH FILE NAME IS THE FORM PAGE NAME.
+		
+		
+		// 4. outputs all generated csv files into a single zip.
+		
+		$zipName = "exported-data.zip";
+		$destZip = $tmp_folder.$zipName;
+		$zip = new ZipArchive;
+		if ($zip->open($destZip,ZIPARCHIVE::OVERWRITE) === TRUE){
+			// exporter will return the generated filenames 
+			foreach($exporter->getFiles() as $fname){
+				$fname = utf8_decode($fname);
+				$addfile = $tmp_folder.$fname;
+				if(!$zip->addFile($addfile,$fname))
+					throw new Exception("[error addZip: ".$addfile."]");
+			}
+			$zip->close();
+			
+			// 5. at this point you must have a zip file. now output it to browser for direct download
+			//
+			header("Cache-Control: public");
+			header("Content-Description: File Transfer");
+			header("Content-Transfer-Encoding: binary");
+			header("Content-Disposition: attachment; filename=".$zipName);
+			header("Content-Type: application/octet-stream");
+			echo readfile($destZip);
+		}
+	}
 }

protected/views/example/eyuiform2.php

 <p>This form is created using <?php echo 
 CHtml::link("eyui form editor",array('example/eyuiformeditor')) ?>.</p>
 
+
+<?php 
+	/*
+		data can be exported too in CSV format using EYuiFormDataExport,
+		please see also: actionDataExport
+			
+		(to access field values stored using EYuiForm please refer to README, item 3 or 4)
+	*/
+	echo "<ul><h4>CSV Export Demo:</h4>";
+		echo "<li>".CHtml::link("export this user in CSV format",
+			array('example/dataexport','userid'=>$user->id))."</li>";
+		echo "<li>".CHtml::link("export all users in CSV format",array('example/dataexport'))."</li>";
+	echo "</ul>";
+?>
+
 <p>Current selected user is: <?php echo $user->username.",".$user->email; ?></p>
 <?php 
 	// controller provides validationModel wich is an EYuiFormDb instance who will provide you
 		'jQueryControl'=>'tabs', // tabs or accordion
 		'jQueryGroupControl'=>'', // use 'accordion' or leave blank
 		'descriptionLocation'=>'title', // field description location: 'title' or 'visible'
-		//
-		// Using EYuiFormEditorDb stored fields: 
-		//
-		//	it will load all fields, groups and pages for model: 'mymodelid' and 'form1'
-		//	
-		//  it loads the pages, groups and fields for 'form1' at model 'mymodelid'
-		//	(not the field values itself..! only form structure. the field values are loaded using the validationModel provided)
+		
+		//	it loads the **form structure** (pages, groups and fields) of 'form1' stored using 
+		//	a key named: 'mymodelid' (this is not the userid !!)
 		'pages'=>EYuiFormEditorDb::model('mymodelid',"form1"),
 	));
 ?>