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 = oxRegistry::getConfig();
00247         $mySession = oxRegistry::getSession();
00248         $oUser = oxNew('oxUser');
00249         $oUser->loadAdminUser();
00250 
00251         if ( ( $oUser->oxuser__oxrights->value == "malladmin" || $oUser->oxuser__oxrights->value == $myConfig->getShopID()) ) {
00252             $this->_sSID        = $mySession->getId();
00253             $this->_blInit      = true;
00254             $this->_iLanguage   = oxRegistry::getLang()->getBaseLanguage();
00255             $this->_sUserID     = $oUser->getId();
00256             //$mySession->freeze();
00257         } else {
00258 
00259             //user does not have sufficient rights for shop
00260             throw new Exception( self::ERROR_USER_NO_RIGHTS);
00261         }
00262 
00263         $this->_resetIdx();
00264 
00265         return $this->_blInit;
00266     }
00267 
00275     public function setCsvFileFieldsOrder( $aCsvFields )
00276     {
00277         $this->_aCsvFileFieldsOrder = $aCsvFields;
00278     }
00279 
00287     public function setCsvContainsHeader( $blCsvContainsHeader )
00288     {
00289         $this->_blCsvContainsHeader = $blCsvContainsHeader;
00290     }
00291 
00297     public function getTotalImportedRowsNumber()
00298     {
00299         return $this->getImportedRowCount();
00300     }
00301 
00314     public function doImport($sPath = null, $sUserName = null, $sUserPassword = null, $sShopId = null, $sShopLanguage = null )
00315     {
00316         $myConfig = oxRegistry::getConfig();
00317         $mySession = oxRegistry::getSession();
00318 
00319         $this->_sReturn = "";
00320         $iMaxLineLength = 8192; //TODO change
00321 
00322         $this->_sPath = $sPath;
00323 
00324         //init with given data
00325         try {
00326             $this->init(null, null);
00327         }catch(Exception $ex){
00328             return $this->_sReturn = 'ERPGENIMPORT_ERROR_USER_NO_RIGHTS';
00329         }
00330 
00331         $file = @fopen($this->_sPath, "r");
00332 
00333         if ( isset($file) && $file ) {
00334             $iRow = 0;
00335             $aRow = array();
00336 
00337             while ( ($aRow = fgetcsv( $file, $iMaxLineLength, $this->_getCsvFieldsTerminator(), $this->_getCsvFieldsEncolser()) ) !== false ) {
00338 
00339                 $this->_aData[] = $aRow;
00340             }
00341 
00342             if ( $this->_blCsvContainsHeader ) {
00343                 //skipping first row - it's header
00344                 array_shift( $this->_aData );
00345             }
00346 
00347             try {
00348                 $this->Import();
00349             } catch (Exception $ex) {
00350                 echo $ex->getMessage();
00351                 $this->_sReturn = 'ERPGENIMPORT_ERROR_DURING_IMPORT';
00352             }
00353 
00354         } else {
00355             $this->_sReturn = 'ERPGENIMPORT_ERROR_WRONG_FILE';
00356         }
00357 
00358         @fclose($file);
00359 
00360         return $this->_sReturn;
00361     }
00362 
00368     protected function _getCsvFieldsTerminator()
00369     {
00370         $myConfig = oxRegistry::getConfig();
00371 
00372         $sChar = $myConfig->getConfigParam( 'sGiCsvFieldTerminator' );
00373 
00374         if ( !$sChar ) {
00375             $sChar = $myConfig->getConfigParam( 'sCSVSign' );
00376         }
00377         if ( !$sChar ) {
00378             $sChar = $this->_sDefaultStringTerminator;
00379         }
00380         return $sChar;
00381     }
00382 
00388     protected function _getCsvFieldsEncolser()
00389     {
00390         $myConfig = oxRegistry::getConfig();
00391 
00392         if ( $sChar = $myConfig->getConfigParam( 'sGiCsvFieldEncloser' ) ) {
00393             return $sChar;
00394         } else {
00395             return $this->_sDefaultStringEncloser;
00396         }
00397     }
00398 }