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         if (!$this->_aFieldList) {
00435             $this->getFieldList();
00436         }
00437 
00438         foreach ($this->_aFieldList as $sField) {
00439             $aRParams[] = strtolower($this->_sTableName.'__'.$sField);
00440         }
00441         return $aRParams;
00442     }
00443 
00449     public function getFieldList()
00450     {
00451         $sObjectName = $this->getShopObjectName();
00452 
00453         if ( $sObjectName ) {
00454             $oShopObject = oxNew( $sObjectName );
00455         } else {
00456             $oShopObject = oxNew( 'oxbase' );
00457             $oShopObject->init( $this->getTableName() );
00458         }
00459 
00460         if ($oShopObject instanceof oxI18n) {
00461             $oShopObject->setLanguage( 0 );
00462             $oShopObject->setEnableMultilang(false);
00463         }
00464 
00465         $sViewName = $oShopObject->getViewName();
00466         $sFields = str_ireplace( '`' . $sViewName . "`.", "", strtoupper($oShopObject->getSelectFields()) );
00467         $sFields = str_ireplace( array(" ", "`"), array("", ""), $sFields );
00468         $this->_aFieldList = explode( ",", $sFields );
00469 
00470         return $this->_aFieldList;
00471     }
00472 
00478     public function getKeyFields()
00479     {
00480         return $this->_aKeyFieldList;
00481     }
00482 
00490     public function getOxidFromKeyFields($aData)
00491     {
00492         $myConfig = oxRegistry::getConfig();
00493 
00494         if (!is_array($this->getKeyFields())) {
00495             return null;
00496         }
00497 
00498         $oDb = oxDb::getDb();
00499 
00500         $aWhere = array();
00501         $blAllKeys = true;
00502         foreach ($this->getKeyFields() as $sKey) {
00503             if (array_key_exists($sKey, $aData)) {
00504                 $aWhere[] = $sKey.'='.$oDb->qstr($aData[$sKey]);
00505             } else {
00506                 $blAllKeys = false;
00507             }
00508         }
00509 
00510         if ($blAllKeys) {
00511             $sSelect = 'SELECT OXID FROM '.$this->getTableName().' WHERE '.implode(' AND ', $aWhere);
00512             return $oDb->getOne( $sSelect );
00513         }
00514 
00515         return null;
00516     }
00517 
00523     public function hasKeyFields()
00524     {
00525         if (isset($this->_aKeyFieldList) && is_array($this->_aKeyFieldList)) {
00526             return true;
00527         }
00528         return false;
00529     }
00530 
00540     protected function _preAssignObject($oShopObject, $aData, $blAllowCustomShopId)
00541     {
00542             if (isset($aData['OXSHOPID'])) {
00543                 $aData['OXSHOPID'] = 'oxbaseshop';
00544             }
00545 
00546 
00547         if (!isset($aData['OXID'])) {
00548             $aData['OXID'] = $this->getOxidFromKeyFields($aData);
00549         }
00550 
00551         // null values support
00552         foreach ($aData as $key => $val) {
00553             if (!strlen((string) $val)) {
00554                 // oxbase whill quote it as string if db does not support null for this field
00555                 $aData[$key] = null;
00556             }
00557         }
00558         return $aData;
00559     }
00560 
00570     protected function _preSaveObject($oShopObject, $aData)
00571     {
00572         return true;
00573     }
00574 
00583     public function saveObject($aData, $blAllowCustomShopId)
00584     {
00585         $sObjectName = $this->getShopObjectName();
00586         if ($sObjectName) {
00587             $oShopObject = oxNew( $sObjectName, 'core');
00588             if ($oShopObject instanceof oxI18n) {
00589                 $oShopObject->setLanguage( 0 );
00590                 $oShopObject->setEnableMultilang(false);
00591             }
00592         } else {
00593             $oShopObject = oxNew( 'oxbase', 'core');
00594             $oShopObject->init($this->getBaseTableName());
00595         }
00596 
00597         foreach ($aData as $key => $value) {
00598             // change case to UPPER
00599             $sUPKey = strtoupper($key);
00600             if (!isset($aData[$sUPKey])) {
00601                 unset($aData[$key]);
00602                 $aData[$sUPKey] = $value;
00603             }
00604         }
00605 
00606 
00607         $blLoaded = false;
00608         if ($aData['OXID']) {
00609             $blLoaded = $oShopObject->load( $aData['OXID']);
00610         }
00611 
00612         $aData = $this->_preAssignObject( $oShopObject, $aData, $blAllowCustomShopId );
00613 
00614         if ($blLoaded) {
00615             $this->checkWriteAccess($oShopObject, $aData);
00616         } else {
00617             $this->checkCreateAccess($aData);
00618         }
00619 
00620         $oShopObject->assign( $aData );
00621 
00622         if ($blAllowCustomShopId) {
00623             $oShopObject->setIsDerived(false);
00624         }
00625 
00626         if ($this->_preSaveObject($oShopObject, $aData)) {
00627             // store
00628             if ( $oShopObject->save()) {
00629                 return $this->_postSaveObject($oShopObject, $aData);
00630             }
00631         }
00632 
00633         return false;
00634     }
00635 
00644     protected function _postSaveObject($oShopObject, $aData)
00645     {
00646         // returning ID on success
00647         return $oShopObject->getId();
00648     }
00649 }
00650