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 $_sStringTerminator = null;
00018 
00023     protected $_sStringEncloser = null;
00024 
00029     protected $_sDefaultStringTerminator = ";";
00030 
00035     protected $_sDefaultStringEncloser = '"';
00036 
00041     protected $_sThisTemplate = "genimport_main.tpl";
00042 
00049     public function render()
00050     {
00051 
00052         $oConfig = $this->getConfig();
00053         $oSession = $this->getSession();
00054 
00055         $oErpImport = new oxErpGenImport();
00056         $this->_sCsvFilePath = null;
00057 
00058         $sNavStep = $oConfig->getParameter( 'sNavStep' );
00059 
00060         if ( !$sNavStep ) {
00061             $sNavStep = 1;
00062         } else {
00063             $sNavStep++;
00064         }
00065 
00066 
00067         $sNavStep = $this->_checkErrors( $sNavStep );
00068 
00069         if ( $sNavStep == 1 ) {
00070             $this->_aViewData['sGiCsvFieldTerminator'] = htmlentities( $this->_getCsvFieldsTerminator() );
00071             $this->_aViewData['sGiCsvFieldEncloser']   = htmlentities( $this->_getCsvFieldsEncolser() );
00072         }
00073 
00074         if ( $sNavStep == 2 ) {
00075             //saving csv field terminator and encloser to config
00076             if ( $sTerminator = $oConfig->getParameter( 'sGiCsvFieldTerminator' ) ) {
00077                 $this->_sStringTerminator = $sTerminator;
00078                 $oConfig->saveShopConfVar( 'str', 'sGiCsvFieldTerminator', $sTerminator );
00079             }
00080 
00081             if ( $sEncloser = $oConfig->getParameter( 'sGiCsvFieldEncloser' ) ) {
00082                 $this->_sStringEncloser = $sEncloser;
00083                 $oConfig->saveShopConfVar( 'str', 'sGiCsvFieldEncloser', $sEncloser );
00084             }
00085 
00086             $sType = $oConfig->getParameter( 'sType' );
00087             $oType = $oErpImport->getImportObject( $sType );
00088             $this->_aViewData['sType'] = $sType;
00089             $this->_aViewData['sImportTable'] =  $oType->getBaseTableName();
00090             $this->_aViewData['aCsvFieldsList'] = $this->_getCsvFieldsNames();
00091             $this->_aViewData['aDbFieldsList'] = $oType->getFieldList();
00092         }
00093 
00094         if ( $sNavStep == 3 ) {
00095             $aCsvFields = $oConfig->getParameter( 'aCsvFields' );
00096             $sType = $oConfig->getParameter( 'sType' );
00097 
00098             $oErpImport = new oxErpGenImport();
00099             $oErpImport->setImportTypePrefix( $sType );
00100             $oErpImport->setCsvFileFieldsOrder( $aCsvFields );
00101             $oErpImport->setCsvContainsHeader( $oSession->getVar('blCsvContainsHeader') );
00102 
00103             $oErpImport->doImport( $this->_getUploadedCsvFilePath() );
00104             $this->_aViewData['iTotalRows'] = $oErpImport->getTotalImportedRowsNumber();
00105 
00106             //checking if errors occured during import
00107             $this->_checkImportErrors( $oErpImport );
00108 
00109             //deleting uploaded csv file from temp dir
00110             $this->_deleteCsvFile();
00111 
00112             //check if repeating import - then forsing first step
00113             if ( $oConfig->getParameter( 'iRepeatImport' ) ) {
00114                 $this->_aViewData['iRepeatImport'] = 1;
00115                 $sNavStep = 1;
00116             }
00117         }
00118 
00119         if ( $sNavStep == 1 ) {
00120             $this->_aViewData['aImportTables'] = $oErpImport->getImportObjectsList();
00121             asort( $this->_aViewData['aImportTables'] );
00122             $this->_resetUploadedCsvData();
00123         }
00124 
00125         $this->_aViewData['sNavStep'] = $sNavStep;
00126 
00127         return parent::render();
00128     }
00129 
00136     protected function _deleteCsvFile()
00137     {
00138         $sPath = $this->_getUploadedCsvFilePath();
00139         if ( is_file($sPath) ) {
00140            @unlink( $sPath );
00141         }
00142     }
00143 
00151     protected function _getCsvFieldsNames() {
00152 
00153         $blCsvContainsHeader = $this->getConfig()->getParameter( 'blContainsHeader' );
00154         $this->getSession()->setVar( 'blCsvContainsHeader', $blCsvContainsHeader );
00155         $sCsvPath = $this->_getUploadedCsvFilePath();
00156 
00157         $aFirstRow = $this->_getCsvFirstRow();
00158 
00159         if ( !$blCsvContainsHeader ) {
00160             $iIndex = 1;
00161             foreach ( $aFirstRow as $sValue ) {
00162                 $aCsvFields[$iIndex] = 'Column ' . $iIndex++;
00163             }
00164         } else {
00165             foreach ( $aFirstRow as $sKey => $sValue ) {
00166                 $aFirstRow[$sKey] = htmlentities( $sValue );
00167             }
00168 
00169             $aCsvFields = $aFirstRow;
00170         }
00171 
00172         return $aCsvFields;
00173     }
00174 
00180     protected function _getCsvFirstRow() {
00181 
00182         $sPath = $this->_getUploadedCsvFilePath();
00183         $iMaxLineLength = 8192;
00184 
00185         //getting first row
00186         $file = fopen($sPath, "r");
00187         if (isset($file) && $file) {
00188             $aRow = fgetcsv( $file, $iMaxLineLength, $this->_getCsvFieldsTerminator(), $this->_getCsvFieldsEncolser());
00189             fclose( $file );
00190         }
00191 
00192         return $aRow;
00193     }
00194 
00200     protected function _resetUploadedCsvData()
00201     {
00202         $this->_sCsvFilePath = null;
00203         $oSession = $this->getSession();
00204         $oSession->setVar( 'sCsvFilePath', null );
00205         $oSession->setVar( 'blCsvContainsHeader', null );
00206     }
00207 
00216     protected function _checkErrors( $iNavStep )
00217     {
00218         if ( $iNavStep == 2 ) {
00219             if ( !$this->_getUploadedCsvFilePath() ) {
00220                 $oEx = new oxExceptionToDisplay();
00221                 $oEx->setMessage( 'GENIMPORT_ERRORUPLOADINGFILE' );
00222                 oxUtilsView::getInstance()->addErrorToDisplay( $oEx, false, true, 'genimport' );
00223                 $iNavStep = 1;
00224             }
00225         }
00226 
00227         if ( $iNavStep == 3 ) {
00228             $blIsEmpty = true;
00229             $aCsvFields = $this->getConfig()->getParameter( 'aCsvFields' );
00230             foreach ( $aCsvFields as $sValue ) {
00231                 if ( $sValue ) {
00232                    $blIsEmpty = false;
00233                    break;
00234                 }
00235             }
00236 
00237             if ( $blIsEmpty ) {
00238                 $oEx = new oxExceptionToDisplay();
00239                 $oEx->setMessage( 'GENIMPORT_ERRORASSIGNINGFIELDS' );
00240                 oxUtilsView::getInstance()->addErrorToDisplay( $oEx, false, true, 'genimport' );
00241                 $iNavStep = 2;
00242             }
00243         }
00244 
00245         return $iNavStep;
00246     }
00247 
00254     protected function _getUploadedCsvFilePath()
00255     {
00256         $oConfig = $this->getConfig();
00257         $oSession = $this->getSession();
00258 
00259         //try to get uploaded csv file path
00260         if ( !empty( $this->_sCsvFilePath ) ) {
00261             return $this->_sCsvFilePath;
00262         } elseif ( $this->_sCsvFilePath = $oSession->getVar( 'sCsvFilePath' ) ) {
00263             return $this->_sCsvFilePath;
00264         }
00265 
00266         $aFile = $oConfig->getUploadedFile('csvfile');
00267         if ( isset( $aFile['name'] ) && !empty($aFile['name']) ) {
00268             $this->_sCsvFilePath = $oConfig->getConfigParam( 'sCompileDir' ) . "/" . basename( $aFile['tmp_name'] );
00269             move_uploaded_file( $aFile['tmp_name'], $this->_sCsvFilePath );
00270             $oSession->setVar( 'sCsvFilePath', $this->_sCsvFilePath );
00271             return $this->_sCsvFilePath;
00272         }
00273     }
00274 
00280     protected function _checkImportErrors( $oErpImport )
00281     {
00282         foreach ( $oErpImport->getStatistics() as $aValue ) {
00283             if ( !$aValue ['r'] ) {
00284                 $oEx = new oxExceptionToDisplay();
00285                 $oEx->setMessage( $aValue ['m'] );
00286                 oxUtilsView::getInstance()->addErrorToDisplay( $oEx, false, true, 'genimport' );
00287             }
00288         }
00289 
00290     }
00291 
00297     protected function _getCsvFieldsTerminator()
00298     {
00299         if ( $this->_sStringTerminator ) {
00300             return $this->_sStringTerminator;
00301         }
00302 
00303         if ( $sChar = $this->getConfig()->getConfigParam( 'sGiCsvFieldTerminator' ) ) {
00304             return $this->_sStringTerminator = $sChar;
00305         } else {
00306             return $this->_sDefaultStringTerminator;
00307         }
00308     }
00309 
00315     protected function _getCsvFieldsEncolser()
00316     {
00317         if ( $this->_sStringEncloser ) {
00318             return $this->_sStringEncloser;
00319         }
00320 
00321         if ( $sChar = $this->getConfig()->getConfigParam( 'sGiCsvFieldEncloser' ) ){
00322             return $this->_sStringEncloser = $sChar;
00323         } else {
00324             return $this->_sDefaultStringEncloser;
00325         }
00326     }
00327 }

Generated on Tue Aug 18 09:21:04 2009 for OXID eShop CE by  doxygen 1.5.5