00001 <?php
00002
00006 abstract class oxERPBase
00007 {
00008 const ERROR_USER_WRONG = "ERROR: Could not login";
00009 const ERROR_USER_NO_RIGHTS = "Not sufficient rights to perform operation!";
00010 const ERROR_USER_EXISTS = "ERROR: User already exists";
00011 const ERROR_NO_INIT = "Init not executed, Access denied!";
00012 const ERROR_DELETE_NO_EMPTY_CATEGORY = "Only empty category can be deleated";
00013 const ERROR_OBJECT_NOT_EXISTING = "Object does not exist";
00014 const ERROR_ERP_VERSION_NOT_SUPPORTED_BY_SHOP = "ERROR: shop does not support requested ERP version.";
00015 const ERROR_SHOP_VERSION_NOT_SUPPORTED_BY_ERP = "ERROR: ERP does not support current shop version.";
00016
00017 static $MODE_IMPORT = "Import";
00018 static $MODE_DELETE = "Delete";
00019
00020 protected $_blInit = false;
00021 protected $_iLanguage = null;
00022 protected $_sUserID = null;
00023
00024 protected $_sSID = null;
00025
00026 protected static $_sRequestedVersion = '';
00027
00040 protected static $_aDbLayer2ShopDbVersions = array(
00041 '2.9.0' => '8',
00042 );
00043
00044 public $_aStatistics = array();
00045 public $_iIdx = 0;
00046
00052 public function getStatistics()
00053 {
00054 return $this->_aStatistics;
00055 }
00056
00062 public function getSessionID()
00063 {
00064 return $this->_sSID;
00065 }
00066
00074 protected abstract function _beforeExport($sType);
00075
00083 protected abstract function _afterExport($sType);
00084
00090 protected abstract function _beforeImport();
00091
00097 protected abstract function _afterImport();
00098
00106 public abstract function getImportData($iIdx = null);
00107
00115 protected abstract function _getImportType(&$aData);
00116
00124 protected abstract function _getImportMode($aData);
00125
00134 protected abstract function _modifyData($aData, $oType);
00135
00146 public function __call($sMethod, $aArguments)
00147 {
00148 throw new Exception( "ERROR: Handler for Object '$sMethod' not implemented!");
00149 }
00150
00151
00152
00153
00154
00155
00156
00157
00158
00170 public function init($sUserName, $sPassword, $iShopID = 1, $iLanguage = 0)
00171 {
00172 ini_set('session.use_cookies', 0);
00173 $_COOKIE = array('admin_sid' => false);
00174 $myConfig = oxRegistry::getConfig();
00175 $myConfig->setConfigParam( 'blForceSessionStart', 1 );
00176 $myConfig->setConfigParam( 'blSessionUseCookies', 0);
00177 $myConfig->setConfigParam( 'blAdmin', 1 );
00178 $myConfig->setAdminMode( true );
00179
00180 $mySession = oxRegistry::getSession();
00181 @$mySession->start();
00182
00183
00184 oxSession::setVar( "lang", $iLanguage);
00185 oxSession::setVar( "language", $iLanguage);
00186
00187 $oUser = oxNew('oxuser');
00188 try {
00189 if (!$oUser->login($sUserName, $sPassword)) {
00190 $oUser = null;
00191 }
00192 }catch(oxUserException $e) {
00193 $oUser = null;
00194 }
00195
00196 self::_checkShopVersion();
00197
00198 if ( !$oUser || ( isset($oUser->iError) && $oUser->iError == -1000)) {
00199
00200 throw new Exception( self::ERROR_USER_WRONG );
00201 } elseif ( ($oUser->oxuser__oxrights->value == "malladmin" || $oUser->oxuser__oxrights->value == $myConfig->getShopID()) ) {
00202 $this->_sSID = $mySession->getId();
00203 $this->_blInit = true;
00204 $this->_iLanguage = $iLanguage;
00205 $this->_sUserID = $oUser->getId();
00206
00207 } else {
00208
00209
00210 throw new Exception( self::ERROR_USER_NO_RIGHTS );
00211 }
00212
00213 $this->_resetIdx();
00214
00215 return $this->_blInit;
00216 }
00217
00226 public function loadSessionData( $sSessionID )
00227 {
00228 if (!$sSessionID) {
00229 throw new Exception( "ERROR: Session ID not valid!");
00230 }
00231 $_COOKIE = array('admin_sid' => $sSessionID);
00232
00233 $myConfig = oxRegistry::getConfig();
00234 $myConfig->setConfigParam( 'blAdmin', 1 );
00235 $myConfig->setAdminMode( true );
00236 $mySession = oxRegistry::getSession();
00237
00238
00239 if ($sSessionID != session_id()) {
00240 if (session_id()) {
00241 session_write_close();
00242 }
00243 session_id($sSessionID);
00244 session_start();
00245 }
00246
00247 $sAuth = $mySession->getVar('auth');
00248
00249 if (!isset($sAuth) || !$sAuth) {
00250 throw new Exception( "ERROR: Session ID not valid!");
00251 }
00252
00253 $this->_iLanguage = $mySession->getVar('lang');
00254 $this->_sUserID = $sAuth;
00255
00256
00257 $this->_blInit = true;
00258 }
00259
00272 public function exportType($sType, $sWhere = null, $iStart = null, $iCount = null, $sSortFieldName = null, $sSortType = null)
00273 {
00274 $this->_beforeExport($sType);
00275 $this->_export($sType, $sWhere, $iStart, $iCount, $sSortFieldName, $sSortType);
00276 $this->_afterExport($sType);
00277 }
00278
00284 public function import()
00285 {
00286 $this->_beforeImport();
00287 while ($this->_importOne()) {
00288 }
00289 $this->_afterImport();
00290 }
00291
00299 protected function _getInstanceOfType($sType)
00300 {
00301 $sClassName = 'oxerptype_'.$sType;
00302 $sFullPath = dirname(__FILE__).'/objects/'.$sClassName.'.php';
00303
00304 if ( !file_exists($sFullPath)) {
00305 throw new Exception( "Type $sType not supported in ERP interface!");
00306 }
00307
00308 include_once $sFullPath;
00309
00310
00311 return oxNew ($sClassName);
00312 }
00313
00327 protected function _export($sType, $sWhere, $iStart = null, $iCount = null, $sSortFieldName = null, $sSortType = null)
00328 {
00329 global $ADODB_FETCH_MODE;
00330
00331 $myConfig = oxRegistry::getConfig();
00332
00333 $oType = $this->_getInstanceOfType($sType);
00334
00335 $sSQL = $oType->getSQL($sWhere, $this->_iLanguage, $myConfig->getShopId());
00336 $sSQL .= $oType->getSortString($sSortFieldName, $sSortType);
00337 $sFnc = '_Export'.$oType->getFunctionSuffix();
00338
00339 $save = $ADODB_FETCH_MODE;
00340
00341 $oDb = oxDb::getDb( oxDb::FETCH_MODE_ASSOC );
00342 if (isset($iCount) || isset($iStart)) {
00343 $rs = $oDb->selectLimit( $sSQL, $iCount, $iStart );
00344 } else {
00345 $rs = $oDb->select( $sSQL );
00346 }
00347
00348 if ($rs != false && $rs->recordCount() > 0) {
00349 while (!$rs->EOF) {
00350 $blExport = false;
00351 $sMessage = '';
00352
00353 $rs->fields = $oType->addExportData($rs->fields);
00354
00355
00356 $this->_checkAccess($oType, false);
00357
00358
00359 try{
00360 $blExport = $this->$sFnc($rs->fields );
00361 } catch (Exception $e) {
00362 $sMessage = $e->getMessage();
00363
00364 }
00365
00366 $this->_aStatistics[$this->_iIdx] = array('r'=>$blExport,'m'=>$sMessage);
00367
00368 $this->_nextIdx();
00369
00370 $rs->moveNext();
00371 }
00372 }
00373 $ADODB_FETCH_MODE = $save;
00374 }
00375
00383 protected function _outputMappingArray($sTable)
00384 {
00385 $aData = GetTableDescription($sTable);
00386
00387 $iIdx = 0;
00388 foreach ($aData as $key => $oADODBField) {
00389 if ( !(is_numeric( substr($oADODBField->name, strlen($oADODBField->name) - 1, 1)) && substr($oADODBField->name, strlen($oADODBField->name) - 2, 1) == '_')) {
00390 echo( "'".$oADODBField->name."'\t\t => '".$oADODBField->name."',\n");
00391 $iIdx++;
00392 }
00393 }
00394 }
00395
00404 protected function _getKeyID($oType, $aData)
00405 {
00406 $sOXID = $oType->getOxidFromKeyFields($aData);
00407 if (isset($sOXID)) {
00408
00409 return $sOXID;
00410 }
00411 return oxUtilsObject::getInstance()->generateUID();
00412 }
00413
00419 protected function _resetIdx()
00420 {
00421 $this->_iIdx = 0;
00422
00423 if (count($this->_aStatistics) && isset($this->_aStatistics[$this->_iIdx])) {
00424 while ( isset($this->_aStatistics[$this->_iIdx]) && $this->_aStatistics[$this->_iIdx]['r'] ) {
00425 $this->_iIdx ++;
00426 }
00427 }
00428 }
00429
00435 protected function _nextIdx()
00436 {
00437 $this->_iIdx ++;
00438
00439 if (count($this->_aStatistics) && isset($this->_aStatistics[$this->_iIdx])) {
00440 while ( isset($this->_aStatistics[$this->_iIdx]) && $this->_aStatistics[$this->_iIdx]['r'] ) {
00441 $this->_iIdx ++;
00442 }
00443 }
00444 }
00445
00455 protected function _checkAccess($oType, $blWrite, $sOxid = null)
00456 {
00457 $myConfig = oxRegistry::getConfig();
00458 static $aAccessCache;
00459
00460 if (!$this->_blInit) {
00461 throw new Exception(self::ERROR_NO_INIT);
00462 }
00463
00464 }
00465
00476 protected function _importOne()
00477 {
00478 $blRet = false;
00479
00480
00481 $aData = $this->getImportData();
00482
00483 if ($aData) {
00484 $blRet = true;
00485 $blImport = false;
00486 $sMessage = '';
00487
00488 $sType = $this->_getImportType($aData);
00489 $sMode = $this->_getImportMode($aData);
00490 $oType = $this->_getInstanceOfType($sType);
00491 $aData = $this->_modifyData($aData, $oType);
00492
00493
00494 $sFnc = '_' . $sMode . $oType->getFunctionSuffix();
00495
00496 if ($sMode == oxERPBase::$MODE_IMPORT) {
00497 $aData = $oType->addImportData($aData);
00498 }
00499
00500 try {
00501 $blImport = $this->$sFnc($oType, $aData);
00502 $sMessage = '';
00503 } catch (Exception $e) {
00504 $sMessage = $e->getMessage();
00505 }
00506
00507 $this->_aStatistics[$this->_iIdx] = array('r'=>$blImport,'m'=>$sMessage);
00508
00509 }
00510
00511 $this->_nextIdx();
00512
00513 return $blRet;
00514 }
00515
00516
00526 protected function _save(oxERPType &$oType, $aData, $blAllowCustomShopId = false)
00527 {
00528 $myConfig = oxRegistry::getConfig();
00529
00530
00531 $sOxid = null;
00532 if (isset($aData['OXID'])) {
00533 $sOxid = $aData['OXID'];
00534 }
00535 $this->_checkAccess($oType, true, $sOxid);
00536
00537 return $oType->saveObject($aData, $blAllowCustomShopId);
00538 }
00539
00547 protected static function _checkShopVersion()
00548 {
00549 $myConfig = oxRegistry::getConfig();
00550 if ( method_exists($myConfig, 'getSerial') ) {
00551 if ($myConfig->getSerial() instanceof oxSerial) {
00552 return;
00553 }
00554 }
00555 throw new Exception(self::ERROR_SHOP_VERSION_NOT_SUPPORTED_BY_ERP);
00556 }
00557
00565 protected static function _checkRequestedVersion()
00566 {
00567 return true;
00568 }
00569
00577 public static function getRequestedVersion()
00578 {
00579 if (!self::$_sRequestedVersion) {
00580 self::setVersion();
00581 }
00582 return self::$_sRequestedVersion;
00583 }
00584
00590 public static function getUsedDbFieldsVersion()
00591 {
00592 return self::$_aDbLayer2ShopDbVersions[self::getRequestedVersion()];
00593 }
00594
00604 public static function setVersion($sDbLayerVersion = '')
00605 {
00606 $sDbLayerVersion = '2.9.0';
00607 self::$_sRequestedVersion = $sDbLayerVersion;
00608 self::_checkRequestedVersion();
00609 }
00610
00618 public function createPluginObject($sId)
00619 {
00620 $sClassName = preg_replace('/[^a-z0-9_]/i', '', $sId);
00621 if (preg_match('/(.*)Plugin$/i', $sClassName, $m)) {
00622
00623 $sClassName = $m[1].'Plugin';
00624 } else {
00625 throw new Exception("Plugin handler class has to end with 'Plugin' word (GOT '$sClassName').");
00626 }
00627
00628 $sFileName = dirname(__FILE__).'/plugins/'.strtolower($sClassName).'.php';
00629 if (!is_readable($sFileName)) {
00630 $sFileName = basename($sFileName);
00631 throw new Exception("Can not find the requested plugin file ('$sFileName').");
00632 }
00633 include_once dirname(__FILE__).'/plugins/oxerppluginbase.php';
00634 include_once $sFileName;
00635 if (!class_exists($sClassName)) {
00636 throw new Exception("Can not find the requested plugin class.");
00637 }
00638 $o = new $sClassName();
00639 if ($o instanceof oxErpPluginBase) {
00640 return $o;
00641 }
00642 throw new Exception("Plugin does not extend oxErpPluginBase class.");
00643 }
00644 }
00645