00001 <?php
00002
00006 abstract class oxERPBase
00007 {
00008
00009 const ERROR_USER_WRONG = "ERROR: Could not login";
00010 const ERROR_USER_NO_RIGHTS = "Not sufficient rights to perform operation!";
00011 const ERROR_USER_EXISTS = "ERROR: User already exists";
00012 const ERROR_NO_INIT = "Init not executed, Access denied!";
00013 const ERROR_DELETE_NO_EMPTY_CATEGORY = "Only empty category can be deleated";
00014 const ERROR_OBJECT_NOT_EXISTING = "Object does not exist";
00015 const ERROR_ERP_VERSION_NOT_SUPPORTED_BY_SHOP = "ERROR: shop does not support requested ERP version.";
00016 const ERROR_SHOP_VERSION_NOT_SUPPORTED_BY_ERP = "ERROR: ERP does not support current shop version.";
00017
00018 public static $MODE_IMPORT = "Import";
00019 public static $MODE_DELETE = "Delete";
00020
00021 protected $_blInit = false;
00022 protected $_iLanguage = null;
00023 protected $_sUserID = null;
00024
00025 protected $_sSID = null;
00026
00027 protected static $_sRequestedVersion = '';
00028
00041 protected static $_aDbLayer2ShopDbVersions = array(
00042 '2.9.0' => '8',
00043 );
00044
00050 protected $_aImportedIds = array();
00051
00057 protected $_iImportedRowCount = 0;
00058
00059 public $_aStatistics = array();
00060 public $_iIdx = 0;
00061
00066 abstract public function getImportedRowCount();
00067
00074 abstract public function setImportedIds($key);
00075
00081 public function getStatistics()
00082 {
00083 return $this->_aStatistics;
00084 }
00085
00091 public function getSessionID()
00092 {
00093 return $this->_sSID;
00094 }
00095
00103 abstract protected function _beforeExport($sType);
00104
00112 abstract protected function _afterExport($sType);
00113
00119 abstract protected function _beforeImport();
00120
00126 abstract protected function _afterImport();
00127
00135 abstract public function getImportData($iIdx = null);
00136
00144 abstract protected function _getImportType(&$aData);
00145
00153 abstract protected function _getImportMode($aData);
00154
00163 abstract protected function _modifyData($aData, $oType);
00164
00173 public function __call($sMethod, $aArguments)
00174 {
00175 throw new Exception("ERROR: Handler for Object '$sMethod' not implemented!");
00176 }
00177
00178
00179
00180
00181
00182
00183
00184
00185
00197 public function init($sUserName, $sPassword, $iShopID = 1, $iLanguage = 0)
00198 {
00199 ini_set('session.use_cookies', 0);
00200 $_COOKIE = array('admin_sid' => false);
00201 $myConfig = oxRegistry::getConfig();
00202 $myConfig->setConfigParam('blForceSessionStart', 1);
00203 $myConfig->setConfigParam('blSessionUseCookies', 0);
00204 $myConfig->setConfigParam('blAdmin', 1);
00205 $myConfig->setAdminMode(true);
00206
00207 $mySession = oxRegistry::getSession();
00208 @$mySession->start();
00209
00210
00211 oxRegistry::getSession()->setVariable("lang", $iLanguage);
00212 oxRegistry::getSession()->setVariable("language", $iLanguage);
00213
00214 $oUser = oxNew('oxuser');
00215 try {
00216 if (!$oUser->login($sUserName, $sPassword)) {
00217 $oUser = null;
00218 }
00219 } catch (oxUserException $e) {
00220 $oUser = null;
00221 }
00222
00223 self::_checkShopVersion();
00224
00225 if (!$oUser || (isset($oUser->iError) && $oUser->iError == -1000)) {
00226
00227 throw new Exception(self::ERROR_USER_WRONG);
00228 } elseif (($oUser->oxuser__oxrights->value == "malladmin" || $oUser->oxuser__oxrights->value == $myConfig->getShopID())) {
00229 $this->_sSID = $mySession->getId();
00230 $this->_blInit = true;
00231 $this->_iLanguage = $iLanguage;
00232 $this->_sUserID = $oUser->getId();
00233
00234 } else {
00235
00236
00237 throw new Exception(self::ERROR_USER_NO_RIGHTS);
00238 }
00239
00240 $this->_resetIdx();
00241
00242 return $this->_blInit;
00243 }
00244
00251 public function loadSessionData($sSessionID)
00252 {
00253 if (!$sSessionID) {
00254 throw new Exception("ERROR: Session ID not valid!");
00255 }
00256 $_COOKIE = array('admin_sid' => $sSessionID);
00257
00258 $myConfig = oxRegistry::getConfig();
00259 $myConfig->setConfigParam('blAdmin', 1);
00260 $myConfig->setAdminMode(true);
00261 $mySession = oxRegistry::getSession();
00262
00263
00264 if ($sSessionID != session_id()) {
00265 if (session_id()) {
00266 session_write_close();
00267 }
00268 session_id($sSessionID);
00269 session_start();
00270 }
00271
00272 $sAuth = $mySession->getVariable('auth');
00273
00274 if (!isset($sAuth) || !$sAuth) {
00275 throw new Exception("ERROR: Session ID not valid!");
00276 }
00277
00278 $this->_iLanguage = $mySession->getVariable('lang');
00279 $this->_sUserID = $sAuth;
00280
00281
00282 $this->_blInit = true;
00283 }
00284
00295 public function exportType($sType, $sWhere = null, $iStart = null, $iCount = null, $sSortFieldName = null, $sSortType = null)
00296 {
00297 $this->_beforeExport($sType);
00298 $this->_export($sType, $sWhere, $iStart, $iCount, $sSortFieldName, $sSortType);
00299 $this->_afterExport($sType);
00300 }
00301
00305 public function import()
00306 {
00307 $this->_beforeImport();
00308 while ($this->_importOne()) {
00309 }
00310 $this->_afterImport();
00311 }
00312
00320 protected function _getInstanceOfType($sType)
00321 {
00322 $sClassName = 'oxerptype_' . $sType;
00323 $sFullPath = dirname(__FILE__) . '/objects/' . $sClassName . '.php';
00324
00325 if (!file_exists($sFullPath)) {
00326 throw new Exception("Type $sType not supported in ERP interface!");
00327 }
00328
00329 include_once $sFullPath;
00330
00331
00332 return oxNew($sClassName);
00333 }
00334
00346 protected function _export($sType, $sWhere, $iStart = null, $iCount = null, $sSortFieldName = null, $sSortType = null)
00347 {
00348 global $ADODB_FETCH_MODE;
00349
00350 $myConfig = oxRegistry::getConfig();
00351
00352 $oType = $this->_getInstanceOfType($sType);
00353
00354 $sSQL = $oType->getSQL($sWhere, $this->_iLanguage, $myConfig->getShopId());
00355 $sSQL .= $oType->getSortString($sSortFieldName, $sSortType);
00356 $sFnc = '_Export' . $oType->getFunctionSuffix();
00357
00358 $save = $ADODB_FETCH_MODE;
00359
00360 $oDb = oxDb::getDb(oxDb::FETCH_MODE_ASSOC);
00361 if (isset($iCount) || isset($iStart)) {
00362 $rs = $oDb->selectLimit($sSQL, $iCount, $iStart);
00363 } else {
00364 $rs = $oDb->select($sSQL);
00365 }
00366
00367 if ($rs != false && $rs->recordCount() > 0) {
00368 while (!$rs->EOF) {
00369 $blExport = false;
00370 $sMessage = '';
00371
00372 $rs->fields = $oType->addExportData($rs->fields);
00373
00374
00375 $this->_checkAccess($oType, false);
00376
00377
00378 try {
00379 $blExport = $this->$sFnc($rs->fields);
00380 } catch (Exception $e) {
00381 $sMessage = $e->getMessage();
00382
00383 }
00384
00385 $this->_aStatistics[$this->_iIdx] = array('r' => $blExport, 'm' => $sMessage);
00386
00387 $this->_nextIdx();
00388
00389 $rs->moveNext();
00390 }
00391 }
00392 $ADODB_FETCH_MODE = $save;
00393 }
00394
00400 protected function _outputMappingArray($sTable)
00401 {
00402 $aData = GetTableDescription($sTable);
00403
00404 $iIdx = 0;
00405 foreach ($aData as $key => $oADODBField) {
00406 if (!(is_numeric(substr($oADODBField->name, strlen($oADODBField->name) - 1, 1)) && substr($oADODBField->name, strlen($oADODBField->name) - 2, 1) == '_')) {
00407 echo("'" . $oADODBField->name . "'\t\t => '" . $oADODBField->name . "',\n");
00408 $iIdx++;
00409 }
00410 }
00411 }
00412
00421 protected function _getKeyID($oType, $aData)
00422 {
00423 $sOXID = $oType->getOxidFromKeyFields($aData);
00424 if (isset($sOXID)) {
00425
00426 return $sOXID;
00427 }
00428
00429 return oxUtilsObject::getInstance()->generateUID();
00430 }
00431
00435 protected function _resetIdx()
00436 {
00437 $this->_iIdx = 0;
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
00449 protected function _nextIdx()
00450 {
00451 $this->_iIdx++;
00452
00453 if (count($this->_aStatistics) && isset($this->_aStatistics[$this->_iIdx])) {
00454 while (isset($this->_aStatistics[$this->_iIdx]) && $this->_aStatistics[$this->_iIdx]['r']) {
00455 $this->_iIdx++;
00456 }
00457 }
00458 }
00459
00467 protected function _checkAccess($oType, $blWrite, $sOxid = null)
00468 {
00469 $myConfig = oxRegistry::getConfig();
00470 static $aAccessCache;
00471
00472 if (!$this->_blInit) {
00473 throw new Exception(self::ERROR_NO_INIT);
00474 }
00475
00476 }
00477
00488 protected function _importOne()
00489 {
00490 $blRet = false;
00491
00492
00493 $aData = $this->getImportData();
00494
00495 if ($aData) {
00496 $blRet = true;
00497 $blImport = false;
00498 $sMessage = '';
00499
00500 $sType = $this->_getImportType($aData);
00501 $sMode = $this->_getImportMode($aData);
00502 $oType = $this->_getInstanceOfType($sType);
00503 $aData = $this->_modifyData($aData, $oType);
00504
00505
00506 $sFnc = '_' . $sMode . $oType->getFunctionSuffix();
00507
00508 if ($sMode == oxERPBase::$MODE_IMPORT) {
00509 $aData = $oType->addImportData($aData);
00510 }
00511
00512 try {
00513 $iId = $this->$sFnc($oType, $aData);
00514 if (!$iId) {
00515 $blImport = false;
00516 } else {
00517 $this->setImportedIds($iId);
00518 $blImport = true;
00519 }
00520 $sMessage = '';
00521 } catch (Exception $e) {
00522 $sMessage = $e->getMessage();
00523 }
00524
00525 $this->_aStatistics[$this->_iIdx] = array('r' => $blImport, 'm' => $sMessage);
00526
00527 }
00528
00529 $this->_nextIdx();
00530
00531 return $blRet;
00532 }
00533
00534
00544 protected function _save(oxERPType &$oType, $aData, $blAllowCustomShopId = false)
00545 {
00546 $myConfig = oxRegistry::getConfig();
00547
00548
00549 $sOxid = null;
00550 if (isset($aData['OXID'])) {
00551 $sOxid = $aData['OXID'];
00552 }
00553 $this->_checkAccess($oType, true, $sOxid);
00554
00555 return $oType->saveObject($aData, $blAllowCustomShopId);
00556 }
00557
00565 protected static function _checkShopVersion()
00566 {
00567 $myConfig = oxRegistry::getConfig();
00568 if (method_exists($myConfig, 'getSerial')) {
00569 if ($myConfig->getSerial() instanceof oxSerial) {
00570 return;
00571 }
00572 }
00573 throw new Exception(self::ERROR_SHOP_VERSION_NOT_SUPPORTED_BY_ERP);
00574 }
00575
00583 protected static function _checkRequestedVersion()
00584 {
00585 return true;
00586 }
00587
00595 public static function getRequestedVersion()
00596 {
00597 if (!self::$_sRequestedVersion) {
00598 self::setVersion();
00599 }
00600
00601 return self::$_sRequestedVersion;
00602 }
00603
00609 public static function getUsedDbFieldsVersion()
00610 {
00611 return self::$_aDbLayer2ShopDbVersions[self::getRequestedVersion()];
00612 }
00613
00621 public static function setVersion($sDbLayerVersion = '')
00622 {
00623 $sDbLayerVersion = '2.9.0';
00624 self::$_sRequestedVersion = $sDbLayerVersion;
00625 self::_checkRequestedVersion();
00626 }
00627
00635 public function createPluginObject($sId)
00636 {
00637 $sClassName = preg_replace('/[^a-z0-9_]/i', '', $sId);
00638 if (preg_match('/(.*)Plugin$/i', $sClassName, $m)) {
00639
00640 $sClassName = $m[1] . 'Plugin';
00641 } else {
00642 throw new Exception("Plugin handler class has to end with 'Plugin' word (GOT '$sClassName').");
00643 }
00644
00645 $sFileName = dirname(__FILE__) . '/plugins/' . strtolower($sClassName) . '.php';
00646 if (!is_readable($sFileName)) {
00647 $sFileName = basename($sFileName);
00648 throw new Exception("Can not find the requested plugin file ('$sFileName').");
00649 }
00650 include_once dirname(__FILE__) . '/plugins/oxerppluginbase.php';
00651 include_once $sFileName;
00652 if (!class_exists($sClassName)) {
00653 throw new Exception("Can not find the requested plugin class.");
00654 }
00655 $o = new $sClassName();
00656 if ($o instanceof oxErpPluginBase) {
00657 return $o;
00658 }
00659 throw new Exception("Plugin does not extend oxErpPluginBase class.");
00660 }
00661 }