00001 <?php
00002
00003 require_once( 'oxerpcompatability.php');
00004
00014 abstract class oxERPBase
00015 {
00016
00017 static $ERROR_USER_WRONG = "ERROR: Could not login";
00018 static $ERROR_USER_NO_RIGHTS = "Not sufficient rights to perform operation!";
00019 static $ERROR_USER_EXISTS = "ERROR: User already exists";
00020 static $ERROR_NO_INIT = "Init not executed, Access denied!";
00021 static $ERROR_DELETE_NO_EMPTY_CATEGORY = "Only empty category can be deleated";
00022 static $ERROR_OBJECT_NOT_EXISTING = "Object does not exist";
00023
00024 static $MODE_IMPORT = "Import";
00025 static $MODE_DELETE = "Delete";
00026
00027 protected $_blInit = false;
00028 protected $_iLanguage = null;
00029 protected $_sUserID = null;
00030
00031 protected $_sSID = null;
00032
00033 protected static $_sRequestedVersion = '';
00034
00041 protected static $_aDbLayer2ShopDbVersions = array(
00042 '1' => '1', '1.1' => '1', '2' => '2',
00043 );
00044
00045 public $_aStatistics = array();
00046 public $_iIdx = 0;
00047
00048
00049 public function getStatistics() { return $this->_aStatistics; }
00050 public function getSessionID() { return $this->_sSID; }
00051
00052 protected abstract function _beforeExport($sType);
00053 protected abstract function _afterExport($sType);
00054 protected abstract function _beforeImport();
00055 protected abstract function _afterImport();
00056 public abstract function getImportData();
00057 protected abstract function _getImportType(& $aData);
00058 protected abstract function _getImportMode($Data);
00059 protected abstract function _modifyData($aData, $oType);
00060
00067 public function __call( $sMethod, $aArguments)
00068 {
00069 throw new Exception( "ERROR: Handler for Object '$sMethod' not implemented!");
00070 }
00071
00072
00073
00074
00075
00076
00077
00078
00079
00092 public function init( $sUserName, $sPassword, $iShopID = 1, $iLanguage = 0)
00093 {
00094 $_COOKIE = array('admin_sid' => false);
00095 $myConfig = oxConfig::getInstance();
00096 $myConfig->setConfigParam( 'blAdmin', 1 );
00097 $myConfig->setAdminMode( true );
00098
00099 $mySession = oxSession::getInstance();
00100 $myConfig->oActView = new FakeView();
00101
00102
00103 if ($iShopID != 1) {
00104 $myConfig->setConfigParam('blMallUsers', false);
00105 }
00106 $myConfig->setShopId($iShopID);
00107
00108 $mySession->setVar( "lang", $iLanguage);
00109 $mySession->setVar( "language", $iLanguage);
00110
00111 $oUser = oxNew('oxuser');
00112 try {
00113 if (!$oUser->login($sUserName, $sPassword)) {
00114 $oUser = null;
00115 }
00116 }catch(oxUserException $e) {
00117 $oUser = null;
00118 }
00119
00120 if ( !$oUser || ( isset($oUser->iError) && $oUser->iError == -1000)) {
00121
00122 throw new Exception( self::$ERROR_USER_WRONG );
00123 }
00124 elseif( ( $oUser->oxuser__oxrights->value == "malladmin" || $oUser->oxuser__oxrights->value == $myConfig->GetShopID()) )
00125 {
00126 $this->_sSID = $mySession->getId();
00127 $this->_blInit = true;
00128 $this->_iLanguage = $iLanguage;
00129 $this->_sUserID = $oUser->getId();
00130
00131 } else {
00132
00133
00134 throw new Exception( self::$ERROR_USER_NO_RIGHTS);
00135 }
00136
00137 $this->_resetIdx();
00138
00139 return $this->_blInit;
00140 }
00141
00149 public abstract function loadSessionData( $sSessionID );
00150
00157 public function exportType( $sType, $sWhere = null,$iStart = null, $iCount = null, $sSortFieldName = null, $sSortType = null)
00158 {
00159 $this->_beforeExport($sType);
00160 $this->_export( $sType, $sWhere, $iStart, $iCount, $sSortFieldName, $sSortType);
00161 $this->_afterExport($sType);
00162 }
00163
00168 public function Import()
00169 {
00170 $this->_beforeImport();
00171 while( $this->_importOne());
00172 $this->_afterImport();
00173 }
00174
00181 protected function _getInstanceOfType( $sType)
00182 {
00183 $sClassName = 'oxerptype_'.$sType;
00184 $sFullPath = dirname(__FILE__).'/objects/'.$sClassName.'.php';
00185
00186 if( !file_exists( $sFullPath))
00187 throw new Exception( "Type $sType not supported in ERP interface!");
00188
00189 require_once( $sFullPath);
00190
00191
00192 return oxNew ($sClassName);
00193 }
00194
00201 protected function _export( $sType, $sWhere, $iStart = null, $iCount = null, $sSortFieldName = null, $sSortType = null)
00202 {
00203 global $ADODB_FETCH_MODE;
00204
00205 $myConfig = oxConfig::getInstance();
00206
00207 $oType = $this->_getInstanceOfType( $sType);
00208
00209 $sSQL = $oType->getSQL( $sWhere, $this->_iLanguage,$myConfig->getShopId());
00210 $sSQL .= $oType->getSortString($sSortFieldName, $sSortType);
00211 $sFnc = '_Export'.$oType->getFunctionSuffix();
00212
00213 $save = $ADODB_FETCH_MODE;
00214
00215 if(isset($iCount) || isset($iStart)){
00216 $rs = oxDb::getDb(true)->SelectLimit( $sSQL,$iCount,$iStart);
00217 } else {
00218 $rs = oxDb::getDb(true)->Execute( $sSQL);
00219 }
00220
00221 if ($rs != false && $rs->RecordCount() > 0) {
00222 while (!$rs->EOF) {
00223 $blExport = false;
00224 $sMessage = '';
00225
00226 $rs->fields = $oType->addExportData( $rs->fields);
00227
00228
00229 $this->_checkAccess( $oType, false);
00230
00231
00232 try{
00233 $blExport = $this->$sFnc( $rs->fields );
00234 } catch (Exception $e) {
00235 $sMessage = $e->getMessage();
00236
00237 }
00238
00239 $this->_aStatistics[$this->_iIdx] = array('r'=>$blExport,'m'=>$sMessage);
00240
00241 $this->_nextIdx();
00242
00243 $rs->MoveNext();
00244 }
00245 }
00246 $ADODB_FETCH_MODE = $save;
00247 }
00248
00254 protected function _OutputMappingArray( $sTable)
00255 {
00256 $aData = GetTableDescription( $sTable);
00257
00258 $iIdx = 0;
00259 foreach( $aData as $key => $oADODBField) {
00260 if( !(is_numeric( substr( $oADODBField->name, strlen( $oADODBField->name) - 1, 1)) && substr( $oADODBField->name, strlen( $oADODBField->name) - 2, 1) == '_')) {
00261 echo( "'".$oADODBField->name."'\t\t => '".$oADODBField->name."',\n");
00262 $iIdx++;
00263 }
00264 }
00265 }
00266
00267 protected function _getKeyID($oType, $aData)
00268 {
00269 $myConfig = oxConfig::getInstance();
00270 $aKeyFields = $oType->getKeyFields();
00271
00272 if(!is_array($aKeyFields)) {
00273 return false;
00274 }
00275
00276 $oDB = oxDb::getDb();
00277
00278
00279 $aWhere = array();
00280 $blAllKeys = true;
00281 foreach($aKeyFields as $sKey) {
00282 if(array_key_exists($sKey,$aData)){
00283 $aWhere[] = $sKey.'='.$oDB->qstr($aData[$sKey]);
00284 }else{
00285 $blAllKeys = false;
00286 }
00287 }
00288
00289 if($blAllKeys) {
00290 $sSelect = 'SELECT OXID FROM '.$oType->getTableName().' WHERE '.implode(' AND ',$aWhere);
00291 $sOXID = $oDB->GetOne($sSelect);
00292
00293 if(isset($sOXID)){
00294 return $sOXID;
00295 }
00296 }
00297
00298 return oxUtilsObject::getInstance()->generateUID();
00299 }
00300
00304 protected function _resetIdx() {
00305
00306 $this->_iIdx = 0;
00307
00308 if(count($this->_aStatistics) && isset($this->_aStatistics[$this->_iIdx])){
00309 while( isset($this->_aStatistics[$this->_iIdx]) && $this->_aStatistics[$this->_iIdx]['r'] ) {
00310 $this->_iIdx ++;
00311 }
00312 }
00313 }
00314
00318 protected function _nextIdx() {
00319 $this->_iIdx ++;
00320
00321 if(count($this->_aStatistics) && isset($this->_aStatistics[$this->_iIdx])){
00322 while( isset($this->_aStatistics[$this->_iIdx]) && $this->_aStatistics[$this->_iIdx]['r'] ) {
00323 $this->_iIdx ++;
00324 }
00325 }
00326 }
00327
00336 protected function _checkAccess( $oType, $blWrite, $sOxid = null) {
00337 $myConfig = oxConfig::getInstance();
00338 static $aAccessCache;
00339
00340 if( !$this->_blInit)
00341 throw new Exception(self::$ERROR_NO_INIT);
00342
00343 if($blWrite){
00344
00345 $oType->checkWriteAccess($sOxid);
00346 }
00347
00348
00349
00350 if($myConfig->blUseRightsRoles)
00351 {
00352 static $aAccessCache;
00353
00354 $sAccessMode = ((boolean)$blWrite)?'2':'1';
00355 $sTypeClass = get_class($oType);
00356
00357 if(!isset($aAccessCache[$sTypeClass][$sAccessMode]))
00358 {
00359
00360 $oDB = oxDb::getDb();
00361
00362
00363 $aIDs = array( $oDB->qstr($this->_sUserID) );
00364 $sQUserGroups = 'SELECT oxgroupsid ' .
00365 'FROM oxobject2group '.
00366
00367 "WHERE oxshopid = '{$myConfig->getShopId()}' ".
00368 "AND oxobjectid ='{$this->_sUserID}'";
00369
00370 $rs = $oDB->Execute( $sQUserGroups);
00371 if ($rs != false && $rs->RecordCount() > 0) {
00372 while (!$rs->EOF) {
00373 $aIDs[] = $oDB->qstr($rs->fields[0]);
00374 $rs->MoveNext();
00375 }
00376 }
00377
00378 $aRParams = $oType->GetRightFields();
00379 foreach ($aRParams as $sKey => $sParam) {
00380 $aRParams[$sKey] = $oDB->qstr($sParam);
00381 }
00382
00383
00384 $sSelect = 'SELECT count(*) '.
00385 'FROM oxfield2role as rr , oxrolefields as rf, oxobject2role as ro, oxroles as rt '.
00386 "WHERE rr.OXIDX < {$sAccessMode} ".
00387 'AND rr.oxroleid = ro.oxroleid '.
00388 'AND rt.oxid = ro.oxroleid '.
00389 'AND rt.oxactive = 1 '.
00390
00391 "AND rt.oxshopid = '{$myConfig->getShopId()}'".
00392 'AND rf.oxparam IN ('.implode(',',$aRParams).') '.
00393 'AND ro.oxobjectid IN ('.implode(',',$aIDs).') '.
00394 'AND rr.oxfieldid=rf.oxid';
00395
00396 $iNoAccess = $oDB->GetOne($sSelect);
00397 $aAccessCache[$sTypeClass][$sAccessMode] = $iNoAccess;
00398 } else {
00399 $iNoAccess = $aAccessCache[$sTypeClass][$sAccessMode];
00400 }
00401
00402 if($iNoAccess) {
00403 throw new Exception( self::$ERROR_USER_NO_RIGHTS);
00404 }
00405 }
00406 }
00407
00413 protected function _importOne()
00414 {
00415 $blRet = false;
00416
00417
00418 $aData = $this->getImportData();
00419
00420 if( $aData) {
00421 $blRet = true;
00422 $blImport = false;
00423 $sMessage = '';
00424
00425 $sType = $this->_getImportType( $aData);
00426 $sMode = $this->_getImportMode($aData);
00427 $oType = $this->_getInstanceOfType( $sType);
00428 $aData = $this->_modifyData($aData, $oType);
00429
00430
00431 $sFnc = '_' . $sMode . $oType->getFunctionSuffix();
00432
00433 if ($sMode == oxERPBase::$MODE_IMPORT) {
00434 $aData = $oType->addImportData( $aData);
00435 }
00436
00437 try{
00438 $blImport = $this->$sFnc( $oType, $aData);
00439 $sMessage = '';
00440 }
00441 catch (Exception $e) {
00442 $sMessage = $e->getMessage();
00443 }
00444
00445 $this->_aStatistics[$this->_iIdx] = array('r'=>$blImport,'m'=>$sMessage);
00446
00447 }
00448
00449 $this->_nextIdx();
00450
00451 return $blRet;
00452 }
00453
00454
00462 protected function _Save( oxERPType & $oType, $aData, $blAllowCustomShopId = false)
00463 {
00464 $myConfig = oxConfig::getInstance();
00465
00466
00467 $this->_checkAccess( $oType, true, $aData['OXID']);
00468
00469 if($oType->hasKeyFields() && !isset($aData['OXID'])){
00470 $sOXID = $this->_getKeyID($oType, $aData);
00471 if($sOXID){
00472 $aData['OXID'] = $sOXID;
00473 } else {
00474 $aData['OXID'] = oxUtilsObject::getInstance()->generateUID();
00475 }
00476 }
00477
00478 return $oType->saveObject($aData, $blAllowCustomShopId);
00479 }
00480
00486 public static function getRequestedVersion()
00487 {
00488 if (!self::$_sRequestedVersion && isset($_GET['version'])) {
00489 self::$_sRequestedVersion = $_GET['version'];
00490 }
00491 if (!isset(self::$_aDbLayer2ShopDbVersions[self::$_sRequestedVersion])) {
00492 self::$_sRequestedVersion = '';
00493 }
00494 if (!self::$_sRequestedVersion) {
00495 reset(self::$_aDbLayer2ShopDbVersions);
00496 self::$_sRequestedVersion = key(self::$_aDbLayer2ShopDbVersions);
00497 }
00498 return self::$_sRequestedVersion;
00499 }
00500
00506 public static function getUsedDbFieldsVersion()
00507 {
00508 return self::$_aDbLayer2ShopDbVersions[self::getRequestedVersion()];
00509 }
00510
00516 public static function setVersion($sDbLayerVersion = '')
00517 {
00518 self::$_sRequestedVersion = $sDbLayerVersion;
00519 }
00520
00521 }
00522
00523
00524
00525 if(!class_exists('FakeView')){
00526 class FakeView { public function AddGlobalParams() { }}
00527 }
00528
00529