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 = oxConfig::getInstance()->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
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
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 = oxConfig::getInstance();
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
00282 if (!$oObj->Load($sId)) {
00283
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 if ($oUsr = oxUser::getAdminUser()) {
00304 if ($oUsr->oxuser__oxrights->value == "malladmin") {
00305 return true;
00306 } elseif ($oUsr->oxuser__oxrights->value == (int) $iShopId) {
00307 return true;
00308 }
00309 }
00310 return false;
00311 }
00312
00322 protected function _directSqlCheckForDeletion($sId)
00323 {
00324 $oDb = oxDb::getDb();
00325 $sSql = "select oxshopid from ".$this->_sTableName." where oxid = " . $oDb->quote( $sId );
00326 try {
00327 $iShopId = $oDb->getOne($sSql);
00328 } catch (Exception $e) {
00329
00330 return;
00331 }
00332 if (!$this->_isAllowedToEdit($iShopId)) {
00333 throw new Exception( "No right to delete object {$sId} !");
00334 }
00335 }
00336
00346 public function checkForDeletion($sId)
00347 {
00348
00349 if ( !isset($sId)) {
00350 throw new Exception( "Missing ID!");
00351 }
00352
00353 if ($oUsr = oxUser::getAdminUser()) {
00354 if ($oUsr->oxuser__oxrights->value == "malladmin") {
00355 return;
00356 }
00357 }
00358 try {
00359 $this->getObjectForDeletion($sId);
00360 } catch (oxSystemComponentException $e) {
00361 if ($e->getMessage() == 'EXCEPTION_SYSTEMCOMPONENT_CLASSNOTFOUND') {
00362 $this->_directSqlCheckForDeletion($sId);
00363 } else {
00364 throw $e;
00365 }
00366 }
00367 }
00368
00376 public function delete($sID)
00377 {
00378 $myConfig = oxConfig::getInstance();
00379 $oDb = oxDb::getDb();
00380 $sSql = "delete from ".$this->_sTableName." where oxid = " . $oDb->quote( $sID );
00381
00382 return $oDb->Execute($sSql);
00383 }
00384
00393 public function deleteObject($oObj, $sID)
00394 {
00395 return $oObj->delete($sID);
00396 }
00397
00405 public function addExportData( $aFields)
00406 {
00407 return $aFields;
00408 }
00409
00420 public function addImportData($aFields)
00421 {
00422 return $aFields;
00423 }
00424
00432 public function getRightFields()
00433 {
00434 $aRParams = array();
00435
00436 foreach ($this->_aFieldList as $sField) {
00437 $aRParams[] = strtolower($this->_sTableName.'__'.$sField);
00438 }
00439 return $aRParams;
00440 }
00441
00447 public function getFieldList()
00448 {
00449 $sObjectName = $this->getShopObjectName();
00450
00451 if ( $sObjectName ) {
00452 $oShopObject = oxNew( $sObjectName );
00453 } else {
00454 $oShopObject = oxNew( 'oxbase' );
00455 $oShopObject->init( $this->getTableName() );
00456 }
00457
00458 if ($oShopObject instanceof oxI18n) {
00459 $oShopObject->setLanguage( 0 );
00460 $oShopObject->setEnableMultilang(false);
00461 }
00462
00463 $sViewName = $oShopObject->getViewName();
00464 $sFields = str_ireplace( $sViewName . ".", "", strtoupper($oShopObject->getSelectFields()) );
00465 $sFields = str_ireplace( " ", "", $sFields );
00466 $this->_aFieldList = explode( ",", $sFields );
00467
00468 return $this->_aFieldList;
00469 }
00470
00476 public function getKeyFields()
00477 {
00478 return $this->_aKeyFieldList;
00479 }
00480
00488 public function getOxidFromKeyFields($aData)
00489 {
00490 $myConfig = oxConfig::getInstance();
00491
00492 if (!is_array($this->getKeyFields())) {
00493 return null;
00494 }
00495
00496 $oDB = oxDb::getDb();
00497
00498 $aWhere = array();
00499 $blAllKeys = true;
00500 foreach ($this->getKeyFields() as $sKey) {
00501 if (array_key_exists($sKey, $aData)) {
00502 $aWhere[] = $sKey.'='.$oDB->qstr($aData[$sKey]);
00503 } else {
00504 $blAllKeys = false;
00505 }
00506 }
00507
00508 if ($blAllKeys) {
00509 $sSelect = 'SELECT OXID FROM '.$this->getTableName().' WHERE '.implode(' AND ', $aWhere);
00510 return $oDB->getOne($sSelect);
00511 }
00512
00513 return null;
00514 }
00515
00521 public function hasKeyFields()
00522 {
00523 if (isset($this->_aKeyFieldList) && is_array($this->_aKeyFieldList)) {
00524 return true;
00525 }
00526 return false;
00527 }
00528
00538 protected function _preAssignObject($oShopObject, $aData, $blAllowCustomShopId)
00539 {
00540 if (isset($aData['OXSHOPID'])) {
00541 $aData['OXSHOPID'] = 'oxbaseshop';
00542 }
00543
00544
00545 if (!isset($aData['OXID'])) {
00546 $aData['OXID'] = $this->getOxidFromKeyFields($aData);
00547 }
00548
00549
00550 foreach ($aData as $key => $val) {
00551 if (!strlen((string) $val)) {
00552
00553 $aData[$key] = null;
00554 }
00555 }
00556 return $aData;
00557 }
00558
00568 protected function _preSaveObject($oShopObject, $aData)
00569 {
00570 return true;
00571 }
00572
00581 public function saveObject($aData, $blAllowCustomShopId)
00582 {
00583 $sObjectName = $this->getShopObjectName();
00584 if ($sObjectName) {
00585 $oShopObject = oxNew( $sObjectName, 'core');
00586 if ($oShopObject instanceof oxI18n) {
00587 $oShopObject->setLanguage( 0 );
00588 $oShopObject->setEnableMultilang(false);
00589 }
00590 } else {
00591 $oShopObject = oxNew( 'oxbase', 'core');
00592 $oShopObject->init($this->getBaseTableName());
00593 }
00594
00595 foreach ($aData as $key => $value) {
00596
00597 $sUPKey = strtoupper($key);
00598 if (!isset($aData[$sUPKey])) {
00599 unset($aData[$key]);
00600 $aData[$sUPKey] = $value;
00601 }
00602 }
00603
00604
00605 $blLoaded = false;
00606 if ($aData['OXID']) {
00607 $blLoaded = $oShopObject->load( $aData['OXID']);
00608 }
00609
00610 $aData = $this->_preAssignObject( $oShopObject, $aData, $blAllowCustomShopId );
00611
00612 if ($blLoaded) {
00613 $this->checkWriteAccess($oShopObject, $aData);
00614 } else {
00615 $this->checkCreateAccess($aData);
00616 }
00617
00618 $oShopObject->assign( $aData );
00619
00620 if ($blAllowCustomShopId) {
00621 $oShopObject->setIsDerived(false);
00622 }
00623
00624 if ($this->_preSaveObject($oShopObject, $aData)) {
00625
00626 if ( $oShopObject->save()) {
00627 return $this->_postSaveObject($oShopObject, $aData);
00628 }
00629 }
00630
00631 return false;
00632 }
00633
00642 protected function _postSaveObject($oShopObject, $aData)
00643 {
00644
00645 return $oShopObject->getId();
00646 }
00647 }
00648