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
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
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
00294 if ( !$oObj->load( $sId ) ) {
00295
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 $oDb =oxDb::getDb();
00335 $sSql = "select oxshopid from ".$this->_sTableName." where oxid = " .$oDb->quote( $sId );
00336 try {
00337 $iShopId = $oDb->getOne($sSql);
00338 } catch (Exception $e) {
00339
00340 return;
00341 }
00342 if ( !$this->_isAllowedToEdit( $iShopId ) ) {
00343 throw new Exception( "No right to delete object {$sId} !");
00344 }
00345 }
00346
00354 public function checkForDeletion($sId)
00355 {
00356
00357 if ( !isset( $sId ) ) {
00358 throw new Exception( "Missing ID!");
00359 }
00360
00361 if ($oUsr = oxUser::getAdminUser()) {
00362 if ($oUsr->oxuser__oxrights->value == "malladmin") {
00363 return;
00364 }
00365 }
00366 try {
00367 $this->getObjectForDeletion($sId);
00368 } catch (oxSystemComponentException $e) {
00369 if ($e->getMessage() == 'EXCEPTION_SYSTEMCOMPONENT_CLASSNOTFOUND') {
00370 $this->_directSqlCheckForDeletion($sId);
00371 } else {
00372 throw $e;
00373 }
00374 }
00375 }
00376
00384 public function delete($sID)
00385 {
00386 $oDb = oxDb::getDb();
00387 $sSql = "delete from ".$this->_sTableName." where oxid = " . $oDb->quote( $sID );
00388
00389 return $oDb->Execute($sSql);
00390 }
00391
00400 public function deleteObject($oObj, $sID)
00401 {
00402
00403 return $oObj->delete($sID);
00404 }
00405
00413 public function addExportData( $aFields)
00414 {
00415 return $aFields;
00416 }
00417
00428 public function addImportData($aFields)
00429 {
00430 return $aFields;
00431 }
00432
00438 public function getRightFields()
00439 {
00440 $aRParams = array();
00441
00442 foreach ( $this->_aFieldList as $sField ) {
00443 $aRParams[] = strtolower($this->_sTableName.'__'.$sField);
00444 }
00445 return $aRParams;
00446 }
00447
00453 public function getFieldList()
00454 {
00455 return $this->_aFieldList;
00456 }
00457
00463 public function getKeyFields()
00464 {
00465 return $this->_aKeyFieldList;
00466 }
00467
00473 public function hasKeyFields()
00474 {
00475 if ( isset( $this->_aKeyFieldList ) && is_array( $this->_aKeyFieldList ) ) {
00476 return true;
00477 }
00478 return false;
00479 }
00480
00490 protected function _preAssignObject($oShopObject, $aData, $blAllowCustomShopId)
00491 {
00492 if ( !isset( $aData['OXID'] ) ) {
00493 throw new Exception( "OXID missing, seems to be wrong Format!");
00494 }
00495 if ( !$oShopObject->exists( $aData['OXID'] ) ) {
00496
00497 if ( !$blAllowCustomShopId ) {
00498 if (isset($aData['OXSHOPID'])) {
00499 $aData['OXSHOPID'] = oxConfig::getInstance()->getShopId();
00500 }
00501 }
00502 if ( !array_key_exists('OXSHOPINCL', $aData ) ) {
00503 $aData['OXSHOPINCL'] = oxUtils::getInstance()->getShopBit($aData['OXSHOPID']);
00504 }
00505 if ( !array_key_exists( 'OXSHOPEXCL', $aData ) ) {
00506 $aData['OXSHOPEXCL'] = 0;
00507 }
00508 }
00509 if (isset($aData['OXACTIV'])) {
00510 $aData['OXACTIVE'] = $aData['OXACTIV'];
00511 }
00512 if (isset($aData['OXACTIVFROM'])) {
00513 $aData['OXACTIVEFROM'] = $aData['OXACTIVFROM'];
00514 }
00515 if (isset($aData['OXACTIVTO'])) {
00516 $aData['OXACTIVETO'] = $aData['OXACTIVTO'];
00517 }
00518 for ($i=1;$i<4;$i++) {
00519 if (isset($aData['OXACTIV_'.$i])) {
00520 $aData['OXACTIVE_'.$i] = $aData['OXACTIV_'.$i];
00521 }
00522 }
00523
00524 foreach ($aData as $key => $val) {
00525 if ( !strlen( (string) $val ) ) {
00526
00527 $aData[$key] = null;
00528 }
00529 }
00530 return $aData;
00531 }
00532
00542 protected function _preSaveObject($oShopObject, $aData)
00543 {
00544 return true;
00545 }
00546
00555 public function saveObject($aData, $blAllowCustomShopId)
00556 {
00557 $sObjectName = $this->getShopObjectName();
00558 if ( $sObjectName ) {
00559 $oShopObject = oxNew( $sObjectName, 'core');
00560 if ( $oShopObject instanceof oxI18n ) {
00561 $oShopObject->setLanguage(0);
00562 $oShopObject->setEnableMultilang(false);
00563 }
00564 } else {
00565 $oShopObject = oxNew( 'oxbase', 'core');
00566 $oShopObject->init($this->getBaseTableName());
00567 }
00568
00569 $aData = $this->_preAssignObject($oShopObject, $aData, $blAllowCustomShopId);
00570
00571
00572 $oShopObject->load( $aData['OXID']);
00573
00574 $oShopObject->assign( $aData );
00575
00576 if ($blAllowCustomShopId) {
00577 $oShopObject->setIsDerived(false);
00578 }
00579
00580 if ($this->_preSaveObject($oShopObject, $aData)) {
00581
00582 if ( $oShopObject->save() ) {
00583 return $this->_postSaveObject($oShopObject, $aData);
00584 }
00585 }
00586
00587 return false;
00588 }
00589
00598 protected function _postSaveObject($oShopObject, $aData)
00599 {
00600
00601 return $oShopObject->getId();
00602 }
00603 }
00604