oxerpgenimport.php

Go to the documentation of this file.
00001 <?php
00002 
00007 class oxErpGenImport extends oxErpCsv
00008 {
00009 
00015     protected $_aObjects = array    (
00016                                 'A' => 'article',
00017                                 'K' => 'category',
00018                                 'H' => 'vendor',
00019                                 'C' => 'crossselling',
00020                                 'Z' => 'accessoire',
00021                                 'T' => 'article2category',
00022                                 'I' => 'article2action',
00023                                 'P' => 'scaleprice',
00024                                 'U' => 'user',
00025                                 'O' => 'order',
00026                                 'R' => 'orderarticle',
00027                                 'N' => 'country',
00028                                 'Y' => 'artextends',
00029                             );
00035     protected $_aCsvFileFieldsOrder = array();
00036 
00042     protected $_blCsvContainsHeader = null;
00043 
00048     protected $_sDefaultStringTerminator = ";";
00049 
00054     protected $_sDefaultStringEncloser = '"';
00055 
00067     public function __call( $sMethod, $aArgs )
00068     {
00069         if ( defined( 'OXID_PHP_UNIT' ) ) {
00070             if ( substr( $sMethod, 0, 4) == "UNIT" ) {
00071                 $sMethod = str_replace( "UNIT", "_", $sMethod );
00072             }
00073             if ( method_exists( $this, $sMethod)) {
00074                 return call_user_func_array( array( & $this, $sMethod ), $aArgs );
00075             }
00076         }
00077 
00078         throw new oxSystemComponentException( "Function '$sMethod' does not exist or is not accessible! (" . get_class($this) . ")".PHP_EOL);
00079     }
00080 
00086     public function __construct()
00087     {
00088         $this->_setDbLayerVersion();
00089     }
00090 
00100     public function getInstanceOfType( $sType )
00101     {
00102         return parent::_getInstanceOfType( $sType );
00103     }
00104 
00110     protected function _setDbLayerVersion()
00111     {
00112         $aVersions = array_keys(oxErpBase::$_aDbLayer2ShopDbVersions);
00113         $sVersion = array_pop( $aVersions) ;
00114         oxErpBase::setVersion( $sVersion );
00115     }
00116 
00126     protected function _modifyData($aData, $oType)
00127     {
00128         return $this->_mapFields($aData, $oType);
00129     }
00130 
00139     protected function _mapFields($aData, $oType)
00140     {
00141         $aRet = array();
00142         $iIndex = 0;
00143 
00144         foreach ( $this->_aCsvFileFieldsOrder as $sValue ) {
00145             if ( !empty($sValue) ) {
00146                 if ( strtolower( $aData[$iIndex] ) == "null" ) {
00147                     $aRet[$sValue] = null;
00148                 } else {
00149                     $aRet[$sValue] = $aData[$iIndex];
00150                 }
00151             }
00152             $iIndex++;
00153         }
00154 
00155         return $aRet;
00156     }
00157 
00167     protected function _getImportType( & $aData )
00168     {
00169         $sType = $this->_sImportTypePrefix;
00170 
00171         if ( strlen($sType) != 1 || !array_key_exists($sType, $this->_aObjects) ) {
00172             throw new Exception("Error unknown command: ".$sType);
00173         } else {
00174             return $this->_aObjects[$sType];
00175         }
00176     }
00177 
00185     protected function _getImportMode( $aData )
00186     {
00187         return oxERPBase::$MODE_IMPORT;
00188     }
00189 
00197     public function getImportObject( $sType )
00198     {
00199         $this->_sImportTypePrefix = $sType;
00200         try {
00201             $sImportType = $this->_getImportType( $this->_aData );
00202             return $this->_getInstanceOfType( $sImportType );
00203         } catch( Exception $e) {
00204         }
00205     }
00206 
00214     public function setImportTypePrefix( $sType )
00215     {
00216         $this->_sImportTypePrefix = $sType;
00217     }
00218 
00224     public function getImportObjectsList()
00225     {
00226         foreach ( $this->_aObjects as $sKey => $sImportType ) {
00227             $oType = $this->_getInstanceOfType( $sImportType );
00228             $aList[$sKey] = $oType->getBaseTableName();
00229         }
00230         return $aList;
00231     }
00232 
00244     public function init( $sUserName, $sPassword, $iShopID = 1, $iLanguage = 0)
00245     {
00246         $myConfig = oxConfig::getInstance();
00247         $mySession = oxSession::getInstance();
00248         $oUser = oxUser::getAdminUser();
00249 
00250         if ( ( $oUser->oxuser__oxrights->value == "malladmin" || $oUser->oxuser__oxrights->value == $myConfig->getShopID()) ) {
00251             $this->_sSID        = $mySession->getId();
00252             $this->_blInit      = true;
00253             $this->_iLanguage   = oxLang::getInstance()->getBaseLanguage();
00254             $this->_sUserID     = $oUser->getId();
00255             //$mySession->freeze();
00256         } else {
00257 
00258             //user does not have sufficient rights for shop
00259             throw new Exception( self::ERROR_USER_NO_RIGHTS);
00260         }
00261 
00262         $this->_resetIdx();
00263 
00264         return $this->_blInit;
00265     }
00266 
00274     public function setCsvFileFieldsOrder( $aCsvFields )
00275     {
00276         $this->_aCsvFileFieldsOrder = $aCsvFields;
00277     }
00278 
00286     public function setCsvContainsHeader( $blCsvContainsHeader )
00287     {
00288         $this->_blCsvContainsHeader = $blCsvContainsHeader;
00289     }
00290 
00296     public function getTotalImportedRowsNumber()
00297     {
00298         $iTotal = 0;
00299         foreach ( $this->getStatistics() as $aValue ) {
00300             if ( $aValue ['r'] ) {
00301                 $iTotal++;
00302             }
00303         }
00304 
00305         return $iTotal;
00306     }
00307 
00320     public function doImport($sPath = null, $sUserName = null, $sUserPassword = null, $sShopId = null, $sShopLanguage = null )
00321     {
00322         $myConfig = oxConfig::getInstance();
00323         $mySession = oxSession::getInstance();
00324 
00325         $this->_sReturn = "";
00326         $iMaxLineLength = 8192; //TODO change
00327 
00328         $this->_sPath = $sPath;
00329 
00330         //init with given data
00331         try {
00332             $this->init(null, null);
00333         }catch(Exception $ex){
00334             return $this->_sReturn = 'ERPGENIMPORT_ERROR_USER_NO_RIGHTS';
00335         }
00336 
00337         $file = @fopen($this->_sPath, "r");
00338 
00339         if ( isset($file) && $file ) {
00340             $iRow = 0;
00341             $aRow = array();
00342 
00343             while ( ($aRow = fgetcsv( $file, $iMaxLineLength, $this->_getCsvFieldsTerminator(), $this->_getCsvFieldsEncolser()) ) !== false ) {
00344 
00345                 $this->_aData[] = $aRow;
00346             }
00347 
00348             if ( $this->_blCsvContainsHeader ) {
00349                 //skipping first row - it's header
00350                 array_shift( $this->_aData );
00351             }
00352 
00353             try {
00354                 $this->Import();
00355             } catch (Exception $ex) {
00356                 echo $ex->getMessage();
00357                 $this->_sReturn = 'ERPGENIMPORT_ERROR_DURING_IMPORT';
00358             }
00359 
00360         } else {
00361             $this->_sReturn = 'ERPGENIMPORT_ERROR_WRONG_FILE';
00362         }
00363 
00364         @fclose($file);
00365 
00366         return $this->_sReturn;
00367     }
00368 
00374     protected function _getCsvFieldsTerminator()
00375     {
00376         $myConfig = oxConfig::getInstance();
00377         // deprecated
00378         $sChar = $myConfig->getConfigParam( 'sGiCsvFieldTerminator' );
00379 
00380         if ( !$sChar ) {
00381             $sChar = $myConfig->getConfigParam( 'sCSVSign' );
00382         }
00383         if ( !$sChar ) {
00384             $sChar = $this->_sDefaultStringTerminator;
00385         }
00386         return $sChar;
00387     }
00388 
00394     protected function _getCsvFieldsEncolser()
00395     {
00396         $myConfig = oxConfig::getInstance();
00397 
00398         if ( $sChar = $myConfig->getConfigParam( 'sGiCsvFieldEncloser' ) ) {
00399             return $sChar;
00400         } else {
00401             return $this->_sDefaultStringEncloser;
00402         }
00403     }
00404 }