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
00048 public function __construct()
00049 {
00050 $this->_setDbLayerVersion();
00051 }
00052
00060 public function getInstanceOfType( $sType )
00061 {
00062 return parent::_getInstanceOfType( $sType );
00063 }
00064
00070 protected function _setDbLayerVersion()
00071 {
00072 $aVersions = array_keys(oxErpBase::$_aDbLayer2ShopDbVersions);
00073 $sVersion = array_pop( $aVersions) ;
00074 oxErpBase::setVersion( $sVersion );
00075 }
00076
00086 protected function _modifyData($aData, $oType)
00087 {
00088 return $this->_mapFields($aData, $oType);
00089 }
00090
00099 protected function _mapFields($aData, $oType)
00100 {
00101 $aRet = array();
00102 $iIndex = 0;
00103 foreach ( $this->_aCsvFileFieldsOrder as $sValue ) {
00104 if ( !empty($sValue) ) {
00105 $aRet[$sValue] = $aData[$iIndex++];
00106 }
00107 }
00108
00109 return $aRet;
00110 }
00111
00120 protected function _getImportType( & $aData )
00121 {
00122 $sType = $this->_sImportTypePrefix;
00123
00124 if(strlen($sType) != 1 || !array_key_exists($sType, $this->_aObjects)){
00125 throw new Exception("Error unknown command: ".$sType);
00126 } else {
00127 return $this->_aObjects[$sType];
00128 }
00129 }
00130
00136 protected function _getImportMode( $aData )
00137 {
00138 return oxERPBase::$MODE_IMPORT;
00139 }
00140
00148 public function getImportObject( $sType )
00149 {
00150 $this->_sImportTypePrefix = $sType;
00151 try {
00152 $sImportType = $this->_getImportType( $this->_aData );
00153 return $this->_getInstanceOfType( $sImportType );
00154 } catch( Exception $e) {
00155 }
00156 }
00157
00165 public function setImportTypePrefix( $sType )
00166 {
00167 $this->_sImportTypePrefix = $sType;
00168 }
00169
00175 public function getImportObjectsList()
00176 {
00177 foreach ( $this->_aObjects as $sKey => $sImportType ) {
00178 $oType = $this->_getInstanceOfType( $sImportType );
00179 $aList[$sKey] = $oType->getBaseTableName();
00180 }
00181 return $aList;
00182 }
00183
00195 public function init( $sUserName, $sPassword, $iShopID = 1, $iLanguage = 0)
00196 {
00197 $myConfig = oxConfig::getInstance();
00198 $mySession = oxSession::getInstance();
00199 $oUser = oxUser::getAdminUser();
00200
00201 if( ( $oUser->oxuser__oxrights->value == "malladmin" || $oUser->oxuser__oxrights->value == $myConfig->GetShopID()) )
00202 {
00203 $this->_sSID = $mySession->getId();
00204 $this->_blInit = true;
00205 $this->_iLanguage = oxLang::getInstance()->getBaseLanguage();
00206 $this->_sUserID = $oUser->getId();
00207
00208 } else {
00209
00210
00211 throw new Exception( self::$ERROR_USER_NO_RIGHTS);
00212 }
00213
00214 $this->_resetIdx();
00215
00216 return $this->_blInit;
00217 }
00218
00226 public function setCsvFileFieldsOrder( $aCsvFields )
00227 {
00228 $this->_aCsvFileFieldsOrder = $aCsvFields;
00229 }
00230
00238 public function setCsvContainsHeader( $blCsvContainsHeader )
00239 {
00240 $this->_blCsvContainsHeader = $blCsvContainsHeader;
00241 }
00242
00248 public function getTotalImportedRowsNumber()
00249 {
00250 $iTotal = 0;
00251 foreach ( $this->getStatistics() as $aValue ) {
00252 if ( $aValue ['r'] ) {
00253 $iTotal++;
00254 }
00255 }
00256
00257 return $iTotal;
00258 }
00259
00268 public function doImport($sPath = null, $sUserName = null, $sUserPassword = null, $sShopId = null, $sShopLanguage = null )
00269 {
00270 $myConfig = oxConfig::getInstance();
00271 $mySession = oxSession::getInstance();
00272
00273 $this->_sReturn = "";
00274 $iMaxLineLength = 8192;
00275
00276 $this->_sPath = $sPath;
00277
00278
00279 try {
00280 $this->init(null, null);
00281 }catch(Exception $ex){
00282 return self::$ERROR_USER_NO_RIGHTS;
00283 }
00284
00285 $file = fopen($this->_sPath, "r");
00286
00287 if(isset($file) && $file){
00288 $iRow = 0;
00289 $aRow = array();
00290
00291 while ( ($aRow = fgetcsv( $file, $iMaxLineLength, ";",'"')) !== FALSE) {
00292
00293 $this->_aData[] = $aRow;
00294 }
00295
00296 if ( $this->_blCsvContainsHeader ) {
00297
00298 array_shift( $this->_aData );
00299 }
00300
00301 try {
00302 $this->Import();
00303 } catch (Exception $ex) {
00304 $this->_sReturn .= self::$ERROR_DURING_IMPORT.PHP_EOL;
00305 }
00306
00307 } else {
00308 return self::$ERROR_WRONG_FILE;
00309 }
00310 fclose($file);
00311
00312 if(strlen($this->_sReturn)==0) {
00313 $this->_sReturn .= self::$IMPORT_DONE;
00314 }
00315 return $this->_sReturn;
00316 }
00317
00318
00319
00320 }