genimport_main.php

Go to the documentation of this file.
00001 <?php
00002 
00006 class GenImport_Main extends oxAdminDetails
00007 {
00008     public $sClass_do   = "genImport_do";
00009     public $sClass_main = "genImport_main";
00010 
00011     protected $_sCsvFilePath;
00012 
00017     protected $_sThisTemplate = "genimport_main.tpl";
00018 
00025     public function render()
00026     {
00027 
00028         $oConfig = $this->getConfig();
00029         $oSession = $this->getSession();
00030 
00031         $oErpImport = new oxErpGenImport();
00032         $this->_sCsvFilePath = null;
00033 
00034         $sNavStep = $oConfig->getParameter( 'sNavStep' );
00035 
00036         if ( !$sNavStep ) {
00037             $sNavStep = 1;
00038         } else {
00039             $sNavStep++;
00040         }
00041 
00042         $sNavStep = $this->_checkErrors( $sNavStep );
00043 
00044         if ( $sNavStep == 2 ) {
00045             $sType = $oConfig->getParameter( 'sType' );
00046             $oType = $oErpImport->getImportObject( $sType );
00047             $this->_aViewData['sType'] = $sType;
00048             $this->_aViewData['sImportTable'] =  $oType->getBaseTableName();
00049             $this->_aViewData['aCsvFieldsList'] = $this->_getCsvFieldsNames();
00050             $this->_aViewData['aDbFieldsList'] = $oType->getFieldList();
00051 
00052         }
00053 
00054         if ( $sNavStep == 3 ) {
00055             $aCsvFields = $oConfig->getParameter( 'aCsvFields' );
00056             $sType = $oConfig->getParameter( 'sType' );
00057 
00058             $oErpImport = new oxErpGenImport();
00059             $oErpImport->setImportTypePrefix( $sType );
00060             $oErpImport->setCsvFileFieldsOrder( $aCsvFields );
00061             $oErpImport->setCsvContainsHeader( $oSession->getVar('blCsvContainsHeader') );
00062 
00063             $oErpImport->doImport( $this->_getUploadedCsvFilePath() );
00064             $this->_aViewData['iTotalRows'] = $oErpImport->getTotalImportedRowsNumber();
00065 
00066             //checking if errors occured during import
00067             $this->_checkImportErrors( $oErpImport );
00068 
00069             //deleting uploaded csv file from temp dir
00070             $this->_deleteCsvFile();
00071 
00072             //check if repeating import - then forsing first step
00073             if ( $oConfig->getParameter( 'iRepeatImport' ) ) {
00074                 $this->_aViewData['iRepeatImport'] = 1;
00075                 $sNavStep = 1;
00076             }
00077         }
00078 
00079         if ( $sNavStep == 1 ) {
00080             $this->_aViewData['aImportTables'] = $oErpImport->getImportObjectsList();
00081             asort( $this->_aViewData['aImportTables'] );
00082             $this->_resetUploadedCsvData();
00083         }
00084 
00085         $this->_aViewData['sNavStep'] = $sNavStep;
00086 
00087         return parent::render();
00088     }
00089 
00096     protected function _deleteCsvFile()
00097     {
00098         $sPath = $this->_getUploadedCsvFilePath();
00099         if ( is_file($sPath) ) {
00100            @unlink( $sPath );
00101         }
00102     }
00103 
00111     protected function _getCsvFieldsNames() {
00112 
00113         $blCsvContainsHeader = $this->getConfig()->getParameter( 'blContainsHeader' );
00114         $this->getSession()->setVar( 'blCsvContainsHeader', $blCsvContainsHeader );
00115         $sCsvPath = $this->_getUploadedCsvFilePath();
00116 
00117         $aFirstRow = $this->_getCsvFirstRow();
00118 
00119         if ( !$blCsvContainsHeader ) {
00120             $iIndex = 1;
00121             foreach ( $aFirstRow as $sValue ) {
00122                 $aCsvFields[$iIndex] = 'Column ' . $iIndex++;
00123             }
00124         } else {
00125             $aCsvFields = $aFirstRow;
00126         }
00127 
00128         return $aCsvFields;
00129     }
00130 
00136     protected function _getCsvFirstRow() {
00137 
00138         $sPath = $this->_getUploadedCsvFilePath();
00139         $iMaxLineLength = 8192;
00140 
00141         //getting first row
00142         $file = fopen($sPath, "r");
00143         if (isset($file) && $file) {
00144             $aRow = fgetcsv( $file, $iMaxLineLength, ";",'"');
00145             fclose( $file );
00146         }
00147 
00148         return $aRow;
00149     }
00150 
00156     protected function _resetUploadedCsvData()
00157     {
00158         $this->_sCsvFilePath = null;
00159         $oSession = $this->getSession();
00160         $oSession->setVar( 'sCsvFilePath', null );
00161         $oSession->setVar( 'blCsvContainsHeader', null );
00162     }
00163 
00172     protected function _checkErrors( $iNavStep )
00173     {
00174         if ( $iNavStep == 2 ) {
00175             if ( !$this->_getUploadedCsvFilePath() ) {
00176                 $oEx = new oxExceptionToDisplay();
00177                 $oEx->setMessage( 'GENIMPORT_ERRORUPLOADINGFILE' );
00178                 oxUtilsView::getInstance()->addErrorToDisplay( $oEx, false, true, 'genimport' );
00179                 $iNavStep = 1;
00180             }
00181         }
00182 
00183         if ( $iNavStep == 3 ) {
00184             $blIsEmpty = true;
00185             $aCsvFields = $this->getConfig()->getParameter( 'aCsvFields' );
00186             foreach ( $aCsvFields as $sValue ) {
00187                 if ( $sValue ) {
00188                    $blIsEmpty = false;
00189                    break;
00190                 }
00191             }
00192 
00193             if ( $blIsEmpty ) {
00194                 $oEx = new oxExceptionToDisplay();
00195                 $oEx->setMessage( 'GENIMPORT_ERRORASSIGNINGFIELDS' );
00196                 oxUtilsView::getInstance()->addErrorToDisplay( $oEx, false, true, 'genimport' );
00197                 $iNavStep = 2;
00198             }
00199         }
00200 
00201         return $iNavStep;
00202     }
00203 
00210     protected function _getUploadedCsvFilePath()
00211     {
00212         $oConfig = $this->getConfig();
00213         $oSession = $this->getSession();
00214 
00215         //try to get uploaded csv file path
00216         if ( !empty( $this->_sCsvFilePath ) ) {
00217             return $this->_sCsvFilePath;
00218         } elseif ( $this->_sCsvFilePath = $oSession->getVar( 'sCsvFilePath' ) ) {
00219             return $this->_sCsvFilePath;
00220         }
00221 
00222         $aFile = $oConfig->getUploadedFile('csvfile');
00223         if ( isset( $aFile['name'] ) && !empty($aFile['name']) ) {
00224             $this->_sCsvFilePath = $oConfig->getConfigParam( 'sCompileDir' ) . "/" . basename( $aFile['tmp_name'] );
00225             move_uploaded_file( $aFile['tmp_name'], $this->_sCsvFilePath );
00226             $oSession->setVar( 'sCsvFilePath', $this->_sCsvFilePath );
00227             return $this->_sCsvFilePath;
00228         }
00229     }
00230 
00236     protected function _checkImportErrors( $oErpImport )
00237     {
00238         foreach ( $oErpImport->getStatistics() as $aValue ) {
00239             if ( !$aValue ['r'] ) {
00240                 $oEx = new oxExceptionToDisplay();
00241                 $oEx->setMessage( $aValue ['m'] );
00242                 oxUtilsView::getInstance()->addErrorToDisplay( $oEx, false, true, 'genimport' );
00243             }
00244         }
00245 
00246     }
00247 }

Generated on Tue Apr 21 15:45:44 2009 for OXID eShop CE by  doxygen 1.5.5