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 foreach ( $this->_aCsvFileFieldsOrder as $sValue ) {
00132 if ( !empty($sValue) ) {
00133 $aRet[$sValue] = $aData[$iIndex++];
00134 }
00135 }
00136
00137 return $aRet;
00138 }
00139
00149 protected function _getImportType( & $aData )
00150 {
00151 $sType = $this->_sImportTypePrefix;
00152
00153 if ( strlen($sType) != 1 || !array_key_exists($sType, $this->_aObjects) ) {
00154 throw new Exception("Error unknown command: ".$sType);
00155 } else {
00156 return $this->_aObjects[$sType];
00157 }
00158 }
00159
00167 protected function _getImportMode( $aData )
00168 {
00169 return oxERPBase::$MODE_IMPORT;
00170 }
00171
00179 public function getImportObject( $sType )
00180 {
00181 $this->_sImportTypePrefix = $sType;
00182 try {
00183 $sImportType = $this->_getImportType( $this->_aData );
00184 return $this->_getInstanceOfType( $sImportType );
00185 } catch( Exception $e) {
00186 }
00187 }
00188
00196 public function setImportTypePrefix( $sType )
00197 {
00198 $this->_sImportTypePrefix = $sType;
00199 }
00200
00206 public function getImportObjectsList()
00207 {
00208 foreach ( $this->_aObjects as $sKey => $sImportType ) {
00209 $oType = $this->_getInstanceOfType( $sImportType );
00210 $aList[$sKey] = $oType->getBaseTableName();
00211 }
00212 return $aList;
00213 }
00214
00226 public function init( $sUserName, $sPassword, $iShopID = 1, $iLanguage = 0)
00227 {
00228 $myConfig = oxConfig::getInstance();
00229 $mySession = oxSession::getInstance();
00230 $oUser = oxUser::getAdminUser();
00231
00232 if ( ( $oUser->oxuser__oxrights->value == "malladmin" || $oUser->oxuser__oxrights->value == $myConfig->getShopID()) ) {
00233 $this->_sSID = $mySession->getId();
00234 $this->_blInit = true;
00235 $this->_iLanguage = oxLang::getInstance()->getBaseLanguage();
00236 $this->_sUserID = $oUser->getId();
00237
00238 } else {
00239
00240
00241 throw new Exception( self::$ERROR_USER_NO_RIGHTS);
00242 }
00243
00244 $this->_resetIdx();
00245
00246 return $this->_blInit;
00247 }
00248
00256 public function setCsvFileFieldsOrder( $aCsvFields )
00257 {
00258 $this->_aCsvFileFieldsOrder = $aCsvFields;
00259 }
00260
00268 public function setCsvContainsHeader( $blCsvContainsHeader )
00269 {
00270 $this->_blCsvContainsHeader = $blCsvContainsHeader;
00271 }
00272
00278 public function getTotalImportedRowsNumber()
00279 {
00280 $iTotal = 0;
00281 foreach ( $this->getStatistics() as $aValue ) {
00282 if ( $aValue ['r'] ) {
00283 $iTotal++;
00284 }
00285 }
00286
00287 return $iTotal;
00288 }
00289
00302 public function doImport($sPath = null, $sUserName = null, $sUserPassword = null, $sShopId = null, $sShopLanguage = null )
00303 {
00304 $myConfig = oxConfig::getInstance();
00305 $mySession = oxSession::getInstance();
00306
00307 $this->_sReturn = "";
00308 $iMaxLineLength = 8192;
00309
00310 $this->_sPath = $sPath;
00311
00312
00313 try {
00314 $this->init(null, null);
00315 }catch(Exception $ex){
00316 return $this->_sReturn = 'ERPGENIMPORT_ERROR_USER_NO_RIGHTS';
00317 }
00318
00319 $file = @fopen($this->_sPath, "r");
00320
00321 if ( isset($file) && $file ) {
00322 $iRow = 0;
00323 $aRow = array();
00324
00325 while ( ($aRow = fgetcsv( $file, $iMaxLineLength, ";", '"') ) !== false ) {
00326
00327 $this->_aData[] = $aRow;
00328 }
00329
00330 if ( $this->_blCsvContainsHeader ) {
00331
00332 array_shift( $this->_aData );
00333 }
00334
00335 try {
00336 $this->Import();
00337 } catch (Exception $ex) {
00338 echo $ex->getMessage();
00339 $this->_sReturn = 'ERPGENIMPORT_ERROR_DURING_IMPORT';
00340 }
00341
00342 } else {
00343 $this->_sReturn = 'ERPGENIMPORT_ERROR_WRONG_FILE';
00344 }
00345
00346 @fclose($file);
00347
00348 return $this->_sReturn;
00349 }
00350
00351 }