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 $_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         // set active shop
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         //do not cache for admin?
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         // implementing lazy loading fields
00217         // This part of the code is slow and normally is called before field cache is built.
00218         // Make sure it is not called after first page is loaded and cache data is fully built.
00219         if ( $this->_blUseLazyLoading && strpos( $sName, $this->_sCoreTable . "__" ) === 0 ) {
00220 
00221             //lazy load it
00222             $sFieldName = str_replace($this->_sCoreTable . "__", '', $sName);
00223             //$sFieldName = $this->getSqlFieldName($sFieldName);
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                     //$sValue = $oDb->getOne( $sQ );
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                     //save names to cache for next loading
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                 //do not use field cache for this page
00260                 //as if we use it for lists then objects are loaded empty instead of lazy lodaing.
00261                 self::$_blDisableFieldCaching[get_class($this)] = true;
00262             }
00263 
00264             /*
00265             //save names to cache for next loading
00266             if ($this->_sCacheKey) {
00267                 $sCacheKey = 'fieldnames_' . $this->_sCoreTable . "_" . $this->_sCacheKey;
00268                 $aFieldNames = oxUtils::getInstance()->fromFileCache($sCacheKey);
00269                 $aFieldNames[$sFieldName] = $iFieldStatus;
00270                 oxUtils::getInstance()->toFileCache($sCacheKey, $aFieldNames);
00271             }*/
00272 
00273             oxUtilsObject::getInstance()->resetInstanceCache(get_class($this));
00274         }
00275 
00276         //returns oxStdClass implementing __toString() method due to uknown scenario where this var should be used.
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         //do not use views?
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             // patch for IIS
00376             //TODO: test it on IIS do we still need it
00377             //if( is_array($value) && count( $value) == 1)
00378             //    $value = current( $value);
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         if( !isset($oxID)){
00547             $oEx = oxNew('oxObjectException','core');
00548             $oEx->setMessage('EXCEPTION_OBJECT_OXIDNOTSET');
00549             $oEx->setObject($this);
00550             throw $oEx;
00551         }*/
00552 
00553         //getting at least one field before lazy loading the object
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         // add active shop
00594 
00595         //echo( "<br><br>$sSelect <br><br>\n\n\n");
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             //do not allow derived deletion
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         // #739A - should be executed here because of date/time formatting feature
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             //do not allow derived update
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         $oDB    = oxDb::getDb(true);
00778         $sSelect= "select oxid from {$this->getViewName()} where {$this->_sExistKey} = ".$oDB->quote($sOXID);
00779         return ( bool ) $oDB->getOne( $sSelect );
00780     }
00781 
00789     public function getSqlFieldName($sField)
00790     {
00791         return $sField;
00792     }
00793 
00801     public function getSqlActiveSnippet( $blForceCoreTable = false )
00802     {
00803         $sQ = '';
00804             $sTable = $this->getCoreTableName();
00805 
00806         // has 'active' field ?
00807         if ( isset( $this->_aFieldNames['oxactive'] ) ) {
00808             $sQ = " $sTable.oxactive = 1 ";
00809         }
00810 
00811         // has 'activefrom'/'activeto' fields ?
00812         if ( isset( $this->_aFieldNames['oxactivefrom'] ) && isset( $this->_aFieldNames['oxactiveto'] ) ) {
00813 
00814             $sDate = date( 'Y-m-d H:i:s', oxUtilsDate::getInstance()->getTime() );
00815 
00816             $sQ = $sQ?" $sQ or ":'';
00817             $sQ = " ( $sQ ( $sTable.oxactivefrom < '$sDate' and $sTable.oxactiveto > '$sDate' ) ) ";
00818         }
00819 
00820         return $sQ;
00821     }
00822 
00829     public function validate()
00830     {
00831         $this->_aErrors = array();
00832         foreach ($this->_aFieldNames as $fName => $iVal) {
00833 
00834             $fName = $this->_getFieldLongName($fName);
00835 
00836             if ( method_exists ( $this, "validate_$fName")) {
00837                 $validatorMethod = "validate_$fName";
00838                 if ( $error = $this->$validatorMethod()) {
00839                     $this->_aErrors[$fName] = $error;
00840                 }
00841             }
00842         }
00843         return !$this->hasErrors();
00844     }
00845 
00854     public function beforeUpdate( $sOXID = null )
00855     {
00856     }
00857 
00868     public function onChange( $iAction = null, $sOXID = null)
00869     {
00870     }
00871 
00872 
00878     public function hasErrors()
00879     {
00880         return count($this->_aErrors) > 0;
00881     }
00882 
00888     public function getErrors()
00889     {
00890         return $this->_aErrors;
00891     }
00892 
00900     public function getError( $sField)
00901     {
00902         if (isset($this->_aErrors[$sField])) {
00903             return $this->_aErrors[$sField];
00904         }
00905 
00906         //T2007-10-19
00907         //return array();
00908         return null;
00909     }
00910 
00918     public function getHtmlError( $sField)
00919     {
00920         if ( $error = $this->getError($sField) ) {
00921             return $error;
00922         }
00923     }
00924 
00930     public function setInList()
00931     {
00932         $this->_blIsInList = true;
00933     }
00934 
00940     protected function _isInList()
00941     {
00942         return $this->_blIsInList;
00943     }
00944 
00953     protected function _getObjectViewName( $sTable, $sShopID = null)
00954     {
00955             return $sTable;
00956 
00957     }
00958 
00959 
00969     protected function _getAllFields($blReturnSimple = false)
00970     {
00971         $myUtils = oxUtils::getInstance();
00972 
00973         if (!$this->_sCoreTable) {
00974             return array();
00975         }
00976 
00977         //T2008-09-04
00978         //The cache here saves almost 7% of execution time
00979         //But it seems that oxeec_tbdsc_oxarticles.txt and oxeec_oxarticles_allfields.txt tmp files are identical.
00980         //TODO: Check what's up with that.
00981         $sCacheKey   = $this->_sCoreTable . "_allfields_" . $blReturnSimple;
00982         $aMetaFields = $myUtils->fromFileCache( $sCacheKey );
00983 
00984         if ($aMetaFields) {
00985             return $aMetaFields;
00986         }
00987 
00988         $aMetaFields = oxDb::getInstance()->getTableDescription( $this->_sCoreTable );
00989 
00990         if ( !$blReturnSimple ) {
00991             $myUtils->toFileCache( $sCacheKey, $aMetaFields );
00992             return $aMetaFields;
00993         }
00994 
00995         //returning simple array
00996         $aRet = array();
00997         if (is_array($aMetaFields)) {
00998             foreach ( $aMetaFields as $oVal ) {
00999                 $aRet[strtolower( $oVal->name )] = 0;
01000             }
01001         }
01002 
01003         $myUtils->toFileCache( $sCacheKey, $aRet);
01004 
01005         return $aRet;
01006     }
01007 
01016     protected function _initDataStructure($blForceFullStructure = false)
01017     {
01018         $myUtils = oxUtils::getInstance();
01019 
01020         //get field names from cache
01021         $aFieldNames = null;
01022         $sFullCacheKey = 'fieldnames_' .$this->_sCoreTable . "_" . $this->_sCacheKey;
01023         if ($this->_sCacheKey && !$this->_isDisabledFieldCache()) {
01024             $aFieldNames = $myUtils->fromFileCache($sFullCacheKey);
01025         }
01026 
01027         if (!$aFieldNames) {
01028             $aFieldNames = $this->_getNonCachedFieldNames($blForceFullStructure);
01029             if ($this->_sCacheKey && !$this->_isDisabledFieldCache()) {
01030                 $myUtils->toFileCache($sFullCacheKey, $aFieldNames);
01031             }
01032         }
01033 
01034         if ( $aFieldNames !== false ) {
01035             foreach ( $aFieldNames as $sField => $sStatus ) {
01036                 $this->_addField($sField, $sStatus);
01037             }
01038         }
01039     }
01040 
01052     protected function _getNonCachedFieldNames($blForceFullStructure = false)
01053     {
01054         //T2008-02-22
01055         //so if this method is executed on cached version we see it when profiling
01056         startProfile("!__CACHABLE__!");
01057 
01058         //case 1. (admin)
01059         if ($this->isAdmin()) {
01060             $aMetaFields = $this->_getAllFields();
01061             foreach ( $aMetaFields as $oField ) {
01062                 if ( $oField->max_length == -1 ) {
01063                     $oField->max_length = 10;      // double or float
01064                 }
01065 
01066                 if ( $oField->type == "datetime" ) {
01067                     $oField->max_length = 20;
01068                 }
01069 
01070                 $this->_addField( $oField->name, $this->_getFieldStatus($oField->name), $oField->type, $oField->max_length );
01071             }
01072             stopProfile("!__CACHABLE__!");
01073             return false;
01074         }
01075 
01076         //case 2. (just get all fields)
01077         if ( $blForceFullStructure || !$this->_blUseLazyLoading ) {
01078             $aMetaFields = $this->_getAllFields(true);
01079             /*
01080             foreach ( $aMetaFields as $sFieldName => $sVal) {
01081                 $this->_addField( $sFieldName, $this->_getFieldStatus($sFieldName));
01082             }*/
01083             stopProfile("!__CACHABLE__!");
01084             return $aMetaFields;
01085         }
01086 
01087         //case 3. (get only oxid field, so we can fetch the rest of the fields over lazy loading mechanism)
01088         stopProfile("!__CACHABLE__!");
01089         return array("oxid" => 0);
01090     }
01091 
01100     protected function _getFieldStatus($sFieldName)
01101     {
01102         return 0;
01103     }
01104 
01115     protected function _addField($sName, $iStatus, $sType = null, $sLength = null)
01116     {
01117         //preparation
01118         $sName = strtolower($sName);
01119 
01120         //adding field names element
01121         $this->_aFieldNames[$sName] = $iStatus;
01122 
01123         //allready set?
01124         $sLongName = $this->_getFieldLongName($sName);
01125         if ( isset($this->$sLongName) ) {
01126             return;
01127         }
01128 
01129         //defining the field
01130         $oField = false;
01131 
01132         if (isset($sType)) {
01133             $oField = new oxField();
01134             $oField->fldtype = $sType;
01135             //T2008-01-29
01136             //can't clone as the fields are objects and are not fully cloned
01137             $this->_blIsSimplyClonable = false;
01138         }
01139 
01140         if (isset($sLength)) {
01141             if (!$oField) {
01142                 $oField = new oxField();
01143             }
01144             $oField->fldmax_length = $sLength;
01145             $this->_blIsSimplyClonable = false;
01146         }
01147 
01148         $this->$sLongName = $oField;
01149     }
01150 
01158     protected function _getFieldLongName( $sFieldName)
01159     {
01160         //trying to avoid strpos call as often as possible
01161         if ($sFieldName[2] == $this->_sCoreTable[2] && strpos($sFieldName, $this->_sCoreTable."__") === 0) {
01162             return $sFieldName;
01163         }
01164 
01165         $sLongName = $this->_sCoreTable."__".strtolower( $sFieldName);
01166         return $sLongName;
01167     }
01168 
01178     protected function _setFieldData( $sFieldName, $sValue, $iDataType = oxField::T_TEXT)
01179     {
01180 
01181 
01182         $sLongFieldName = $this->_getFieldLongName( $sFieldName);
01183         //$sLongFieldName = $this->_sCoreTable . "__" . strtolower($sFieldName);
01184 
01185         //T2008-03-14
01186         //doing this because in lazy loaded lists on first load it is harmful to have initilised fields but not yet set
01187         //situation: only first article is loaded fully for "select oxid from oxarticles"
01188         /*
01189         if ($this->_blUseLazyLoading && !isset($this->$sLongFieldName))
01190             return;*/
01191 
01192         //in non lazy loading case we just add a field and do not care about it more
01193         if (!$this->_blUseLazyLoading && !isset($this->$sLongFieldName)) {
01194             $aFields = $this->_getAllFields(true);
01195             if ( isset( $aFields[strtolower($sFieldName)] ) ) {
01196                 $this->_addField($sFieldName, $this->_getFieldStatus($sFieldName));
01197             }
01198         }
01199         // if we have a double field we replace "," with "." in case somebody enters it in european format
01200         if (isset($this->$sLongFieldName) && isset($this->$sLongFieldName->fldtype) && $this->$sLongFieldName->fldtype == "double") {
01201             $sValue = str_replace( ",", ".", $sValue );
01202         }
01203 
01204         // isset is REQUIRED here not to use getter
01205         if (isset($this->$sLongFieldName) && is_object($this->$sLongFieldName)) {
01206             $this->$sLongFieldName->setValue($sValue, $iDataType);
01207         } else {
01208             $this->$sLongFieldName = new oxField($sValue, $iDataType);
01209         }
01210 
01211     }
01212 
01221     protected function _getUpdateFieldValue($sFieldName, $oField)
01222     {
01223         $blPassNullValue = false;
01224         if ($oField instanceof oxField) {
01225             $value = $oField->getRawValue();
01226         } else {
01227             $value = $oField->value;
01228         }
01229         // R. check if this field value is null AND it can be null according to table description
01230         if (null === $value) {
01231             $aMetaData = $this->_getAllFields();
01232             foreach ($aMetaData as $oMetaInfo) {
01233                 if (!strcasecmp($oMetaInfo->name, $sFieldName)) {
01234                     $blPassNullValue = !$oMetaInfo->not_null;
01235                     break;
01236                 }
01237             }
01238         }
01239         if ($blPassNullValue) {
01240             return 'null';
01241         } else {
01242             return oxDb::getDb()->quote( $value );
01243         }
01244     }
01245 
01254     protected function _getUpdateFields( $blUseSkipSaveFields = true )
01255     {
01256         $sSql = '';
01257         $blSep  = false;
01258         foreach (array_keys($this->_aFieldNames) as $sKey) {
01259             $sLongName = $this->_getFieldLongName($sKey);
01260             $oField = $this->$sLongName;
01261 
01262 
01263             if ( !$blUseSkipSaveFields || ($blUseSkipSaveFields && !in_array(strtolower($sKey), $this->_aSkipSaveFields)) ) {
01264                 $sKey = $this->getSqlFieldName($sKey);
01265                 $sSql .= (( $blSep) ? ',':'' ).$sKey." = ".$this->_getUpdateFieldValue($sKey, $oField);
01266                 $blSep = true;
01267             }
01268         }
01269 
01270         return $sSql;
01271     }
01272 
01282     protected function _update()
01283     {
01284         //do not allow derived item update
01285         if ( !$this->allowDerivedUpdate() ) {
01286             return false;
01287         }
01288 
01289 
01290         if ( !$this->getId() ) {
01291             $oEx = oxNew( 'oxObjectException' );
01292             $oEx->setMessage( 'EXCEPTION_OBJECT_OXIDNOTSET' );
01293             $oEx->setObject($this);
01294             throw $oEx;
01295         }
01296 
01297         $sIDKey = oxUtils::getInstance()->getArrFldName( $this->_sCoreTable.".oxid");
01298         $this->$sIDKey = new oxField($this->getId(), oxField::T_RAW);
01299 
01300         $sUpdate= "update {$this->_sCoreTable} set ".$this->_getUpdateFields()
01301                  ." where {$this->_sCoreTable}.oxid = '".$this->getId()."' ";
01302 
01303         //echo         $sUpdate."\n\n\n";
01304 
01305         //trigger event
01306         $this->beforeUpdate();
01307 
01308         $blRet = (bool) oxDB::getDb()->execute( $sUpdate);
01309         $this->_rebuildCache();
01310 
01311         return $blRet;
01312     }
01313 
01321     protected function _insert()
01322     {
01323 
01324         $oDB      = oxDb::getDb(true);
01325         $myConfig = $this->getConfig();
01326         $myUtils  = oxUtils::getInstance();
01327 
01328         // let's get a new ID
01329         if ( !$this->getId()) {
01330             $this->setId();
01331         }
01332 
01333         $sIDKey = $myUtils->getArrFldName( $this->_sCoreTable.".oxid");
01334         $this->$sIDKey = new oxField($this->getId(), oxField::T_RAW);
01335         $sInsert= "Insert into {$this->_sCoreTable} set ";
01336 
01337         //setting oxshopid
01338         $sShopField = $myUtils->getArrFldName($this->_sCoreTable.".oxshopid");
01339         if (isset($this->$sShopField) && !$this->$sShopField->value)
01340             $this->$sShopField = new oxField($myConfig->getShopId(), oxField::T_RAW);
01341         $sInsert .= $this->_getUpdateFields( false );
01342 
01343         $blRet = (bool) $oDB->execute( $sInsert);
01344 
01345         $this->_rebuildCache();
01346 
01347         return $blRet;
01348 
01349     }
01350 
01356     protected function _rebuildCache()
01357     {
01358         if ( !$this->_blIsNewCache) {
01359             oxUtils::getInstance()->rebuildCache();
01360             $this->_blIsNewCache = true;
01361         }
01362     }
01363 
01373     protected function _setRecordNumber( $sMaxField, $aWhere = null, $iMaxTryCnt = 5 )
01374     {
01375         // filtering
01376         $sWhere = "";
01377         if ( is_array( $aWhere ) && count( $aWhere ) > 0) {
01378             $sWhere = implode(" and ", $aWhere).' and ';
01379         }
01380 
01381         // SQL to set record number
01382         $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()."'";
01383 
01384         // SQL to check record number dublicates
01385         //this should not happen normally but we prefer to take extra care in this case due to parallel script execution etc.
01386         $sMaxSelect = "select $sMaxField from ".$this->getViewName()." where oxid='".$this->getId()."' ";
01387         $sCheck = "select count(oxid) from ".$this->getViewName()." where $sMaxField = ($sMaxSelect) and $sWhere 1 ";
01388 
01389         do {
01390             if ( oxDb::getDb(true)->Execute( $sUpdate ) === false ) {
01391                 return false;
01392             }
01393 
01394             $iChkCnt = oxDb::getDb(true)->GetOne( $sCheck );
01395         } while ( ( $iChkCnt > 1 ) && $iMaxTryCnt-- );
01396 
01397         $sFieldName = $this->getViewName().'__'.$sMaxField;
01398         $this->$sFieldName = new oxField(oxDb::getDb(true)->GetOne( $sMaxSelect ), oxField::T_RAW);//int value
01399 
01400         return ( $iChkCnt == 1 );
01401     }
01402 
01409     protected function _isDisabledFieldCache()
01410     {
01411         $sClass = get_class($this);
01412         if (isset(self::$_blDisableFieldCaching[$sClass]) && self::$_blDisableFieldCaching[$sClass]) {
01413             return true;
01414         }
01415 
01416         return false;
01417     }
01418 
01424     public function isOx()
01425     {
01426         $sOxId = $this->getId();
01427         if ( $sOxId[0] == 'o' && $sOxId[1] == 'x' ) {
01428             return true;
01429         }
01430         return false;
01431     }
01432 
01438     public function isReadOnly()
01439     {
01440         return $this->_blReadOnly;
01441     }
01442 
01450     public function setReadOnly( $blReadOnly )
01451     {
01452         $this->_blReadOnly = $blReadOnly;
01453     }
01454 
01455 }