00001 <?php
00002
00007 class oxErpGenImport extends oxErpCsv {
00008
00014 protected $_aObjects = array (
00015 'A' => 'article',
00016 'K' => 'category',
00017 'H' => 'vendor',
00018 'C' => 'crossselling',
00019 'Z' => 'accessoire',
00020 'T' => 'article2category',
00021 'I' => 'article2action',
00022 'P' => 'scaleprice',
00023 'U' => 'user',
00024 'O' => 'order',
00025 'R' => 'orderarticle',
00026 'N' => 'country',
00027 'Y' => 'artextends',
00028 );
00034 protected $_aCsvFileFieldsOrder = array();
00035
00041 protected $_blCsvContainsHeader = null;
00042
00054 public function __call( $sMethod, $aArgs )
00055 {
00056 if ( defined( 'OXID_PHP_UNIT' ) ) {
00057 if ( substr( $sMethod, 0, 4) == "UNIT" ) {
00058 $sMethod = str_replace( "UNIT", "_", $sMethod );
00059 }
00060 if ( method_exists( $this, $sMethod)) {
00061 return call_user_func_array( array( & $this, $sMethod ), $aArgs );
00062 }
00063 }
00064
00065 throw new oxSystemComponentException( "Function '$sMethod' does not exist or is not accessible! (" . get_class($this) . ")".PHP_EOL);
00066 }
00072 public function __construct()
00073 {
00074 $this->_setDbLayerVersion();
00075 }
00076
00086 public function getInstanceOfType( $sType )
00087 {
00088 return parent::_getInstanceOfType( $sType );
00089 }
00090
00096 protected function _setDbLayerVersion()
00097 {
00098 $aVersions = array_keys(oxErpBase::$_aDbLayer2ShopDbVersions);
00099 $sVersion = array_pop( $aVersions) ;
00100 oxErpBase::setVersion( $sVersion );
00101 }
00102
00112 protected function _modifyData($aData, $oType)
00113 {
00114 return $this->_mapFields($aData, $oType);
00115 }
00116
00125 protected function _mapFields($aData, $oType)
00126 {
00127 $aRet = array();
00128 $iIndex = 0;
00129 foreach ( $this->_aCsvFileFieldsOrder as $sValue ) {
00130 if ( !empty($sValue) ) {
00131 $aRet[$sValue] = $aData[$iIndex++];
00132 }
00133 }
00134
00135 return $aRet;
00136 }
00137
00146 protected function _getImportType( & $aData )
00147 {
00148 $sType = $this->_sImportTypePrefix;
00149
00150 if(strlen($sType) != 1 || !array_key_exists($sType, $this->_aObjects)){
00151 throw new Exception("Error unknown command: ".$sType);
00152 } else {
00153 return $this->_aObjects[$sType];
00154 }
00155 }
00156
00162 protected function _getImportMode( $aData )
00163 {
00164 return oxERPBase::$MODE_IMPORT;
00165 }
00166
00174 public function getImportObject( $sType )
00175 {
00176 $this->_sImportTypePrefix = $sType;
00177 try {
00178 $sImportType = $this->_getImportType( $this->_aData );
00179 return $this->_getInstanceOfType( $sImportType );
00180 } catch( Exception $e) {
00181 }
00182 }
00183
00191 public function setImportTypePrefix( $sType )
00192 {
00193 $this->_sImportTypePrefix = $sType;
00194 }
00195
00201 public function getImportObjectsList()
00202 {
00203 foreach ( $this->_aObjects as $sKey => $sImportType ) {
00204 $oType = $this->_getInstanceOfType( $sImportType );
00205 $aList[$sKey] = $oType->getBaseTableName();
00206 }
00207 return $aList;
00208 }
00209
00221 public function init( $sUserName, $sPassword, $iShopID = 1, $iLanguage = 0)
00222 {
00223 $myConfig = oxConfig::getInstance();
00224 $mySession = oxSession::getInstance();
00225 $oUser = oxUser::getAdminUser();
00226
00227 if( ( $oUser->oxuser__oxrights->value == "malladmin" || $oUser->oxuser__oxrights->value == $myConfig->GetShopID()) )
00228 {
00229 $this->_sSID = $mySession->getId();
00230 $this->_blInit = true;
00231 $this->_iLanguage = oxLang::getInstance()->getBaseLanguage();
00232 $this->_sUserID = $oUser->getId();
00233
00234 } else {
00235
00236
00237 throw new Exception( self::$ERROR_USER_NO_RIGHTS);
00238 }
00239
00240 $this->_resetIdx();
00241
00242 return $this->_blInit;
00243 }
00244
00252 public function setCsvFileFieldsOrder( $aCsvFields )
00253 {
00254 $this->_aCsvFileFieldsOrder = $aCsvFields;
00255 }
00256
00264 public function setCsvContainsHeader( $blCsvContainsHeader )
00265 {
00266 $this->_blCsvContainsHeader = $blCsvContainsHeader;
00267 }
00268
00274 public function getTotalImportedRowsNumber()
00275 {
00276 $iTotal = 0;
00277 foreach ( $this->getStatistics() as $aValue ) {
00278 if ( $aValue ['r'] ) {
00279 $iTotal++;
00280 }
00281 }
00282
00283 return $iTotal;
00284 }
00285
00294 public function doImport($sPath = null, $sUserName = null, $sUserPassword = null, $sShopId = null, $sShopLanguage = null )
00295 {
00296 $myConfig = oxConfig::getInstance();
00297 $mySession = oxSession::getInstance();
00298
00299 $this->_sReturn = "";
00300 $iMaxLineLength = 8192;
00301
00302 $this->_sPath = $sPath;
00303
00304
00305 try {
00306 $this->init(null, null);
00307 }catch(Exception $ex){
00308 return $this->_sReturn = 'ERPGENIMPORT_ERROR_USER_NO_RIGHTS';
00309 }
00310
00311 $file = @fopen($this->_sPath, "r");
00312
00313 if(isset($file) && $file){
00314 $iRow = 0;
00315 $aRow = array();
00316
00317 while ( ($aRow = fgetcsv( $file, $iMaxLineLength, ";",'"')) !== FALSE) {
00318
00319 $this->_aData[] = $aRow;
00320 }
00321
00322 if ( $this->_blCsvContainsHeader ) {
00323
00324 array_shift( $this->_aData );
00325 }
00326
00327 try {
00328 $this->Import();
00329 } catch (Exception $ex) {
00330 echo $ex->getMessage();
00331 $this->_sReturn = 'ERPGENIMPORT_ERROR_DURING_IMPORT';
00332 }
00333
00334 } else {
00335 $this->_sReturn = 'ERPGENIMPORT_ERROR_WRONG_FILE';
00336 }
00337
00338 @fclose($file);
00339
00340 return $this->_sReturn;
00341 }
00342
00343 }