oxbase.php

Go to the documentation of this file.
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 
00044     protected $_aErrors = array();
00045 
00050     protected $_sClassName = 'oxbase';
00051 
00057     protected $_sCoreTable = null;
00058 
00066     protected $_sCoreTbl = null;
00067 
00072     protected $_sViewTable  = null;
00073 
00074 
00080     protected $_aFieldNames = array('oxid' => 0);
00081 
00086     protected $_blIsNewCache = false;
00087 
00093     protected $_sCacheKey = null;
00094 
00100     protected $_blUseLazyLoading = false;
00101 
00107     protected $_aSkipSaveFields = array();
00108 
00113     protected $_sExistKey = "oxid";
00114 
00121     protected $_blIsDerived = null;
00122 
00132     protected static $_blDisableFieldCaching = array();
00133 
00139     protected $_blIsSeoObject = false;
00140 
00146     protected $_blReadOnly = false;
00147 
00153     protected $_blIsInList = false;
00154 
00160     protected $_isLoaded = false;
00161 
00167     protected $_aInnerLazyCache = null;
00168 
00174     protected $_blEmployMultilanguage = false;
00175 
00179     public function __construct()
00180     {
00181         // set active shop
00182         $myConfig = $this->getConfig();
00183         $this->_sCacheKey = $this->getViewName();
00184         if ( $this->_blUseLazyLoading ) {
00185             $this->_sCacheKey .= $myConfig->getActiveView()->getClassName();
00186         } else {
00187             $this->_sCacheKey .= "allviews";
00188         }
00189 
00190         //do not cache for admin?
00191         if ( $this->isAdmin() ) {
00192             $this->_sCacheKey = null;
00193         }
00194 
00195         $this->setShopId( $myConfig->getShopId() );
00196     }
00197 
00206     public function __set( $sName, $sValue )
00207     {
00208         $this->$sName = $sValue;
00209         if ( $this->_blUseLazyLoading && strpos( $sName, $this->_sCoreTable . "__" ) === 0 ) {
00210             $sFieldName = str_replace( $this->_sCoreTable . "__", '', $sName );
00211             if ( $sFieldName != 'oxnid' && ( !isset( $this->_aFieldNames[$sFieldName] ) || !$this->_aFieldNames[$sFieldName] ) ) {
00212                 $aAllFields = $this->_getAllFields(true);
00213                 if ( isset( $aAllFields[strtolower($sFieldName)] ) ) {
00214                     $iFieldStatus = $this->_getFieldStatus( $sFieldName );
00215                     $this->_addField( $sFieldName, $iFieldStatus );
00216                 }
00217             }
00218         }
00219     }
00220 
00228     public function __get( $sName )
00229     {
00230         switch ( $sName ) {
00231             case 'blIsDerived':
00232                 return $this->isDerived();
00233                 break;
00234             case 'sOXID':
00235                 return $this->getId();
00236                 break;
00237             case 'blReadOnly':
00238                 return $this->isReadOnly();
00239                 break;
00240         }
00241 
00242         // implementing lazy loading fields
00243         // This part of the code is slow and normally is called before field cache is built.
00244         // Make sure it is not called after first page is loaded and cache data is fully built.
00245         if ( $this->_blUseLazyLoading && stripos( $sName, $this->_sCoreTable . "__" ) === 0 ) {
00246 
00247             if ( $this->getId() ) {
00248 
00249                 //lazy load it
00250                 $sFieldName      = str_replace( $this->_sCoreTable . "__", '', $sName );
00251                 $sCacheFieldName = strtoupper( $sFieldName );
00252 
00253                 $iFieldStatus = $this->_getFieldStatus( $sFieldName );
00254                 $sViewName    = $this->getViewName();
00255                 $sId = $this->getId();
00256 
00257                 try {
00258                     if ( $this->_aInnerLazyCache === null ) {
00259 
00260                         $oDb = oxDb::getDb( oxDb::FETCH_MODE_ASSOC );
00261                         $sQ = "SELECT * FROM " . $sViewName . " WHERE `oxid` = " . $oDb->quote( $sId );
00262                         $rs = $oDb->select( $sQ );
00263                         if ( $rs && $rs->RecordCount() ) {
00264                             $this->_aInnerLazyCache = array_change_key_case( $rs->fields, CASE_UPPER );
00265                             if ( array_key_exists( $sCacheFieldName, $this->_aInnerLazyCache ) ) {
00266                                 $sFieldValue = $this->_aInnerLazyCache[$sCacheFieldName];
00267                             } else {
00268                                 return null;
00269                             }
00270                         } else {
00271                             return null;
00272                         }
00273                     } elseif ( array_key_exists( $sCacheFieldName, $this->_aInnerLazyCache ) ) {
00274                         $sFieldValue = $this->_aInnerLazyCache[$sCacheFieldName];
00275                     } else {
00276                         return null;
00277                     }
00278 
00279                     $this->_addField( $sFieldName, $iFieldStatus );
00280                     $this->_setFieldData( $sFieldName, $sFieldValue );
00281 
00282                     //save names to cache for next loading
00283                     if ($this->_sCacheKey) {
00284                         $myUtils = oxUtils::getInstance();
00285                         $sCacheKey = 'fieldnames_' . $this->_sCoreTable . "_" . $this->_sCacheKey;
00286                         $aFieldNames = $myUtils->fromFileCache( $sCacheKey );
00287                         $aFieldNames[$sFieldName] = $iFieldStatus;
00288                         $myUtils->toFileCache( $sCacheKey, $aFieldNames );
00289                     }
00290                 } catch ( Exception $e ) {
00291                     return null;
00292                 }
00293 
00294                 //do not use field cache for this page
00295                 //as if we use it for lists then objects are loaded empty instead of lazy loading.
00296                 self::$_blDisableFieldCaching[get_class( $this )] = true;
00297             }
00298 
00299             oxUtilsObject::getInstance()->resetInstanceCache(get_class($this));
00300         }
00301 
00302         //returns oxStdClass implementing __toString() method due to uknown scenario where this var should be used.
00303         if (!isset( $this->$sName ) ) {
00304             $this->$sName = null;
00305         }
00306 
00307         return $this->$sName;
00308     }
00309 
00317     public function __isset($mVar)
00318     {
00319         return isset($this->$mVar);
00320     }
00321 
00327     public function __clone()
00328     {
00329         if (!$this->_blIsSimplyClonable) {
00330             foreach ( $this->_aFieldNames as $sField => $sVal ) {
00331                 $sLongName = $this->_getFieldLongName( $sField );
00332                 if ( is_object($this->$sLongName)) {
00333                     $this->$sLongName = clone $this->$sLongName;
00334                 }
00335             }
00336         }
00337     }
00338 
00346     public function oxClone($oObject)
00347     {
00348         $aClasVars = get_object_vars( $oObject);
00349         while (list($name, $value) = each($aClasVars)) {
00350             if ( is_object( $oObject->$name ) ) {
00351                 $this->$name = clone $oObject->$name;
00352             } else {
00353                 $this->$name = $oObject->$name;
00354             }
00355         }
00356     }
00357 
00366     public function init( $sTableName = null, $blForceAllFields = false)
00367     {
00368 
00369         if ( !$sTableName ) {
00370             $sTableName = $this->_sCoreTable;
00371         } else {
00372             $this->_sCoreTable = $sTableName;
00373         }
00374 
00375         // compatibility due to parameter deprecation
00376         $this->_sCoreTbl = $sTableName;
00377 
00378         // reset view table
00379         $this->_sViewTable = false;
00380 
00381         if ( count( $this->_aFieldNames ) <= 1 ) {
00382             $this->_initDataStructure( $blForceAllFields );
00383         }
00384     }
00385 
00393     public function assign( $dbRecord )
00394     {
00395         if ( !is_array( $dbRecord ) ) {
00396             return;
00397         }
00398 
00399 
00400         reset($dbRecord );
00401         while ( list( $sName, $sValue ) = each( $dbRecord ) ) {
00402 
00403             // patch for IIS
00404             //TODO: test it on IIS do we still need it
00405             //if( is_array($value) && count( $value) == 1)
00406             //    $value = current( $value);
00407 
00408             $this->_setFieldData( $sName, $sValue );
00409         }
00410 
00411         $sOxidField = $this->_getFieldLongName( 'oxid' );
00412         $this->_sOXID = $this->$sOxidField->value;
00413 
00414     }
00415 
00421     public function getClassName()
00422     {
00423         return $this->_sClassName;
00424     }
00425 
00431     public function getCoreTableName()
00432     {
00433         return $this->_sCoreTable;
00434     }
00435 
00441     public function getId()
00442     {
00443         return $this->_sOXID;
00444     }
00445 
00453     public function setId($sOXID = null)
00454     {
00455         if ( $sOXID ) {
00456             $this->_sOXID = $sOXID;
00457         } else {
00458             $this->_sOXID = oxUtilsObject::getInstance()->generateUID();
00459         }
00460 
00461         $sIdVarName = $this->_sCoreTable . "__oxid";
00462         $this->$sIdVarName = new oxField($this->_sOXID, oxField::T_RAW);
00463 
00464         return $this->_sOXID;
00465     }
00466 
00474     public function setShopId($iShopId)
00475     {
00476         $this->_iShopId = $iShopId;
00477     }
00478 
00484     public function getShopId()
00485     {
00486         return $this->_iShopId;
00487     }
00488 
00496     public function getViewName($blForceCoreTableUsage = null)
00497     {
00498         if (!$this->_sViewTable || ($blForceCoreTableUsage !== null)) {
00499             if ( $blForceCoreTableUsage === true ) {
00500                 return $this->_sCoreTable;
00501             }
00502 
00503 
00504             if ( ( $blForceCoreTableUsage !== null ) && $blForceCoreTableUsage ) {
00505                 $iShopId = -1;
00506             } else {
00507                 $iShopId = oxConfig::getInstance()->getShopId();
00508             }
00509 
00510 
00511             $sViewName = getViewName( $this->_sCoreTable, $this->_blEmployMultilanguage == false ? -1 : $this->getLanguage(), $iShopId );
00512             if ( $blForceCoreTableUsage !== null ) {
00513                 return $sViewName;
00514             }
00515             $this->_sViewTable = $sViewName;
00516         }
00517         return $this->_sViewTable;
00518     }
00519 
00528     public function modifyCacheKey( $sCacheKey, $blOverride = false )
00529     {
00530         if ( $blOverride ) {
00531             $this->_sCacheKey = $sCacheKey;
00532         } else {
00533             $this->_sCacheKey .= $sCacheKey;
00534         }
00535     }
00536 
00542     public function disableLazyLoading()
00543     {
00544         $this->_blUseLazyLoading = false;
00545         $this->_initDataStructure(true);
00546     }
00547 
00548 
00554     public function isDerived()
00555     {
00556 
00557         return $this->_blIsDerived;
00558     }
00559 
00567     public function setIsDerived($blVal)
00568     {
00569         $this->_blIsDerived = $blVal;
00570     }
00571 
00578     public function isMultilang()
00579     {
00580         return false;
00581     }
00582 
00592     public function load( $sOXID)
00593     {
00594         /*
00595         if( !isset($oxID)){
00596             $oEx = oxNew('oxObjectException','core');
00597             $oEx->setMessage('EXCEPTION_OBJECT_OXIDNOTSET');
00598             $oEx->setObject($this);
00599             throw $oEx;
00600         }*/
00601 
00602         //getting at least one field before lazy loading the object
00603         $this->_addField('oxid', 0);
00604         $sSelect = $this->buildSelectString( array( $this->getViewName().".oxid" => $sOXID));
00605 
00606         return $this->_isLoaded = $this->assignRecord( $sSelect );
00607     }
00608 
00614     public function isLoaded()
00615     {
00616         return $this->_isLoaded;
00617     }
00618 
00626     public function buildSelectString( $aWhere = null)
00627     {
00628         $oDB = oxDb::getDb();
00629         $myUtils = oxUtils::getInstance();
00630 
00631         $sGet = $this->getSelectFields();
00632         $sSelect = "select $sGet from " . $this->getViewName() . " where 1 ";
00633 
00634         if ( $aWhere) {
00635             reset($aWhere);
00636             while (list($name, $value) = each($aWhere)) {
00637                 $sSelect .=  " and " . $name.' = '.$oDB->quote($value);
00638             }
00639         }
00640 
00641         // add active shop
00642 
00643         return $sSelect;
00644     }
00645 
00653     public function assignRecord( $sSelect )
00654     {
00655         $blRet = false;
00656 
00657         $rs = oxDb::getDb( oxDb::FETCH_MODE_ASSOC )->select( $sSelect );
00658 
00659         if ($rs != false && $rs->recordCount() > 0) {
00660             $blRet = true;
00661             $this->assign( $rs->fields);
00662         }
00663 
00664         return $blRet;
00665     }
00666 
00674     public function getFieldData( $sFieldName )
00675     {
00676         $sLongFieldName = $this->_getFieldLongName( $sFieldName );
00677             return $this->$sLongFieldName->value;
00678     }
00679 
00687     public function getSelectFields( $blForceCoreTableUsage = null )
00688     {
00689         $aSelectFields = array();
00690 
00691         $sViewName = $this->getViewName( $blForceCoreTableUsage );
00692 
00693         foreach ( $this->_aFieldNames as $sKey => $sField ) {
00694             $aSelectFields[] = $sViewName . '.' . $sKey;
00695         }
00696 
00697         $sSelectFields = join( ", ", $aSelectFields );
00698         return $sSelectFields;
00699     }
00700 
00708     public function delete( $sOXID = null)
00709     {
00710         if ( !$sOXID ) {
00711             $sOXID = $this->getId();
00712 
00713             //do not allow derived deletion
00714             if ( !$this->allowDerivedDelete() ) {
00715                 return false;
00716             }
00717         }
00718 
00719         if ( !$sOXID ) {
00720             return false;
00721         }
00722 
00723 
00724         $oDB = oxDb::getDb( oxDb::FETCH_MODE_ASSOC );
00725         $sDelete = "delete from $this->_sCoreTable where oxid = ".$oDB->quote( $sOXID );
00726         $rs = $oDB->execute( $sDelete );
00727         if ( $blDelete = ( bool ) $oDB->affected_Rows() ) {
00728             $this->onChange(ACTION_DELETE, $sOXID);
00729         }
00730 
00731         return $blDelete;
00732     }
00733 
00734 
00740     public function save()
00741     {
00742         if ( !is_array( $this->_aFieldNames ) ) {
00743             return false;
00744         }
00745 
00746         $blRet = false;
00747 
00748         // #739A - should be executed here because of date/time formatting feature
00749         if ( $this->isAdmin() && !$this->getConfig()->getConfigParam( 'blSkipFormatConversion' ) ) {
00750             foreach ($this->_aFieldNames as $sName => $sVal) {
00751                 $sLongName = $this->_getFieldLongName($sName);
00752                 if ( isset($this->$sLongName->fldtype) && $this->$sLongName->fldtype == "datetime" ) {
00753                     oxDb::getInstance()->convertDBDateTime( $this->$sLongName, true );
00754                 } elseif ( isset($this->$sLongName->fldtype) && $this->$sLongName->fldtype == "timestamp" ) {
00755                     oxDb::getInstance()->convertDBTimestamp( $this->$sLongName, true);
00756                 } elseif ( isset($this->$sLongName->fldtype) && $this->$sLongName->fldtype == "date" ) {
00757                     oxDb::getInstance()->convertDBDate( $this->$sLongName, true);
00758                 }
00759             }
00760         }
00761         if ( $this->exists() ) {
00762             //do not allow derived update
00763             if ( !$this->allowDerivedUpdate() ) {
00764                 return false;
00765             }
00766 
00767             $blRet = $this->_update();
00768             $sAction = ACTION_UPDATE;
00769         } else {
00770             $blRet = $this->_insert();
00771             $sAction = ACTION_INSERT;
00772         }
00773 
00774         $this->onChange($sAction);
00775 
00776         if ( $blRet ) {
00777             return $this->getId();
00778         } else {
00779             return false;
00780         }
00781     }
00782 
00788     public function allowDerivedUpdate()
00789     {
00790         return !$this->isDerived();
00791     }
00792 
00798     public function allowDerivedDelete()
00799     {
00800         return !$this->isDerived();
00801     }
00802 
00810     public function exists( $sOXID = null)
00811     {
00812         if ( !$sOXID ) {
00813             $sOXID = $this->getId();
00814         }
00815         if ( !$sOXID ) {
00816             return false;
00817         }
00818 
00819         $sViewName = $this->getCoreTableName();
00820         $oDb = oxDb::getDb( oxDb::FETCH_MODE_ASSOC );
00821         $sSelect= "select {$this->_sExistKey} from {$sViewName} where {$this->_sExistKey} = ".$oDb->quote( $sOXID );
00822 
00823         return ( bool ) $oDb->getOne( $sSelect );
00824     }
00825 
00833     public function getSqlActiveSnippet( $blForceCoreTable = null )
00834     {
00835         $sQ = '';
00836         $sTable = $this->getViewName($blForceCoreTable);
00837 
00838         // has 'active' field ?
00839         if ( isset( $this->_aFieldNames['oxactive'] ) ) {
00840             $sQ = " $sTable.oxactive = 1 ";
00841         }
00842 
00843         // has 'activefrom'/'activeto' fields ?
00844         if ( isset( $this->_aFieldNames['oxactivefrom'] ) && isset( $this->_aFieldNames['oxactiveto'] ) ) {
00845 
00846             $sDate = date( 'Y-m-d H:i:s', oxUtilsDate::getInstance()->getTime() );
00847 
00848             $sQ = $sQ?" $sQ or ":'';
00849             $sQ = " ( $sQ ( $sTable.oxactivefrom < '$sDate' and $sTable.oxactiveto > '$sDate' ) ) ";
00850         }
00851 
00852         return $sQ;
00853     }
00854 
00863     public function validate()
00864     {
00865         $this->_aErrors = array();
00866         foreach ($this->_aFieldNames as $fName => $iVal) {
00867 
00868             $fName = $this->_getFieldLongName($fName);
00869 
00870             if ( method_exists ( $this, "validate_$fName")) {
00871                 $validatorMethod = "validate_$fName";
00872                 if ( $error = $this->$validatorMethod()) {
00873                     $this->_aErrors[$fName] = $error;
00874                 }
00875             }
00876         }
00877         return !$this->hasErrors();
00878     }
00879 
00888     public function beforeUpdate( $sOXID = null )
00889     {
00890     }
00891 
00902     public function onChange( $iAction = null, $sOXID = null)
00903     {
00904     }
00905 
00906 
00914     public function hasErrors()
00915     {
00916         return count($this->_aErrors) > 0;
00917     }
00918 
00926     public function getErrors()
00927     {
00928         return $this->_aErrors;
00929     }
00930 
00940     public function getError( $sField)
00941     {
00942         if (isset($this->_aErrors[$sField])) {
00943             return $this->_aErrors[$sField];
00944         }
00945 
00946         //T2007-10-19
00947         //return array();
00948         return null;
00949     }
00950 
00960     public function getHtmlError( $sField)
00961     {
00962         if ( $error = $this->getError($sField) ) {
00963             return $error;
00964         }
00965     }
00966 
00972     public function setInList()
00973     {
00974         $this->_blIsInList = true;
00975     }
00976 
00982     protected function _isInList()
00983     {
00984         return $this->_blIsInList;
00985     }
00986 
00995     protected function _getObjectViewName( $sTable, $sShopID = null)
00996     {
00997         return getViewName( $sTable, -1, $sShopID);
00998     }
00999 
01000 
01011     protected function _getTableFields($sTable, $blReturnSimple = false)
01012     {
01013         $myUtils = oxUtils::getInstance();
01014 
01015         $sCacheKey   = $sTable . "_allfields_" . $blReturnSimple;
01016         $aMetaFields = $myUtils->fromFileCache( $sCacheKey );
01017 
01018         if ( $aMetaFields ) {
01019             return $aMetaFields;
01020         }
01021 
01022         $aMetaFields = oxDb::getInstance()->getTableDescription( $sTable );
01023 
01024         if ( !$blReturnSimple ) {
01025             $myUtils->toFileCache( $sCacheKey, $aMetaFields );
01026             return $aMetaFields;
01027         }
01028 
01029         //returning simple array
01030         $aRet = array();
01031         if (is_array($aMetaFields)) {
01032             foreach ( $aMetaFields as $oVal ) {
01033                 $aRet[strtolower( $oVal->name )] = 0;
01034             }
01035         }
01036 
01037         $myUtils->toFileCache( $sCacheKey, $aRet);
01038 
01039         return $aRet;
01040     }
01041 
01053     protected function _getAllFields($blReturnSimple = false)
01054     {
01055         if (!$this->_sCoreTable) {
01056             return array();
01057         }
01058         return $this->_getTableFields($this->_sCoreTable, $blReturnSimple);
01059     }
01060 
01069     protected function _initDataStructure($blForceFullStructure = false)
01070     {
01071         $myUtils = oxUtils::getInstance();
01072 
01073         //get field names from cache
01074         $aFieldNames = null;
01075         $sFullCacheKey = 'fieldnames_' .$this->_sCoreTable . "_" . $this->_sCacheKey;
01076         if ($this->_sCacheKey && !$this->_isDisabledFieldCache()) {
01077             $aFieldNames = $myUtils->fromFileCache($sFullCacheKey);
01078         }
01079 
01080         if (!$aFieldNames) {
01081             $aFieldNames = $this->_getNonCachedFieldNames($blForceFullStructure);
01082             if ($this->_sCacheKey && !$this->_isDisabledFieldCache()) {
01083                 $myUtils->toFileCache($sFullCacheKey, $aFieldNames);
01084             }
01085         }
01086 
01087         if ( $aFieldNames !== false ) {
01088             foreach ( $aFieldNames as $sField => $sStatus ) {
01089                 $this->_addField($sField, $sStatus);
01090             }
01091         }
01092     }
01093 
01105     protected function _getNonCachedFieldNames($blForceFullStructure = false)
01106     {
01107         //T2008-02-22
01108         //so if this method is executed on cached version we see it when profiling
01109         startProfile("!__CACHABLE__!");
01110 
01111         //case 1. (admin)
01112         if ($this->isAdmin()) {
01113             $aMetaFields = $this->_getAllFields();
01114             foreach ( $aMetaFields as $oField ) {
01115                 if ( $oField->max_length == -1 ) {
01116                     $oField->max_length = 10;      // double or float
01117                 }
01118 
01119                 if ( $oField->type == "datetime" ) {
01120                     $oField->max_length = 20;
01121                 }
01122 
01123                 $this->_addField( $oField->name, $this->_getFieldStatus($oField->name), $oField->type, $oField->max_length );
01124             }
01125             stopProfile("!__CACHABLE__!");
01126             return false;
01127         }
01128 
01129         //case 2. (just get all fields)
01130         if ( $blForceFullStructure || !$this->_blUseLazyLoading ) {
01131             $aMetaFields = $this->_getAllFields(true);
01132             /*
01133             foreach ( $aMetaFields as $sFieldName => $sVal) {
01134                 $this->_addField( $sFieldName, $this->_getFieldStatus($sFieldName));
01135             }*/
01136             stopProfile("!__CACHABLE__!");
01137             return $aMetaFields;
01138         }
01139 
01140         //case 3. (get only oxid field, so we can fetch the rest of the fields over lazy loading mechanism)
01141         stopProfile("!__CACHABLE__!");
01142         return array("oxid" => 0);
01143     }
01144 
01153     protected function _getFieldStatus( $sFieldName )
01154     {
01155         return 0;
01156     }
01157 
01168     protected function _addField($sName, $iStatus, $sType = null, $sLength = null)
01169     {
01170         //preparation
01171         $sName = strtolower( $sName );
01172 
01173         //adding field names element
01174         $this->_aFieldNames[$sName] = $iStatus;
01175 
01176         //allready set?
01177         $sLongName = $this->_getFieldLongName($sName);
01178         if ( isset($this->$sLongName) ) {
01179             return;
01180         }
01181 
01182         //defining the field
01183         $oField = false;
01184 
01185         if ( isset( $sType ) ) {
01186             $oField = new oxField();
01187             $oField->fldtype = $sType;
01188             //T2008-01-29
01189             //can't clone as the fields are objects and are not fully cloned
01190             $this->_blIsSimplyClonable = false;
01191         }
01192 
01193         if ( isset( $sLength ) ) {
01194             if ( !$oField ) {
01195                 $oField = new oxField();
01196             }
01197             $oField->fldmax_length = $sLength;
01198             $this->_blIsSimplyClonable = false;
01199         }
01200 
01201         $this->$sLongName = $oField;
01202     }
01203 
01211     protected function _getFieldLongName( $sFieldName )
01212     {
01213         //trying to avoid strpos call as often as possible
01214         if ( $sFieldName[2] == $this->_sCoreTable[2] && strpos( $sFieldName, $this->_sCoreTable . "__" ) === 0 ) {
01215             return $sFieldName;
01216         }
01217 
01218         return $this->_sCoreTable . "__" . strtolower( $sFieldName );
01219     }
01220 
01230     protected function _setFieldData( $sFieldName, $sValue, $iDataType = oxField::T_TEXT)
01231     {
01232 
01233         $sLongFieldName = $this->_getFieldLongName( $sFieldName);
01234         //$sLongFieldName = $this->_sCoreTable . "__" . strtolower($sFieldName);
01235 
01236         //T2008-03-14
01237         //doing this because in lazy loaded lists on first load it is harmful to have initilised fields but not yet set
01238         //situation: only first article is loaded fully for "select oxid from oxarticles"
01239         /*
01240         if ($this->_blUseLazyLoading && !isset($this->$sLongFieldName))
01241             return;*/
01242 
01243         //in non lazy loading case we just add a field and do not care about it more
01244         if (!$this->_blUseLazyLoading && !isset($this->$sLongFieldName)) {
01245             $aFields = $this->_getAllFields(true);
01246             if ( isset( $aFields[strtolower($sFieldName)] ) ) {
01247                 $this->_addField($sFieldName, $this->_getFieldStatus($sFieldName));
01248             }
01249         }
01250         // if we have a double field we replace "," with "." in case somebody enters it in european format
01251         if (isset($this->$sLongFieldName) && isset($this->$sLongFieldName->fldtype) && $this->$sLongFieldName->fldtype == "double") {
01252             $sValue = str_replace( ",", ".", $sValue );
01253         }
01254 
01255         // isset is REQUIRED here not to use getter
01256         if (isset($this->$sLongFieldName) && is_object($this->$sLongFieldName)) {
01257             $this->$sLongFieldName->setValue($sValue, $iDataType);
01258         } else {
01259             $this->$sLongFieldName = new oxField($sValue, $iDataType);
01260         }
01261 
01262     }
01263 
01271     protected function _canFieldBeNull( $sFieldName )
01272     {
01273         $aMetaData = $this->_getAllFields();
01274         foreach ( $aMetaData as $oMetaInfo ) {
01275             if ( strcasecmp( $oMetaInfo->name, $sFieldName ) == 0 ) {
01276                 return !$oMetaInfo->not_null;
01277             }
01278         }
01279         return false;
01280     }
01281 
01282 
01290     protected function _getFieldDefaultValue( $sFieldName )
01291     {
01292         $aMetaData = $this->_getAllFields();
01293         foreach ( $aMetaData as $oMetaInfo ) {
01294             if ( strcasecmp( $oMetaInfo->name, $sFieldName ) == 0 ) {
01295                 return $oMetaInfo->default_value;
01296             }
01297         }
01298         return false;
01299     }
01300 
01301 
01310     protected function _getUpdateFieldValue( $sFieldName, $oField )
01311     {
01312         $mValue = null;
01313         if ( $oField instanceof oxField ) {
01314             $mValue = $oField->getRawValue();
01315         } elseif ( isset( $oField->value ) ) {
01316             $mValue = $oField->value;
01317         }
01318 
01319         $oDb = oxDb::getDb();
01320         //Check if this field value is null AND it can be null according if not returning default value
01321         if ( ( null === $mValue ) ) {
01322             if ( $this->_canFieldBeNull( $sFieldName ) ) {
01323                 return 'null';
01324             } elseif ( $mValue = $this->_getFieldDefaultValue( $sFieldName ) ) {
01325                 return $oDb->quote( $mValue );
01326             }
01327         }
01328 
01329         return $oDb->quote( $mValue );
01330     }
01331 
01340     protected function _getUpdateFields( $blUseSkipSaveFields = true )
01341     {
01342         $sSql = '';
01343         $blSep  = false;
01344 
01345         foreach (array_keys($this->_aFieldNames) as $sKey) {
01346             $sLongName = $this->_getFieldLongName($sKey);
01347             $oField = $this->$sLongName;
01348 
01349 
01350             if ( !$blUseSkipSaveFields || ($blUseSkipSaveFields && !in_array(strtolower($sKey), $this->_aSkipSaveFields)) ) {
01351                 $sSql .= (( $blSep) ? ',':'' ).$sKey." = ".$this->_getUpdateFieldValue($sKey, $oField);
01352                 $blSep = true;
01353             }
01354         }
01355 
01356         return $sSql;
01357     }
01358 
01368     protected function _update()
01369     {
01370         //do not allow derived item update
01371         if ( !$this->allowDerivedUpdate() ) {
01372             return false;
01373         }
01374 
01375 
01376         if ( !$this->getId() ) {
01377             $oEx = oxNew( 'oxObjectException' );
01378             $oEx->setMessage( 'EXCEPTION_OBJECT_OXIDNOTSET' );
01379             $oEx->setObject($this);
01380             throw $oEx;
01381         }
01382 
01383         $sIDKey = oxUtils::getInstance()->getArrFldName( $this->_sCoreTable.".oxid");
01384         $this->$sIDKey = new oxField($this->getId(), oxField::T_RAW);
01385         $oDb = oxDb::getDb();
01386 
01387         $sUpdate= "update {$this->_sCoreTable} set ".$this->_getUpdateFields()
01388                  ." where {$this->_sCoreTable}.oxid = ".$oDb->quote( $this->getId() );
01389 
01390         //trigger event
01391         $this->beforeUpdate();
01392 
01393         $blRet = (bool) $oDb->execute( $sUpdate);
01394         $this->_rebuildCache();
01395 
01396         return $blRet;
01397     }
01398 
01406     protected function _insert()
01407     {
01408 
01409         $oDb      = oxDb::getDb( oxDb::FETCH_MODE_ASSOC );
01410         $myConfig = $this->getConfig();
01411         $myUtils  = oxUtils::getInstance();
01412 
01413         // let's get a new ID
01414         if ( !$this->getId()) {
01415             $this->setId();
01416         }
01417 
01418         $sIDKey = $myUtils->getArrFldName( $this->_sCoreTable.".oxid");
01419         $this->$sIDKey = new oxField($this->getId(), oxField::T_RAW);
01420         $sInsert= "Insert into {$this->_sCoreTable} set ";
01421 
01422         //setting oxshopid
01423         $sShopField = $myUtils->getArrFldName($this->_sCoreTable.".oxshopid");
01424 
01425         if (isset($this->$sShopField) && !$this->$sShopField->value) {
01426             $this->$sShopField = new oxField($myConfig->getShopId(), oxField::T_RAW);
01427         }
01428 
01429 
01430         $sInsert .= $this->_getUpdateFields( false );
01431         $blRet = (bool) $oDb->execute( $sInsert);
01432 
01433         $this->_rebuildCache();
01434 
01435         return $blRet;
01436 
01437     }
01438 
01444     protected function _rebuildCache()
01445     {
01446         if ( !$this->_blIsNewCache) {
01447             oxUtils::getInstance()->rebuildCache();
01448             $this->_blIsNewCache = true;
01449         }
01450     }
01451 
01463     protected function _setRecordNumber( $sMaxField, $aWhere = null, $iMaxTryCnt = 5 )
01464     {
01465         // filtering
01466         $sWhere = "";
01467         if ( is_array( $aWhere ) && count( $aWhere ) > 0) {
01468             $sWhere = implode(" and ", $aWhere).' and ';
01469         }
01470         $oDb = oxDb::getDb( oxDb::FETCH_MODE_ASSOC );
01471 
01472         // SQL to set record number
01473         $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 = ".$oDb->quote( $this->getId() );
01474 
01475         // SQL to check record number dublicates
01476         //this should not happen normally but we prefer to take extra care in this case due to parallel script execution etc.
01477         $sMaxSelect = "select $sMaxField from ".$this->getViewName()." where oxid=".$oDb->quote( $this->getId() );
01478         $sCheck = "select count(oxid) from ".$this->getViewName()." where $sMaxField = ($sMaxSelect) and $sWhere 1 ";
01479 
01480         do {
01481             if ( $oDb->execute( $sUpdate ) === false ) {
01482                 return false;
01483             }
01484 
01485             $iChkCnt = $oDb->getOne( $sCheck );
01486         } while ( ( $iChkCnt > 1 ) && $iMaxTryCnt-- );
01487 
01488         $sFieldName = $this->getViewName().'__'.$sMaxField;
01489         $this->$sFieldName = new oxField( $oDb->getOne( $sMaxSelect ), oxField::T_RAW);//int value
01490 
01491         return ( $iChkCnt == 1 );
01492     }
01493 
01500     protected function _isDisabledFieldCache()
01501     {
01502         $sClass = get_class($this);
01503         if (isset(self::$_blDisableFieldCaching[$sClass]) && self::$_blDisableFieldCaching[$sClass]) {
01504             return true;
01505         }
01506 
01507         return false;
01508     }
01509 
01515     public function isOx()
01516     {
01517         $sOxId = $this->getId();
01518         if ( $sOxId[0] == 'o' && $sOxId[1] == 'x' ) {
01519             return true;
01520         }
01521         return false;
01522     }
01523 
01529     public function isReadOnly()
01530     {
01531         return $this->_blReadOnly;
01532     }
01533 
01541     public function setReadOnly( $blReadOnly )
01542     {
01543         $this->_blReadOnly = $blReadOnly;
01544     }
01545 
01551     public function getFieldNames()
01552     {
01553         return array_keys( $this->_aFieldNames );
01554     }
01555 
01563     public function addFieldName( $sName )
01564     {
01565         //preparation
01566         $sName = strtolower( $sName );
01567         $this->_aFieldNames[$sName] = 0;
01568     }
01569 
01570 
01576     public function getLanguage()
01577     {
01578         return -1;
01579     }
01580 }