00001 <?php
00002
00006 DEFINE('ACTION_NA', 0);
00007 DEFINE('ACTION_DELETE', 1);
00008 DEFINE('ACTION_INSERT', 2);
00009 DEFINE('ACTION_UPDATE', 3);
00010 DEFINE('ACTION_UPDATE_STOCK', 4);
00011
00016 class oxBase extends oxSuperCfg
00017 {
00022 protected $_sOXID = null;
00023
00028 protected $_iShopId = null;
00029
00035 protected $_blIsSimplyClonable = true;
00036
00041 protected $_aErrors = array();
00042
00047 protected $_sClassName = 'oxbase';
00048
00054 protected $_sCoreTable = null;
00055
00060 protected $_sViewTable = null;
00061
00062
00068 protected $_aFieldNames = array('oxid' => 0);
00069
00074 protected $_blIsNewCache = false;
00075
00081 protected $_sCacheKey = null;
00082
00088 protected $_blUseLazyLoading = false;
00089
00095 protected $_aSkipSaveFields = array();
00096
00101 protected $_sExistKey = "oxid";
00102
00109 protected $_blIsDerived = null;
00110
00120 protected static $_blDisableFieldCaching = array();
00121
00127 protected $_blIsSeoObject = false;
00128
00134 protected $_blReadOnly = false;
00135
00141 protected $_blIsInList = false;
00142
00148 protected $_isLoaded = false;
00149
00153 public function __construct()
00154 {
00155
00156 $myConfig = $this->getConfig();
00157 $this->_sCacheKey = $this->getViewName();
00158 if ( $this->_blUseLazyLoading ) {
00159 $this->_sCacheKey .= $myConfig->getActiveView()->getClassName();
00160 } else {
00161 $this->_sCacheKey .= "allviews";
00162 }
00163
00164
00165 if ( $this->isAdmin() ) {
00166 $this->_sCacheKey = null;
00167 }
00168
00169 $this->setShopId( $myConfig->getShopId() );
00170 }
00171
00180 public function __set($sName, $sValue)
00181 {
00182 $this->$sName = $sValue;
00183 if ( $this->_blUseLazyLoading && strpos( $sName, $this->_sCoreTable . "__" ) === 0 ) {
00184 $sFieldName = str_replace( $this->_sCoreTable . "__", '', $sName );
00185 if ($sFieldName != 'oxnid' && !$this->_aFieldNames[$sFieldName]) {
00186 $aAllFields = $this->_getAllFields(true);
00187 if (isset($aAllFields[strtolower($sFieldName)])) {
00188 $iFieldStatus = $this->_getFieldStatus($sFieldName);
00189 $this->_addField($sFieldName, $iFieldStatus);
00190 }
00191 }
00192 }
00193 }
00194
00202 public function __get( $sName )
00203 {
00204 switch ($sName) {
00205 case 'blIsDerived':
00206 return $this->isDerived();
00207 break;
00208 case 'sOXID':
00209 return $this->getId();
00210 break;
00211 case 'blReadOnly':
00212 return $this->isReadOnly();
00213 break;
00214 }
00215
00216
00217
00218
00219 if ( $this->_blUseLazyLoading && strpos( $sName, $this->_sCoreTable . "__" ) === 0 ) {
00220
00221
00222 $sFieldName = str_replace($this->_sCoreTable . "__", '', $sName);
00223
00224 $iFieldStatus = $this->_getFieldStatus($sFieldName);
00225
00226 $sSqlFieldName = $sFieldName;
00227 if ($iFieldStatus && $this->isMultilang() ) {
00228 $sSqlFieldName = $sFieldName.oxLang::getInstance()->getLanguageTag( $this->getLanguage() );
00229 }
00230
00231 if ( $this->getId() ) {
00232 $oDb = oxDb::getDb();
00233 $sQ = "select $sSqlFieldName from " . $this->_sCoreTable . " where oxid = " . $oDb->quote($this->getId());
00234
00235 try {
00236
00237 $rs = $oDb->execute( $sQ );
00238 if ( $rs === false ) {
00239 return null;
00240 }
00241
00242 $this->_addField($sFieldName, $iFieldStatus);
00243 $sValue = $rs->fields[0];
00244 $this->_setFieldData( $sFieldName, $sValue );
00245
00246
00247 if ($this->_sCacheKey) {
00248 $myUtils = oxUtils::getInstance();
00249 $sCacheKey = 'fieldnames_' . $this->_sCoreTable . "_" . $this->_sCacheKey;
00250 $aFieldNames = $myUtils->fromFileCache($sCacheKey);
00251 $aFieldNames[$sFieldName] = $iFieldStatus;
00252 $myUtils->toFileCache($sCacheKey, $aFieldNames);
00253 }
00254
00255 } catch ( Exception $e ) {
00256 return null;
00257 }
00258
00259
00260
00261 self::$_blDisableFieldCaching[get_class($this)] = true;
00262 }
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273 oxUtilsObject::getInstance()->resetInstanceCache(get_class($this));
00274 }
00275
00276
00277 if (!isset( $this->$sName ) ) {
00278 $this->$sName = null;
00279 }
00280
00281 return $this->$sName;
00282 }
00283
00291 public function __isset($mVar)
00292 {
00293 return isset($this->$mVar);
00294 }
00295
00301 public function __clone()
00302 {
00303 if (!$this->_blIsSimplyClonable) {
00304 foreach ( $this->_aFieldNames as $sField => $sVal ) {
00305 $sLongName = $this->_getFieldLongName( $sField );
00306 if ( is_object($this->$sLongName)) {
00307 $this->$sLongName = clone $this->$sLongName;
00308 }
00309 }
00310 }
00311 }
00312
00320 public function oxClone($oObject)
00321 {
00322 $aClasVars = get_object_vars( $oObject);
00323 while (list($name, $value) = each($aClasVars)) {
00324 if ( is_object( $oObject->$name ) ) {
00325 $this->$name = clone $oObject->$name;
00326 } else {
00327 $this->$name = $oObject->$name;
00328 }
00329 }
00330 }
00331
00340 public function init( $sTableName = null, $blForceAllFields = false)
00341 {
00342
00343 if ( !$sTableName ) {
00344 $sTableName = $this->_sCoreTable;
00345 } else {
00346 $this->_sCoreTable = $sTableName;
00347 }
00348
00349 $this->_sViewTable = getViewName( $this->_sCoreTable );
00350
00351
00352
00353 if ( count( $this->_aFieldNames ) <= 1 ) {
00354 $this->_initDataStructure( $blForceAllFields );
00355 }
00356 }
00357
00365 public function assign( $dbRecord )
00366 {
00367 if ( !is_array( $dbRecord ) ) {
00368 return;
00369 }
00370
00371
00372 reset($dbRecord );
00373 while ( list( $sName, $sValue ) = each( $dbRecord ) ) {
00374
00375
00376
00377
00378
00379
00380 $this->_setFieldData( $sName, $sValue );
00381 }
00382
00383 $sOxidField = $this->_getFieldLongName( 'oxid' );
00384 $this->_sOXID = $this->$sOxidField->value;
00385
00386 }
00387
00393 public function getClassName()
00394 {
00395 return $this->_sClassName;
00396 }
00397
00403 public function getCoreTableName()
00404 {
00405 return $this->_sCoreTable;
00406 }
00407
00413 public function getId()
00414 {
00415 return $this->_sOXID;
00416 }
00417
00425 public function setId($sOXID = null)
00426 {
00427 if ( $sOXID ) {
00428 $this->_sOXID = $sOXID;
00429 } else {
00430 $this->_sOXID = oxUtilsObject::getInstance()->generateUID();
00431 }
00432
00433 $sIdVarName = $this->_sCoreTable . "__oxid";
00434 $this->$sIdVarName = new oxField($this->_sOXID, oxField::T_RAW);
00435
00436 return $this->_sOXID;
00437 }
00438
00446 public function setShopId($iShopId)
00447 {
00448 $this->_iShopId = $iShopId;
00449 }
00450
00456 public function getShopId()
00457 {
00458 return $this->_iShopId;
00459 }
00460
00466 public function getViewName()
00467 {
00468 return $this->_sViewTable;
00469 }
00470
00479 public function modifyCacheKey( $sCacheKey, $blOverride = false )
00480 {
00481 if ( $blOverride ) {
00482 $this->_sCacheKey = $sCacheKey;
00483 } else {
00484 $this->_sCacheKey .= $sCacheKey;
00485 }
00486 }
00487
00493 public function disableLazyLoading()
00494 {
00495 $this->_blUseLazyLoading = false;
00496 $this->_initDataStructure(true);
00497 }
00498
00499
00505 public function isDerived()
00506 {
00507
00508 return $this->_blIsDerived;
00509 }
00510
00518 public function setIsDerived($blVal)
00519 {
00520 $this->_blIsDerived = $blVal;
00521 }
00522
00529 public function isMultilang()
00530 {
00531 return false;
00532 }
00533
00543 public function load( $sOXID)
00544 {
00545
00546
00547
00548
00549
00550
00551
00552
00553
00554 $this->_addField('oxid', 0);
00555
00556 $sSelect = $this->buildSelectString( array( $this->getViewName().".oxid" => $sOXID));
00557
00558 return $this->_isLoaded = $this->assignRecord( $sSelect );
00559 }
00560
00566 public function isLoaded()
00567 {
00568 return $this->_isLoaded;
00569 }
00570
00578 public function buildSelectString( $aWhere = null)
00579 {
00580 $oDB = oxDb::getDb(true);
00581 $myUtils = oxUtils::getInstance();
00582
00583 $sGet = $this->getSelectFields();
00584 $sSelect = "select $sGet from " . $this->_sViewTable . " where 1 ";
00585
00586 if ( $aWhere) {
00587 reset($aWhere);
00588 while (list($name, $value) = each($aWhere)) {
00589 $sSelect .= " and " . $name.' = '.$oDB->quote($value);
00590 }
00591 }
00592
00593
00594
00595
00596
00597 return $sSelect;
00598 }
00599
00607 public function assignRecord( $sSelect)
00608 {
00609 $blRet = false;
00610
00611 $oDB = oxDb::getDb(true);
00612
00613 $rs = $oDB->execute( $sSelect);
00614 if ($rs != false && $rs->recordCount() > 0) {
00615 $blRet = true;
00616 $this->assign( $rs->fields);
00617 }
00618
00619 return $blRet;
00620 }
00621
00629 public function getFieldData( $sFieldName)
00630 {
00631 $sLongFieldName = $this->_getFieldLongName($sFieldName);
00632 return $this->$sLongFieldName->value;
00633 }
00634
00640 public function getSelectFields()
00641 {
00642 $aSelectFields = array();
00643
00644 foreach ( $this->_aFieldNames as $sKey => $sField ) {
00645 $sRealField = $this->getSqlFieldName($sKey);
00646 $sSelectSufix = "";
00647 if ( $sRealField !== $sKey ) {
00648 $sSelectSufix = " as $sKey";
00649 }
00650 $aSelectFields[] = $this->_sViewTable . '.' . $sRealField . $sSelectSufix;
00651 }
00652
00653 $sSelectFields = join( ", ", $aSelectFields );
00654 return $sSelectFields;
00655 }
00656
00664 public function delete( $sOXID = null)
00665 {
00666 if ( !$sOXID ) {
00667 $sOXID = $this->getId();
00668
00669
00670 if ( !$this->allowDerivedDelete() ) {
00671 return false;
00672 }
00673 }
00674
00675 if ( !$sOXID ) {
00676 return false;
00677 }
00678
00679
00680 $oDB = oxDb::getDb(true);
00681 $sDelete = "delete from $this->_sCoreTable where oxid = ".$oDB->quote( $sOXID );
00682 $rs = $oDB->execute( $sDelete );
00683 if ( $blDelete = ( bool ) $oDB->affected_Rows() ) {
00684 $this->onChange(ACTION_DELETE, $sOXID);
00685 }
00686
00687 return $blDelete;
00688 }
00689
00690
00696 public function save()
00697 {
00698 if ( !is_array( $this->_aFieldNames ) ) {
00699 return false;
00700 }
00701
00702 $blRet = false;
00703
00704
00705 if ( $this->isAdmin() && !$this->getConfig()->getConfigParam( 'blSkipFormatConversion' ) ) {
00706 foreach ($this->_aFieldNames as $sName => $sVal) {
00707 $sLongName = $this->_getFieldLongName($sName);
00708 if ( isset($this->$sLongName->fldtype) && $this->$sLongName->fldtype == "datetime" ) {
00709 oxDb::getInstance()->convertDBDateTime( $this->$sLongName, true );
00710 } elseif ( isset($this->$sLongName->fldtype) && $this->$sLongName->fldtype == "timestamp" ) {
00711 oxDb::getInstance()->convertDBTimestamp( $this->$sLongName, true);
00712 } elseif ( isset($this->$sLongName->fldtype) && $this->$sLongName->fldtype == "date" ) {
00713 oxDb::getInstance()->convertDBDate( $this->$sLongName, true);
00714 }
00715 }
00716 }
00717
00718 if ( $this->exists() ) {
00719
00720
00721 if ( !$this->allowDerivedUpdate() ) {
00722 return false;
00723 }
00724
00725 $blRet = $this->_update();
00726 $sAction = ACTION_UPDATE;
00727 } else {
00728 $blRet = $this->_insert();
00729 $sAction = ACTION_INSERT;
00730 }
00731
00732 $this->onChange($sAction);
00733
00734 if ( $blRet ) {
00735 return $this->getId();
00736 } else {
00737 return false;
00738 }
00739 }
00740
00746 public function allowDerivedUpdate()
00747 {
00748 return !$this->isDerived();
00749 }
00750
00756 public function allowDerivedDelete()
00757 {
00758 return !$this->isDerived();
00759 }
00760
00768 public function exists( $sOXID = null)
00769 {
00770 if ( !$sOXID ) {
00771 $sOXID = $this->getId();
00772 }
00773 if ( !$sOXID ) {
00774 return false;
00775 }
00776
00777 $sViewName = $this->getViewName();
00778 $oDB = oxDb::getDb( true );
00779 $sSelect= "select {$this->_sExistKey} from {$sViewName} where {$this->_sExistKey} = ".$oDB->quote( $sOXID );
00780 return ( bool ) $oDB->getOne( $sSelect );
00781 }
00782
00790 public function getSqlFieldName($sField)
00791 {
00792 return $sField;
00793 }
00794
00802 public function getSqlActiveSnippet( $blForceCoreTable = false )
00803 {
00804 $sQ = '';
00805 $sTable = $this->getCoreTableName();
00806
00807
00808 if ( isset( $this->_aFieldNames['oxactive'] ) ) {
00809 $sQ = " $sTable.oxactive = 1 ";
00810 }
00811
00812
00813 if ( isset( $this->_aFieldNames['oxactivefrom'] ) && isset( $this->_aFieldNames['oxactiveto'] ) ) {
00814
00815 $sDate = date( 'Y-m-d H:i:s', oxUtilsDate::getInstance()->getTime() );
00816
00817 $sQ = $sQ?" $sQ or ":'';
00818 $sQ = " ( $sQ ( $sTable.oxactivefrom < '$sDate' and $sTable.oxactiveto > '$sDate' ) ) ";
00819 }
00820
00821 return $sQ;
00822 }
00823
00830 public function validate()
00831 {
00832 $this->_aErrors = array();
00833 foreach ($this->_aFieldNames as $fName => $iVal) {
00834
00835 $fName = $this->_getFieldLongName($fName);
00836
00837 if ( method_exists ( $this, "validate_$fName")) {
00838 $validatorMethod = "validate_$fName";
00839 if ( $error = $this->$validatorMethod()) {
00840 $this->_aErrors[$fName] = $error;
00841 }
00842 }
00843 }
00844 return !$this->hasErrors();
00845 }
00846
00855 public function beforeUpdate( $sOXID = null )
00856 {
00857 }
00858
00869 public function onChange( $iAction = null, $sOXID = null)
00870 {
00871 }
00872
00873
00879 public function hasErrors()
00880 {
00881 return count($this->_aErrors) > 0;
00882 }
00883
00889 public function getErrors()
00890 {
00891 return $this->_aErrors;
00892 }
00893
00901 public function getError( $sField)
00902 {
00903 if (isset($this->_aErrors[$sField])) {
00904 return $this->_aErrors[$sField];
00905 }
00906
00907
00908
00909 return null;
00910 }
00911
00919 public function getHtmlError( $sField)
00920 {
00921 if ( $error = $this->getError($sField) ) {
00922 return $error;
00923 }
00924 }
00925
00931 public function setInList()
00932 {
00933 $this->_blIsInList = true;
00934 }
00935
00941 protected function _isInList()
00942 {
00943 return $this->_blIsInList;
00944 }
00945
00954 protected function _getObjectViewName( $sTable, $sShopID = null)
00955 {
00956 return $sTable;
00957
00958 }
00959
00960
00970 protected function _getAllFields($blReturnSimple = false)
00971 {
00972 $myUtils = oxUtils::getInstance();
00973
00974 if (!$this->_sCoreTable) {
00975 return array();
00976 }
00977
00978
00979
00980
00981
00982 $sCacheKey = $this->_sCoreTable . "_allfields_" . $blReturnSimple;
00983 $aMetaFields = $myUtils->fromFileCache( $sCacheKey );
00984
00985 if ($aMetaFields) {
00986 return $aMetaFields;
00987 }
00988
00989 $aMetaFields = oxDb::getInstance()->getTableDescription( $this->_sCoreTable );
00990
00991 if ( !$blReturnSimple ) {
00992 $myUtils->toFileCache( $sCacheKey, $aMetaFields );
00993 return $aMetaFields;
00994 }
00995
00996
00997 $aRet = array();
00998 if (is_array($aMetaFields)) {
00999 foreach ( $aMetaFields as $oVal ) {
01000 $aRet[strtolower( $oVal->name )] = 0;
01001 }
01002 }
01003
01004 $myUtils->toFileCache( $sCacheKey, $aRet);
01005
01006 return $aRet;
01007 }
01008
01017 protected function _initDataStructure($blForceFullStructure = false)
01018 {
01019 $myUtils = oxUtils::getInstance();
01020
01021
01022 $aFieldNames = null;
01023 $sFullCacheKey = 'fieldnames_' .$this->_sCoreTable . "_" . $this->_sCacheKey;
01024 if ($this->_sCacheKey && !$this->_isDisabledFieldCache()) {
01025 $aFieldNames = $myUtils->fromFileCache($sFullCacheKey);
01026 }
01027
01028 if (!$aFieldNames) {
01029 $aFieldNames = $this->_getNonCachedFieldNames($blForceFullStructure);
01030 if ($this->_sCacheKey && !$this->_isDisabledFieldCache()) {
01031 $myUtils->toFileCache($sFullCacheKey, $aFieldNames);
01032 }
01033 }
01034
01035 if ( $aFieldNames !== false ) {
01036 foreach ( $aFieldNames as $sField => $sStatus ) {
01037 $this->_addField($sField, $sStatus);
01038 }
01039 }
01040 }
01041
01053 protected function _getNonCachedFieldNames($blForceFullStructure = false)
01054 {
01055
01056
01057 startProfile("!__CACHABLE__!");
01058
01059
01060 if ($this->isAdmin()) {
01061 $aMetaFields = $this->_getAllFields();
01062 foreach ( $aMetaFields as $oField ) {
01063 if ( $oField->max_length == -1 ) {
01064 $oField->max_length = 10;
01065 }
01066
01067 if ( $oField->type == "datetime" ) {
01068 $oField->max_length = 20;
01069 }
01070
01071 $this->_addField( $oField->name, $this->_getFieldStatus($oField->name), $oField->type, $oField->max_length );
01072 }
01073 stopProfile("!__CACHABLE__!");
01074 return false;
01075 }
01076
01077
01078 if ( $blForceFullStructure || !$this->_blUseLazyLoading ) {
01079 $aMetaFields = $this->_getAllFields(true);
01080
01081
01082
01083
01084 stopProfile("!__CACHABLE__!");
01085 return $aMetaFields;
01086 }
01087
01088
01089 stopProfile("!__CACHABLE__!");
01090 return array("oxid" => 0);
01091 }
01092
01101 protected function _getFieldStatus($sFieldName)
01102 {
01103 return 0;
01104 }
01105
01116 protected function _addField($sName, $iStatus, $sType = null, $sLength = null)
01117 {
01118
01119 $sName = strtolower($sName);
01120
01121
01122 $this->_aFieldNames[$sName] = $iStatus;
01123
01124
01125 $sLongName = $this->_getFieldLongName($sName);
01126 if ( isset($this->$sLongName) ) {
01127 return;
01128 }
01129
01130
01131 $oField = false;
01132
01133 if (isset($sType)) {
01134 $oField = new oxField();
01135 $oField->fldtype = $sType;
01136
01137
01138 $this->_blIsSimplyClonable = false;
01139 }
01140
01141 if (isset($sLength)) {
01142 if (!$oField) {
01143 $oField = new oxField();
01144 }
01145 $oField->fldmax_length = $sLength;
01146 $this->_blIsSimplyClonable = false;
01147 }
01148
01149 $this->$sLongName = $oField;
01150 }
01151
01159 protected function _getFieldLongName( $sFieldName)
01160 {
01161
01162 if ($sFieldName[2] == $this->_sCoreTable[2] && strpos($sFieldName, $this->_sCoreTable."__") === 0) {
01163 return $sFieldName;
01164 }
01165
01166 $sLongName = $this->_sCoreTable."__".strtolower( $sFieldName);
01167 return $sLongName;
01168 }
01169
01179 protected function _setFieldData( $sFieldName, $sValue, $iDataType = oxField::T_TEXT)
01180 {
01181
01182
01183 $sLongFieldName = $this->_getFieldLongName( $sFieldName);
01184
01185
01186
01187
01188
01189
01190
01191
01192
01193
01194 if (!$this->_blUseLazyLoading && !isset($this->$sLongFieldName)) {
01195 $aFields = $this->_getAllFields(true);
01196 if ( isset( $aFields[strtolower($sFieldName)] ) ) {
01197 $this->_addField($sFieldName, $this->_getFieldStatus($sFieldName));
01198 }
01199 }
01200
01201 if (isset($this->$sLongFieldName) && isset($this->$sLongFieldName->fldtype) && $this->$sLongFieldName->fldtype == "double") {
01202 $sValue = str_replace( ",", ".", $sValue );
01203 }
01204
01205
01206 if (isset($this->$sLongFieldName) && is_object($this->$sLongFieldName)) {
01207 $this->$sLongFieldName->setValue($sValue, $iDataType);
01208 } else {
01209 $this->$sLongFieldName = new oxField($sValue, $iDataType);
01210 }
01211
01212 }
01213
01222 protected function _getUpdateFieldValue($sFieldName, $oField)
01223 {
01224 $blPassNullValue = false;
01225 if ($oField instanceof oxField) {
01226 $value = $oField->getRawValue();
01227 } else {
01228 $value = $oField->value;
01229 }
01230
01231 if (null === $value) {
01232 $aMetaData = $this->_getAllFields();
01233 foreach ($aMetaData as $oMetaInfo) {
01234 if (!strcasecmp($oMetaInfo->name, $sFieldName)) {
01235 $blPassNullValue = !$oMetaInfo->not_null;
01236 break;
01237 }
01238 }
01239 }
01240 if ($blPassNullValue) {
01241 return 'null';
01242 } else {
01243 return oxDb::getDb()->quote( $value );
01244 }
01245 }
01246
01255 protected function _getUpdateFields( $blUseSkipSaveFields = true )
01256 {
01257 $sSql = '';
01258 $blSep = false;
01259 foreach (array_keys($this->_aFieldNames) as $sKey) {
01260 $sLongName = $this->_getFieldLongName($sKey);
01261 $oField = $this->$sLongName;
01262
01263
01264 if ( !$blUseSkipSaveFields || ($blUseSkipSaveFields && !in_array(strtolower($sKey), $this->_aSkipSaveFields)) ) {
01265 $sKey = $this->getSqlFieldName($sKey);
01266 $sSql .= (( $blSep) ? ',':'' ).$sKey." = ".$this->_getUpdateFieldValue($sKey, $oField);
01267 $blSep = true;
01268 }
01269 }
01270
01271 return $sSql;
01272 }
01273
01283 protected function _update()
01284 {
01285
01286 if ( !$this->allowDerivedUpdate() ) {
01287 return false;
01288 }
01289
01290
01291 if ( !$this->getId() ) {
01292 $oEx = oxNew( 'oxObjectException' );
01293 $oEx->setMessage( 'EXCEPTION_OBJECT_OXIDNOTSET' );
01294 $oEx->setObject($this);
01295 throw $oEx;
01296 }
01297
01298 $sIDKey = oxUtils::getInstance()->getArrFldName( $this->_sCoreTable.".oxid");
01299 $this->$sIDKey = new oxField($this->getId(), oxField::T_RAW);
01300
01301 $sUpdate= "update {$this->_sCoreTable} set ".$this->_getUpdateFields()
01302 ." where {$this->_sCoreTable}.oxid = '".$this->getId()."' ";
01303
01304
01305
01306
01307 $this->beforeUpdate();
01308
01309 $blRet = (bool) oxDB::getDb()->execute( $sUpdate);
01310 $this->_rebuildCache();
01311
01312 return $blRet;
01313 }
01314
01322 protected function _insert()
01323 {
01324
01325 $oDB = oxDb::getDb(true);
01326 $myConfig = $this->getConfig();
01327 $myUtils = oxUtils::getInstance();
01328
01329
01330 if ( !$this->getId()) {
01331 $this->setId();
01332 }
01333
01334 $sIDKey = $myUtils->getArrFldName( $this->_sCoreTable.".oxid");
01335 $this->$sIDKey = new oxField($this->getId(), oxField::T_RAW);
01336 $sInsert= "Insert into {$this->_sCoreTable} set ";
01337
01338
01339 $sShopField = $myUtils->getArrFldName($this->_sCoreTable.".oxshopid");
01340 if (isset($this->$sShopField) && !$this->$sShopField->value)
01341 $this->$sShopField = new oxField($myConfig->getShopId(), oxField::T_RAW);
01342 $sInsert .= $this->_getUpdateFields( false );
01343
01344 $blRet = (bool) $oDB->execute( $sInsert);
01345
01346 $this->_rebuildCache();
01347
01348 return $blRet;
01349
01350 }
01351
01357 protected function _rebuildCache()
01358 {
01359 if ( !$this->_blIsNewCache) {
01360 oxUtils::getInstance()->rebuildCache();
01361 $this->_blIsNewCache = true;
01362 }
01363 }
01364
01374 protected function _setRecordNumber( $sMaxField, $aWhere = null, $iMaxTryCnt = 5 )
01375 {
01376
01377 $sWhere = "";
01378 if ( is_array( $aWhere ) && count( $aWhere ) > 0) {
01379 $sWhere = implode(" and ", $aWhere).' and ';
01380 }
01381
01382
01383 $sUpdate = "update {$this->getViewName()} as t1, (select (max($sMaxField)+1) as t2max from {$this->getViewName()} where $sWhere 1) as t2 set t1.$sMaxField=t2.t2max where t1.oxid = '".$this->getId()."'";
01384
01385
01386
01387 $sMaxSelect = "select $sMaxField from ".$this->getViewName()." where oxid='".$this->getId()."' ";
01388 $sCheck = "select count(oxid) from ".$this->getViewName()." where $sMaxField = ($sMaxSelect) and $sWhere 1 ";
01389
01390 do {
01391 if ( oxDb::getDb(true)->Execute( $sUpdate ) === false ) {
01392 return false;
01393 }
01394
01395 $iChkCnt = oxDb::getDb(true)->GetOne( $sCheck );
01396 } while ( ( $iChkCnt > 1 ) && $iMaxTryCnt-- );
01397
01398 $sFieldName = $this->getViewName().'__'.$sMaxField;
01399 $this->$sFieldName = new oxField(oxDb::getDb(true)->GetOne( $sMaxSelect ), oxField::T_RAW);
01400
01401 return ( $iChkCnt == 1 );
01402 }
01403
01410 protected function _isDisabledFieldCache()
01411 {
01412 $sClass = get_class($this);
01413 if (isset(self::$_blDisableFieldCaching[$sClass]) && self::$_blDisableFieldCaching[$sClass]) {
01414 return true;
01415 }
01416
01417 return false;
01418 }
01419
01425 public function isOx()
01426 {
01427 $sOxId = $this->getId();
01428 if ( $sOxId[0] == 'o' && $sOxId[1] == 'x' ) {
01429 return true;
01430 }
01431 return false;
01432 }
01433
01439 public function isReadOnly()
01440 {
01441 return $this->_blReadOnly;
01442 }
01443
01451 public function setReadOnly( $blReadOnly )
01452 {
01453 $this->_blReadOnly = $blReadOnly;
01454 }
01455
01456 }