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 = oxRegistry::getConfig()->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 = oxRegistry::getConfig();
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         $oUsr = oxNew('oxUser');
00304         $oUsr->loadAdminUser();
00305 
00306         if ($oUsr->oxuser__oxrights->value == "malladmin") {
00307             return true;
00308         } elseif ($oUsr->oxuser__oxrights->value == (int) $iShopId) {
00309             return true;
00310         }
00311 
00312         return false;
00313     }
00314 
00324     protected function _directSqlCheckForDeletion($sId)
00325     {
00326         $oDb = oxDb::getDb();
00327         $sSql = "select oxshopid from ".$this->_sTableName." where oxid = " . $oDb->quote( $sId );
00328         try {
00329             $iShopId = $oDb->getOne($sSql);
00330         } catch (Exception $e) {
00331             // no shopid was found
00332             return;
00333         }
00334         if (!$this->_isAllowedToEdit($iShopId)) {
00335             throw new Exception( "No right to delete object {$sId} !");
00336         }
00337     }
00338 
00348     public function checkForDeletion($sId)
00349     {
00350 
00351         if ( !isset($sId)) {
00352             throw new Exception( "Missing ID!");
00353         }
00354         // malladmin can do it
00355         $oUsr = oxNew('oxUser');
00356         $oUsr->loadAdminUser();
00357         if ($oUsr->oxuser__oxrights->value == "malladmin") {
00358             return;
00359         }
00360         try {
00361             $this->getObjectForDeletion($sId);
00362         } catch (oxSystemComponentException $e) {
00363             if ($e->getMessage() == 'EXCEPTION_SYSTEMCOMPONENT_CLASSNOTFOUND') {
00364                 $this->_directSqlCheckForDeletion($sId);
00365             } else {
00366                 throw $e;
00367             }
00368         }
00369     }
00370 
00378     public function delete($sID)
00379     {
00380         $myConfig = oxRegistry::getConfig();
00381         $oDb = oxDb::getDb();
00382         $sSql = "delete from ".$this->_sTableName." where oxid = " . $oDb->quote( $sID );
00383 
00384         return $oDb->Execute($sSql);
00385     }
00386 
00395     public function deleteObject($oObj, $sID)
00396     {
00397         return $oObj->delete($sID);
00398     }
00399 
00407     public function addExportData( $aFields)
00408     {
00409         return $aFields;
00410     }
00411 
00421     public function addImportData($aFields)
00422     {
00423         return $aFields;
00424     }
00425 
00431     public function getRightFields()
00432     {
00433         $aRParams = array();
00434 
00435         foreach ($this->_aFieldList as $sField) {
00436             $aRParams[] = strtolower($this->_sTableName.'__'.$sField);
00437         }
00438         return $aRParams;
00439     }
00440 
00446     public function getFieldList()
00447     {
00448         $sObjectName = $this->getShopObjectName();
00449 
00450         if ( $sObjectName ) {
00451             $oShopObject = oxNew( $sObjectName );
00452         } else {
00453             $oShopObject = oxNew( 'oxbase' );
00454             $oShopObject->init( $this->getTableName() );
00455         }
00456 
00457         if ($oShopObject instanceof oxI18n) {
00458             $oShopObject->setLanguage( 0 );
00459             $oShopObject->setEnableMultilang(false);
00460         }
00461 
00462         $sViewName = $oShopObject->getViewName();
00463         $sFields = str_ireplace( $sViewName . ".", "", strtoupper($oShopObject->getSelectFields()) );
00464         $sFields = str_ireplace( " ", "", $sFields );
00465         $this->_aFieldList = explode( ",", $sFields );
00466 
00467         return $this->_aFieldList;
00468     }
00469 
00475     public function getKeyFields()
00476     {
00477         return $this->_aKeyFieldList;
00478     }
00479 
00487     public function getOxidFromKeyFields($aData)
00488     {
00489         $myConfig = oxRegistry::getConfig();
00490 
00491         if (!is_array($this->getKeyFields())) {
00492             return null;
00493         }
00494 
00495         $oDb = oxDb::getDb();
00496 
00497         $aWhere = array();
00498         $blAllKeys = true;
00499         foreach ($this->getKeyFields() as $sKey) {
00500             if (array_key_exists($sKey, $aData)) {
00501                 $aWhere[] = $sKey.'='.$oDb->qstr($aData[$sKey]);
00502             } else {
00503                 $blAllKeys = false;
00504             }
00505         }
00506 
00507         if ($blAllKeys) {
00508             $sSelect = 'SELECT OXID FROM '.$this->getTableName().' WHERE '.implode(' AND ', $aWhere);
00509             return $oDb->getOne( $sSelect );
00510         }
00511 
00512         return null;
00513     }
00514 
00520     public function hasKeyFields()
00521     {
00522         if (isset($this->_aKeyFieldList) && is_array($this->_aKeyFieldList)) {
00523             return true;
00524         }
00525         return false;
00526     }
00527 
00537     protected function _preAssignObject($oShopObject, $aData, $blAllowCustomShopId)
00538     {
00539             if (isset($aData['OXSHOPID'])) {
00540                 $aData['OXSHOPID'] = 'oxbaseshop';
00541             }
00542 
00543 
00544         if (!isset($aData['OXID'])) {
00545             $aData['OXID'] = $this->getOxidFromKeyFields($aData);
00546         }
00547 
00548         // null values support
00549         foreach ($aData as $key => $val) {
00550             if (!strlen((string) $val)) {
00551                 // oxbase whill quote it as string if db does not support null for this field
00552                 $aData[$key] = null;
00553             }
00554         }
00555         return $aData;
00556     }
00557 
00567     protected function _preSaveObject($oShopObject, $aData)
00568     {
00569         return true;
00570     }
00571 
00580     public function saveObject($aData, $blAllowCustomShopId)
00581     {
00582         $sObjectName = $this->getShopObjectName();
00583         if ($sObjectName) {
00584             $oShopObject = oxNew( $sObjectName, 'core');
00585             if ($oShopObject instanceof oxI18n) {
00586                 $oShopObject->setLanguage( 0 );
00587                 $oShopObject->setEnableMultilang(false);
00588             }
00589         } else {
00590             $oShopObject = oxNew( 'oxbase', 'core');
00591             $oShopObject->init($this->getBaseTableName());
00592         }
00593 
00594         foreach ($aData as $key => $value) {
00595             // change case to UPPER
00596             $sUPKey = strtoupper($key);
00597             if (!isset($aData[$sUPKey])) {
00598                 unset($aData[$key]);
00599                 $aData[$sUPKey] = $value;
00600             }
00601         }
00602 
00603 
00604         $blLoaded = false;
00605         if ($aData['OXID']) {
00606             $blLoaded = $oShopObject->load( $aData['OXID']);
00607         }
00608 
00609         $aData = $this->_preAssignObject( $oShopObject, $aData, $blAllowCustomShopId );
00610 
00611         if ($blLoaded) {
00612             $this->checkWriteAccess($oShopObject, $aData);
00613         } else {
00614             $this->checkCreateAccess($aData);
00615         }
00616 
00617         $oShopObject->assign( $aData );
00618 
00619         if ($blAllowCustomShopId) {
00620             $oShopObject->setIsDerived(false);
00621         }
00622 
00623         if ($this->_preSaveObject($oShopObject, $aData)) {
00624             // store
00625             if ( $oShopObject->save()) {
00626                 return $this->_postSaveObject($oShopObject, $aData);
00627             }
00628         }
00629 
00630         return false;
00631     }
00632 
00641     protected function _postSaveObject($oShopObject, $aData)
00642     {
00643         // returning ID on success
00644         return $oShopObject->getId();
00645     }
00646 }
00647