oxerptype.php

Go to the documentation of this file.
00001 <?php
00002 
00006 class oxERPType
00007 {
00012     public static $ERROR_WRONG_SHOPID = "Wrong shop id, operation not allowed!";
00013 
00018     protected $_sTableName        = null;
00019 
00024     protected $_sFunctionSuffix   = null;
00025 
00030     protected $_aFieldList        = null;
00031 
00036     protected $_aKeyFieldList     = null;
00037 
00042     protected $_sShopObjectName   = null;
00043 
00049     protected $_blRestrictedByShopId = false;
00050 
00056     protected $_aFieldListVersions = null;
00057 
00063     public function getFunctionSuffix()
00064     {
00065         return $this->_sFunctionSuffix;
00066     }
00067 
00073     public function getShopObjectName()
00074     {
00075         return $this->_sShopObjectName;
00076     }
00077 
00083     public function getBaseTableName()
00084     {
00085         return $this->_sTableName;
00086     }
00087 
00093     public function __construct()
00094     {
00095         $this->_sFunctionSuffix = str_replace( "oxERPType_", "", get_class( $this));
00096         if (isset($this->_aFieldListVersions)) {
00097             $this->_aFieldList = $this->_aFieldListVersions[oxERPBase::getUsedDbFieldsVersion()];
00098         }
00099     }
00100 
00108     public function setFunctionSuffix( $sNew )
00109     {
00110         $this->_sFunctionSuffix = $sNew;
00111     }
00112 
00120     public function setFieldList($aFieldList)
00121     {
00122         $this->_aFieldList = $aFieldList;
00123     }
00124 
00132     public function getTableName( $iShopID = 1 )
00133     {
00134         return getViewName( $this->_sTableName, $iShopID );
00135     }
00136 
00142     private function _getMultilangualFields()
00143     {
00144         $aRet = array();
00145 
00146         $aData = oxDb::getInstance()->getTableDescription( $this->_sTableName);
00147 
00148         foreach ( $aData as $key => $oADODBField) {
00149             $iLang = substr( $oADODBField->name, strlen( $oADODBField->name) - 1, 1);
00150             if ( is_numeric( $iLang) &&  substr( $oADODBField->name, strlen( $oADODBField->name) - 2, 1) == '_') {
00151                 // multilangual field
00152                 $sMainFld = str_replace( '_'.$iLang, "", $oADODBField->name);
00153                 $aRet[$iLang][$sMainFld] = $oADODBField->name.' as '.$sMainFld;
00154             }
00155         }
00156 
00157         return $aRet;
00158     }
00159 
00169     protected function getSqlFieldName($sField, $iLanguage = 0, $iShopID = 1)
00170     {
00171         if ( $iLanguage ) {
00172             $aMultiLang = $this->_getMultilangualFields();
00173             // we need to load different fields
00174             if ( isset( $aMultiLang[$iLanguage][$sField] ) ) {
00175                 $sField = $aMultiLang[$iLanguage][$sField];
00176             }
00177         }
00178         return $sField;
00179     }
00180 
00190     public function getSQL( $sWhere, $iLanguage = 0, $iShopID = 1)
00191     {
00192         if ( !$this->_aFieldList ) {
00193             return;
00194         }
00195 
00196         $sSQL    = 'select ';
00197         $blSep = false;
00198 
00199         foreach ( $this->_aFieldList as $sField) {
00200             if ( $blSep ) {
00201                 $sSQL .= ',';
00202             }
00203 
00204             $sSQL .= $this->getSqlFieldName($sField, $iLanguage, $iShopID);
00205             $blSep = true;
00206         }
00207 
00208         if ( $this->_blRestrictedByShopId ) {
00209             if ( strstr( $sWhere, 'where')) {
00210                 $sWhere .= ' and ';
00211             } else {
00212                 $sWhere .= ' where ';
00213             }
00214 
00215             $sWhere .= 'oxshopid = \''.$iShopID.'\'';
00216         }
00217 
00218         $sSQL .= ' from '.$this->getTableName($iShopID).' '.$sWhere;
00219 
00220         return $sSQL;
00221     }
00222 
00231     public function getSortString($sFieldName = null, $sType = null)
00232     {
00233         $sRes = " order by ";
00234         if ($sFieldName) {
00235             $sRes .= $sFieldName;
00236         } else {
00237             $sRes .= "oxid";
00238         }
00239         if ($sType && ($sType == "ASC" || $sType == "DESC")) {
00240             $sRes .= " ". $sType;
00241         }
00242         return $sRes;
00243     }
00244 
00254     public function checkWriteAccess($sOxid)
00255     {
00256         $oObj = oxNew("oxbase");
00257         $oObj->init($this->_sTableName);
00258         if ( $oObj->load( $sOxid ) ) {
00259             $sFld = $this->_sTableName.'__oxshopid';
00260             if ( isset( $oObj->$sFld ) ) {
00261                 $sRes = $oObj->$sFld->value;
00262                 if ( $sRes && $sRes != oxConfig::getInstance()->getShopId() ) {
00263                     throw new Exception( oxERPBase::$ERROR_USER_NO_RIGHTS);
00264                 }
00265             }
00266         }
00267     }
00268 
00278     public function getObjectForDeletion( $sId)
00279     {
00280         $myConfig = oxConfig::getInstance();
00281 
00282         if ( !isset( $sId ) ) {
00283             throw new Exception( "Missing ID!");
00284         }
00285 
00286         $oObj = oxNew( $this->getShopObjectName(), "core");
00287 
00288         if ( !$oObj->exists( $sId ) ) {
00289             throw new Exception( $this->getShopObjectName(). " " . $sId. " does not exists!");
00290         }
00291 
00292         //We must load the object here, to check shopid and return it for further checks
00293         if ( !$oObj->load( $sId ) ) {
00294             //its possible that access is restricted allready
00295             throw new Exception( "No right to delete object {$sId} !");
00296         }
00297 
00298         if ( !$this->_isAllowedToEdit($oObj->getShopId() ) ) {
00299             throw new Exception( "No right to delete object {$sId} !");
00300         }
00301 
00302         return $oObj;
00303     }
00304 
00312     protected function _isAllowedToEdit($iShopId)
00313     {
00314         if ($oUsr = oxUser::getAdminUser()) {
00315             if ($oUsr->oxuser__oxrights->value == "malladmin") {
00316                 return true;
00317             } elseif ($oUsr->oxuser__oxrights->value == (int) $iShopId) {
00318                 return true;
00319             }
00320         }
00321         return false;
00322     }
00323 
00331     protected function _directSqlCheckForDeletion($sId)
00332     {
00333         $sSql = "select oxshopid from ".$this->_sTableName." where oxid = '" . $sId . "'";
00334         try {
00335             $iShopId = oxDb::getDb()->getOne($sSql);
00336         } catch (Exception $e) {
00337             // no shopid was found
00338             return;
00339         }
00340         if ( !$this->_isAllowedToEdit( $iShopId ) ) {
00341             throw new Exception( "No right to delete object {$sId} !");
00342         }
00343     }
00344 
00352     public function checkForDeletion($sId)
00353     {
00354 
00355         if ( !isset( $sId ) ) {
00356             throw new Exception( "Missing ID!");
00357         }
00358         // malladmin can do it
00359         if ($oUsr = oxUser::getAdminUser()) {
00360             if ($oUsr->oxuser__oxrights->value == "malladmin") {
00361                 return;
00362             }
00363         }
00364         try {
00365             $this->getObjectForDeletion($sId);
00366         } catch (oxSystemComponentException $e) {
00367             if ($e->getMessage() == 'EXCEPTION_SYSTEMCOMPONENT_CLASSNOTFOUND') {
00368                 $this->_directSqlCheckForDeletion($sId);
00369             } else {
00370                 throw $e;
00371             }
00372         }
00373     }
00374 
00382     public function delete($sID)
00383     {
00384         $myConfig = oxConfig::getInstance();
00385         $sSql = "delete from ".$this->_sTableName." where oxid = '" . $sID . "'";
00386 
00387         return oxDb::getDb()->Execute($sSql);
00388     }
00389 
00398     public function deleteObject($oObj, $sID)
00399     {
00400 
00401         return $oObj->delete($sID);
00402     }
00403 
00411     public function addExportData( $aFields)
00412     {
00413         return $aFields;
00414     }
00415 
00426     public function addImportData($aFields)
00427     {
00428         return $aFields;
00429     }
00430 
00436     public function getRightFields()
00437     {
00438         $aRParams = array();
00439 
00440         foreach ( $this->_aFieldList as $sField ) {
00441             $aRParams[] = strtolower($this->_sTableName.'__'.$sField);
00442         }
00443         return $aRParams;
00444     }
00445 
00451     public function getFieldList()
00452     {
00453         return $this->_aFieldList;
00454     }
00455 
00461     public function getKeyFields()
00462     {
00463         return $this->_aKeyFieldList;
00464     }
00465 
00471     public function hasKeyFields()
00472     {
00473         if ( isset( $this->_aKeyFieldList ) && is_array( $this->_aKeyFieldList ) ) {
00474             return true;
00475         }
00476         return false;
00477     }
00478 
00488     protected function _preAssignObject($oShopObject, $aData, $blAllowCustomShopId)
00489     {
00490         if ( !isset( $aData['OXID'] ) ) {
00491             throw new Exception( "OXID missing, seems to be wrong Format!");
00492         }
00493         if ( !$oShopObject->exists( $aData['OXID'] ) ) {
00494             //$aData['OXSHOPID'] = $this->_iShopID;
00495             if ( !$blAllowCustomShopId ) {
00496                 if (isset($aData['OXSHOPID'])) {
00497                     $aData['OXSHOPID'] = oxConfig::getInstance()->getShopId();
00498                 }
00499             }
00500             if ( !array_key_exists('OXSHOPINCL', $aData ) ) {
00501                 $aData['OXSHOPINCL'] = oxUtils::getInstance()->getShopBit($aData['OXSHOPID']);
00502             }
00503             if ( !array_key_exists( 'OXSHOPEXCL', $aData ) ) {
00504                 $aData['OXSHOPEXCL'] = 0;
00505             }
00506         }
00507         if (isset($aData['OXACTIV'])) {
00508             $aData['OXACTIVE'] = $aData['OXACTIV'];
00509         }
00510         if (isset($aData['OXACTIVFROM'])) {
00511             $aData['OXACTIVEFROM'] = $aData['OXACTIVFROM'];
00512         }
00513         if (isset($aData['OXACTIVTO'])) {
00514             $aData['OXACTIVETO'] = $aData['OXACTIVTO'];
00515         }
00516         for ($i=1;$i<4;$i++) {
00517             if (isset($aData['OXACTIV_'.$i])) {
00518                 $aData['OXACTIVE_'.$i] = $aData['OXACTIV_'.$i];
00519             }
00520         }
00521         // null values support
00522         foreach ($aData as $key => $val) {
00523             if ( !strlen( (string) $val ) ) {
00524                 // oxbase whill quote it as string if db does not support null for this field
00525                 $aData[$key] = null;
00526             }
00527         }
00528         return $aData;
00529     }
00530 
00540     protected function _preSaveObject($oShopObject, $aData)
00541     {
00542         return true;
00543     }
00544 
00553     public function saveObject($aData, $blAllowCustomShopId)
00554     {
00555         $sObjectName = $this->getShopObjectName();
00556         if ( $sObjectName ) {
00557             $oShopObject = oxNew( $sObjectName, 'core');
00558             if ( $oShopObject instanceof oxI18n ) {
00559                 $oShopObject->setLanguage(0);
00560                 $oShopObject->setEnableMultilang(false);
00561             }
00562         } else {
00563             $oShopObject = oxNew( 'oxbase', 'core');
00564             $oShopObject->init($this->getBaseTableName());
00565         }
00566 
00567         $aData = $this->_preAssignObject($oShopObject, $aData, $blAllowCustomShopId);
00568 
00569 
00570         $oShopObject->load( $aData['OXID']);
00571 
00572         $oShopObject->assign( $aData );
00573 
00574         if ($blAllowCustomShopId) {
00575             $oShopObject->setIsDerived(false);
00576         }
00577 
00578         if ($this->_preSaveObject($oShopObject, $aData)) {
00579             // store
00580             if ( $oShopObject->save() ) {
00581                 return $this->_postSaveObject($oShopObject, $aData);
00582             }
00583         }
00584 
00585         return false;
00586     }
00587 
00596     protected function _postSaveObject($oShopObject, $aData)
00597     {
00598         // returning ID on success
00599         return $oShopObject->getId();
00600     }
00601 }
00602 

Generated on Wed Apr 22 12:26:30 2009 for OXID eShop CE by  doxygen 1.5.5