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 
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     // public interface
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 = oxConfig::getInstance();
00175         $myConfig->setConfigParam( 'blForceSessionStart', 1 );
00176         $myConfig->setConfigParam( 'blSessionUseCookies', 0);
00177         $myConfig->setConfigParam( 'blAdmin', 1 );
00178         $myConfig->setAdminMode( true );
00179 
00180         $mySession = oxSession::getInstance();
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             // authorization error
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             //$mySession->freeze();
00207         } else {
00208 
00209             //user does not have sufficient rights for shop
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         // start session
00233         $myConfig = oxConfig::getInstance();
00234         $myConfig->setConfigParam( 'blAdmin', 1 );
00235         $myConfig->setAdminMode( true );
00236         $mySession = oxSession::getInstance();
00237 
00238         // change session if needed
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         //return new $sClassName;
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 = oxConfig::getInstance();
00332         // prepare
00333         $oType   = $this->_getInstanceOfType($sType);
00334         //$sSQL    = $oType->getSQL($sWhere, $this->_iLanguage, $this->_iShopID);
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         if (isset($iCount) || isset($iStart)) {
00342             $rs = oxDb::getDb(true)->SelectLimit($sSQL, $iCount, $iStart);
00343         } else {
00344             $rs = oxDb::getDb(true)->Execute($sSQL);
00345         }
00346 
00347         if ($rs != false && $rs->recordCount() > 0) {
00348             while (!$rs->EOF) {
00349                 $blExport = false;
00350                 $sMessage = '';
00351 
00352                 $rs->fields = $oType->addExportData($rs->fields);
00353 
00354                 // check rights
00355                 $this->_checkAccess($oType, false);
00356 
00357                 // export now
00358                 try{
00359                     $blExport = $this->$sFnc($rs->fields );
00360                 } catch (Exception $e) {
00361                     $sMessage = $e->getMessage();
00362 
00363                 }
00364 
00365                 $this->_aStatistics[$this->_iIdx] = array('r'=>$blExport,'m'=>$sMessage);
00366                 //#2428 MAFI
00367                 $this->_nextIdx();
00368 
00369                 $rs->moveNext();
00370             }
00371         }
00372         $ADODB_FETCH_MODE       = $save;
00373     }
00374 
00382     protected function _outputMappingArray($sTable)
00383     {
00384         $aData = GetTableDescription($sTable);
00385 
00386         $iIdx = 0;
00387         foreach ($aData as $key => $oADODBField) {
00388             if ( !(is_numeric( substr($oADODBField->name, strlen($oADODBField->name) - 1, 1)) &&  substr($oADODBField->name, strlen($oADODBField->name) - 2, 1) == '_')) {
00389                 echo( "'".$oADODBField->name."'\t\t => '".$oADODBField->name."',\n");
00390                 $iIdx++;
00391             }
00392         }
00393     }
00394 
00403     protected function _getKeyID($oType, $aData)
00404     {
00405         $sOXID = $oType->getOxidFromKeyFields($aData);
00406         if (isset($sOXID)) {
00407             // note: also pass false here
00408             return $sOXID;
00409         }
00410         return oxUtilsObject::getInstance()->generateUID();
00411     }
00412 
00418     protected function _resetIdx()
00419     {
00420         $this->_iIdx = 0;
00421 
00422         if (count($this->_aStatistics) && isset($this->_aStatistics[$this->_iIdx])) {
00423             while ( isset($this->_aStatistics[$this->_iIdx]) && $this->_aStatistics[$this->_iIdx]['r'] ) {
00424                 $this->_iIdx ++;
00425             }
00426         }
00427     }
00428 
00434     protected function _nextIdx()
00435     {
00436         $this->_iIdx ++;
00437 
00438         if (count($this->_aStatistics) && isset($this->_aStatistics[$this->_iIdx])) {
00439             while ( isset($this->_aStatistics[$this->_iIdx]) && $this->_aStatistics[$this->_iIdx]['r'] ) {
00440                 $this->_iIdx ++;
00441             }
00442         }
00443     }
00444 
00454     protected function _checkAccess($oType, $blWrite, $sOxid = null)
00455     {
00456         $myConfig = oxConfig::getInstance();
00457         static $aAccessCache;
00458 
00459         if (!$this->_blInit) {
00460             throw new Exception(self::ERROR_NO_INIT);
00461         }
00462 
00463     }
00464 
00475     protected function _importOne()
00476     {
00477         $blRet = false;
00478 
00479         // import one row/call/object...
00480         $aData = $this->getImportData();
00481 
00482         if ($aData) {
00483             $blRet = true;
00484             $blImport = false;
00485             $sMessage = '';
00486 
00487             $sType  = $this->_getImportType($aData);
00488             $sMode = $this->_getImportMode($aData);
00489             $oType  = $this->_getInstanceOfType($sType);
00490             $aData = $this->_modifyData($aData, $oType);
00491 
00492             // import now
00493             $sFnc   = '_' . $sMode . $oType->getFunctionSuffix();
00494 
00495             if ($sMode == oxERPBase::$MODE_IMPORT) {
00496                 $aData = $oType->addImportData($aData);
00497             }
00498 
00499             try {
00500                 $blImport = $this->$sFnc($oType, $aData);
00501                 $sMessage = '';
00502             } catch (Exception $e) {
00503                 $sMessage = $e->getMessage();
00504             }
00505 
00506             $this->_aStatistics[$this->_iIdx] = array('r'=>$blImport,'m'=>$sMessage);
00507 
00508         }
00509         //hotfix #2428 MAFI
00510         $this->_nextIdx();
00511 
00512         return $blRet;
00513     }
00514 
00515 
00525     protected function _save(oxERPType &$oType, $aData, $blAllowCustomShopId = false)
00526     {
00527         $myConfig = oxConfig::getInstance();
00528 
00529         // check rights
00530         $sOxid = null;
00531         if (isset($aData['OXID'])) {
00532             $sOxid = $aData['OXID'];
00533         }
00534         $this->_checkAccess($oType, true, $sOxid);
00535 
00536         return $oType->saveObject($aData, $blAllowCustomShopId);
00537     }
00538 
00546     protected static function _checkShopVersion()
00547     {
00548         $myConfig = oxConfig::getInstance();
00549         if ( method_exists($myConfig, 'getSerial') ) {
00550             if ($myConfig->getSerial() instanceof oxSerial) {
00551                 return;
00552             }
00553         }
00554         throw new Exception(self::ERROR_SHOP_VERSION_NOT_SUPPORTED_BY_ERP);
00555     }
00556 
00564     protected static function _checkRequestedVersion()
00565     {
00566         return true;
00567     }
00568 
00576     public static function getRequestedVersion()
00577     {
00578         if (!self::$_sRequestedVersion) {
00579             self::setVersion();
00580         }
00581         return self::$_sRequestedVersion;
00582     }
00583 
00589     public static function getUsedDbFieldsVersion()
00590     {
00591         return self::$_aDbLayer2ShopDbVersions[self::getRequestedVersion()];
00592     }
00593 
00603     public static function setVersion($sDbLayerVersion = '')
00604     {
00605         $sDbLayerVersion =  '2.9.0';
00606         self::$_sRequestedVersion = $sDbLayerVersion;
00607         self::_checkRequestedVersion();
00608     }
00609 
00617     public function createPluginObject($sId)
00618     {
00619         $sClassName = preg_replace('/[^a-z0-9_]/i', '', $sId);
00620         if (preg_match('/(.*)Plugin$/i', $sClassName, $m)) {
00621             // fix possible case changes
00622             $sClassName = $m[1].'Plugin';
00623         } else {
00624             throw new Exception("Plugin handler class has to end with 'Plugin' word (GOT '$sClassName').");
00625         }
00626 
00627         $sFileName = dirname(__FILE__).'/plugins/'.strtolower($sClassName).'.php';
00628         if (!is_readable($sFileName)) {
00629             $sFileName = basename($sFileName);
00630             throw new Exception("Can not find the requested plugin file ('$sFileName').");
00631         }
00632         include_once dirname(__FILE__).'/plugins/oxerppluginbase.php';
00633         include_once $sFileName;
00634         if (!class_exists($sClassName)) {
00635             throw new Exception("Can not find the requested plugin class.");
00636         }
00637         $o = new $sClassName();
00638         if ($o instanceof oxErpPluginBase) {
00639             return $o;
00640         }
00641         throw new Exception("Plugin does not extend oxErpPluginBase class.");
00642     }
00643 }
00644