oxerptype.php

Go to the documentation of this file.
00001 <?php
00002 
00003 class oxERPType
00004 {
00005     public static $ERROR_WRONG_SHOPID = "Wrong shop id, operation not allowed!";
00006 
00007     protected   $_sTableName        = null;
00008     protected   $_sFunctionSuffix   = null;
00009     protected   $_aFieldList        = null;
00010     protected   $_aKeyFieldList     = null;
00011     protected   $_sShopObjectName   = null;
00012 
00018     protected $_blRestrictedByShopId = false;
00019 
00025     protected $_aFieldListVersions = null;
00026 
00032     public function getFunctionSuffix()     {   return $this->_sFunctionSuffix;     }
00033 
00039     public function getShopObjectName()     {   return $this->_sShopObjectName;     }
00040 
00046     public function getBaseTableName()      {   return $this->_sTableName;          }
00047 
00048     public function __construct()
00049     {
00050         $this->_sFunctionSuffix = str_replace( "oxERPType_", "", get_class( $this));
00051         if (isset($this->_aFieldListVersions)) {
00052             $this->_aFieldList = $this->_aFieldListVersions[oxERPBase::getUsedDbFieldsVersion()];
00053         }
00054     }
00055 
00061     public function setFunctionSuffix($sNew)
00062     {
00063         $this->_sFunctionSuffix = $sNew;
00064     }
00065 
00071     public function setFieldList($aFieldList)
00072     {
00073         $this->_aFieldList = $aFieldList;
00074     }
00075 
00081     public function getTableName($iShopID=1)
00082     {
00083         return getViewName($this->_sTableName,$iShopID);
00084     }
00085 
00091     private function _getMultilangualFields()
00092     {
00093         $aRet = array();
00094 
00095         $aData = oxDb::getInstance()->getTableDescription( $this->_sTableName);
00096 
00097         foreach( $aData as $key => $oADODBField) {
00098             $iLang = substr( $oADODBField->name, strlen( $oADODBField->name) - 1, 1);
00099             if( is_numeric( $iLang) &&  substr( $oADODBField->name, strlen( $oADODBField->name) - 2, 1) == '_') {
00100                 // multilangual field
00101                 $sMainFld = str_replace( '_'.$iLang, "", $oADODBField->name);
00102                 $aRet[$iLang][$sMainFld] = $oADODBField->name.' as '.$sMainFld;
00103             }
00104         }
00105 
00106         return $aRet;
00107     }
00108 
00117     protected function getSqlFieldName($sField, $iLanguage = 0, $iShopID = 1)
00118     {
00119         if( $iLanguage) {
00120             $aMultiLang = $this->_getMultilangualFields();
00121             // we need to load different fields
00122             if( isset( $aMultiLang[$iLanguage][$sField]))
00123                 $sField = $aMultiLang[$iLanguage][$sField];
00124         }
00125         return $sField;
00126     }
00127 
00135     public function getSQL( $sWhere, $iLanguage = 0, $iShopID = 1)
00136     {
00137         if( !$this->_aFieldList)
00138             return;
00139 
00140         $sSQL    = 'select ';
00141         $blSep = false;
00142 
00143         foreach( $this->_aFieldList as $sField) {
00144             if( $blSep)
00145                 $sSQL .= ',';
00146 
00147             $sSQL .= $this->getSqlFieldName($sField, $iLanguage, $iShopID);
00148             $blSep = true;
00149         }
00150 
00151         if($this->_blRestrictedByShopId){
00152             if( strstr( $sWhere, 'where'))
00153                 $sWhere .= ' and ';
00154             else
00155                 $sWhere .= ' where ';
00156 
00157             $sWhere .= 'oxshopid = \''.$iShopID.'\'';
00158         }
00159 
00160         $sSQL .= ' from '.$this->getTableName($iShopID).' '.$sWhere;
00161 
00162         return $sSQL;
00163     }
00164 
00172     public function getSortString($sFieldName = null, $sType = null)
00173     {
00174         $sRes = " order by ";
00175         if ($sFieldName) {
00176             $sRes .= $sFieldName;
00177         } else {
00178             $sRes .= "oxid";
00179         }
00180         if ($sType && ($sType == "ASC" || $sType == "DESC")) {
00181             $sRes .= " ". $sType;
00182         }
00183         return $sRes;
00184     }
00185 
00193     public function checkWriteAccess($sOxid)
00194     {
00195         $oObj = oxNew("oxbase");
00196         $oObj->init($this->_sTableName);
00197         if ($oObj->load($sOxid)) {
00198             $sFld = $this->_sTableName.'__oxshopid';
00199             if (isset($oObj->$sFld)) {
00200                 $sRes = $oObj->$sFld->value;
00201                 if($sRes && $sRes != oxConfig::getInstance()->getShopId()) {
00202                     throw new Exception( oxERPBase::$ERROR_USER_NO_RIGHTS);
00203                 }
00204             }
00205         }
00206     }
00207 
00217     public function getObjectForDeletion( $sId)
00218     {
00219 
00220         $myConfig = oxConfig::getInstance();
00221 
00222         if( !isset($sId))
00223             throw new Exception( "Missing ID!");
00224 
00225         $oObj = oxNew( $this->getShopObjectName(), "core");
00226 
00227         if(!$oObj->exists($sId)){
00228             throw new Exception( $this->getShopObjectName(). " " . $sId. " does not exists!");
00229         }
00230 
00231         //We must load the object here, to check shopid and return it for further checks
00232         if(!$oObj->Load($sId)){
00233             //its possible that access is restricted allready
00234             throw new Exception( "No right to delete object {$sId} !");
00235         }
00236 
00237         if(!$this->_isAllowedToEdit($oObj->getShopId())) {
00238             throw new Exception( "No right to delete object {$sId} !");
00239         }
00240 
00241         return $oObj;
00242     }
00243 
00244     protected function _isAllowedToEdit($iShopId)
00245     {
00246         if ($oUsr = oxUser::getAdminUser()) {
00247             if ($oUsr->oxuser__oxrights->value == "malladmin") {
00248                 return true;
00249             } elseif ($oUsr->oxuser__oxrights->value == (int) $iShopId) {
00250                 return true;
00251             }
00252         }
00253         return false;
00254     }
00255 
00261     protected function _directSqlCheckForDeletion($sId)
00262     {
00263         $sSql = "select oxshopid from ".$this->_sTableName." where oxid = '" . $sId . "'";
00264         try {
00265             $iShopId = oxDb::getDb()->getOne($sSql);
00266         } catch (Exception $e) {
00267             // no shopid was found
00268             return;
00269         }
00270         if(!$this->_isAllowedToEdit($iShopId)) {
00271             throw new Exception( "No right to delete object {$sId} !");
00272         }
00273     }
00274 
00280     public function checkForDeletion($sId)
00281     {
00282 
00283         if( !isset($sId)) {
00284             throw new Exception( "Missing ID!");
00285         }
00286         // malladmin can do it
00287         if ($oUsr = oxUser::getAdminUser()) {
00288             if ($oUsr->oxuser__oxrights->value == "malladmin") {
00289                 return;
00290             }
00291         }
00292         try {
00293             $this->getObjectForDeletion($sId);
00294         } catch (oxSystemComponentException $e) {
00295             if ($e->getMessage() == 'EXCEPTION_SYSTEMCOMPONENT_CLASSNOTFOUND') {
00296                 $this->_directSqlCheckForDeletion($sId);
00297             } else {
00298                 throw $e;
00299             }
00300         }
00301     }
00302 
00309     public function delete($sID)
00310     {
00311         $myConfig = oxConfig::getInstance();
00312         $sSql = "delete from ".$this->_sTableName." where oxid = '" . $sID . "'";
00313 
00314         return oxDb::getDb()->Execute($sSql);
00315     }
00316 
00324     public function deleteObject($oObj, $sID)
00325     {
00326 
00327         return $oObj->delete($sID);
00328     }
00329 
00335     public function addExportData( $aFields)
00336     {
00337         return $aFields;
00338     }
00339 
00350     public function addImportData($aFields)
00351     {
00352         return $aFields;
00353     }
00354 
00360     public function GetRightFields()
00361     {
00362         $aRParams = array();
00363 
00364         foreach($this->_aFieldList as $sField) {
00365             $aRParams[] = strtolower($this->_sTableName.'__'.$sField);
00366         }
00367         return $aRParams;
00368     }
00369 
00375     public function getFieldList() {
00376         return $this->_aFieldList;
00377     }
00378 
00379   /*  public function GetTable() {
00380         return $this->_sTableName;
00381     }*/
00382 
00388     public function getKeyFields() {
00389         return $this->_aKeyFieldList;
00390     }
00391 
00397     public function hasKeyFields() {
00398         if(isset($this->_aKeyFieldList) && is_array($this->_aKeyFieldList)){
00399             return true;
00400         }
00401         return false;
00402     }
00403 
00413     protected function _preAssignObject($oShopObject, $aData, $blAllowCustomShopId)
00414     {
00415         if( !isset( $aData['OXID'])) {
00416             throw new Exception( "OXID missing, seems to be wrong Format!");
00417         }
00418         if(!$oShopObject->exists( $aData['OXID'] )) {
00419             //$aData['OXSHOPID'] = $this->_iShopID;
00420             if (!$blAllowCustomShopId) {
00421                 if (isset($aData['OXSHOPID'])) {
00422                     $aData['OXSHOPID'] = oxConfig::getInstance()->getShopId();
00423                 }
00424             }
00425             if(!array_key_exists('OXSHOPINCL',$aData)) {
00426                 $aData['OXSHOPINCL'] = oxUtils::getInstance()->getShopBit($aData['OXSHOPID']);
00427             }
00428             if(!array_key_exists('OXSHOPEXCL',$aData)) {
00429                 $aData['OXSHOPEXCL'] = 0;
00430             }
00431         }
00432         if (isset($aData['OXACTIV'])) {
00433             $aData['OXACTIVE'] = $aData['OXACTIV'];
00434         }
00435         if (isset($aData['OXACTIVFROM'])) {
00436             $aData['OXACTIVEFROM'] = $aData['OXACTIVFROM'];
00437         }
00438         if (isset($aData['OXACTIVTO'])) {
00439             $aData['OXACTIVETO'] = $aData['OXACTIVTO'];
00440         }
00441         for ($i=1;$i<4;$i++) {
00442             if (isset($aData['OXACTIV_'.$i])) {
00443                 $aData['OXACTIVE_'.$i] = $aData['OXACTIV_'.$i];
00444             }
00445         }
00446         // null values support
00447         foreach ($aData as $key => $val) {
00448             if (!strlen((string)$val)) {
00449                 // oxbase whill quote it as string if db does not support null for this field
00450                 $aData[$key] = null;
00451             }
00452         }
00453         return $aData;
00454     }
00455 
00465     protected function _preSaveObject($oShopObject, $aData)
00466     {
00467         return true;
00468     }
00469 
00478     public function saveObject($aData, $blAllowCustomShopId)
00479     {
00480         $sObjectName = $this->getShopObjectName();
00481         if( $sObjectName){
00482             $oShopObject = oxNew( $sObjectName, 'core');
00483             if ($oShopObject instanceof oxI18n) {
00484                 $oShopObject->SetLanguage(0);
00485                 $oShopObject->setEnableMultilang(false);
00486             }
00487         } else {
00488             $oShopObject = oxNew( 'oxbase', 'core');
00489             $oShopObject->init($this->getBaseTableName());
00490         }
00491 
00492         $aData = $this->_preAssignObject($oShopObject, $aData, $blAllowCustomShopId);
00493 
00494 
00495         $oShopObject->Load( $aData['OXID']);
00496 
00497         $oShopObject->Assign( $aData );
00498 
00499         if ($blAllowCustomShopId) {
00500             $oShopObject->setIsDerived(false);
00501         }
00502 
00503         if ($this->_preSaveObject($oShopObject, $aData)) {
00504             // store
00505             if( $oShopObject->save()) {
00506                 return $this->_postSaveObject($oShopObject, $aData);
00507             }
00508         }
00509 
00510         return false;
00511     }
00512 
00520     protected function _postSaveObject($oShopObject, $aData)
00521     {
00522         // returning ID on success
00523         return $oShopObject->getId();
00524     }
00525 }
00526 

Generated on Tue Apr 21 15:45:44 2009 for OXID eShop CE by  doxygen 1.5.5