oxerptype.php

Go to the documentation of this file.
00001 <?php
00002 
00007 class oxERPType
00008 {
00009     public static $ERROR_WRONG_SHOPID = "Wrong shop id, operation not allowed!";
00010 
00011     protected   $_sTableName        = null;
00012     protected   $_sFunctionSuffix   = null;
00013     protected   $_aFieldList        = null;
00014     protected   $_aKeyFieldList     = null;
00015     protected   $_sShopObjectName   = null;
00016 
00022     protected $_blRestrictedByShopId = false;
00023 
00029     protected $_aFieldListVersions = null;
00030 
00036     public function getFunctionSuffix()
00037     {
00038         return $this->_sFunctionSuffix;
00039     }
00040 
00046     public function getShopObjectName()
00047     {
00048         return $this->_sShopObjectName;
00049     }
00050 
00056     public function getBaseTableName()
00057     {
00058         return $this->_sTableName;
00059     }
00060 
00066     public function __construct()
00067     {
00068         $this->_sFunctionSuffix = str_replace( "oxERPType_", "", get_class( $this));
00069     }
00070 
00078     public function setFunctionSuffix($sNew)
00079     {
00080         $this->_sFunctionSuffix = $sNew;
00081     }
00082 
00090     public function setFieldList($aFieldList)
00091     {
00092         $this->_aFieldList = $aFieldList;
00093     }
00094 
00103     public function getTableName($iShopID=null, $iLanguage = 0)
00104     {
00105         if ($iShopID === null) {
00106             $iShopID = oxConfig::getInstance()->getShopId();
00107         }
00108         
00109         return getViewName($this->_sTableName, -1, $iShopID);
00110     }
00111 
00117     private function _getMultilangualFields()
00118     {
00119         $aRet = array();
00120 
00121         $aData = oxDb::getInstance()->getTableDescription( $this->_sTableName);
00122 
00123         foreach ($aData as $key => $oADODBField) {
00124             $iLang = substr( $oADODBField->name, strlen( $oADODBField->name) - 1, 1);
00125             if ( is_numeric( $iLang) &&  substr( $oADODBField->name, strlen( $oADODBField->name) - 2, 1) == '_') {
00126                 // multilangual field
00127                 $sMainFld = str_replace( '_'.$iLang, "", $oADODBField->name);
00128                 $aRet[$iLang][$sMainFld] = $oADODBField->name.' as '.$sMainFld;
00129             }
00130         }
00131 
00132         return $aRet;
00133     }
00134 
00144     protected function _getSqlFieldName($sField, $iLanguage = 0, $iShopID = 1)
00145     {
00146         if ($iLanguage) {
00147             $aMultiLang = $this->_getMultilangualFields();
00148             // we need to load different fields
00149             if ( isset( $aMultiLang[$iLanguage][$sField])) {
00150                 $sField = $aMultiLang[$iLanguage][$sField];
00151             }
00152         }
00153 
00154             switch ($sField) {
00155                 case 'OXSHOPID':
00156                 case 'OXSHOPINCL':
00157                     return "1 as $sField";
00158                 case 'OXSHOPEXCL':
00159                     return "0 as $sField";
00160             }
00161 
00162         return $sField;
00163     }
00164 
00174     public function getSQL( $sWhere, $iLanguage = 0, $iShopId = 1)
00175     {
00176         if ( !$this->_aFieldList) {
00177             return;
00178         }
00179 
00180         $sSQL    = 'select ';
00181         $blSep = false;
00182 
00183         foreach ( $this->_aFieldList as $sField) {
00184             if ( $blSep) {
00185                 $sSQL .= ',';
00186             }
00187 
00188             $sSQL .= $this->_getSqlFieldName($sField, $iLanguage, $iShopId);
00189             $blSep = true;
00190         }
00191 
00192 
00193         $sSQL .= ' from '.$this->getTableName($iShopId, $iLanguage).' '.$sWhere;
00194 
00195         return $sSQL;
00196     }
00197 
00206     public function getSortString($sFieldName = null, $sType = null)
00207     {
00208         $sRes = " order by ";
00209         if ($sFieldName) {
00210             $sRes .= $sFieldName;
00211         } else {
00212             $sRes .= "oxid";
00213         }
00214         if ($sType && ($sType == "ASC" || $sType == "DESC")) {
00215             $sRes .= " ". $sType;
00216         }
00217         return $sRes;
00218     }
00219 
00230     public function checkWriteAccess($oObj, $aData = null)
00231     {
00232             return;
00233         
00234         if ($oObj->isDerived()) {
00235             throw new Exception( oxERPBase::$ERROR_USER_NO_RIGHTS);
00236         }
00237     }
00238 
00248     public function checkCreateAccess($aData)
00249     {
00250     }
00251 
00261     public function getObjectForDeletion( $sId)
00262     {
00263         $myConfig = oxConfig::getInstance();
00264 
00265         if (!isset($sId)) {
00266             throw new Exception( "Missing ID!");
00267         }
00268 
00269         $sName = $this->getShopObjectName();
00270         if ($sName) {
00271             $oObj = oxNew( $sName, "core");
00272         } else {
00273             $oObj = oxNew( 'oxbase', 'core');
00274             $oObj->init($this->getBaseTableName());
00275         }
00276 
00277         if (!$oObj->exists($sId)) {
00278             throw new Exception( $this->getShopObjectName(). " " . $sId. " does not exists!");
00279         }
00280 
00281         //We must load the object here, to check shopid and return it for further checks
00282         if (!$oObj->Load($sId)) {
00283             //its possible that access is restricted allready
00284             throw new Exception( "No right to delete object {$sId} !");
00285         }
00286 
00287         if (!$this->_isAllowedToEdit($oObj->getShopId())) {
00288             throw new Exception( "No right to delete object {$sId} !");
00289         }
00290 
00291         return $oObj;
00292     }
00293 
00301     protected function _isAllowedToEdit($iShopId)
00302     {
00303         if ($oUsr = oxUser::getAdminUser()) {
00304             if ($oUsr->oxuser__oxrights->value == "malladmin") {
00305                 return true;
00306             } elseif ($oUsr->oxuser__oxrights->value == (int) $iShopId) {
00307                 return true;
00308             }
00309         }
00310         return false;
00311     }
00312 
00322     protected function _directSqlCheckForDeletion($sId)
00323     {
00324         $sSql = "select oxshopid from ".$this->_sTableName." where oxid = '" . $sId . "'";
00325         try {
00326             $iShopId = oxDb::getDb()->getOne($sSql);
00327         } catch (Exception $e) {
00328             // no shopid was found
00329             return;
00330         }
00331         if (!$this->_isAllowedToEdit($iShopId)) {
00332             throw new Exception( "No right to delete object {$sId} !");
00333         }
00334     }
00335 
00345     public function checkForDeletion($sId)
00346     {
00347 
00348         if ( !isset($sId)) {
00349             throw new Exception( "Missing ID!");
00350         }
00351         // malladmin can do it
00352         if ($oUsr = oxUser::getAdminUser()) {
00353             if ($oUsr->oxuser__oxrights->value == "malladmin") {
00354                 return;
00355             }
00356         }
00357         try {
00358             $this->getObjectForDeletion($sId);
00359         } catch (oxSystemComponentException $e) {
00360             if ($e->getMessage() == 'EXCEPTION_SYSTEMCOMPONENT_CLASSNOTFOUND') {
00361                 $this->_directSqlCheckForDeletion($sId);
00362             } else {
00363                 throw $e;
00364             }
00365         }
00366     }
00367 
00375     public function delete($sID)
00376     {
00377         $myConfig = oxConfig::getInstance();
00378         $sSql = "delete from ".$this->_sTableName." where oxid = '" . $sID . "'";
00379 
00380         return oxDb::getDb()->Execute($sSql);
00381     }
00382 
00391     public function deleteObject($oObj, $sID)
00392     {
00393         return $oObj->delete($sID);
00394     }
00395 
00403     public function addExportData( $aFields)
00404     {
00405         return $aFields;
00406     }
00407 
00418     public function addImportData($aFields)
00419     {
00420         return $aFields;
00421     }
00422 
00430     public function getRightFields()
00431     {
00432         $aRParams = array();
00433 
00434         foreach ($this->_aFieldList as $sField) {
00435             $aRParams[] = strtolower($this->_sTableName.'__'.$sField);
00436         }
00437         return $aRParams;
00438     }
00439 
00445     public function getFieldList()
00446     {
00447         $sObjectName = $this->getShopObjectName();
00448 
00449         if ( $sObjectName ) {
00450             $oShopObject = oxNew( $sObjectName );
00451         } else {
00452             $oShopObject = oxNew( 'oxbase' );
00453             $oShopObject->init( $this->getTableName() );
00454         }
00455 
00456         if ($oShopObject instanceof oxI18n) {
00457             $oShopObject->setLanguage( 0 );
00458             $oShopObject->setEnableMultilang(false);
00459         }
00460 
00461         $sViewName = $oShopObject->getViewName();
00462         $sFields = str_ireplace( $sViewName . ".", "", strtoupper($oShopObject->getSelectFields()) );
00463         $sFields = str_ireplace( " ", "", $sFields );
00464         $this->_aFieldList = explode( ",", $sFields );
00465 
00466         return $this->_aFieldList;
00467     }
00468 
00474     public function getKeyFields()
00475     {
00476         return $this->_aKeyFieldList;
00477     }
00478 
00486     public function getOxidFromKeyFields($aData)
00487     {
00488         $myConfig = oxConfig::getInstance();
00489 
00490         if (!is_array($this->getKeyFields())) {
00491             return null;
00492         }
00493 
00494         $oDB = oxDb::getDb();
00495 
00496         $aWhere = array();
00497         $blAllKeys = true;
00498         foreach ($this->getKeyFields() as $sKey) {
00499             if (array_key_exists($sKey, $aData)) {
00500                 $aWhere[] = $sKey.'='.$oDB->qstr($aData[$sKey]);
00501             } else {
00502                 $blAllKeys = false;
00503             }
00504         }
00505 
00506         if ($blAllKeys) {
00507             $sSelect = 'SELECT OXID FROM '.$this->getTableName().' WHERE '.implode(' AND ', $aWhere);
00508             return $oDB->getOne($sSelect);
00509         }
00510 
00511         return null;
00512     }
00513 
00519     public function hasKeyFields()
00520     {
00521         if (isset($this->_aKeyFieldList) && is_array($this->_aKeyFieldList)) {
00522             return true;
00523         }
00524         return false;
00525     }
00526 
00536     protected function _preAssignObject($oShopObject, $aData, $blAllowCustomShopId)
00537     {
00538             if (isset($aData['OXSHOPID'])) {
00539                 $aData['OXSHOPID'] = 'oxbaseshop';
00540             }
00541 
00542 
00543         if (!isset($aData['OXID'])) {
00544             $aData['OXID'] = $this->getOxidFromKeyFields($aData);
00545         }
00546 
00547         // null values support
00548         foreach ($aData as $key => $val) {
00549             if (!strlen((string) $val)) {
00550                 // oxbase whill quote it as string if db does not support null for this field
00551                 $aData[$key] = null;
00552             }
00553         }
00554         return $aData;
00555     }
00556 
00566     protected function _preSaveObject($oShopObject, $aData)
00567     {
00568         return true;
00569     }
00570 
00579     public function saveObject($aData, $blAllowCustomShopId)
00580     {
00581         $sObjectName = $this->getShopObjectName();
00582         if ($sObjectName) {
00583             $oShopObject = oxNew( $sObjectName, 'core');
00584             if ($oShopObject instanceof oxI18n) {
00585                 $oShopObject->setLanguage( 0 );
00586                 $oShopObject->setEnableMultilang(false);
00587             }
00588         } else {
00589             $oShopObject = oxNew( 'oxbase', 'core');
00590             $oShopObject->init($this->getBaseTableName());
00591         }
00592 
00593         foreach ($aData as $key => $value) {
00594             // change case to UPPER
00595             $sUPKey = strtoupper($key);
00596             if (!isset($aData[$sUPKey])) {
00597                 unset($aData[$key]);
00598                 $aData[$sUPKey] = $value;
00599             }
00600         }
00601 
00602 
00603         $blLoaded = false;
00604         if ($aData['OXID']) {
00605             $blLoaded = $oShopObject->load( $aData['OXID']);
00606         }
00607 
00608         $aData = $this->_preAssignObject( $oShopObject, $aData, $blAllowCustomShopId );
00609 
00610         if ($blLoaded) {
00611             $this->checkWriteAccess($oShopObject, $aData);
00612         } else {
00613             $this->checkCreateAccess($aData);
00614         }
00615 
00616         $oShopObject->assign( $aData );
00617 
00618         if ($blAllowCustomShopId) {
00619             $oShopObject->setIsDerived(false);
00620         }
00621 
00622         if ($this->_preSaveObject($oShopObject, $aData)) {
00623             // store
00624             if ( $oShopObject->save()) {
00625                 return $this->_postSaveObject($oShopObject, $aData);
00626             }
00627         }
00628 
00629         return false;
00630     }
00631 
00640     protected function _postSaveObject($oShopObject, $aData)
00641     {
00642         // returning ID on success
00643         return $oShopObject->getId();
00644     }
00645 }
00646