oxerpbase.php

Go to the documentation of this file.
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     //session id
00024     protected   $_sSID      = null;
00025 
00026     protected static $_sRequestedVersion = '';
00027 
00040     protected static $_aDbLayer2ShopDbVersions = array(
00041         '2.9.0' => '8', // added new fields to oxcategories, oxorderarticle
00042     );
00043 
00048     protected $_aImportedIds = array();
00049 
00054     protected $_iImportedRowCount = 0;
00055 
00056     public $_aStatistics = array();
00057     public $_iIdx        = 0;
00058 
00063     public abstract function getImportedRowCount();
00064 
00071     public abstract function setImportedIds( $key );
00077     public function getStatistics()
00078     {
00079         return $this->_aStatistics;
00080     }
00081 
00087     public function getSessionID()
00088     {
00089         return $this->_sSID;
00090     }
00091 
00099     protected abstract function _beforeExport($sType);
00100 
00108     protected abstract function _afterExport($sType);
00109 
00115     protected abstract function _beforeImport();
00116 
00122     protected abstract function _afterImport();
00123 
00131     public abstract function getImportData($iIdx = null);
00132 
00140     protected abstract function _getImportType(&$aData);
00141 
00149     protected abstract function _getImportMode($aData);
00150 
00159     protected abstract function _modifyData($aData, $oType);
00160 
00171     public function __call($sMethod, $aArguments)
00172     {
00173         throw new Exception( "ERROR: Handler for Object '$sMethod' not implemented!");
00174     }
00175 
00176 
00177     // -------------------------------------------------------------------------
00178     //
00179     // public interface
00180     //
00181     // -------------------------------------------------------------------------
00182 
00183 
00195     public function init($sUserName, $sPassword, $iShopID = 1, $iLanguage = 0)
00196     {
00197         ini_set('session.use_cookies', 0);
00198         $_COOKIE = array('admin_sid' => false);
00199         $myConfig = oxRegistry::getConfig();
00200         $myConfig->setConfigParam( 'blForceSessionStart', 1 );
00201         $myConfig->setConfigParam( 'blSessionUseCookies', 0);
00202         $myConfig->setConfigParam( 'blAdmin', 1 );
00203         $myConfig->setAdminMode( true );
00204 
00205         $mySession = oxRegistry::getSession();
00206         @$mySession->start();
00207 
00208 
00209         oxSession::setVar( "lang", $iLanguage);
00210         oxSession::setVar( "language", $iLanguage);
00211 
00212         $oUser = oxNew('oxuser');
00213         try {
00214             if (!$oUser->login($sUserName, $sPassword)) {
00215                 $oUser = null;
00216             }
00217         }catch(oxUserException $e) {
00218             $oUser = null;
00219         }
00220 
00221         self::_checkShopVersion();
00222 
00223         if ( !$oUser || ( isset($oUser->iError) && $oUser->iError == -1000)) {
00224             // authorization error
00225             throw new Exception( self::ERROR_USER_WRONG );
00226         } elseif ( ($oUser->oxuser__oxrights->value == "malladmin" || $oUser->oxuser__oxrights->value == $myConfig->getShopID()) ) {
00227             $this->_sSID        = $mySession->getId();
00228             $this->_blInit      = true;
00229             $this->_iLanguage   = $iLanguage;
00230             $this->_sUserID     = $oUser->getId();
00231             //$mySession->freeze();
00232         } else {
00233 
00234             //user does not have sufficient rights for shop
00235             throw new Exception( self::ERROR_USER_NO_RIGHTS );
00236         }
00237 
00238         $this->_resetIdx();
00239 
00240         return $this->_blInit;
00241     }
00242 
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         // start session
00258         $myConfig = oxRegistry::getConfig();
00259         $myConfig->setConfigParam( 'blAdmin', 1 );
00260         $myConfig->setAdminMode( true );
00261         $mySession = oxRegistry::getSession();
00262 
00263         // change session if needed
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->getVar('auth');
00273 
00274         if (!isset($sAuth) || !$sAuth) {
00275             throw new Exception( "ERROR: Session ID not valid!");
00276         }
00277 
00278         $this->_iLanguage   = $mySession->getVar('lang');
00279         $this->_sUserID     = $sAuth;
00280 
00281 
00282         $this->_blInit      = true;
00283     }
00284 
00297     public function exportType($sType, $sWhere = null, $iStart = null, $iCount = null, $sSortFieldName = null, $sSortType = null)
00298     {
00299         $this->_beforeExport($sType);
00300         $this->_export($sType, $sWhere, $iStart, $iCount, $sSortFieldName, $sSortType);
00301         $this->_afterExport($sType);
00302     }
00303 
00309     public function import()
00310     {
00311         $this->_beforeImport();
00312         while ($this->_importOne()) {
00313         }
00314         $this->_afterImport();
00315     }
00316 
00324     protected function _getInstanceOfType($sType)
00325     {
00326         $sClassName = 'oxerptype_'.$sType;
00327         $sFullPath  = dirname(__FILE__).'/objects/'.$sClassName.'.php';
00328 
00329         if ( !file_exists($sFullPath)) {
00330             throw new Exception( "Type $sType not supported in ERP interface!");
00331         }
00332 
00333         include_once $sFullPath;
00334 
00335         //return new $sClassName;
00336         return oxNew ($sClassName);
00337     }
00338 
00352     protected function _export($sType, $sWhere, $iStart = null, $iCount = null, $sSortFieldName = null, $sSortType = null)
00353     {
00354         global $ADODB_FETCH_MODE;
00355 
00356         $myConfig = oxRegistry::getConfig();
00357         // prepare
00358         $oType   = $this->_getInstanceOfType($sType);
00359         //$sSQL    = $oType->getSQL($sWhere, $this->_iLanguage, $this->_iShopID);
00360         $sSQL    = $oType->getSQL($sWhere, $this->_iLanguage, $myConfig->getShopId());
00361         $sSQL    .= $oType->getSortString($sSortFieldName, $sSortType);
00362         $sFnc    = '_Export'.$oType->getFunctionSuffix();
00363 
00364         $save = $ADODB_FETCH_MODE;
00365 
00366         $oDb = oxDb::getDb( oxDb::FETCH_MODE_ASSOC );
00367         if (isset($iCount) || isset($iStart)) {
00368             $rs = $oDb->selectLimit( $sSQL, $iCount, $iStart );
00369         } else {
00370             $rs = $oDb->select( $sSQL );
00371         }
00372 
00373         if ($rs != false && $rs->recordCount() > 0) {
00374             while (!$rs->EOF) {
00375                 $blExport = false;
00376                 $sMessage = '';
00377 
00378                 $rs->fields = $oType->addExportData($rs->fields);
00379 
00380                 // check rights
00381                 $this->_checkAccess($oType, false);
00382 
00383                 // export now
00384                 try{
00385                     $blExport = $this->$sFnc($rs->fields );
00386                 } catch (Exception $e) {
00387                     $sMessage = $e->getMessage();
00388 
00389                 }
00390 
00391                 $this->_aStatistics[$this->_iIdx] = array('r'=>$blExport,'m'=>$sMessage);
00392                 //#2428 MAFI
00393                 $this->_nextIdx();
00394 
00395                 $rs->moveNext();
00396             }
00397         }
00398         $ADODB_FETCH_MODE       = $save;
00399     }
00400 
00408     protected function _outputMappingArray($sTable)
00409     {
00410         $aData = GetTableDescription($sTable);
00411 
00412         $iIdx = 0;
00413         foreach ($aData as $key => $oADODBField) {
00414             if ( !(is_numeric( substr($oADODBField->name, strlen($oADODBField->name) - 1, 1)) &&  substr($oADODBField->name, strlen($oADODBField->name) - 2, 1) == '_')) {
00415                 echo( "'".$oADODBField->name."'\t\t => '".$oADODBField->name."',\n");
00416                 $iIdx++;
00417             }
00418         }
00419     }
00420 
00429     protected function _getKeyID($oType, $aData)
00430     {
00431         $sOXID = $oType->getOxidFromKeyFields($aData);
00432         if (isset($sOXID)) {
00433             // note: also pass false here
00434             return $sOXID;
00435         }
00436         return oxUtilsObject::getInstance()->generateUID();
00437     }
00438 
00444     protected function _resetIdx()
00445     {
00446         $this->_iIdx = 0;
00447 
00448         if (count($this->_aStatistics) && isset($this->_aStatistics[$this->_iIdx])) {
00449             while ( isset($this->_aStatistics[$this->_iIdx]) && $this->_aStatistics[$this->_iIdx]['r'] ) {
00450                 $this->_iIdx ++;
00451             }
00452         }
00453     }
00454 
00460     protected function _nextIdx()
00461     {
00462         $this->_iIdx ++;
00463 
00464         if (count($this->_aStatistics) && isset($this->_aStatistics[$this->_iIdx])) {
00465             while ( isset($this->_aStatistics[$this->_iIdx]) && $this->_aStatistics[$this->_iIdx]['r'] ) {
00466                 $this->_iIdx ++;
00467             }
00468         }
00469     }
00470 
00480     protected function _checkAccess($oType, $blWrite, $sOxid = null)
00481     {
00482         $myConfig = oxRegistry::getConfig();
00483         static $aAccessCache;
00484 
00485         if (!$this->_blInit) {
00486             throw new Exception(self::ERROR_NO_INIT);
00487         }
00488 
00489     }
00490 
00501     protected function _importOne()
00502     {
00503         $blRet = false;
00504 
00505         // import one row/call/object...
00506         $aData = $this->getImportData();
00507 
00508         if ($aData) {
00509             $blRet = true;
00510             $blImport = false;
00511             $sMessage = '';
00512 
00513             $sType  = $this->_getImportType($aData);
00514             $sMode = $this->_getImportMode($aData);
00515             $oType  = $this->_getInstanceOfType($sType);
00516             $aData = $this->_modifyData($aData, $oType);
00517 
00518             // import now
00519             $sFnc   = '_' . $sMode . $oType->getFunctionSuffix();
00520 
00521             if ($sMode == oxERPBase::$MODE_IMPORT) {
00522                 $aData = $oType->addImportData($aData);
00523             }
00524 
00525             try {
00526                 $iId = $this->$sFnc($oType, $aData);
00527                 if ( !$iId )
00528                     $blImport = false;
00529                 else {
00530                     $this->setImportedIds( $iId );
00531                     $blImport = true;
00532                 }
00533                 $sMessage = '';
00534             } catch (Exception $e) {
00535                 $sMessage = $e->getMessage();
00536             }
00537 
00538             $this->_aStatistics[$this->_iIdx] = array('r'=>$blImport,'m'=>$sMessage);
00539 
00540         }
00541         //hotfix #2428 MAFI
00542         $this->_nextIdx();
00543 
00544         return $blRet;
00545     }
00546 
00547 
00557     protected function _save(oxERPType &$oType, $aData, $blAllowCustomShopId = false)
00558     {
00559         $myConfig = oxRegistry::getConfig();
00560 
00561         // check rights
00562         $sOxid = null;
00563         if (isset($aData['OXID'])) {
00564             $sOxid = $aData['OXID'];
00565         }
00566         $this->_checkAccess($oType, true, $sOxid);
00567 
00568         return $oType->saveObject($aData, $blAllowCustomShopId);
00569     }
00570 
00578     protected static function _checkShopVersion()
00579     {
00580         $myConfig = oxRegistry::getConfig();
00581         if ( method_exists($myConfig, 'getSerial') ) {
00582             if ($myConfig->getSerial() instanceof oxSerial) {
00583                 return;
00584             }
00585         }
00586         throw new Exception(self::ERROR_SHOP_VERSION_NOT_SUPPORTED_BY_ERP);
00587     }
00588 
00596     protected static function _checkRequestedVersion()
00597     {
00598         return true;
00599     }
00600 
00608     public static function getRequestedVersion()
00609     {
00610         if (!self::$_sRequestedVersion) {
00611             self::setVersion();
00612         }
00613         return self::$_sRequestedVersion;
00614     }
00615 
00621     public static function getUsedDbFieldsVersion()
00622     {
00623         return self::$_aDbLayer2ShopDbVersions[self::getRequestedVersion()];
00624     }
00625 
00635     public static function setVersion($sDbLayerVersion = '')
00636     {
00637         $sDbLayerVersion =  '2.9.0';
00638         self::$_sRequestedVersion = $sDbLayerVersion;
00639         self::_checkRequestedVersion();
00640     }
00641 
00649     public function createPluginObject($sId)
00650     {
00651         $sClassName = preg_replace('/[^a-z0-9_]/i', '', $sId);
00652         if (preg_match('/(.*)Plugin$/i', $sClassName, $m)) {
00653             // fix possible case changes
00654             $sClassName = $m[1].'Plugin';
00655         } else {
00656             throw new Exception("Plugin handler class has to end with 'Plugin' word (GOT '$sClassName').");
00657         }
00658 
00659         $sFileName = dirname(__FILE__).'/plugins/'.strtolower($sClassName).'.php';
00660         if (!is_readable($sFileName)) {
00661             $sFileName = basename($sFileName);
00662             throw new Exception("Can not find the requested plugin file ('$sFileName').");
00663         }
00664         include_once dirname(__FILE__).'/plugins/oxerppluginbase.php';
00665         include_once $sFileName;
00666         if (!class_exists($sClassName)) {
00667             throw new Exception("Can not find the requested plugin class.");
00668         }
00669         $o = new $sClassName();
00670         if ($o instanceof oxErpPluginBase) {
00671             return $o;
00672         }
00673         throw new Exception("Plugin does not extend oxErpPluginBase class.");
00674     }
00675 }
00676