oxerpbase.php

Go to the documentation of this file.
00001 <?php
00002 
00003 require_once 'oxerpcompatability.php';
00004 
00013 abstract class oxERPBase
00014 {
00015     static $ERROR_USER_WRONG = "ERROR: Could not login";
00016     static $ERROR_USER_NO_RIGHTS =  "Not sufficient rights to perform operation!";
00017     static $ERROR_USER_EXISTS = "ERROR: User already exists";
00018     static $ERROR_NO_INIT = "Init not executed, Access denied!";
00019     static $ERROR_DELETE_NO_EMPTY_CATEGORY = "Only empty category can be deleated";
00020     static $ERROR_OBJECT_NOT_EXISTING = "Object does not exist";
00021 
00022     static $MODE_IMPORT = "Import";
00023     static $MODE_DELETE = "Delete";
00024 
00030     protected $_blInit = false;
00031 
00037     protected $_iLanguage = null;
00038 
00044     protected $_sUserID = null;
00045 
00051     protected $_sSID = null;
00052 
00058     protected static $_sRequestedVersion = '';
00059 
00066     protected static $_aDbLayer2ShopDbVersions = array(
00067         '1' => '1', '1.1' => '1', '2' => '2',
00068     );
00069 
00075     public $_aStatistics = array();
00076 
00082     public $_iIdx = 0;
00083 
00089     public function getStatistics()
00090     {
00091         return $this->_aStatistics;
00092     }
00093 
00099     public function getSessionID()
00100     {
00101         return $this->_sSID;
00102     }
00103 
00111     protected abstract function _beforeExport($sType);
00112 
00120     protected abstract function _afterExport($sType);
00121 
00127     protected abstract function _beforeImport();
00128 
00134     protected abstract function _afterImport();
00135 
00141     public abstract function getImportData();
00142 
00150     protected abstract function _getImportType( & $aData );
00151 
00159     protected abstract function _getImportMode( $aData );
00160 
00169     protected abstract function _modifyData( $aData, $oType );
00170 
00179     public function __call( $sMethod, $aArguments )
00180     {
00181         throw new Exception( "ERROR: Handler for Object '$sMethod' not implemented!");
00182     }
00183 
00184 
00185     // -------------------------------------------------------------------------
00186     //
00187     // public interface
00188     //
00189     // -------------------------------------------------------------------------
00190 
00191 
00204     public function init( $sUserName, $sPassword, $iShopID = 1, $iLanguage = 0)
00205     {
00206         $_COOKIE = array('admin_sid' => false);
00207         $myConfig = oxConfig::getInstance();
00208         $myConfig->setConfigParam( 'blAdmin', 1 );
00209         $myConfig->setAdminMode( true );
00210 
00211         $mySession = oxSession::getInstance();
00212         $myConfig->oActView = new FakeView();
00213 
00214         // hotfix #2429, #2430 MAFI
00215         if ($iShopID != 1) {
00216             $myConfig->setConfigParam('blMallUsers', false);
00217         }
00218         $myConfig->setShopId($iShopID);
00219 
00220         $mySession->setVar( "lang", $iLanguage);
00221         $mySession->setVar( "language", $iLanguage);
00222 
00223         $oUser = oxNew('oxuser');
00224         try {
00225             if ( !$oUser->login( $sUserName, $sPassword ) ) {
00226                 $oUser = null;
00227             }
00228         } catch( oxUserException $e ) {
00229             $oUser = null;
00230         }
00231 
00232         if ( !$oUser || ( isset( $oUser->iError ) && $oUser->iError == -1000 ) ) {
00233             // authorization error
00234             throw new Exception( self::$ERROR_USER_WRONG );
00235         } elseif ( ( $oUser->oxuser__oxrights->value == "malladmin" || $oUser->oxuser__oxrights->value == $myConfig->getShopId() ) ) {
00236             $this->_sSID        = $mySession->getId();
00237             $this->_blInit      = true;
00238             $this->_iLanguage   = $iLanguage;
00239             $this->_sUserID     = $oUser->getId();
00240             //$mySession->freeze();
00241         } else {
00242 
00243             //user does not have sufficient rights for shop
00244             throw new Exception( self::$ERROR_USER_NO_RIGHTS );
00245         }
00246 
00247         $this->_resetIdx();
00248 
00249         return $this->_blInit;
00250     }
00251 
00261     public abstract function loadSessionData( $sSessionID );
00262 
00275     public function exportType( $sType, $sWhere = null,$iStart = null, $iCount = null, $sSortFieldName = null, $sSortType = null)
00276     {
00277         $this->_beforeExport($sType);
00278         $this->_export( $sType, $sWhere, $iStart, $iCount, $sSortFieldName, $sSortType);
00279         $this->_afterExport($sType);
00280     }
00281 
00287     public function import()
00288     {
00289         $this->_beforeImport();
00290         while ( $this->_importOne() ) {
00291         }
00292         $this->_afterImport();
00293     }
00294 
00302     protected function _getInstanceOfType( $sType )
00303     {
00304         $sClassName = 'oxerptype_'.$sType;
00305         $sFullPath  = dirname(__FILE__).'/objects/'.$sClassName.'.php';
00306 
00307         if ( !file_exists( $sFullPath ) ) {
00308             throw new Exception( "Type $sType not supported in ERP interface!");
00309         }
00310 
00311         include_once $sFullPath;
00312         return oxNew ($sClassName);
00313     }
00314 
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, $myConfig->getShopId() );
00335         $sSQL .= $oType->getSortString( $sSortFieldName, $sSortType );
00336         $sFnc  = '_Export'.$oType->getFunctionSuffix();
00337 
00338         $save = $ADODB_FETCH_MODE;
00339 
00340         if ( isset( $iCount ) || isset( $iStart ) ) {
00341             $rs = oxDb::getDb(true)->selectLimit( $sSQL, $iCount, $iStart );
00342         } else {
00343             $rs = oxDb::getDb(true)->execute( $sSQL );
00344         }
00345 
00346         if ( $rs != false && $rs->recordCount() > 0 ) {
00347             while (!$rs->EOF) {
00348                 $blExport = false;
00349                 $sMessage = '';
00350 
00351                 $rs->fields = $oType->addExportData( $rs->fields);
00352 
00353                 // check rights
00354                 $this->_checkAccess( $oType, false);
00355 
00356                 // export now
00357                 try{
00358                     $blExport = $this->$sFnc( $rs->fields );
00359                 } catch (Exception $e) {
00360                     $sMessage = $e->getMessage();
00361 
00362                 }
00363 
00364                 $this->_aStatistics[$this->_iIdx] = array('r'=>$blExport,'m'=>$sMessage);
00365                 //#2428 MAFI
00366                 $this->_nextIdx();
00367 
00368                 $rs->moveNext();
00369             }
00370         }
00371         $ADODB_FETCH_MODE = $save;
00372     }
00373 
00381     protected function _outputMappingArray( $sTable)
00382     {
00383         $aData = getTableDescription( $sTable );
00384 
00385         $iIdx = 0;
00386         foreach ( $aData as $key => $oADODBField) {
00387             if ( !( is_numeric( substr( $oADODBField->name, strlen( $oADODBField->name) - 1, 1)) &&  substr( $oADODBField->name, strlen( $oADODBField->name) - 2, 1) == '_' ) ) {
00388                 echo( "'".$oADODBField->name."'\t\t => '".$oADODBField->name."',\n" );
00389                 $iIdx++;
00390             }
00391         }
00392     }
00393 
00402     protected function _getKeyID( $oType, $aData )
00403     {
00404         $myConfig = oxConfig::getInstance();
00405         $aKeyFields = $oType->getKeyFields();
00406 
00407         if ( !is_array($aKeyFields ) ) {
00408             return false;
00409         }
00410 
00411         $oDB = oxDb::getDb();
00412         //$aKeys = array_intersect_key($aData,$aKeyFields);
00413 
00414         $aWhere = array();
00415         $blAllKeys = true;
00416         foreach ( $aKeyFields as $sKey ) {
00417             if ( array_key_exists( $sKey, $aData ) ) {
00418                 $aWhere[] = $sKey.'='.$oDB->qstr( $aData[$sKey] );
00419             } else {
00420                 $blAllKeys = false;
00421             }
00422         }
00423 
00424         if ( $blAllKeys ) {
00425             $sSelect = 'SELECT OXID FROM '.$oType->getTableName().' WHERE '.implode(' AND ', $aWhere );
00426             $sOXID = $oDB->getOne($sSelect);
00427 
00428             if ( isset( $sOXID ) ) {
00429                 return $sOXID;
00430             }
00431         }
00432 
00433         return oxUtilsObject::getInstance()->generateUID();
00434     }
00435 
00441     protected function _resetIdx()
00442     {
00443         $this->_iIdx = 0;
00444         if ( count( $this->_aStatistics ) && isset( $this->_aStatistics[$this->_iIdx] ) ) {
00445             while ( isset( $this->_aStatistics[$this->_iIdx]) && $this->_aStatistics[$this->_iIdx]['r'] ) {
00446                 $this->_iIdx ++;
00447             }
00448         }
00449     }
00450 
00456     protected function _nextIdx()
00457     {
00458         $this->_iIdx ++;
00459         if ( count( $this->_aStatistics ) && isset( $this->_aStatistics[$this->_iIdx] ) ) {
00460             while ( isset( $this->_aStatistics[$this->_iIdx]) && $this->_aStatistics[$this->_iIdx]['r'] ) {
00461                 $this->_iIdx ++;
00462             }
00463         }
00464     }
00465 
00475     protected function _checkAccess( $oType, $blWrite, $sOxid = null )
00476     {
00477         $myConfig = oxConfig::getInstance();
00478         static $aAccessCache;
00479 
00480         if ( !$this->_blInit ) {
00481             throw new Exception(self::$ERROR_NO_INIT);
00482         }
00483 
00484         if ( $blWrite ) {
00485             //check against Shop id if it exists
00486             $oType->checkWriteAccess( $sOxid );
00487         }
00488 
00489         // TODO
00490         // add R&R check for access
00491         if ( $myConfig->blUseRightsRoles ) {
00492             static $aAccessCache;
00493 
00494             $sAccessMode = ( (boolean) $blWrite ) ? '2' : '1';
00495             $sTypeClass  = get_class($oType);
00496 
00497             if ( !isset( $aAccessCache[$sTypeClass][$sAccessMode] ) ) {
00498 
00499                 $oDB = oxDb::getDb();
00500 
00501                 //create list of user/group id's
00502                 $aIDs = array( $oDB->qstr($this->_sUserID) );
00503                 $sQUserGroups = 'SELECT oxgroupsid ' .
00504                                 'FROM oxobject2group '.
00505                                 //"WHERE oxshopid = '{$this->_iShopID}' ".
00506                                 "WHERE oxshopid = '{$myConfig->getShopId()}' ".
00507                                 "AND oxobjectid ='{$this->_sUserID}'";
00508 
00509                 $rs = $oDB->execute( $sQUserGroups);
00510                 if ($rs != false && $rs->recordCount() > 0) {
00511                     while (!$rs->EOF) {
00512                         $aIDs[] = $oDB->qstr($rs->fields[0]);
00513                         $rs->moveNext();
00514                     }
00515                 }
00516 
00517                 $aRParams = $oType->getRightFields();
00518                 foreach ($aRParams as $sKey => $sParam) {
00519                     $aRParams[$sKey] = $oDB->qstr($sParam);
00520                 }
00521 
00522                 //check user rights...
00523                 $sSelect = 'SELECT count(*) '.
00524                            'FROM oxfield2role as rr , oxrolefields as rf, oxobject2role as ro, oxroles as rt '.
00525                            "WHERE rr.OXIDX < {$sAccessMode} ".
00526                            'AND rr.oxroleid = ro.oxroleid  '.
00527                            'AND rt.oxid = ro.oxroleid '.
00528                            'AND rt.oxactive = 1 '.
00529                            //"AND rt.oxshopid = '{$this->_iShopID}'".
00530                            "AND rt.oxshopid = '{$myConfig->getShopId()}'".
00531                            'AND rf.oxparam IN ('.implode(',', $aRParams).') '.
00532                            'AND ro.oxobjectid IN ('.implode(',', $aIDs).') '.
00533                            'AND rr.oxfieldid=rf.oxid';
00534 
00535                 $iNoAccess = $oDB->getOne($sSelect);
00536                 $aAccessCache[$sTypeClass][$sAccessMode] = $iNoAccess;
00537             } else {
00538                 $iNoAccess = $aAccessCache[$sTypeClass][$sAccessMode];
00539             }
00540 
00541             if ( $iNoAccess ) {
00542                 throw new Exception( self::$ERROR_USER_NO_RIGHTS );
00543             }
00544         }
00545     }
00546 
00552     protected function _importOne()
00553     {
00554         $blRet = false;
00555 
00556         // import one row/call/object...
00557         $aData = $this->getImportData();
00558 
00559         if ( $aData ) {
00560             $blRet = true;
00561             $blImport = false;
00562             $sMessage = '';
00563 
00564             $sType  = $this->_getImportType( $aData);
00565             $sMode = $this->_getImportMode($aData);
00566             $oType  = $this->_getInstanceOfType( $sType);
00567             $aData = $this->_modifyData($aData, $oType);
00568 
00569             // import now
00570             $sFnc   = '_' . $sMode . $oType->getFunctionSuffix();
00571 
00572             if ( $sMode == oxERPBase::$MODE_IMPORT ) {
00573                 $aData = $oType->addImportData( $aData );
00574             }
00575 
00576             try{
00577                 $blImport = $this->$sFnc( $oType, $aData);
00578                 $sMessage = '';
00579             }
00580             catch (Exception $e) {
00581                 $sMessage = $e->getMessage();
00582             }
00583 
00584             $this->_aStatistics[$this->_iIdx] = array('r'=>$blImport,'m'=>$sMessage);
00585 
00586         }
00587         //hotfix #2428 MAFI
00588         $this->_nextIdx();
00589 
00590         return $blRet;
00591     }
00592 
00593 
00603     protected function _save( oxERPType $oType, $aData, $blAllowCustomShopId = false)
00604     {
00605         $myConfig = oxConfig::getInstance();
00606 
00607         // check rights
00608         $this->_checkAccess( $oType, true, $aData['OXID'] );
00609 
00610         if ( $oType->hasKeyFields() && !isset($aData['OXID'] ) ) {
00611             $sOXID = $this->_getKeyID($oType, $aData);
00612             if ( $sOXID ) {
00613                 $aData['OXID'] = $sOXID;
00614             } else {
00615                 $aData['OXID'] = oxUtilsObject::getInstance()->generateUID();
00616             }
00617         }
00618 
00619         return $oType->saveObject($aData, $blAllowCustomShopId);
00620     }
00621 
00627     public static function getRequestedVersion()
00628     {
00629         if (!self::$_sRequestedVersion && isset($_GET['version'])) {
00630             self::$_sRequestedVersion = $_GET['version'];
00631         }
00632         if (!isset(self::$_aDbLayer2ShopDbVersions[self::$_sRequestedVersion])) {
00633             self::$_sRequestedVersion = '';
00634         }
00635         if (!self::$_sRequestedVersion) {
00636             reset(self::$_aDbLayer2ShopDbVersions);
00637             self::$_sRequestedVersion = key(self::$_aDbLayer2ShopDbVersions);
00638         }
00639         return self::$_sRequestedVersion;
00640     }
00641 
00647     public static function getUsedDbFieldsVersion()
00648     {
00649         return self::$_aDbLayer2ShopDbVersions[self::getRequestedVersion()];
00650     }
00651 
00659     public static function setVersion( $sDbLayerVersion = '' )
00660     {
00661         self::$_sRequestedVersion = $sDbLayerVersion;
00662     }
00663 }
00664 
00665 
00666 // the following statements and class is just for pretending some error messages in oxconfig
00667 if ( !class_exists( 'FakeView' ) ) {
00671 class FakeView
00672 {
00678     public function addGlobalParams()
00679     {
00680     }
00681 }
00682 }

Generated by  doxygen 1.6.2