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         $oDb = oxDb::getDb();
00325         $sSql = "select oxshopid from ".$this->_sTableName." where oxid = " . $oDb->quote( $sId );
00326         try {
00327             $iShopId = $oDb->getOne($sSql);
00328         } catch (Exception $e) {
00329             // no shopid was found
00330             return;
00331         }
00332         if (!$this->_isAllowedToEdit($iShopId)) {
00333             throw new Exception( "No right to delete object {$sId} !");
00334         }
00335     }
00336 
00346     public function checkForDeletion($sId)
00347     {
00348 
00349         if ( !isset($sId)) {
00350             throw new Exception( "Missing ID!");
00351         }
00352         // malladmin can do it
00353         if ($oUsr = oxUser::getAdminUser()) {
00354             if ($oUsr->oxuser__oxrights->value == "malladmin") {
00355                 return;
00356             }
00357         }
00358         try {
00359             $this->getObjectForDeletion($sId);
00360         } catch (oxSystemComponentException $e) {
00361             if ($e->getMessage() == 'EXCEPTION_SYSTEMCOMPONENT_CLASSNOTFOUND') {
00362                 $this->_directSqlCheckForDeletion($sId);
00363             } else {
00364                 throw $e;
00365             }
00366         }
00367     }
00368 
00376     public function delete($sID)
00377     {
00378         $myConfig = oxConfig::getInstance();
00379         $oDb = oxDb::getDb();
00380         $sSql = "delete from ".$this->_sTableName." where oxid = " . $oDb->quote( $sID );
00381 
00382         return $oDb->Execute($sSql);
00383     }
00384 
00393     public function deleteObject($oObj, $sID)
00394     {
00395         return $oObj->delete($sID);
00396     }
00397 
00405     public function addExportData( $aFields)
00406     {
00407         return $aFields;
00408     }
00409 
00420     public function addImportData($aFields)
00421     {
00422         return $aFields;
00423     }
00424 
00432     public function getRightFields()
00433     {
00434         $aRParams = array();
00435         if (!$this->_aFieldList) {
00436             $this->getFieldList();
00437         }
00438 
00439         foreach ($this->_aFieldList as $sField) {
00440             $aRParams[] = strtolower($this->_sTableName.'__'.$sField);
00441         }
00442         return $aRParams;
00443     }
00444 
00450     public function getFieldList()
00451     {
00452         $sObjectName = $this->getShopObjectName();
00453 
00454         if ( $sObjectName ) {
00455             $oShopObject = oxNew( $sObjectName );
00456         } else {
00457             $oShopObject = oxNew( 'oxbase' );
00458             $oShopObject->init( $this->getTableName() );
00459         }
00460 
00461         if ($oShopObject instanceof oxI18n) {
00462             $oShopObject->setLanguage( 0 );
00463             $oShopObject->setEnableMultilang(false);
00464         }
00465 
00466         $sViewName = $oShopObject->getViewName();
00467         $sFields = str_ireplace( $sViewName . ".", "", strtoupper($oShopObject->getSelectFields()) );
00468         $sFields = str_ireplace( " ", "", $sFields );
00469         $this->_aFieldList = explode( ",", $sFields );
00470 
00471         return $this->_aFieldList;
00472     }
00473 
00479     public function getKeyFields()
00480     {
00481         return $this->_aKeyFieldList;
00482     }
00483 
00491     public function getOxidFromKeyFields($aData)
00492     {
00493         $myConfig = oxConfig::getInstance();
00494 
00495         if (!is_array($this->getKeyFields())) {
00496             return null;
00497         }
00498 
00499         $oDb = oxDb::getDb();
00500 
00501         $aWhere = array();
00502         $blAllKeys = true;
00503         foreach ($this->getKeyFields() as $sKey) {
00504             if (array_key_exists($sKey, $aData)) {
00505                 $aWhere[] = $sKey.'='.$oDb->qstr($aData[$sKey]);
00506             } else {
00507                 $blAllKeys = false;
00508             }
00509         }
00510 
00511         if ($blAllKeys) {
00512             $sSelect = 'SELECT OXID FROM '.$this->getTableName().' WHERE '.implode(' AND ', $aWhere);
00513             return $oDb->getOne( $sSelect );
00514         }
00515 
00516         return null;
00517     }
00518 
00524     public function hasKeyFields()
00525     {
00526         if (isset($this->_aKeyFieldList) && is_array($this->_aKeyFieldList)) {
00527             return true;
00528         }
00529         return false;
00530     }
00531 
00541     protected function _preAssignObject($oShopObject, $aData, $blAllowCustomShopId)
00542     {
00543             if (isset($aData['OXSHOPID'])) {
00544                 $aData['OXSHOPID'] = 'oxbaseshop';
00545             }
00546 
00547 
00548         if (!isset($aData['OXID'])) {
00549             $aData['OXID'] = $this->getOxidFromKeyFields($aData);
00550         }
00551 
00552         // null values support
00553         foreach ($aData as $key => $val) {
00554             if (!strlen((string) $val)) {
00555                 // oxbase whill quote it as string if db does not support null for this field
00556                 $aData[$key] = null;
00557             }
00558         }
00559         return $aData;
00560     }
00561 
00571     protected function _preSaveObject($oShopObject, $aData)
00572     {
00573         return true;
00574     }
00575 
00584     public function saveObject($aData, $blAllowCustomShopId)
00585     {
00586         $sObjectName = $this->getShopObjectName();
00587         if ($sObjectName) {
00588             $oShopObject = oxNew( $sObjectName, 'core');
00589             if ($oShopObject instanceof oxI18n) {
00590                 $oShopObject->setLanguage( 0 );
00591                 $oShopObject->setEnableMultilang(false);
00592             }
00593         } else {
00594             $oShopObject = oxNew( 'oxbase', 'core');
00595             $oShopObject->init($this->getBaseTableName());
00596         }
00597 
00598         foreach ($aData as $key => $value) {
00599             // change case to UPPER
00600             $sUPKey = strtoupper($key);
00601             if (!isset($aData[$sUPKey])) {
00602                 unset($aData[$key]);
00603                 $aData[$sUPKey] = $value;
00604             }
00605         }
00606 
00607 
00608         $blLoaded = false;
00609         if ($aData['OXID']) {
00610             $blLoaded = $oShopObject->load( $aData['OXID']);
00611         }
00612 
00613         $aData = $this->_preAssignObject( $oShopObject, $aData, $blAllowCustomShopId );
00614 
00615         if ($blLoaded) {
00616             $this->checkWriteAccess($oShopObject, $aData);
00617         } else {
00618             $this->checkCreateAccess($aData);
00619         }
00620 
00621         $oShopObject->assign( $aData );
00622 
00623         if ($blAllowCustomShopId) {
00624             $oShopObject->setIsDerived(false);
00625         }
00626 
00627         if ($this->_preSaveObject($oShopObject, $aData)) {
00628             // store
00629             if ( $oShopObject->save()) {
00630                 return $this->_postSaveObject($oShopObject, $aData);
00631             }
00632         }
00633 
00634         return false;
00635     }
00636 
00645     protected function _postSaveObject($oShopObject, $aData)
00646     {
00647         // returning ID on success
00648         return $oShopObject->getId();
00649     }
00650 }
00651