BUG 0000 New feature Export data to CSV from a PMTables.

This commit is contained in:
Hector Cortez
2011-08-31 17:58:22 -04:00
parent 563e3f8f5e
commit 76ac4663ad
2 changed files with 292 additions and 103 deletions

View File

@@ -567,6 +567,71 @@ class pmTablesProxy extends HttpProxyController
}
}
/**
* export a pm tables record to CSV
* @param string $httpData->id
*/
public function exportCSV($httpData)
{
try{
$link = '';
$size = '';
$META = 'Content';
$bytesSaved = 0;
require_once 'classes/model/AdditionalTables.php';
$oAdditionalTables = new AdditionalTables();
$aAdditionalTables = $oAdditionalTables->load($_POST['ADD_TAB_UID'], true);
$sErrorMessages = '';
$sDelimiter = $_POST['CSV_DELIMITER'];
$resultData = $oAdditionalTables->getAllData($_POST['ADD_TAB_UID']);
$rows = $resultData['rows'];
$count = $resultData['count'];
$PUBLIC_ROOT_PATH = PATH_DATA . 'sites' . PATH_SEP . SYS_SYS . PATH_SEP . 'public' . PATH_SEP;
$filenameOnly = strtolower( $aAdditionalTables['ADD_TAB_NAME'] ."_".date("Y-m-d").'_'.date("Hi").".csv");
$filename = $PUBLIC_ROOT_PATH . $filenameOnly;
$fp = fopen( $filename, "wb");
foreach($rows as $keyCol => $cols ){
$SDATA = "";
$cnt = count($cols);
foreach($cols as $key => $val){
$SDATA .= $val;
if(--$cnt > 0 ) $SDATA .= $sDelimiter;
}
$SDATA .= "\n";
$bytesSaved += fwrite($fp, $SDATA);
}
fclose ($fp);
// $filenameLink = "pmTables/streamExported?f=$filenameOnly";
$filenameLink = "streamExported?f=$filenameOnly";
$size = round(($bytesSaved/1024), 2)." Kb";
$filename = $filenameOnly;
$link = $filenameLink;
$result->success = true;
$result->filename = $filenameOnly;
$result->link = $link;
$result->message = "Generated file: $filenameOnly, size: $size";
}
catch (Exception $e) {
$result->success = false;
$result->message = $e->getMessage();
}
return $result;
}
/**
* import a pm table
* @param string $httpData->id

View File

@@ -43,12 +43,19 @@ Ext.onReady(function(){
});
importButton = new Ext.Action({
text: _('ID_IMPORT'),
text : _('ID_IMPORT_CSV'),
iconCls : 'silk-add',
icon : '/images/import.gif',
handler : ImportPMTableCSV
});
exportButton = new Ext.Action({
text : _('ID_EXPORT_CSV'),
iconCls : 'silk-add',
icon : '/images/export.png',
handler : ExportPMTableCSV
});
backButton = new Ext.Action({
text : _('ID_BACK'),
icon : '/images/back-icon.png',
@@ -56,7 +63,8 @@ Ext.onReady(function(){
});
contextMenu = new Ext.menu.Menu({
items: [editButton, deleteButton]
items : [ editButton,
deleteButton ]
});
//This loop loads columns and fields to store and column model
@@ -253,8 +261,15 @@ Ext.onReady(function(){
store: store,
cm: cmodel,
sm: smodel,
tbar:[newButton,'-',editButton, deleteButton,'-',importButton,{xtype: 'tbfill'}, backButton],
// tbar:[newButton,'-',editButton, deleteButton,'-',{xtype: 'tbfill' }, backButton],
tbar:[ newButton,
'-',
editButton,
deleteButton,
'-',
importButton,
exportButton,
{xtype: 'tbfill'},
backButton ],
bbar: bbarpaging
}
@@ -379,7 +394,7 @@ ImportPMTableCSV = function(){
fileUpload : true,
width : 420,
frame : true,
title: 'Import Data from CSV file',
title : _('ID_IMPORT_DATA_CSV'),
autoHeight : false,
bodyStyle : 'padding: 10px 10px 0 10px;',
labelWidth : 80,
@@ -470,6 +485,115 @@ ImportPMTableCSV = function(){
w.show();
}
ExportPMTableCSV = function(){
var comboDelimiter = new Ext.data.SimpleStore({
fields : ['id', 'value'],
data : [[';', 'SemiColon (;)'],
[',', 'Comma (,)']]
});
var w = new Ext.Window({
title : '',
width : 320,
height : 140,
modal : true,
autoScroll : false,
maximizable : false,
resizable : false,
items: [
new Ext.FormPanel({
id :'uploader',
fileUpload : true,
width : 300,
frame : true,
title : _('ID_EXPORT_DATA_CSV'),
autoHeight : false,
bodyStyle : 'padding: 10px 10px 0 10px;',
labelWidth : 80,
defaults : {
anchor : '90%',
allowBlank : false,
msgTarget : 'side'
},
items : [{
xtype : 'combo',
id : 'csv_delimiter',
fieldLabel : 'Delimited by',
hiddenName : 'form[CSV_DELIMITER]',
mode : 'local',
store : comboDelimiter,
displayField : 'value',
valueField : 'id',
allowBlank : false,
triggerAction : 'all',
emptyText : _('ID_SELECT'),
selectOnFocus : true
},{
xtype : 'hidden',
name : 'form[ADD_TAB_UID]',
value : tableDef.ADD_TAB_UID
} ],
buttons : [{
text : _('ID_EXPORT'),
handler : function(){
var csvDelimiter = Ext.getCmp('csv_delimiter').getValue();
if(csvDelimiter != '') {
Ext.Msg.show({
title : '',
msg : _('ID_PROCESSING'),
wait : true,
waitConfig : {interval:500}
});
Ext.Ajax.request({
url: '../pmTablesProxy/exportCSV',
params: {
ADD_TAB_UID : tableDef.ADD_TAB_UID,
CSV_DELIMITER : Ext.getCmp('csv_delimiter').getValue()
},
success: function(resp){
Ext.Msg.hide();
w.close();
result = Ext.util.JSON.decode(resp.responseText);
if (result.success) {
location.href = result.link;
} else {
PMExt.error(_('ID_ERROR', result.message));
}
},
failure: function(obj, resp){
Ext.Msg.alert( _('ID_ERROR'), resp.result.msg);
}
});
} else {
Ext.MessageBox.show({ title : '',
msg : _('ID_INVALID_DELIMITER'),
buttons : Ext.MessageBox.OK,
animEl : 'mb9',
fn : function(){},
icon : Ext.MessageBox.ERROR
});
}
}
},{
text : TRANSLATIONS.ID_CANCEL,
handler : function(){
w.close();
}
}]
})
]
});
w.show();
}
//Load PM Table List
BackPMList = function(){
//location.href = 'additionalTablesList';