genimport_main.php

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

Generated on Thu Feb 19 15:02:21 2009 for OXID eShop CE by  doxygen 1.5.5