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 $aRet[$sValue] = $aData[$iIndex];
00147 }
00148 $iIndex++;
00149 }
00150
00151 return $aRet;
00152 }
00153
00163 protected function _getImportType( & $aData )
00164 {
00165 $sType = $this->_sImportTypePrefix;
00166
00167 if ( strlen($sType) != 1 || !array_key_exists($sType, $this->_aObjects) ) {
00168 throw new Exception("Error unknown command: ".$sType);
00169 } else {
00170 return $this->_aObjects[$sType];
00171 }
00172 }
00173
00181 protected function _getImportMode( $aData )
00182 {
00183 return oxERPBase::$MODE_IMPORT;
00184 }
00185
00193 public function getImportObject( $sType )
00194 {
00195 $this->_sImportTypePrefix = $sType;
00196 try {
00197 $sImportType = $this->_getImportType( $this->_aData );
00198 return $this->_getInstanceOfType( $sImportType );
00199 } catch( Exception $e) {
00200 }
00201 }
00202
00210 public function setImportTypePrefix( $sType )
00211 {
00212 $this->_sImportTypePrefix = $sType;
00213 }
00214
00220 public function getImportObjectsList()
00221 {
00222 foreach ( $this->_aObjects as $sKey => $sImportType ) {
00223 $oType = $this->_getInstanceOfType( $sImportType );
00224 $aList[$sKey] = $oType->getBaseTableName();
00225 }
00226 return $aList;
00227 }
00228
00240 public function init( $sUserName, $sPassword, $iShopID = 1, $iLanguage = 0)
00241 {
00242 $myConfig = oxConfig::getInstance();
00243 $mySession = oxSession::getInstance();
00244 $oUser = oxUser::getAdminUser();
00245
00246 if ( ( $oUser->oxuser__oxrights->value == "malladmin" || $oUser->oxuser__oxrights->value == $myConfig->getShopID()) ) {
00247 $this->_sSID = $mySession->getId();
00248 $this->_blInit = true;
00249 $this->_iLanguage = oxLang::getInstance()->getBaseLanguage();
00250 $this->_sUserID = $oUser->getId();
00251
00252 } else {
00253
00254
00255 throw new Exception( self::$ERROR_USER_NO_RIGHTS);
00256 }
00257
00258 $this->_resetIdx();
00259
00260 return $this->_blInit;
00261 }
00262
00270 public function setCsvFileFieldsOrder( $aCsvFields )
00271 {
00272 $this->_aCsvFileFieldsOrder = $aCsvFields;
00273 }
00274
00282 public function setCsvContainsHeader( $blCsvContainsHeader )
00283 {
00284 $this->_blCsvContainsHeader = $blCsvContainsHeader;
00285 }
00286
00292 public function getTotalImportedRowsNumber()
00293 {
00294 $iTotal = 0;
00295 foreach ( $this->getStatistics() as $aValue ) {
00296 if ( $aValue ['r'] ) {
00297 $iTotal++;
00298 }
00299 }
00300
00301 return $iTotal;
00302 }
00303
00316 public function doImport($sPath = null, $sUserName = null, $sUserPassword = null, $sShopId = null, $sShopLanguage = null )
00317 {
00318 $myConfig = oxConfig::getInstance();
00319 $mySession = oxSession::getInstance();
00320
00321 $this->_sReturn = "";
00322 $iMaxLineLength = 8192;
00323
00324 $this->_sPath = $sPath;
00325
00326
00327 try {
00328 $this->init(null, null);
00329 }catch(Exception $ex){
00330 return $this->_sReturn = 'ERPGENIMPORT_ERROR_USER_NO_RIGHTS';
00331 }
00332
00333 $file = @fopen($this->_sPath, "r");
00334
00335 if ( isset($file) && $file ) {
00336 $iRow = 0;
00337 $aRow = array();
00338
00339 while ( ($aRow = fgetcsv( $file, $iMaxLineLength, $this->_getCsvFieldsTerminator(), $this->_getCsvFieldsEncolser()) ) !== false ) {
00340
00341 $this->_aData[] = $aRow;
00342 }
00343
00344 if ( $this->_blCsvContainsHeader ) {
00345
00346 array_shift( $this->_aData );
00347 }
00348
00349 try {
00350 $this->Import();
00351 } catch (Exception $ex) {
00352 echo $ex->getMessage();
00353 $this->_sReturn = 'ERPGENIMPORT_ERROR_DURING_IMPORT';
00354 }
00355
00356 } else {
00357 $this->_sReturn = 'ERPGENIMPORT_ERROR_WRONG_FILE';
00358 }
00359
00360 @fclose($file);
00361
00362 return $this->_sReturn;
00363 }
00364
00370 protected function _getCsvFieldsTerminator()
00371 {
00372 $myConfig = oxConfig::getInstance();
00373
00374 if ( $sChar = $myConfig->getConfigParam( 'sGiCsvFieldTerminator' ) ){
00375 return $sChar;
00376 } else {
00377 return $this->_sDefaultStringTerminator;
00378 }
00379 }
00380
00386 protected function _getCsvFieldsEncolser()
00387 {
00388 $myConfig = oxConfig::getInstance();
00389
00390 if ( $sChar = $myConfig->getConfigParam( 'sGiCsvFieldEncloser' ) ){
00391 return $sChar;
00392 } else {
00393 return $this->_sDefaultStringEncloser;
00394 }
00395 }
00396 }