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 
00041     protected $_sClassName = 'oxbase';
00042 
00048     protected $_sCoreTable = null;
00049 
00054     protected $_sViewTable  = null;
00055 
00056 
00062     protected $_aFieldNames = array('oxid' => 0);
00063 
00069     protected $_sCacheKey = null;
00070 
00076     protected $_blUseLazyLoading = false;
00077 
00083     protected $_aSkipSaveFields = array('oxtimestamp');
00084 
00085 
00091     protected $_blUseSkipSaveFields = true;
00092 
00097     protected $_sExistKey = "oxid";
00098 
00105     protected $_blIsDerived = null;
00106 
00116     protected static $_blDisableFieldCaching = array();
00117 
00123     protected $_blIsSeoObject = false;
00124 
00130     protected $_blReadOnly = false;
00131 
00137     protected $_blIsInList = false;
00138 
00144     protected $_isLoaded = false;
00145 
00151     protected $_aInnerLazyCache = null;
00152 
00158     protected $_blEmployMultilanguage = false;
00159 
00160 
00166     public function getUseSkipSaveFields()
00167     {
00168         return $this->_blUseSkipSaveFields;
00169     }
00170 
00178     public function setUseSkipSaveFields( $blUseSkipSaveFields )
00179     {
00180         $this->_blUseSkipSaveFields = $blUseSkipSaveFields;
00181     }
00182 
00186     public function __construct()
00187     {
00188         // set active shop
00189         $myConfig = $this->getConfig();
00190         $this->_sCacheKey = $this->getViewName();
00191 
00192 
00193         if ( $this->_blUseLazyLoading ) {
00194             $this->_sCacheKey .= $myConfig->getActiveView()->getClassName();
00195         } else {
00196             $this->_sCacheKey .= "allviews";
00197         }
00198 
00199         //do not cache for admin?
00200         if ( $this->isAdmin() ) {
00201             $this->_sCacheKey = null;
00202         }
00203 
00204         $this->setShopId( $myConfig->getShopId() );
00205     }
00206 
00215     public function __set( $sName, $sValue )
00216     {
00217         $this->$sName = $sValue;
00218         if ( $this->_blUseLazyLoading && strpos( $sName, $this->_sCoreTable . "__" ) === 0 ) {
00219             $sFieldName = str_replace( $this->_sCoreTable . "__", '', $sName );
00220             if ( $sFieldName != 'oxnid' && ( !isset( $this->_aFieldNames[$sFieldName] ) || !$this->_aFieldNames[$sFieldName] ) ) {
00221                 $aAllFields = $this->_getAllFields(true);
00222                 if ( isset( $aAllFields[strtolower($sFieldName)] ) ) {
00223                     $iFieldStatus = $this->_getFieldStatus( $sFieldName );
00224                     $this->_addField( $sFieldName, $iFieldStatus );
00225                 }
00226             }
00227         }
00228     }
00229 
00237     public function __get( $sName )
00238     {
00239         switch ( $sName ) {
00240             case 'blIsDerived':
00241                 return $this->isDerived();
00242                 break;
00243             case 'sOXID':
00244                 return $this->getId();
00245                 break;
00246             case 'blReadOnly':
00247                 return $this->isReadOnly();
00248                 break;
00249         }
00250 
00251         // implementing lazy loading fields
00252         // This part of the code is slow and normally is called before field cache is built.
00253         // Make sure it is not called after first page is loaded and cache data is fully built.
00254         if ( $this->_blUseLazyLoading && stripos( $sName, $this->_sCoreTable . "__" ) === 0 ) {
00255 
00256             if ( $this->getId() ) {
00257 
00258                 //lazy load it
00259                 $sFieldName      = str_replace( $this->_sCoreTable . "__", '', $sName );
00260                 $sCacheFieldName = strtoupper( $sFieldName );
00261 
00262                 $iFieldStatus = $this->_getFieldStatus( $sFieldName );
00263                 $sViewName    = $this->getViewName();
00264                 $sId = $this->getId();
00265 
00266                 try {
00267                     if ( $this->_aInnerLazyCache === null ) {
00268 
00269                         $oDb = oxDb::getDb( oxDb::FETCH_MODE_ASSOC );
00270                         $sQ = "SELECT * FROM " . $sViewName . " WHERE `oxid` = " . $oDb->quote( $sId );
00271                         $rs = $oDb->select( $sQ );
00272                         if ( $rs && $rs->RecordCount() ) {
00273                             $this->_aInnerLazyCache = array_change_key_case( $rs->fields, CASE_UPPER );
00274                             if ( array_key_exists( $sCacheFieldName, $this->_aInnerLazyCache ) ) {
00275                                 $sFieldValue = $this->_aInnerLazyCache[$sCacheFieldName];
00276                             } else {
00277                                 return null;
00278                             }
00279                         } else {
00280                             return null;
00281                         }
00282                     } elseif ( array_key_exists( $sCacheFieldName, $this->_aInnerLazyCache ) ) {
00283                         $sFieldValue = $this->_aInnerLazyCache[$sCacheFieldName];
00284                     } else {
00285                         return null;
00286                     }
00287 
00288                     $this->_addField( $sFieldName, $iFieldStatus );
00289                     $this->_setFieldData( $sFieldName, $sFieldValue );
00290 
00291                     //save names to cache for next loading
00292                     if ($this->_sCacheKey) {
00293                         $myUtils = oxRegistry::getUtils();
00294                         $sCacheKey = 'fieldnames_' . $this->_sCoreTable . "_" . $this->_sCacheKey;
00295                         $aFieldNames = $myUtils->fromFileCache( $sCacheKey );
00296                         $aFieldNames[$sFieldName] = $iFieldStatus;
00297                         $myUtils->toFileCache( $sCacheKey, $aFieldNames );
00298                     }
00299                 } catch ( Exception $e ) {
00300                     return null;
00301                 }
00302 
00303                 //do not use field cache for this page
00304                 //as if we use it for lists then objects are loaded empty instead of lazy loading.
00305                 self::$_blDisableFieldCaching[get_class( $this )] = true;
00306             }
00307 
00308             oxUtilsObject::getInstance()->resetInstanceCache(get_class($this));
00309         }
00310 
00311         //returns stdClass implementing __toString() method due to uknown scenario where this var should be used.
00312         if (!isset( $this->$sName ) ) {
00313             $this->$sName = null;
00314         }
00315 
00316         return $this->$sName;
00317     }
00318 
00326     public function __isset($mVar)
00327     {
00328         return isset($this->$mVar);
00329     }
00330 
00336     public function __clone()
00337     {
00338         if (!$this->_blIsSimplyClonable) {
00339             foreach ( $this->_aFieldNames as $sField => $sVal ) {
00340                 $sLongName = $this->_getFieldLongName( $sField );
00341                 if ( is_object($this->$sLongName)) {
00342                     $this->$sLongName = clone $this->$sLongName;
00343                 }
00344             }
00345         }
00346     }
00347 
00355     public function oxClone($oObject)
00356     {
00357         $aClasVars = get_object_vars( $oObject);
00358         while (list($name, $value) = each($aClasVars)) {
00359             if ( is_object( $oObject->$name ) ) {
00360                 $this->$name = clone $oObject->$name;
00361             } else {
00362                 $this->$name = $oObject->$name;
00363             }
00364         }
00365     }
00366 
00375     public function init( $sTableName = null, $blForceAllFields = false)
00376     {
00377 
00378         if ( $sTableName ) {
00379             $this->_sCoreTable = $sTableName;
00380         }
00381 
00382         // reset view table
00383         $this->_sViewTable = false;
00384 
00385         if ( count( $this->_aFieldNames ) <= 1 ) {
00386             $this->_initDataStructure( $blForceAllFields );
00387         }
00388     }
00389 
00397     public function assign( $dbRecord )
00398     {
00399         if ( !is_array( $dbRecord ) ) {
00400             return;
00401         }
00402 
00403 
00404         reset($dbRecord );
00405         while ( list( $sName, $sValue ) = each( $dbRecord ) ) {
00406 
00407             // patch for IIS
00408             //TODO: test it on IIS do we still need it
00409             //if( is_array($value) && count( $value) == 1)
00410             //    $value = current( $value);
00411 
00412             $this->_setFieldData( $sName, $sValue );
00413         }
00414 
00415         $sOxidField = $this->_getFieldLongName( 'oxid' );
00416         $this->_sOXID = $this->$sOxidField->value;
00417 
00418     }
00419 
00425     public function getClassName()
00426     {
00427         return $this->_sClassName;
00428     }
00429 
00435     public function getCoreTableName()
00436     {
00437         return $this->_sCoreTable;
00438     }
00439 
00445     public function getId()
00446     {
00447         return $this->_sOXID;
00448     }
00449 
00457     public function setId($sOXID = null)
00458     {
00459         if ( $sOXID ) {
00460             $this->_sOXID = $sOXID;
00461         } else {
00462             $this->_sOXID = oxUtilsObject::getInstance()->generateUID();
00463         }
00464 
00465         $sIdVarName = $this->_sCoreTable . "__oxid";
00466         $this->$sIdVarName = new oxField($this->_sOXID, oxField::T_RAW);
00467 
00468         return $this->_sOXID;
00469     }
00470 
00478     public function setShopId($iShopId)
00479     {
00480         $this->_iShopId = $iShopId;
00481     }
00482 
00488     public function getShopId()
00489     {
00490         return $this->_iShopId;
00491     }
00492 
00500     public function getViewName($blForceCoreTableUsage = null)
00501     {
00502         if (!$this->_sViewTable || ($blForceCoreTableUsage !== null)) {
00503             if ( $blForceCoreTableUsage === true ) {
00504                 return $this->_sCoreTable;
00505             }
00506 
00507 
00508             if ( ( $blForceCoreTableUsage !== null ) && $blForceCoreTableUsage ) {
00509                 $iShopId = -1;
00510             } else {
00511                 $iShopId = oxRegistry::getConfig()->getShopId();
00512             }
00513 
00514 
00515             $sViewName = getViewName( $this->_sCoreTable, $this->_blEmployMultilanguage == false ? -1 : $this->getLanguage(), $iShopId );
00516             if ( $blForceCoreTableUsage !== null ) {
00517                 return $sViewName;
00518             }
00519             $this->_sViewTable = $sViewName;
00520         }
00521         return $this->_sViewTable;
00522     }
00523 
00532     public function modifyCacheKey( $sCacheKey, $blOverride = false )
00533     {
00534         if ( $blOverride ) {
00535             $this->_sCacheKey = $sCacheKey;
00536         } else {
00537             $this->_sCacheKey .= $sCacheKey;
00538         }
00539     }
00540 
00546     public function disableLazyLoading()
00547     {
00548         $this->_blUseLazyLoading = false;
00549         $this->_initDataStructure(true);
00550     }
00551 
00552 
00558     public function isDerived()
00559     {
00560 
00561         return $this->_blIsDerived;
00562     }
00563 
00571     public function setIsDerived($blVal)
00572     {
00573         $this->_blIsDerived = $blVal;
00574     }
00575 
00582     public function isMultilang()
00583     {
00584         return false;
00585     }
00586 
00596     public function load( $sOXID )
00597     {
00598         $blExistingOldForceCoreTable = $this->_blForceCoreTableUsage;
00599 
00600         $this->_blForceCoreTableUsage = true;
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         $this->_isLoaded = $this->assignRecord( $sSelect );
00606 
00607         $this->_blForceCoreTableUsage = $blExistingOldForceCoreTable;
00608 
00609         return $this->_isLoaded;
00610     }
00611 
00617     public function isLoaded()
00618     {
00619         return $this->_isLoaded;
00620     }
00621 
00629     public function buildSelectString( $aWhere = null)
00630     {
00631         $oDB = oxDb::getDb();
00632         $myUtils = oxRegistry::getUtils();
00633 
00634         $sGet = $this->getSelectFields();
00635         $sSelect = "select $sGet from " . $this->getViewName() . " where 1 ";
00636 
00637         if ( $aWhere) {
00638             reset($aWhere);
00639             while (list($name, $value) = each($aWhere)) {
00640                 $sSelect .=  " and " . $name.' = '.$oDB->quote($value);
00641             }
00642         }
00643 
00644         // add active shop
00645 
00646         return $sSelect;
00647     }
00648 
00656     public function assignRecord( $sSelect )
00657     {
00658         $blRet = false;
00659 
00660         $rs = oxDb::getDb( oxDb::FETCH_MODE_ASSOC )->select( $sSelect );
00661 
00662         if ($rs != false && $rs->recordCount() > 0) {
00663             $blRet = true;
00664             $this->assign( $rs->fields);
00665         }
00666 
00667         return $blRet;
00668     }
00669 
00677     public function getFieldData( $sFieldName )
00678     {
00679         $sLongFieldName = $this->_getFieldLongName( $sFieldName );
00680             return $this->$sLongFieldName->value;
00681     }
00682 
00690     public function getSelectFields( $blForceCoreTableUsage = null )
00691     {
00692         $aSelectFields = array();
00693 
00694         $sViewName = $this->getViewName( $blForceCoreTableUsage );
00695 
00696         foreach ( $this->_aFieldNames as $sKey => $sField ) {
00697             if ( $sViewName ) {
00698                 $aSelectFields[] = "`$sViewName`.`$sKey`";
00699             } else {
00700                 $aSelectFields[] = ".`$sKey`";
00701             }
00702 
00703         }
00704 
00705         $sSelectFields = join( ", ", $aSelectFields );
00706         return $sSelectFields;
00707     }
00708 
00716     public function delete( $sOXID = null)
00717     {
00718         if ( !$sOXID ) {
00719             $sOXID = $this->getId();
00720 
00721             //do not allow derived deletion
00722             if ( !$this->allowDerivedDelete() ) {
00723                 return false;
00724             }
00725         }
00726 
00727         if ( !$sOXID ) {
00728             return false;
00729         }
00730 
00731 
00732         $oDB = oxDb::getDb( oxDb::FETCH_MODE_ASSOC );
00733         $sDelete = "delete from $this->_sCoreTable where oxid = ".$oDB->quote( $sOXID );
00734         $rs = $oDB->execute( $sDelete );
00735         if ( $blDelete = ( bool ) $oDB->affected_Rows() ) {
00736             $this->onChange(ACTION_DELETE, $sOXID);
00737         }
00738 
00739         return $blDelete;
00740     }
00741 
00742 
00748     public function save()
00749     {
00750         if ( !is_array( $this->_aFieldNames ) ) {
00751             return false;
00752         }
00753 
00754         $blRet = false;
00755 
00756         // #739A - should be executed here because of date/time formatting feature
00757         if ( $this->isAdmin() && !$this->getConfig()->getConfigParam( 'blSkipFormatConversion' ) ) {
00758             foreach ($this->_aFieldNames as $sName => $sVal) {
00759                 $sLongName = $this->_getFieldLongName($sName);
00760                 if ( isset($this->$sLongName->fldtype) && $this->$sLongName->fldtype == "datetime" ) {
00761                     oxRegistry::get("oxUtilsDate")->convertDBDateTime( $this->$sLongName, true );
00762                 } elseif ( isset($this->$sLongName->fldtype) && $this->$sLongName->fldtype == "timestamp" ) {
00763                     oxRegistry::get("oxUtilsDate")->convertDBTimestamp( $this->$sLongName, true);
00764                 } elseif ( isset($this->$sLongName->fldtype) && $this->$sLongName->fldtype == "date" ) {
00765                     oxRegistry::get("oxUtilsDate")->convertDBDate( $this->$sLongName, true);
00766                 }
00767             }
00768         }
00769         if ( $this->exists() ) {
00770             //do not allow derived update
00771             if ( !$this->allowDerivedUpdate() ) {
00772                 return false;
00773             }
00774 
00775             $blRet = $this->_update();
00776             $sAction = ACTION_UPDATE;
00777         } else {
00778             $blRet = $this->_insert();
00779             $sAction = ACTION_INSERT;
00780         }
00781 
00782         $this->onChange($sAction);
00783 
00784         if ( $blRet ) {
00785             return $this->getId();
00786         } else {
00787             return false;
00788         }
00789     }
00790 
00796     public function allowDerivedUpdate()
00797     {
00798         return !$this->isDerived();
00799     }
00800 
00806     public function allowDerivedDelete()
00807     {
00808         return !$this->isDerived();
00809     }
00810 
00818     public function exists( $sOXID = null)
00819     {
00820         if ( !$sOXID ) {
00821             $sOXID = $this->getId();
00822         }
00823         if ( !$sOXID ) {
00824             return false;
00825         }
00826 
00827         $sViewName = $this->getCoreTableName();
00828         $oDb = oxDb::getDb( oxDb::FETCH_MODE_ASSOC );
00829         $sSelect= "select {$this->_sExistKey} from {$sViewName} where {$this->_sExistKey} = ".$oDb->quote( $sOXID );
00830 
00831         return ( bool ) $oDb->getOne( $sSelect, false, false );
00832     }
00833 
00841     public function getSqlActiveSnippet( $blForceCoreTable = null )
00842     {
00843         $sQ = '';
00844         $sTable = $this->getViewName($blForceCoreTable);
00845 
00846         // has 'active' field ?
00847         if ( isset( $this->_aFieldNames['oxactive'] ) ) {
00848             $sQ = " $sTable.oxactive = 1 ";
00849         }
00850 
00851         // has 'activefrom'/'activeto' fields ?
00852         if ( isset( $this->_aFieldNames['oxactivefrom'] ) && isset( $this->_aFieldNames['oxactiveto'] ) ) {
00853 
00854             $sDate = date( 'Y-m-d H:i:s', oxRegistry::get("oxUtilsDate")->getTime() );
00855 
00856             $sQ = $sQ?" $sQ or ":'';
00857             $sQ = " ( $sQ ( $sTable.oxactivefrom < '$sDate' and $sTable.oxactiveto > '$sDate' ) ) ";
00858         }
00859 
00860         return $sQ;
00861     }
00862 
00871     public function beforeUpdate( $sOXID = null )
00872     {
00873     }
00874 
00885     public function onChange( $iAction = null, $sOXID = null)
00886     {
00887     }
00888 
00889 
00895     public function setInList()
00896     {
00897         $this->_blIsInList = true;
00898     }
00899 
00905     protected function _isInList()
00906     {
00907         return $this->_blIsInList;
00908     }
00909 
00918     protected function _getObjectViewName( $sTable, $sShopID = null)
00919     {
00920         return getViewName( $sTable, -1, $sShopID);
00921     }
00922 
00923 
00934     protected function _getTableFields($sTable, $blReturnSimple = false)
00935     {
00936         $myUtils = oxRegistry::getUtils();
00937 
00938         $sCacheKey   = $sTable . "_allfields_" . $blReturnSimple;
00939         $aMetaFields = $myUtils->fromFileCache( $sCacheKey );
00940 
00941         if ( $aMetaFields ) {
00942             return $aMetaFields;
00943         }
00944 
00945         $aMetaFields = oxDb::getInstance()->getTableDescription( $sTable );
00946 
00947         if ( !$blReturnSimple ) {
00948             $myUtils->toFileCache( $sCacheKey, $aMetaFields );
00949             return $aMetaFields;
00950         }
00951 
00952         //returning simple array
00953         $aRet = array();
00954         if (is_array($aMetaFields)) {
00955             foreach ( $aMetaFields as $oVal ) {
00956                 $aRet[strtolower( $oVal->name )] = 0;
00957             }
00958         }
00959 
00960         $myUtils->toFileCache( $sCacheKey, $aRet);
00961 
00962         return $aRet;
00963     }
00964 
00976     protected function _getAllFields($blReturnSimple = false)
00977     {
00978         if (!$this->_sCoreTable) {
00979             return array();
00980         }
00981         return $this->_getTableFields($this->_sCoreTable, $blReturnSimple);
00982     }
00983 
00992     protected function _initDataStructure($blForceFullStructure = false)
00993     {
00994         $myUtils = oxRegistry::getUtils();
00995 
00996         //get field names from cache
00997         $aFieldNames = null;
00998         $sFullCacheKey = 'fieldnames_' .$this->_sCoreTable . "_" . $this->_sCacheKey;
00999         if ($this->_sCacheKey && !$this->_isDisabledFieldCache()) {
01000             $aFieldNames = $myUtils->fromFileCache($sFullCacheKey);
01001         }
01002 
01003         if (!$aFieldNames) {
01004             $aFieldNames = $this->_getNonCachedFieldNames($blForceFullStructure);
01005             if ($this->_sCacheKey && !$this->_isDisabledFieldCache()) {
01006                 $myUtils->toFileCache($sFullCacheKey, $aFieldNames);
01007             }
01008         }
01009 
01010         if ( $aFieldNames !== false ) {
01011             foreach ( $aFieldNames as $sField => $sStatus ) {
01012                 $this->_addField($sField, $sStatus);
01013             }
01014         }
01015     }
01016 
01028     protected function _getNonCachedFieldNames($blForceFullStructure = false)
01029     {
01030         //T2008-02-22
01031         //so if this method is executed on cached version we see it when profiling
01032         startProfile("!__CACHABLE__!");
01033 
01034         //case 1. (admin)
01035         if ($this->isAdmin()) {
01036             $aMetaFields = $this->_getAllFields();
01037             foreach ( $aMetaFields as $oField ) {
01038                 if ( $oField->max_length == -1 ) {
01039                     $oField->max_length = 10;      // double or float
01040                 }
01041 
01042                 if ( $oField->type == "datetime" ) {
01043                     $oField->max_length = 20;
01044                 }
01045 
01046                 $this->_addField( $oField->name, $this->_getFieldStatus($oField->name), $oField->type, $oField->max_length );
01047             }
01048             stopProfile("!__CACHABLE__!");
01049             return false;
01050         }
01051 
01052         //case 2. (just get all fields)
01053         if ( $blForceFullStructure || !$this->_blUseLazyLoading ) {
01054             $aMetaFields = $this->_getAllFields(true);
01055             /*
01056             foreach ( $aMetaFields as $sFieldName => $sVal) {
01057                 $this->_addField( $sFieldName, $this->_getFieldStatus($sFieldName));
01058             }*/
01059             stopProfile("!__CACHABLE__!");
01060             return $aMetaFields;
01061         }
01062 
01063         //case 3. (get only oxid field, so we can fetch the rest of the fields over lazy loading mechanism)
01064         stopProfile("!__CACHABLE__!");
01065         return array("oxid" => 0);
01066     }
01067 
01076     protected function _getFieldStatus( $sFieldName )
01077     {
01078         return 0;
01079     }
01080 
01091     protected function _addField($sName, $iStatus, $sType = null, $sLength = null)
01092     {
01093         //preparation
01094         $sName = strtolower( $sName );
01095 
01096         //adding field names element
01097         $this->_aFieldNames[$sName] = $iStatus;
01098 
01099         //already set?
01100         $sLongName = $this->_getFieldLongName($sName);
01101         if ( isset($this->$sLongName) ) {
01102             return;
01103         }
01104 
01105         //defining the field
01106         $oField = false;
01107 
01108         if ( isset( $sType ) ) {
01109             $oField = new oxField();
01110             $oField->fldtype = $sType;
01111             //T2008-01-29
01112             //can't clone as the fields are objects and are not fully cloned
01113             $this->_blIsSimplyClonable = false;
01114         }
01115 
01116         if ( isset( $sLength ) ) {
01117             if ( !$oField ) {
01118                 $oField = new oxField();
01119             }
01120             $oField->fldmax_length = $sLength;
01121             $this->_blIsSimplyClonable = false;
01122         }
01123 
01124         $this->$sLongName = $oField;
01125     }
01126 
01134     protected function _getFieldLongName( $sFieldName )
01135     {
01136         //trying to avoid strpos call as often as possible
01137         if ( $sFieldName[2] == $this->_sCoreTable[2] && strpos( $sFieldName, $this->_sCoreTable . "__" ) === 0 ) {
01138             return $sFieldName;
01139         }
01140 
01141         return $this->_sCoreTable . "__" . strtolower( $sFieldName );
01142     }
01143 
01153     protected function _setFieldData( $sFieldName, $sValue, $iDataType = oxField::T_TEXT)
01154     {
01155 
01156         $sLongFieldName = $this->_getFieldLongName( $sFieldName);
01157         //$sLongFieldName = $this->_sCoreTable . "__" . strtolower($sFieldName);
01158 
01159         //T2008-03-14
01160         //doing this because in lazy loaded lists on first load it is harmful to have initialised fields but not yet set
01161         //situation: only first article is loaded fully for "select oxid from oxarticles"
01162         /*
01163         if ($this->_blUseLazyLoading && !isset($this->$sLongFieldName))
01164             return;*/
01165 
01166         //in non lazy loading case we just add a field and do not care about it more
01167         if (!$this->_blUseLazyLoading && !isset($this->$sLongFieldName)) {
01168             $aFields = $this->_getAllFields(true);
01169             if ( isset( $aFields[strtolower($sFieldName)] ) ) {
01170                 $this->_addField($sFieldName, $this->_getFieldStatus($sFieldName));
01171             }
01172         }
01173         // if we have a double field we replace "," with "." in case somebody enters it in european format
01174         if (isset($this->$sLongFieldName) && isset($this->$sLongFieldName->fldtype) && $this->$sLongFieldName->fldtype == "double") {
01175             $sValue = str_replace( ",", ".", $sValue );
01176         }
01177 
01178         // isset is REQUIRED here not to use getter
01179         if (isset($this->$sLongFieldName) && is_object($this->$sLongFieldName)) {
01180             $this->$sLongFieldName->setValue($sValue, $iDataType);
01181         } else {
01182             $this->$sLongFieldName = new oxField($sValue, $iDataType);
01183         }
01184 
01185     }
01186 
01194     protected function _canFieldBeNull( $sFieldName )
01195     {
01196         $aMetaData = $this->_getAllFields();
01197         foreach ( $aMetaData as $oMetaInfo ) {
01198             if ( strcasecmp( $oMetaInfo->name, $sFieldName ) == 0 ) {
01199                 return !$oMetaInfo->not_null;
01200             }
01201         }
01202         return false;
01203     }
01204 
01205 
01213     protected function _getFieldDefaultValue( $sFieldName )
01214     {
01215         $aMetaData = $this->_getAllFields();
01216         foreach ( $aMetaData as $oMetaInfo ) {
01217             if ( strcasecmp( $oMetaInfo->name, $sFieldName ) == 0 ) {
01218                 return $oMetaInfo->default_value;
01219             }
01220         }
01221         return false;
01222     }
01223 
01224 
01233     protected function _getUpdateFieldValue( $sFieldName, $oField )
01234     {
01235         $mValue = null;
01236         if ( $oField instanceof oxField ) {
01237             $mValue = $oField->getRawValue();
01238         } elseif ( isset( $oField->value ) ) {
01239             $mValue = $oField->value;
01240         }
01241 
01242         $oDb = oxDb::getDb();
01243         //Check if this field value is null AND it can be null according if not returning default value
01244         if ( ( null === $mValue ) ) {
01245             if ( $this->_canFieldBeNull( $sFieldName ) ) {
01246                 return 'null';
01247             } elseif ( $mValue = $this->_getFieldDefaultValue( $sFieldName ) ) {
01248                 return $oDb->quote( $mValue );
01249             }
01250         }
01251 
01252         return $oDb->quote( $mValue );
01253     }
01254 
01263     protected function _getUpdateFields( $blUseSkipSaveFields = true )
01264     {
01265         $sSql = '';
01266         $blSep  = false;
01267 
01268         foreach (array_keys($this->_aFieldNames) as $sKey) {
01269             $sLongName = $this->_getFieldLongName($sKey);
01270             $oField = $this->$sLongName;
01271 
01272 
01273             if ( !$blUseSkipSaveFields || ($blUseSkipSaveFields && !in_array(strtolower($sKey), $this->_aSkipSaveFields)) ) {
01274                 $sSql .= (( $blSep) ? ',':'' ).$sKey." = ".$this->_getUpdateFieldValue($sKey, $oField);
01275                 $blSep = true;
01276             }
01277         }
01278 
01279         return $sSql;
01280     }
01281 
01291     protected function _update()
01292     {
01293         //do not allow derived item update
01294         if ( !$this->allowDerivedUpdate() ) {
01295             return false;
01296         }
01297 
01298 
01299         if ( !$this->getId() ) {
01300             $oEx = oxNew( 'oxObjectException' );
01301             $oEx->setMessage( 'EXCEPTION_OBJECT_OXIDNOTSET' );
01302             $oEx->setObject($this);
01303             throw $oEx;
01304         }
01305 
01306         $sIDKey = oxRegistry::getUtils()->getArrFldName( $this->_sCoreTable.".oxid");
01307         $this->$sIDKey = new oxField($this->getId(), oxField::T_RAW);
01308         $oDb = oxDb::getDb();
01309 
01310         $sUpdate= "update {$this->_sCoreTable} set ".$this->_getUpdateFields()
01311                  ." where {$this->_sCoreTable}.oxid = ".$oDb->quote( $this->getId() );
01312 
01313         //trigger event
01314         $this->beforeUpdate();
01315 
01316         $blRet = (bool) $oDb->execute( $sUpdate);
01317 
01318         return $blRet;
01319     }
01320 
01328     protected function _insert()
01329     {
01330 
01331         $oDb      = oxDb::getDb( oxDb::FETCH_MODE_ASSOC );
01332         $myConfig = $this->getConfig();
01333         $myUtils  = oxRegistry::getUtils();
01334 
01335         // let's get a new ID
01336         if ( !$this->getId()) {
01337             $this->setId();
01338         }
01339 
01340         $sIDKey = $myUtils->getArrFldName( $this->_sCoreTable.".oxid");
01341         $this->$sIDKey = new oxField($this->getId(), oxField::T_RAW);
01342         $sInsert= "Insert into {$this->_sCoreTable} set ";
01343 
01344         //setting oxshopid
01345         $sShopField = $myUtils->getArrFldName($this->_sCoreTable.".oxshopid");
01346 
01347         if (isset($this->$sShopField) && !$this->$sShopField->value) {
01348             $this->$sShopField = new oxField($myConfig->getShopId(), oxField::T_RAW);
01349         }
01350 
01351 
01352         $sInsert .= $this->_getUpdateFields( $this->getUseSkipSaveFields() );
01353 
01354         $blRet = (bool) $oDb->execute( $sInsert );
01355 
01356         return $blRet;
01357     }
01358 
01365     protected function _isDisabledFieldCache()
01366     {
01367         $sClass = get_class($this);
01368         if (isset(self::$_blDisableFieldCaching[$sClass]) && self::$_blDisableFieldCaching[$sClass]) {
01369             return true;
01370         }
01371 
01372         return false;
01373     }
01374 
01380     public function isOx()
01381     {
01382         $sOxId = $this->getId();
01383         if ( $sOxId[0] == 'o' && $sOxId[1] == 'x' ) {
01384             return true;
01385         }
01386         return false;
01387     }
01388 
01394     public function isReadOnly()
01395     {
01396         return $this->_blReadOnly;
01397     }
01398 
01406     public function setReadOnly( $blReadOnly )
01407     {
01408         $this->_blReadOnly = $blReadOnly;
01409     }
01410 
01416     public function getFieldNames()
01417     {
01418         return array_keys( $this->_aFieldNames );
01419     }
01420 
01428     public function addFieldName( $sName )
01429     {
01430         //preparation
01431         $sName = strtolower( $sName );
01432         $this->_aFieldNames[$sName] = 0;
01433     }
01434 
01435 
01441     public function getLanguage()
01442     {
01443         return -1;
01444     }
01445 
01446 }