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 
00055     public function __call( $sMethod, $aArgs )
00056     {
00057         if ( defined( 'OXID_PHP_UNIT' ) ) {
00058             if ( substr( $sMethod, 0, 4) == "UNIT" ) {
00059                 $sMethod = str_replace( "UNIT", "_", $sMethod );
00060             }
00061             if ( method_exists( $this, $sMethod)) {
00062                 return call_user_func_array( array( & $this, $sMethod ), $aArgs );
00063             }
00064         }
00065 
00066         throw new oxSystemComponentException( "Function '$sMethod' does not exist or is not accessible! (" . get_class($this) . ")".PHP_EOL);
00067     }
00068 
00074     public function __construct()
00075     {
00076         $this->_setDbLayerVersion();
00077     }
00078 
00088     public function getInstanceOfType( $sType )
00089     {
00090         return parent::_getInstanceOfType( $sType );
00091     }
00092 
00098     protected function _setDbLayerVersion()
00099     {
00100         $aVersions = array_keys(oxErpBase::$_aDbLayer2ShopDbVersions);
00101         $sVersion = array_pop( $aVersions) ;
00102         oxErpBase::setVersion( $sVersion );
00103     }
00104 
00114     protected function _modifyData($aData, $oType)
00115     {
00116         return $this->_mapFields($aData, $oType);
00117     }
00118 
00127     protected function _mapFields($aData, $oType)
00128     {
00129         $aRet = array();
00130         $iIndex = 0;
00131 
00132         foreach ( $this->_aCsvFileFieldsOrder as $sValue ) {
00133             if ( !empty($sValue) ) {
00134                 $aRet[$sValue] = $aData[$iIndex];
00135             }
00136             $iIndex++;
00137         }
00138 
00139         return $aRet;
00140     }
00141 
00151     protected function _getImportType( & $aData )
00152     {
00153         $sType = $this->_sImportTypePrefix;
00154 
00155         if ( strlen($sType) != 1 || !array_key_exists($sType, $this->_aObjects) ) {
00156             throw new Exception("Error unknown command: ".$sType);
00157         } else {
00158             return $this->_aObjects[$sType];
00159         }
00160     }
00161 
00169     protected function _getImportMode( $aData )
00170     {
00171         return oxERPBase::$MODE_IMPORT;
00172     }
00173 
00181     public function getImportObject( $sType )
00182     {
00183         $this->_sImportTypePrefix = $sType;
00184         try {
00185             $sImportType = $this->_getImportType( $this->_aData );
00186             return $this->_getInstanceOfType( $sImportType );
00187         } catch( Exception $e) {
00188         }
00189     }
00190 
00198     public function setImportTypePrefix( $sType )
00199     {
00200         $this->_sImportTypePrefix = $sType;
00201     }
00202 
00208     public function getImportObjectsList()
00209     {
00210         foreach ( $this->_aObjects as $sKey => $sImportType ) {
00211             $oType = $this->_getInstanceOfType( $sImportType );
00212             $aList[$sKey] = $oType->getBaseTableName();
00213         }
00214         return $aList;
00215     }
00216 
00228     public function init( $sUserName, $sPassword, $iShopID = 1, $iLanguage = 0)
00229     {
00230         $myConfig = oxConfig::getInstance();
00231         $mySession = oxSession::getInstance();
00232         $oUser = oxUser::getAdminUser();
00233 
00234         if ( ( $oUser->oxuser__oxrights->value == "malladmin" || $oUser->oxuser__oxrights->value == $myConfig->getShopID()) ) {
00235             $this->_sSID        = $mySession->getId();
00236             $this->_blInit      = true;
00237             $this->_iLanguage   = oxLang::getInstance()->getBaseLanguage();
00238             $this->_sUserID     = $oUser->getId();
00239             //$mySession->freeze();
00240         } else {
00241 
00242             //user does not have sufficient rights for shop
00243             throw new Exception( self::$ERROR_USER_NO_RIGHTS);
00244         }
00245 
00246         $this->_resetIdx();
00247 
00248         return $this->_blInit;
00249     }
00250 
00258     public function setCsvFileFieldsOrder( $aCsvFields )
00259     {
00260         $this->_aCsvFileFieldsOrder = $aCsvFields;
00261     }
00262 
00270     public function setCsvContainsHeader( $blCsvContainsHeader )
00271     {
00272         $this->_blCsvContainsHeader = $blCsvContainsHeader;
00273     }
00274 
00280     public function getTotalImportedRowsNumber()
00281     {
00282         $iTotal = 0;
00283         foreach ( $this->getStatistics() as $aValue ) {
00284             if ( $aValue ['r'] ) {
00285                 $iTotal++;
00286             }
00287         }
00288 
00289         return $iTotal;
00290     }
00291 
00304     public function doImport($sPath = null, $sUserName = null, $sUserPassword = null, $sShopId = null, $sShopLanguage = null )
00305     {
00306         $myConfig = oxConfig::getInstance();
00307         $mySession = oxSession::getInstance();
00308 
00309         $this->_sReturn = "";
00310         $iMaxLineLength = 8192; //TODO change
00311 
00312         $this->_sPath = $sPath;
00313 
00314         //init with given data
00315         try {
00316             $this->init(null, null);
00317         }catch(Exception $ex){
00318             return $this->_sReturn = 'ERPGENIMPORT_ERROR_USER_NO_RIGHTS';
00319         }
00320 
00321         $file = @fopen($this->_sPath, "r");
00322 
00323         if ( isset($file) && $file ) {
00324             $iRow = 0;
00325             $aRow = array();
00326 
00327             while ( ($aRow = fgetcsv( $file, $iMaxLineLength, ";", '"') ) !== false ) {
00328 
00329                 $this->_aData[] = $aRow;
00330             }
00331 
00332             if ( $this->_blCsvContainsHeader ) {
00333                 //skipping first row - it's header
00334                 array_shift( $this->_aData );
00335             }
00336 
00337             try {
00338                 $this->Import();
00339             } catch (Exception $ex) {
00340                 echo $ex->getMessage();
00341                 $this->_sReturn = 'ERPGENIMPORT_ERROR_DURING_IMPORT';
00342             }
00343 
00344         } else {
00345             $this->_sReturn = 'ERPGENIMPORT_ERROR_WRONG_FILE';
00346         }
00347 
00348         @fclose($file);
00349 
00350         return $this->_sReturn;
00351     }
00352 
00353 }

Generated on Wed May 13 13:25:51 2009 for OXID eShop CE by  doxygen 1.5.5