From 46db68ea71597d8f4501a0d881efe24069e7be6f Mon Sep 17 00:00:00 2001 From: Hector Cortez Date: Tue, 26 Jul 2011 11:48:12 -0400 Subject: [PATCH] BUG 0000 Adjustments to import a CSV file in PMTables. --- workflow/engine/controllers/pmTablesProxy.php | 60 ++++++++++ workflow/engine/templates/pmTables/data.js | 103 +++++++++++++++++- 2 files changed, 159 insertions(+), 4 deletions(-) diff --git a/workflow/engine/controllers/pmTablesProxy.php b/workflow/engine/controllers/pmTablesProxy.php index 128c7a4ef..410aa1bf7 100644 --- a/workflow/engine/controllers/pmTablesProxy.php +++ b/workflow/engine/controllers/pmTablesProxy.php @@ -533,7 +533,67 @@ class pmTablesProxy extends HttpProxyController $this->message = $this->success ? 'Deleted Successfully' : 'Error Deleting record'; } + /** + * import a CSV to pm tables record + * @param string $httpData->id + */ + public function importCSV($httpData) + { + if (preg_match('/[\x00-\x08\x0b-\x0c\x0e\x1f]/', file_get_contents($_FILES['form']['tmp_name']['CSV_FILE'])) === 0) { + $filename = $_FILES['form']['name']['CSV_FILE']; + if ($oFile = fopen($_FILES['form']['tmp_name']['CSV_FILE'], 'r')) { + require_once 'classes/model/AdditionalTables.php'; + $oAdditionalTables = new AdditionalTables(); + $aAdditionalTables = $oAdditionalTables->load($_POST['form']['ADD_TAB_UID'], true); + $sErrorMessages = ''; + $i = 1; + $swHead = false; + while (($aAux = fgetcsv($oFile, 4096, $_POST['form']['CSV_DELIMITER'])) !== false) { + if($i == 1) { + $j = 0; + foreach ($aAdditionalTables['FIELDS'] as $aField) { + if($aField['FLD_NAME'] === $aAux[$j]) $swHead = true; + $j++; + } + } + if ($swHead == false) { + $aData = array(); + $j = 0; + foreach ($aAdditionalTables['FIELDS'] as $aField) { + $aData[$aField['FLD_NAME']] = (isset($aAux[$j]) ? $aAux[$j] : ''); + $j++; + } + try { + if (!$oAdditionalTables->saveDataInTable($_POST['form']['ADD_TAB_UID'], $aData)) { + $sErrorMessages .= G::LoadTranslation('ID_DUPLICATE_ENTRY_PRIMARY_KEY') . ', ' . G::LoadTranslation('ID_LINE') . ' ' . $i . '
'; + } + } + catch (Exception $oError) { + $sErrorMessages .= G::LoadTranslation('ID_ERROR_INSERT_LINE') . ': ' . G::LoadTranslation('ID_LINE') . ' ' . $i . '
'; + } + } else { + $swHead = false; + } + $i++; + } + fclose($oFile); + } + if ($sErrorMessages != '') { + $this->success = false; + $this->message = $sErrorMessages; + } + $this->success = true; + $this->message = 'File Imported "'.$filename.'" Successfully'; + } + else { + $sMessage = G::LoadTranslation('ID_UPLOAD_VALID_CSV_FILE'); + $this->success = false; + $this->message = $sMessage; + + } + } + /** * import a pm table * @param string $httpData->id diff --git a/workflow/engine/templates/pmTables/data.js b/workflow/engine/templates/pmTables/data.js index e0bfa9f63..296b4d80a 100644 --- a/workflow/engine/templates/pmTables/data.js +++ b/workflow/engine/templates/pmTables/data.js @@ -227,8 +227,8 @@ 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,{xtype: 'tbfill'}, backButton], + // tbar:[newButton,'-',editButton, deleteButton,'-',{xtype: 'tbfill' }, backButton], bbar: bbarpaging } @@ -324,9 +324,104 @@ DeletePMTableRow = function(){ }; //Load Import PM Table From CSV Source +//ImportPMTableCSV = function(){ +// location.href = 'additionalTablesDataImportForm?sUID=' + TABLES.UID; +//}; + ImportPMTableCSV = function(){ - location.href = 'additionalTablesDataImportForm?sUID=' + TABLES.UID; -}; + + var comboDelimiter = new Ext.data.SimpleStore({ + fields: ['id', 'value'], + data: [[';', 'SemiColon (;)'], + [',', 'Comma (,)']] + }); + var w = new Ext.Window({ + title: '', + width: 440, + height: 180, + modal: true, + autoScroll: false, + maximizable: false, + resizable: false, + items: [ + new Ext.FormPanel({ + id:'uploader', + fileUpload: true, + width: 420, + frame: true, + title: 'Import Data from CSV file', + autoHeight: false, + bodyStyle: 'padding: 10px 10px 0 10px;', + labelWidth: 80, + defaults: { + anchor: '90%', + allowBlank: false, + msgTarget: 'side' + }, + items: [{ + xtype: 'fileuploadfield', + id: 'form-file', + emptyText: 'Select a file', + fieldLabel: 'CSV File', // _('ID_FILE'), + name: 'form[CSV_FILE]', + buttonText: '', + buttonCfg: { + iconCls: 'upload-icon' + } + }, { + xtype: 'combo', + 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_UPLOAD'), + handler: function(){ + var uploader = Ext.getCmp('uploader'); + + if(uploader.getForm().isValid()){ + uploader.getForm().submit({ + url: '../pmTablesProxy/importCSV', + waitMsg: 'Uploading file...', + success: function(o, resp){ + w.close(); + infoGrid.store.reload(); + + PMExt.notify('IMPORT RESULT', resp.result.message); + }, + failure: function(o, resp){ + w.close(); + Ext.MessageBox.show({title: '', msg: resp.result.msg, 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(){