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             $iFieldStatus = $this->_getFieldStatus($sFieldName);
00224 
00225 
00226             if ( $this->getId() ) {
00227                 $oDb = oxDb::getDb();
00228                 $sQ = "select $sFieldName from " . $this->getViewName() . " where oxid = " . $oDb->quote($this->getId());
00229 
00230                 try {
00231                     //$sValue = $oDb->getOne( $sQ );
00232                     $rs = $oDb->execute( $sQ );
00233                     if ( $rs === false ) {
00234                         return null;
00235                     }
00236 
00237                     $this->_addField($sFieldName, $iFieldStatus);
00238                     $sValue = $rs->fields[0];
00239                     $this->_setFieldData( $sFieldName, $sValue );
00240 
00241                     //save names to cache for next loading
00242                     if ($this->_sCacheKey) {
00243                         $myUtils = oxUtils::getInstance();
00244                         $sCacheKey = 'fieldnames_' . $this->_sCoreTable . "_" . $this->_sCacheKey;
00245                         $aFieldNames = $myUtils->fromFileCache($sCacheKey);
00246                         $aFieldNames[$sFieldName] = $iFieldStatus;
00247                         $myUtils->toFileCache($sCacheKey, $aFieldNames);
00248                     }
00249 
00250                 } catch ( Exception $e ) {
00251                     return null;
00252                 }
00253 
00254                 //do not use field cache for this page
00255                 //as if we use it for lists then objects are loaded empty instead of lazy loading.
00256                 self::$_blDisableFieldCaching[get_class($this)] = true;
00257             }
00258 
00259             /*
00260             //save names to cache for next loading
00261             if ($this->_sCacheKey) {
00262                 $sCacheKey = 'fieldnames_' . $this->_sCoreTable . "_" . $this->_sCacheKey;
00263                 $aFieldNames = oxUtils::getInstance()->fromFileCache($sCacheKey);
00264                 $aFieldNames[$sFieldName] = $iFieldStatus;
00265                 oxUtils::getInstance()->toFileCache($sCacheKey, $aFieldNames);
00266             }*/
00267 
00268             oxUtilsObject::getInstance()->resetInstanceCache(get_class($this));
00269         }
00270 
00271         //returns oxStdClass implementing __toString() method due to uknown scenario where this var should be used.
00272         if (!isset( $this->$sName ) ) {
00273             $this->$sName = null;
00274         }
00275 
00276         return $this->$sName;
00277     }
00278 
00286     public function __isset($mVar)
00287     {
00288         return isset($this->$mVar);
00289     }
00290 
00296     public function __clone()
00297     {
00298         if (!$this->_blIsSimplyClonable) {
00299             foreach ( $this->_aFieldNames as $sField => $sVal ) {
00300                 $sLongName = $this->_getFieldLongName( $sField );
00301                 if ( is_object($this->$sLongName)) {
00302                     $this->$sLongName = clone $this->$sLongName;
00303                 }
00304             }
00305         }
00306     }
00307 
00315     public function oxClone($oObject)
00316     {
00317         $aClasVars = get_object_vars( $oObject);
00318         while (list($name, $value) = each($aClasVars)) {
00319             if ( is_object( $oObject->$name ) ) {
00320                 $this->$name = clone $oObject->$name;
00321             } else {
00322                 $this->$name = $oObject->$name;
00323             }
00324         }
00325     }
00326 
00335     public function init( $sTableName = null, $blForceAllFields = false)
00336     {
00337 
00338         if ( !$sTableName ) {
00339             $sTableName = $this->_sCoreTable;
00340         } else {
00341             $this->_sCoreTable = $sTableName;
00342         }
00343 
00344         // reset view table
00345         $this->_sViewTable = false;
00346 
00347         if ( count( $this->_aFieldNames ) <= 1 ) {
00348             $this->_initDataStructure( $blForceAllFields );
00349         }
00350     }
00351 
00359     public function assign( $dbRecord )
00360     {
00361         if ( !is_array( $dbRecord ) ) {
00362             return;
00363         }
00364 
00365 
00366         reset($dbRecord );
00367         while ( list( $sName, $sValue ) = each( $dbRecord ) ) {
00368 
00369             // patch for IIS
00370             //TODO: test it on IIS do we still need it
00371             //if( is_array($value) && count( $value) == 1)
00372             //    $value = current( $value);
00373 
00374             $this->_setFieldData( $sName, $sValue );
00375         }
00376 
00377         $sOxidField = $this->_getFieldLongName( 'oxid' );
00378         $this->_sOXID = $this->$sOxidField->value;
00379 
00380     }
00381 
00387     public function getClassName()
00388     {
00389         return $this->_sClassName;
00390     }
00391 
00397     public function getCoreTableName()
00398     {
00399         return $this->_sCoreTable;
00400     }
00401 
00407     public function getId()
00408     {
00409         return $this->_sOXID;
00410     }
00411 
00419     public function setId($sOXID = null)
00420     {
00421         if ( $sOXID ) {
00422             $this->_sOXID = $sOXID;
00423         } else {
00424             $this->_sOXID = oxUtilsObject::getInstance()->generateUID();
00425         }
00426 
00427         $sIdVarName = $this->_sCoreTable . "__oxid";
00428         $this->$sIdVarName = new oxField($this->_sOXID, oxField::T_RAW);
00429 
00430         return $this->_sOXID;
00431     }
00432 
00440     public function setShopId($iShopId)
00441     {
00442         $this->_iShopId = $iShopId;
00443     }
00444 
00450     public function getShopId()
00451     {
00452         return $this->_iShopId;
00453     }
00454 
00462     public function getViewName($blForceCoreTableUsage = null)
00463     {
00464         if (!$this->_sViewTable || ($blForceCoreTableUsage !== null)) {
00465             if ( ($blForceCoreTableUsage !== null)?$blForceCoreTableUsage:$this->_blForceCoreTableUsage ) {
00466                 $iShopId = -1;
00467             } else {
00468                 $iShopId = oxConfig::getInstance()->getShopId();
00469             }
00470             $sViewName = getViewName( $this->_sCoreTable, -1, $iShopId);
00471             if ($blForceCoreTableUsage !== null) {
00472                 return $sViewName;
00473             }
00474             $this->_sViewTable = $sViewName;
00475         }
00476         return $this->_sViewTable;
00477     }
00478 
00487     public function modifyCacheKey( $sCacheKey, $blOverride = false )
00488     {
00489         if ( $blOverride ) {
00490             $this->_sCacheKey = $sCacheKey;
00491         } else {
00492             $this->_sCacheKey .= $sCacheKey;
00493         }
00494     }
00495 
00501     public function disableLazyLoading()
00502     {
00503         $this->_blUseLazyLoading = false;
00504         $this->_initDataStructure(true);
00505     }
00506 
00507 
00513     public function isDerived()
00514     {
00515 
00516         return $this->_blIsDerived;
00517     }
00518 
00526     public function setIsDerived($blVal)
00527     {
00528         $this->_blIsDerived = $blVal;
00529     }
00530 
00537     public function isMultilang()
00538     {
00539         return false;
00540     }
00541 
00551     public function load( $sOXID)
00552     {
00553         /*
00554         if( !isset($oxID)){
00555             $oEx = oxNew('oxObjectException','core');
00556             $oEx->setMessage('EXCEPTION_OBJECT_OXIDNOTSET');
00557             $oEx->setObject($this);
00558             throw $oEx;
00559         }*/
00560 
00561         //getting at least one field before lazy loading the object
00562         $this->_addField('oxid', 0);
00563         $sSelect = $this->buildSelectString( array( $this->getViewName().".oxid" => $sOXID));
00564 
00565         return $this->_isLoaded = $this->assignRecord( $sSelect );
00566     }
00567 
00573     public function isLoaded()
00574     {
00575         return $this->_isLoaded;
00576     }
00577 
00585     public function buildSelectString( $aWhere = null)
00586     {
00587         $oDB = oxDb::getDb(true);
00588         $myUtils = oxUtils::getInstance();
00589 
00590         $sGet = $this->getSelectFields();
00591         $sSelect = "select $sGet from " . $this->getViewName() . " where 1 ";
00592 
00593         if ( $aWhere) {
00594             reset($aWhere);
00595             while (list($name, $value) = each($aWhere)) {
00596                 $sSelect .=  " and " . $name.' = '.$oDB->quote($value);
00597             }
00598         }
00599 
00600         // add active shop
00601 
00602         return $sSelect;
00603     }
00604 
00612     public function assignRecord( $sSelect)
00613     {
00614         $blRet = false;
00615 
00616         $oDB = oxDb::getDb(true);
00617 
00618         $rs = $oDB->execute( $sSelect);
00619         if ($rs != false && $rs->recordCount() > 0) {
00620             $blRet = true;
00621             $this->assign( $rs->fields);
00622         }
00623 
00624         return $blRet;
00625     }
00626 
00634     public function getFieldData( $sFieldName)
00635     {
00636         $sLongFieldName = $this->_getFieldLongName($sFieldName);
00637         return $this->$sLongFieldName->value;
00638     }
00639 
00645     public function getSelectFields()
00646     {
00647         $aSelectFields = array();
00648 
00649         $sViewName = $this->getViewName();
00650 
00651         foreach ( $this->_aFieldNames as $sKey => $sField ) {
00652             $aSelectFields[] = $sViewName . '.' . $sKey;
00653         }
00654 
00655         $sSelectFields = join( ", ", $aSelectFields );
00656         return $sSelectFields;
00657     }
00658 
00666     public function delete( $sOXID = null)
00667     {
00668         if ( !$sOXID ) {
00669             $sOXID = $this->getId();
00670 
00671             //do not allow derived deletion
00672             if ( !$this->allowDerivedDelete() ) {
00673                 return false;
00674             }
00675         }
00676 
00677         if ( !$sOXID ) {
00678             return false;
00679         }
00680 
00681 
00682         $oDB = oxDb::getDb(true);
00683         $sDelete = "delete from $this->_sCoreTable where oxid = ".$oDB->quote( $sOXID );
00684         $rs = $oDB->execute( $sDelete );
00685         if ( $blDelete = ( bool ) $oDB->affected_Rows() ) {
00686             $this->onChange(ACTION_DELETE, $sOXID);
00687         }
00688 
00689         return $blDelete;
00690     }
00691 
00692 
00698     public function save()
00699     {
00700         if ( !is_array( $this->_aFieldNames ) ) {
00701             return false;
00702         }
00703 
00704         $blRet = false;
00705 
00706         // #739A - should be executed here because of date/time formatting feature
00707         if ( $this->isAdmin() && !$this->getConfig()->getConfigParam( 'blSkipFormatConversion' ) ) {
00708             foreach ($this->_aFieldNames as $sName => $sVal) {
00709                 $sLongName = $this->_getFieldLongName($sName);
00710                 if ( isset($this->$sLongName->fldtype) && $this->$sLongName->fldtype == "datetime" ) {
00711                     oxDb::getInstance()->convertDBDateTime( $this->$sLongName, true );
00712                 } elseif ( isset($this->$sLongName->fldtype) && $this->$sLongName->fldtype == "timestamp" ) {
00713                     oxDb::getInstance()->convertDBTimestamp( $this->$sLongName, true);
00714                 } elseif ( isset($this->$sLongName->fldtype) && $this->$sLongName->fldtype == "date" ) {
00715                     oxDb::getInstance()->convertDBDate( $this->$sLongName, true);
00716                 }
00717             }
00718         }
00719         if ( $this->exists() ) {
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         $sViewName = $this->getCoreTableName();
00778         $oDB = oxDb::getDb( true );
00779         $sSelect= "select {$this->_sExistKey} from {$sViewName} where {$this->_sExistKey} = ".$oDB->quote( $sOXID );
00780 
00781         return ( bool ) $oDB->getOne( $sSelect );
00782     }
00783 
00791     public function getSqlActiveSnippet( $blForceCoreTable = null )
00792     {
00793         $sQ = '';
00794         $sTable = $this->getViewName($blForceCoreTable);
00795 
00796         // has 'active' field ?
00797         if ( isset( $this->_aFieldNames['oxactive'] ) ) {
00798             $sQ = " $sTable.oxactive = 1 ";
00799         }
00800 
00801         // has 'activefrom'/'activeto' fields ?
00802         if ( isset( $this->_aFieldNames['oxactivefrom'] ) && isset( $this->_aFieldNames['oxactiveto'] ) ) {
00803 
00804             $sDate = date( 'Y-m-d H:i:s', oxUtilsDate::getInstance()->getTime() );
00805 
00806             $sQ = $sQ?" $sQ or ":'';
00807             $sQ = " ( $sQ ( $sTable.oxactivefrom < '$sDate' and $sTable.oxactiveto > '$sDate' ) ) ";
00808         }
00809 
00810         return $sQ;
00811     }
00812 
00819     public function validate()
00820     {
00821         $this->_aErrors = array();
00822         foreach ($this->_aFieldNames as $fName => $iVal) {
00823 
00824             $fName = $this->_getFieldLongName($fName);
00825 
00826             if ( method_exists ( $this, "validate_$fName")) {
00827                 $validatorMethod = "validate_$fName";
00828                 if ( $error = $this->$validatorMethod()) {
00829                     $this->_aErrors[$fName] = $error;
00830                 }
00831             }
00832         }
00833         return !$this->hasErrors();
00834     }
00835 
00844     public function beforeUpdate( $sOXID = null )
00845     {
00846     }
00847 
00858     public function onChange( $iAction = null, $sOXID = null)
00859     {
00860     }
00861 
00862 
00868     public function hasErrors()
00869     {
00870         return count($this->_aErrors) > 0;
00871     }
00872 
00878     public function getErrors()
00879     {
00880         return $this->_aErrors;
00881     }
00882 
00890     public function getError( $sField)
00891     {
00892         if (isset($this->_aErrors[$sField])) {
00893             return $this->_aErrors[$sField];
00894         }
00895 
00896         //T2007-10-19
00897         //return array();
00898         return null;
00899     }
00900 
00908     public function getHtmlError( $sField)
00909     {
00910         if ( $error = $this->getError($sField) ) {
00911             return $error;
00912         }
00913     }
00914 
00920     public function setInList()
00921     {
00922         $this->_blIsInList = true;
00923     }
00924 
00930     protected function _isInList()
00931     {
00932         return $this->_blIsInList;
00933     }
00934 
00943     protected function _getObjectViewName( $sTable, $sShopID = null)
00944     {
00945         if ( $this->_blForceCoreTableUsage ) {
00946             $sShopID = -1;
00947         }
00948         return getViewName( $sTable, -1, $sShopID);
00949     }
00950 
00951 
00962     protected function _getTableFields($sTable, $blReturnSimple = false)
00963     {
00964         $myUtils = oxUtils::getInstance();
00965 
00966         $sCacheKey   = $sTable . "_allfields_" . $blReturnSimple;
00967         $aMetaFields = $myUtils->fromFileCache( $sCacheKey );
00968 
00969         if ($aMetaFields) {
00970             return $aMetaFields;
00971         }
00972 
00973         $aMetaFields = oxDb::getInstance()->getTableDescription( $sTable );
00974 
00975         if ( !$blReturnSimple ) {
00976             $myUtils->toFileCache( $sCacheKey, $aMetaFields );
00977             return $aMetaFields;
00978         }
00979 
00980         //returning simple array
00981         $aRet = array();
00982         if (is_array($aMetaFields)) {
00983             foreach ( $aMetaFields as $oVal ) {
00984                 $aRet[strtolower( $oVal->name )] = 0;
00985             }
00986         }
00987 
00988         $myUtils->toFileCache( $sCacheKey, $aRet);
00989 
00990         return $aRet;
00991     }
00992 
01004     protected function _getAllFields($blReturnSimple = false)
01005     {
01006         if (!$this->_sCoreTable) {
01007             return array();
01008         }
01009         return $this->_getTableFields($this->_sCoreTable, $blReturnSimple);
01010     }
01011 
01020     protected function _initDataStructure($blForceFullStructure = false)
01021     {
01022         $myUtils = oxUtils::getInstance();
01023 
01024         //get field names from cache
01025         $aFieldNames = null;
01026         $sFullCacheKey = 'fieldnames_' .$this->_sCoreTable . "_" . $this->_sCacheKey;
01027         if ($this->_sCacheKey && !$this->_isDisabledFieldCache()) {
01028             $aFieldNames = $myUtils->fromFileCache($sFullCacheKey);
01029         }
01030 
01031         if (!$aFieldNames) {
01032             $aFieldNames = $this->_getNonCachedFieldNames($blForceFullStructure);
01033             if ($this->_sCacheKey && !$this->_isDisabledFieldCache()) {
01034                 $myUtils->toFileCache($sFullCacheKey, $aFieldNames);
01035             }
01036         }
01037 
01038         if ( $aFieldNames !== false ) {
01039             foreach ( $aFieldNames as $sField => $sStatus ) {
01040                 $this->_addField($sField, $sStatus);
01041             }
01042         }
01043     }
01044 
01056     protected function _getNonCachedFieldNames($blForceFullStructure = false)
01057     {
01058         //T2008-02-22
01059         //so if this method is executed on cached version we see it when profiling
01060         startProfile("!__CACHABLE__!");
01061 
01062         //case 1. (admin)
01063         if ($this->isAdmin()) {
01064             $aMetaFields = $this->_getAllFields();
01065             foreach ( $aMetaFields as $oField ) {
01066                 if ( $oField->max_length == -1 ) {
01067                     $oField->max_length = 10;      // double or float
01068                 }
01069 
01070                 if ( $oField->type == "datetime" ) {
01071                     $oField->max_length = 20;
01072                 }
01073 
01074                 $this->_addField( $oField->name, $this->_getFieldStatus($oField->name), $oField->type, $oField->max_length );
01075             }
01076             stopProfile("!__CACHABLE__!");
01077             return false;
01078         }
01079 
01080         //case 2. (just get all fields)
01081         if ( $blForceFullStructure || !$this->_blUseLazyLoading ) {
01082             $aMetaFields = $this->_getAllFields(true);
01083             /*
01084             foreach ( $aMetaFields as $sFieldName => $sVal) {
01085                 $this->_addField( $sFieldName, $this->_getFieldStatus($sFieldName));
01086             }*/
01087             stopProfile("!__CACHABLE__!");
01088             return $aMetaFields;
01089         }
01090 
01091         //case 3. (get only oxid field, so we can fetch the rest of the fields over lazy loading mechanism)
01092         stopProfile("!__CACHABLE__!");
01093         return array("oxid" => 0);
01094     }
01095 
01104     protected function _getFieldStatus($sFieldName)
01105     {
01106         return 0;
01107     }
01108 
01119     protected function _addField($sName, $iStatus, $sType = null, $sLength = null)
01120     {
01121         //preparation
01122         $sName = strtolower($sName);
01123 
01124         //adding field names element
01125         $this->_aFieldNames[$sName] = $iStatus;
01126 
01127         //allready set?
01128         $sLongName = $this->_getFieldLongName($sName);
01129         if ( isset($this->$sLongName) ) {
01130             return;
01131         }
01132 
01133         //defining the field
01134         $oField = false;
01135 
01136         if (isset($sType)) {
01137             $oField = new oxField();
01138             $oField->fldtype = $sType;
01139             //T2008-01-29
01140             //can't clone as the fields are objects and are not fully cloned
01141             $this->_blIsSimplyClonable = false;
01142         }
01143 
01144         if (isset($sLength)) {
01145             if (!$oField) {
01146                 $oField = new oxField();
01147             }
01148             $oField->fldmax_length = $sLength;
01149             $this->_blIsSimplyClonable = false;
01150         }
01151 
01152         $this->$sLongName = $oField;
01153     }
01154 
01162     protected function _getFieldLongName( $sFieldName)
01163     {
01164         //trying to avoid strpos call as often as possible
01165         if ($sFieldName[2] == $this->_sCoreTable[2] && strpos($sFieldName, $this->_sCoreTable."__") === 0) {
01166             return $sFieldName;
01167         }
01168 
01169         return $this->_sCoreTable."__".strtolower( $sFieldName);
01170     }
01171 
01181     protected function _setFieldData( $sFieldName, $sValue, $iDataType = oxField::T_TEXT)
01182     {
01183 
01184 
01185         $sLongFieldName = $this->_getFieldLongName( $sFieldName);
01186         //$sLongFieldName = $this->_sCoreTable . "__" . strtolower($sFieldName);
01187 
01188         //T2008-03-14
01189         //doing this because in lazy loaded lists on first load it is harmful to have initilised fields but not yet set
01190         //situation: only first article is loaded fully for "select oxid from oxarticles"
01191         /*
01192         if ($this->_blUseLazyLoading && !isset($this->$sLongFieldName))
01193             return;*/
01194 
01195         //in non lazy loading case we just add a field and do not care about it more
01196         if (!$this->_blUseLazyLoading && !isset($this->$sLongFieldName)) {
01197             $aFields = $this->_getAllFields(true);
01198             if ( isset( $aFields[strtolower($sFieldName)] ) ) {
01199                 $this->_addField($sFieldName, $this->_getFieldStatus($sFieldName));
01200             }
01201         }
01202         // if we have a double field we replace "," with "." in case somebody enters it in european format
01203         if (isset($this->$sLongFieldName) && isset($this->$sLongFieldName->fldtype) && $this->$sLongFieldName->fldtype == "double") {
01204             $sValue = str_replace( ",", ".", $sValue );
01205         }
01206 
01207         // isset is REQUIRED here not to use getter
01208         if (isset($this->$sLongFieldName) && is_object($this->$sLongFieldName)) {
01209             $this->$sLongFieldName->setValue($sValue, $iDataType);
01210         } else {
01211             $this->$sLongFieldName = new oxField($sValue, $iDataType);
01212         }
01213 
01214     }
01215 
01223     protected function _canFieldBeNull($sFieldName)
01224     {
01225         $aMetaData = $this->_getAllFields();
01226         foreach ($aMetaData as $oMetaInfo) {
01227             if (!strcasecmp($oMetaInfo->name, $sFieldName)) {
01228                 return !$oMetaInfo->not_null;
01229             }
01230         }
01231         return false;
01232     }
01233 
01242     protected function _getUpdateFieldValue($sFieldName, $oField)
01243     {
01244         if ($oField instanceof oxField) {
01245             $value = $oField->getRawValue();
01246         } else {
01247             $value = $oField->value;
01248         }
01249         // Sarunas. check if this field value is null AND it can be null according to table description
01250         if ((null === $value) && $this->_canFieldBeNull($sFieldName)) {
01251             return 'null';
01252         }
01253         return oxDb::getDb()->quote( $value );
01254     }
01255 
01264     protected function _getUpdateFields( $blUseSkipSaveFields = true )
01265     {
01266         $sSql = '';
01267         $blSep  = false;
01268 
01269         foreach (array_keys($this->_aFieldNames) as $sKey) {
01270             $sLongName = $this->_getFieldLongName($sKey);
01271             $oField = $this->$sLongName;
01272 
01273 
01274             if ( !$blUseSkipSaveFields || ($blUseSkipSaveFields && !in_array(strtolower($sKey), $this->_aSkipSaveFields)) ) {
01275                 $sSql .= (( $blSep) ? ',':'' ).$sKey." = ".$this->_getUpdateFieldValue($sKey, $oField);
01276                 $blSep = true;
01277             }
01278         }
01279 
01280         return $sSql;
01281     }
01282 
01292     protected function _update()
01293     {
01294         //do not allow derived item update
01295         if ( !$this->allowDerivedUpdate() ) {
01296             return false;
01297         }
01298 
01299 
01300         if ( !$this->getId() ) {
01301             $oEx = oxNew( 'oxObjectException' );
01302             $oEx->setMessage( 'EXCEPTION_OBJECT_OXIDNOTSET' );
01303             $oEx->setObject($this);
01304             throw $oEx;
01305         }
01306 
01307         $sIDKey = oxUtils::getInstance()->getArrFldName( $this->_sCoreTable.".oxid");
01308         $this->$sIDKey = new oxField($this->getId(), oxField::T_RAW);
01309 
01310         $sUpdate= "update {$this->_sCoreTable} set ".$this->_getUpdateFields()
01311                  ." where {$this->_sCoreTable}.oxid = '".$this->getId()."' ";
01312 
01313         //trigger event
01314         $this->beforeUpdate();
01315 
01316         $blRet = (bool) oxDB::getDb()->execute( $sUpdate);
01317         $this->_rebuildCache();
01318 
01319         return $blRet;
01320     }
01321 
01329     protected function _insert()
01330     {
01331 
01332         $oDB      = oxDb::getDb(true);
01333         $myConfig = $this->getConfig();
01334         $myUtils  = oxUtils::getInstance();
01335 
01336         // let's get a new ID
01337         if ( !$this->getId()) {
01338             $this->setId();
01339         }
01340 
01341         $sIDKey = $myUtils->getArrFldName( $this->_sCoreTable.".oxid");
01342         $this->$sIDKey = new oxField($this->getId(), oxField::T_RAW);
01343         $sInsert= "Insert into {$this->_sCoreTable} set ";
01344 
01345         //setting oxshopid
01346         $sShopField = $myUtils->getArrFldName($this->_sCoreTable.".oxshopid");
01347         if (isset($this->$sShopField) && !$this->$sShopField->value)
01348             $this->$sShopField = new oxField($myConfig->getShopId(), oxField::T_RAW);
01349 
01350         $sInsert .= $this->_getUpdateFields( false );
01351         $blRet = (bool) $oDB->execute( $sInsert);
01352 
01353         $this->_rebuildCache();
01354 
01355         return $blRet;
01356 
01357     }
01358 
01364     protected function _rebuildCache()
01365     {
01366         if ( !$this->_blIsNewCache) {
01367             oxUtils::getInstance()->rebuildCache();
01368             $this->_blIsNewCache = true;
01369         }
01370     }
01371 
01381     protected function _setRecordNumber( $sMaxField, $aWhere = null, $iMaxTryCnt = 5 )
01382     {
01383         // filtering
01384         $sWhere = "";
01385         if ( is_array( $aWhere ) && count( $aWhere ) > 0) {
01386             $sWhere = implode(" and ", $aWhere).' and ';
01387         }
01388 
01389         // SQL to set record number
01390         $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()."'";
01391 
01392         // SQL to check record number dublicates
01393         //this should not happen normally but we prefer to take extra care in this case due to parallel script execution etc.
01394         $sMaxSelect = "select $sMaxField from ".$this->getViewName()." where oxid='".$this->getId()."' ";
01395         $sCheck = "select count(oxid) from ".$this->getViewName()." where $sMaxField = ($sMaxSelect) and $sWhere 1 ";
01396 
01397         do {
01398             if ( oxDb::getDb(true)->Execute( $sUpdate ) === false ) {
01399                 return false;
01400             }
01401 
01402             $iChkCnt = oxDb::getDb(true)->GetOne( $sCheck );
01403         } while ( ( $iChkCnt > 1 ) && $iMaxTryCnt-- );
01404 
01405         $sFieldName = $this->getViewName().'__'.$sMaxField;
01406         $this->$sFieldName = new oxField(oxDb::getDb(true)->GetOne( $sMaxSelect ), oxField::T_RAW);//int value
01407 
01408         return ( $iChkCnt == 1 );
01409     }
01410 
01417     protected function _isDisabledFieldCache()
01418     {
01419         $sClass = get_class($this);
01420         if (isset(self::$_blDisableFieldCaching[$sClass]) && self::$_blDisableFieldCaching[$sClass]) {
01421             return true;
01422         }
01423 
01424         return false;
01425     }
01426 
01432     public function isOx()
01433     {
01434         $sOxId = $this->getId();
01435         if ( $sOxId[0] == 'o' && $sOxId[1] == 'x' ) {
01436             return true;
01437         }
01438         return false;
01439     }
01440 
01446     public function isReadOnly()
01447     {
01448         return $this->_blReadOnly;
01449     }
01450 
01458     public function setReadOnly( $blReadOnly )
01459     {
01460         $this->_blReadOnly = $blReadOnly;
01461     }
01462 
01463 }