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             $oStr = getStr();
00210             if ( $oStr->strstr( $sWhere, 'where')) {
00211                 $sWhere .= ' and ';
00212             } else {
00213                 $sWhere .= ' where ';
00214             }
00215 
00216             $sWhere .= 'oxshopid = \''.$iShopID.'\'';
00217         }
00218 
00219         $sSQL .= ' from '.$this->getTableName($iShopID).' '.$sWhere;
00220 
00221         return $sSQL;
00222     }
00223 
00232     public function getSortString($sFieldName = null, $sType = null)
00233     {
00234         $sRes = " order by ";
00235         if ($sFieldName) {
00236             $sRes .= $sFieldName;
00237         } else {
00238             $sRes .= "oxid";
00239         }
00240         if ($sType && ($sType == "ASC" || $sType == "DESC")) {
00241             $sRes .= " ". $sType;
00242         }
00243         return $sRes;
00244     }
00245 
00255     public function checkWriteAccess($sOxid)
00256     {
00257         $oObj = oxNew("oxbase");
00258         $oObj->init($this->_sTableName);
00259         if ( $oObj->load( $sOxid ) ) {
00260             $sFld = $this->_sTableName.'__oxshopid';
00261             if ( isset( $oObj->$sFld ) ) {
00262                 $sRes = $oObj->$sFld->value;
00263                 if ( $sRes && $sRes != oxConfig::getInstance()->getShopId() ) {
00264                     throw new Exception( oxERPBase::$ERROR_USER_NO_RIGHTS);
00265                 }
00266             }
00267         }
00268     }
00269 
00279     public function getObjectForDeletion( $sId)
00280     {
00281         $myConfig = oxConfig::getInstance();
00282 
00283         if ( !isset( $sId ) ) {
00284             throw new Exception( "Missing ID!");
00285         }
00286 
00287         $oObj = oxNew( $this->getShopObjectName(), "core");
00288 
00289         if ( !$oObj->exists( $sId ) ) {
00290             throw new Exception( $this->getShopObjectName(). " " . $sId. " does not exists!");
00291         }
00292 
00293         //We must load the object here, to check shopid and return it for further checks
00294         if ( !$oObj->load( $sId ) ) {
00295             //its possible that access is restricted allready
00296             throw new Exception( "No right to delete object {$sId} !");
00297         }
00298 
00299         if ( !$this->_isAllowedToEdit($oObj->getShopId() ) ) {
00300             throw new Exception( "No right to delete object {$sId} !");
00301         }
00302 
00303         return $oObj;
00304     }
00305 
00313     protected function _isAllowedToEdit($iShopId)
00314     {
00315         if ($oUsr = oxUser::getAdminUser()) {
00316             if ($oUsr->oxuser__oxrights->value == "malladmin") {
00317                 return true;
00318             } elseif ($oUsr->oxuser__oxrights->value == (int) $iShopId) {
00319                 return true;
00320             }
00321         }
00322         return false;
00323     }
00324 
00332     protected function _directSqlCheckForDeletion($sId)
00333     {
00334         $sSql = "select oxshopid from ".$this->_sTableName." where oxid = '" . $sId . "'";
00335         try {
00336             $iShopId = oxDb::getDb()->getOne($sSql);
00337         } catch (Exception $e) {
00338             // no shopid was found
00339             return;
00340         }
00341         if ( !$this->_isAllowedToEdit( $iShopId ) ) {
00342             throw new Exception( "No right to delete object {$sId} !");
00343         }
00344     }
00345 
00353     public function checkForDeletion($sId)
00354     {
00355 
00356         if ( !isset( $sId ) ) {
00357             throw new Exception( "Missing ID!");
00358         }
00359         // malladmin can do it
00360         if ($oUsr = oxUser::getAdminUser()) {
00361             if ($oUsr->oxuser__oxrights->value == "malladmin") {
00362                 return;
00363             }
00364         }
00365         try {
00366             $this->getObjectForDeletion($sId);
00367         } catch (oxSystemComponentException $e) {
00368             if ($e->getMessage() == 'EXCEPTION_SYSTEMCOMPONENT_CLASSNOTFOUND') {
00369                 $this->_directSqlCheckForDeletion($sId);
00370             } else {
00371                 throw $e;
00372             }
00373         }
00374     }
00375 
00383     public function delete($sID)
00384     {
00385         $myConfig = oxConfig::getInstance();
00386         $sSql = "delete from ".$this->_sTableName." where oxid = '" . $sID . "'";
00387 
00388         return oxDb::getDb()->Execute($sSql);
00389     }
00390 
00399     public function deleteObject($oObj, $sID)
00400     {
00401 
00402         return $oObj->delete($sID);
00403     }
00404 
00412     public function addExportData( $aFields)
00413     {
00414         return $aFields;
00415     }
00416 
00427     public function addImportData($aFields)
00428     {
00429         return $aFields;
00430     }
00431 
00437     public function getRightFields()
00438     {
00439         $aRParams = array();
00440 
00441         foreach ( $this->_aFieldList as $sField ) {
00442             $aRParams[] = strtolower($this->_sTableName.'__'.$sField);
00443         }
00444         return $aRParams;
00445     }
00446 
00452     public function getFieldList()
00453     {
00454         return $this->_aFieldList;
00455     }
00456 
00462     public function getKeyFields()
00463     {
00464         return $this->_aKeyFieldList;
00465     }
00466 
00472     public function hasKeyFields()
00473     {
00474         if ( isset( $this->_aKeyFieldList ) && is_array( $this->_aKeyFieldList ) ) {
00475             return true;
00476         }
00477         return false;
00478     }
00479 
00489     protected function _preAssignObject($oShopObject, $aData, $blAllowCustomShopId)
00490     {
00491         if ( !isset( $aData['OXID'] ) ) {
00492             throw new Exception( "OXID missing, seems to be wrong Format!");
00493         }
00494         if ( !$oShopObject->exists( $aData['OXID'] ) ) {
00495             //$aData['OXSHOPID'] = $this->_iShopID;
00496             if ( !$blAllowCustomShopId ) {
00497                 if (isset($aData['OXSHOPID'])) {
00498                     $aData['OXSHOPID'] = oxConfig::getInstance()->getShopId();
00499                 }
00500             }
00501             if ( !array_key_exists('OXSHOPINCL', $aData ) ) {
00502                 $aData['OXSHOPINCL'] = oxUtils::getInstance()->getShopBit($aData['OXSHOPID']);
00503             }
00504             if ( !array_key_exists( 'OXSHOPEXCL', $aData ) ) {
00505                 $aData['OXSHOPEXCL'] = 0;
00506             }
00507         }
00508         if (isset($aData['OXACTIV'])) {
00509             $aData['OXACTIVE'] = $aData['OXACTIV'];
00510         }
00511         if (isset($aData['OXACTIVFROM'])) {
00512             $aData['OXACTIVEFROM'] = $aData['OXACTIVFROM'];
00513         }
00514         if (isset($aData['OXACTIVTO'])) {
00515             $aData['OXACTIVETO'] = $aData['OXACTIVTO'];
00516         }
00517         for ($i=1;$i<4;$i++) {
00518             if (isset($aData['OXACTIV_'.$i])) {
00519                 $aData['OXACTIVE_'.$i] = $aData['OXACTIV_'.$i];
00520             }
00521         }
00522         // null values support
00523         foreach ($aData as $key => $val) {
00524             if ( !strlen( (string) $val ) ) {
00525                 // oxbase whill quote it as string if db does not support null for this field
00526                 $aData[$key] = null;
00527             }
00528         }
00529         return $aData;
00530     }
00531 
00541     protected function _preSaveObject($oShopObject, $aData)
00542     {
00543         return true;
00544     }
00545 
00554     public function saveObject($aData, $blAllowCustomShopId)
00555     {
00556         $sObjectName = $this->getShopObjectName();
00557         if ( $sObjectName ) {
00558             $oShopObject = oxNew( $sObjectName, 'core');
00559             if ( $oShopObject instanceof oxI18n ) {
00560                 $oShopObject->setLanguage(0);
00561                 $oShopObject->setEnableMultilang(false);
00562             }
00563         } else {
00564             $oShopObject = oxNew( 'oxbase', 'core');
00565             $oShopObject->init($this->getBaseTableName());
00566         }
00567 
00568         $aData = $this->_preAssignObject($oShopObject, $aData, $blAllowCustomShopId);
00569 
00570 
00571         $oShopObject->load( $aData['OXID']);
00572 
00573         $oShopObject->assign( $aData );
00574 
00575         if ($blAllowCustomShopId) {
00576             $oShopObject->setIsDerived(false);
00577         }
00578 
00579         if ($this->_preSaveObject($oShopObject, $aData)) {
00580             // store
00581             if ( $oShopObject->save() ) {
00582                 return $this->_postSaveObject($oShopObject, $aData);
00583             }
00584         }
00585 
00586         return false;
00587     }
00588 
00597     protected function _postSaveObject($oShopObject, $aData)
00598     {
00599         // returning ID on success
00600         return $oShopObject->getId();
00601     }
00602 }
00603 

Generated on Tue Aug 18 09:21:05 2009 for OXID eShop CE by  doxygen 1.5.5