genimport_main.php

Go to the documentation of this file.
00001 <?php
00002 
00006 class GenImport_Main extends oxAdminDetails
00007 {
00013     public $sClass_do   = "genImport_do";
00014 
00020     public $sClass_main = "genImport_main";
00021 
00027     protected $_sCsvFilePath = null;
00028 
00033     protected $_sStringTerminator = null;
00034 
00039     protected $_sStringEncloser = null;
00040 
00045     protected $_sDefaultStringTerminator = ";";
00046 
00051     protected $_sDefaultStringEncloser = '"';
00052 
00057     protected $_sThisTemplate = "genimport_main.tpl";
00058 
00065     public function render()
00066     {
00067         $oConfig = $this->getConfig();
00068 
00069         $oErpImport = new oxErpGenImport();
00070         $this->_sCsvFilePath = null;
00071 
00072         $sNavStep = $oConfig->getParameter( 'sNavStep' );
00073 
00074         if ( !$sNavStep ) {
00075             $sNavStep = 1;
00076         } else {
00077             $sNavStep++;
00078         }
00079 
00080 
00081         $sNavStep = $this->_checkErrors( $sNavStep );
00082 
00083         if ( $sNavStep == 1 ) {
00084             $this->_aViewData['sGiCsvFieldTerminator'] = htmlentities( $this->_getCsvFieldsTerminator() );
00085             $this->_aViewData['sGiCsvFieldEncloser']   = htmlentities( $this->_getCsvFieldsEncolser() );
00086         }
00087 
00088         if ( $sNavStep == 2 ) {
00089             //saving csv field terminator and encloser to config
00090             if ( $sTerminator = $oConfig->getParameter( 'sGiCsvFieldTerminator' ) ) {
00091                 $this->_sStringTerminator = $sTerminator;
00092                 $oConfig->saveShopConfVar( 'str', 'sGiCsvFieldTerminator', $sTerminator );
00093             }
00094 
00095             if ( $sEncloser = $oConfig->getParameter( 'sGiCsvFieldEncloser' ) ) {
00096                 $this->_sStringEncloser = $sEncloser;
00097                 $oConfig->saveShopConfVar( 'str', 'sGiCsvFieldEncloser', $sEncloser );
00098             }
00099 
00100             $sType = $oConfig->getParameter( 'sType' );
00101             $oType = $oErpImport->getImportObject( $sType );
00102             $this->_aViewData['sType'] = $sType;
00103             $this->_aViewData['sImportTable'] =  $oType->getBaseTableName();
00104             $this->_aViewData['aCsvFieldsList'] = $this->_getCsvFieldsNames();
00105             $this->_aViewData['aDbFieldsList'] = $oType->getFieldList();
00106         }
00107 
00108         if ( $sNavStep == 3 ) {
00109             $aCsvFields = $oConfig->getParameter( 'aCsvFields' );
00110             $sType = $oConfig->getParameter( 'sType' );
00111 
00112             $oErpImport = new oxErpGenImport();
00113             $oErpImport->setImportTypePrefix( $sType );
00114             $oErpImport->setCsvFileFieldsOrder( $aCsvFields );
00115             $oErpImport->setCsvContainsHeader( oxSession::getVar( 'blCsvContainsHeader' ) );
00116 
00117             $oErpImport->doImport( $this->_getUploadedCsvFilePath() );
00118             $this->_aViewData['iTotalRows'] = $oErpImport->getTotalImportedRowsNumber();
00119 
00120             //checking if errors occured during import
00121             $this->_checkImportErrors( $oErpImport );
00122 
00123             //deleting uploaded csv file from temp dir
00124             $this->_deleteCsvFile();
00125 
00126             //check if repeating import - then forsing first step
00127             if ( $oConfig->getParameter( 'iRepeatImport' ) ) {
00128                 $this->_aViewData['iRepeatImport'] = 1;
00129                 $sNavStep = 1;
00130             }
00131         }
00132 
00133         if ( $sNavStep == 1 ) {
00134             $this->_aViewData['aImportTables'] = $oErpImport->getImportObjectsList();
00135             asort( $this->_aViewData['aImportTables'] );
00136             $this->_resetUploadedCsvData();
00137         }
00138 
00139         $this->_aViewData['sNavStep'] = $sNavStep;
00140 
00141         return parent::render();
00142     }
00143 
00150     protected function _deleteCsvFile()
00151     {
00152         $sPath = $this->_getUploadedCsvFilePath();
00153         if ( is_file( $sPath ) ) {
00154            @unlink( $sPath );
00155         }
00156     }
00157 
00164     protected function _getCsvFieldsNames()
00165     {
00166         $blCsvContainsHeader = $this->getConfig()->getParameter( 'blContainsHeader' );
00167         oxSession::setVar( 'blCsvContainsHeader', $blCsvContainsHeader );
00168         $sCsvPath = $this->_getUploadedCsvFilePath();
00169 
00170         $aFirstRow = $this->_getCsvFirstRow();
00171 
00172         if ( !$blCsvContainsHeader ) {
00173             $iIndex = 1;
00174             foreach ( $aFirstRow as $sValue ) {
00175                 $aCsvFields[$iIndex] = 'Column ' . $iIndex++;
00176             }
00177         } else {
00178             foreach ( $aFirstRow as $sKey => $sValue ) {
00179                 $aFirstRow[$sKey] = htmlentities( $sValue );
00180             }
00181 
00182             $aCsvFields = $aFirstRow;
00183         }
00184 
00185         return $aCsvFields;
00186     }
00187 
00193     protected function _getCsvFirstRow()
00194     {
00195         $sPath = $this->_getUploadedCsvFilePath();
00196         $iMaxLineLength = 8192;
00197 
00198         //getting first row
00199         if ( ( $rFile = @fopen( $sPath, "r" ) ) !== false ) {
00200             $aRow = fgetcsv( $rFile, $iMaxLineLength, $this->_getCsvFieldsTerminator(), $this->_getCsvFieldsEncolser() );
00201             fclose( $rFile );
00202         }
00203 
00204         return $aRow;
00205     }
00206 
00212     protected function _resetUploadedCsvData()
00213     {
00214         $this->_sCsvFilePath = null;
00215         oxSession::setVar( 'sCsvFilePath', null );
00216         oxSession::setVar( 'blCsvContainsHeader', null );
00217     }
00218 
00227     protected function _checkErrors( $iNavStep )
00228     {
00229         if ( $iNavStep == 2 ) {
00230             if ( !$this->_getUploadedCsvFilePath() ) {
00231                 $oEx = new oxExceptionToDisplay();
00232                 $oEx->setMessage( 'GENIMPORT_ERRORUPLOADINGFILE' );
00233                 oxUtilsView::getInstance()->addErrorToDisplay( $oEx, false, true, 'genimport' );
00234                 $iNavStep = 1;
00235             }
00236         }
00237 
00238         if ( $iNavStep == 3 ) {
00239             $blIsEmpty = true;
00240             $aCsvFields = $this->getConfig()->getParameter( 'aCsvFields' );
00241             foreach ( $aCsvFields as $sValue ) {
00242                 if ( $sValue ) {
00243                    $blIsEmpty = false;
00244                    break;
00245                 }
00246             }
00247 
00248             if ( $blIsEmpty ) {
00249                 $oEx = new oxExceptionToDisplay();
00250                 $oEx->setMessage( 'GENIMPORT_ERRORASSIGNINGFIELDS' );
00251                 oxUtilsView::getInstance()->addErrorToDisplay( $oEx, false, true, 'genimport' );
00252                 $iNavStep = 2;
00253             }
00254         }
00255 
00256         return $iNavStep;
00257     }
00258 
00265     protected function _getUploadedCsvFilePath()
00266     {
00267         //try to get uploaded csv file path
00268         if ( $this->_sCsvFilePath !== null ) {
00269             return $this->_sCsvFilePath;
00270         } elseif ( $this->_sCsvFilePath = oxSession::getVar( 'sCsvFilePath' ) ) {
00271             return $this->_sCsvFilePath;
00272         }
00273 
00274         $oConfig = $this->getConfig();
00275         $aFile = $oConfig->getUploadedFile( 'csvfile' );
00276         if ( isset( $aFile['name'] ) && $aFile['name'] ) {
00277             $this->_sCsvFilePath = $oConfig->getConfigParam( 'sCompileDir' ) . basename( $aFile['tmp_name'] );
00278             move_uploaded_file( $aFile['tmp_name'], $this->_sCsvFilePath );
00279             oxSession::setVar( 'sCsvFilePath', $this->_sCsvFilePath );
00280             return $this->_sCsvFilePath;
00281         }
00282     }
00283 
00291     protected function _checkImportErrors( $oErpImport )
00292     {
00293         foreach ( $oErpImport->getStatistics() as $aValue ) {
00294             if ( !$aValue ['r'] ) {
00295                 $oEx = new oxExceptionToDisplay();
00296                 $oEx->setMessage( $aValue ['m'] );
00297                 oxUtilsView::getInstance()->addErrorToDisplay( $oEx, false, true, 'genimport' );
00298             }
00299         }
00300 
00301     }
00302 
00308     protected function _getCsvFieldsTerminator()
00309     {
00310         if ( $this->_sStringTerminator === null ) {
00311             $this->_sStringTerminator = $this->_sDefaultStringTerminator;
00312             if ( $sChar = $this->getConfig()->getConfigParam( 'sGiCsvFieldTerminator' ) ) {
00313                 $this->_sStringTerminator = $sChar;
00314             }
00315         }
00316         return $this->_sStringTerminator;
00317     }
00318 
00324     protected function _getCsvFieldsEncolser()
00325     {
00326         if ( $this->_sStringEncloser === null ) {
00327             $this->_sStringEncloser = $this->_sDefaultStringEncloser;
00328             if ( $sChar = $this->getConfig()->getConfigParam( 'sGiCsvFieldEncloser' ) ) {
00329                 $this->_sStringEncloser = $sChar;
00330             }
00331         }
00332         return $this->_sStringEncloser;
00333     }
00334 }

Generated by  doxygen 1.6.2