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 
00146     public function __construct()
00147     {
00148         // set active shop
00149         $myConfig = $this->getConfig();
00150         $this->_sCacheKey = $this->getViewName();
00151         if ( $this->_blUseLazyLoading ) {
00152             $this->_sCacheKey .= $myConfig->getActiveView()->getClassName();
00153         } else {
00154             $this->_sCacheKey .= "allviews";
00155         }
00156 
00157         //do not cache for admin?
00158         if ( $this->isAdmin() ) {
00159             $this->_sCacheKey = null;
00160         }
00161 
00162         $this->setShopId( $myConfig->getShopId() );
00163     }
00164 
00173     public function __set($sName, $sValue)
00174     {
00175         $this->$sName = $sValue;
00176         if ( $this->_blUseLazyLoading && strpos( $sName, $this->_sCoreTable . "__" ) === 0 ) {
00177             $sFieldName = str_replace( $this->_sCoreTable . "__", '', $sName );
00178             if ($sFieldName != 'oxnid' && !$this->_aFieldNames[$sFieldName]) {
00179                 $aAllFields = $this->_getAllFields(true);
00180                 if (isset($aAllFields[strtolower($sFieldName)])) {
00181                     $iFieldStatus = $this->_getFieldStatus($sFieldName);
00182                     $this->_addField($sFieldName, $iFieldStatus);
00183                 }
00184             }
00185         }
00186     }
00187 
00195     public function __get( $sName )
00196     {
00197         switch ($sName) {
00198             case 'blIsDerived':
00199                 return $this->isDerived();
00200                 break;
00201             case 'sOXID':
00202                 return $this->getId();
00203                 break;
00204             case 'blReadOnly':
00205                 return $this->isReadOnly();
00206                 break;
00207         }
00208 
00209         // implementing lazy loading fields
00210         // This part of the code is slow and normally is called before field cache is built.
00211         // Make sure it is not called after first page is loaded and cache data is fully built.
00212         if ( $this->_blUseLazyLoading && strpos( $sName, $this->_sCoreTable . "__" ) === 0 ) {
00213 
00214             //lazy load it
00215             $sFieldName = str_replace($this->_sCoreTable . "__", '', $sName);
00216             //$sFieldName = $this->getSqlFieldName($sFieldName);
00217             $iFieldStatus = $this->_getFieldStatus($sFieldName);
00218 
00219             $sSqlFieldName = $sFieldName;
00220             if ($iFieldStatus && $this->isMultilang() ) {
00221                 $sSqlFieldName =  $sFieldName.oxLang::getInstance()->getLanguageTag( $this->getLanguage() );
00222             }
00223 
00224             if ( $this->getId() ) {
00225                 $oDb = oxDb::getDb();
00226                 $sQ = "select $sSqlFieldName from " . $this->_sCoreTable . " where oxid = " . $oDb->quote($this->getId());
00227 
00228                 try {
00229                     //$sValue = $oDb->getOne( $sQ );
00230                     $rs = $oDb->execute( $sQ );
00231                     if ( $rs === false ) {
00232                         return null;
00233                     }
00234 
00235                     $this->_addField($sFieldName, $iFieldStatus);
00236                     $sValue = $rs->fields[0];
00237                     $this->_setFieldData( $sFieldName, $sValue );
00238 
00239                     //save names to cache for next loading
00240                     if ($this->_sCacheKey) {
00241                         $myUtils = oxUtils::getInstance();
00242                         $sCacheKey = 'fieldnames_' . $this->_sCoreTable . "_" . $this->_sCacheKey;
00243                         $aFieldNames = $myUtils->fromFileCache($sCacheKey);
00244                         $aFieldNames[$sFieldName] = $iFieldStatus;
00245                         $myUtils->toFileCache($sCacheKey, $aFieldNames);
00246                     }
00247 
00248                 } catch ( Exception $e ) {
00249                     return null;
00250                 }
00251 
00252                 //do not use field cache for this page
00253                 //as if we use it for lists then objects are loaded empty instead of lazy lodaing.
00254                 self::$_blDisableFieldCaching[get_class($this)] = true;
00255             }
00256 
00257             /*
00258             //save names to cache for next loading
00259             if ($this->_sCacheKey) {
00260                 $sCacheKey = 'fieldnames_' . $this->_sCoreTable . "_" . $this->_sCacheKey;
00261                 $aFieldNames = oxUtils::getInstance()->fromFileCache($sCacheKey);
00262                 $aFieldNames[$sFieldName] = $iFieldStatus;
00263                 oxUtils::getInstance()->toFileCache($sCacheKey, $aFieldNames);
00264             }*/
00265 
00266             oxUtilsObject::getInstance()->resetInstanceCache(get_class($this));
00267         }
00268 
00269         //returns oxStdClass implementing __toString() method due to uknown scenario where this var should be used.
00270         if (!isset( $this->$sName ) ) {
00271             $this->$sName = null;
00272         }
00273 
00274         return $this->$sName;
00275     }
00276 
00284     public function __isset($mVar)
00285     {
00286         return isset($this->$mVar);
00287     }
00288 
00294     public function __clone()
00295     {
00296         if (!$this->_blIsSimplyClonable) {
00297             foreach ( $this->_aFieldNames as $sField => $sVal ) {
00298                 $sLongName = $this->_getFieldLongName( $sField );
00299                 if ( is_object($this->$sLongName)) {
00300                     $this->$sLongName = clone $this->$sLongName;
00301                 }
00302             }
00303         }
00304     }
00305 
00313     public function oxClone($oObject)
00314     {
00315         $aClass_vars = get_object_vars( $oObject);
00316         while (list($name, $value) = each($aClass_vars)) {
00317             if ( is_object( $oObject->$name ) ) {
00318                 $this->$name = clone $oObject->$name;
00319             } else {
00320                 $this->$name = $oObject->$name;
00321             }
00322         }
00323     }
00324 
00333     public function init( $sTableName = null, $blForceAllFields = false)
00334     {
00335 
00336         if ( !$sTableName ) {
00337             $sTableName = $this->_sCoreTable;
00338         } else {
00339             $this->_sCoreTable = $sTableName;
00340         }
00341 
00342         $this->_sViewTable = getViewName( $this->_sCoreTable );
00343 
00344         //do not use views?
00345 
00346         if ( count( $this->_aFieldNames ) <= 1 ) {
00347             $this->_initDataStructure( $blForceAllFields );
00348         }
00349     }
00350 
00358     public function assign( $dbRecord )
00359     {
00360         if ( !is_array( $dbRecord ) ) {
00361             return;
00362         }
00363 
00364 
00365         reset($dbRecord );
00366         while ( list( $sName, $sValue ) = each( $dbRecord ) ) {
00367 
00368             // patch for IIS
00369             //TODO: test it on IIS do we still need it
00370             //if( is_array($value) && count( $value) == 1)
00371             //    $value = current( $value);
00372 
00373             $this->_setFieldData( $sName, $sValue );
00374         }
00375 
00376         $sOxidField = $this->_getFieldLongName( 'oxid' );
00377         $this->_sOXID = $this->$sOxidField->value;
00378 
00379     }
00380 
00386     public function getClassName()
00387     {
00388         return $this->_sClassName;
00389     }
00390 
00396     public function getCoreTableName()
00397     {
00398         return $this->_sCoreTable;
00399     }
00400 
00406     public function getId()
00407     {
00408         return $this->_sOXID;
00409     }
00410 
00418     public function setId($sOXID = null)
00419     {
00420         if ( $sOXID ) {
00421             $this->_sOXID = $sOXID;
00422         } else {
00423             $this->_sOXID = oxUtilsObject::getInstance()->generateUID();
00424         }
00425 
00426         $sIdVarName = $this->_sCoreTable . "__oxid";
00427         $this->$sIdVarName = new oxField($this->_sOXID, oxField::T_RAW);
00428 
00429         return $this->_sOXID;
00430     }
00431 
00439     public function setShopId($iShopId)
00440     {
00441         $this->_iShopId = $iShopId;
00442     }
00443 
00449     public function getShopId()
00450     {
00451         return $this->_iShopId;
00452     }
00453 
00459     public function getViewName()
00460     {
00461         return $this->_sViewTable;
00462     }
00463 
00472     public function modifyCacheKey( $sCacheKey, $blOverride = false )
00473     {
00474         if ( $blOverride ) {
00475             $this->_sCacheKey = $sCacheKey;
00476         } else {
00477             $this->_sCacheKey .= $sCacheKey;
00478         }
00479     }
00480 
00486     public function disableLazyLoading()
00487     {
00488         $this->_blUseLazyLoading = false;
00489         $this->_initDataStructure(true);
00490     }
00491 
00492 
00498     public function isDerived()
00499     {
00500 
00501         return $this->_blIsDerived;
00502     }
00503 
00511     public function setIsDerived($blVal)
00512     {
00513         $this->_blIsDerived = $blVal;
00514     }
00515 
00522     public function isMultilang()
00523     {
00524         return false;
00525     }
00526 
00536     public function load( $sOXID)
00537     {
00538         /*
00539         if( !isset($oxID)){
00540             $oEx = oxNew('oxObjectException','core');
00541             $oEx->setMessage('EXCEPTION_OBJECT_OXIDNOTSET');
00542             $oEx->setObject($this);
00543             throw $oEx;
00544         }*/
00545 
00546         //getting at least one field before lazy loading the object
00547         $this->_addField('oxid', 0);
00548 
00549         $sSelect = $this->buildSelectString( array( $this->getViewName().".oxid" => $sOXID));
00550 
00551         return $this->assignRecord( $sSelect);
00552     }
00553 
00561     public function buildSelectString( $aWhere = null)
00562     {
00563         $oDB = oxDb::getDb(true);
00564         $myUtils = oxUtils::getInstance();
00565 
00566         $sGet = $this->getSelectFields();
00567         $sSelect = "select $sGet from " . $this->_sViewTable . " where 1 ";
00568 
00569         if ( $aWhere) {
00570             reset($aWhere);
00571             while (list($name, $value) = each($aWhere)) {
00572                 $sSelect .=  " and " . $name.' = '.$oDB->quote($value);
00573             }
00574         }
00575 
00576         // add active shop
00577 
00578         //echo( "<br><br>$sSelect <br><br>\n\n\n");
00579 
00580         return $sSelect;
00581     }
00582 
00590     public function assignRecord( $sSelect)
00591     {
00592         $blRet = false;
00593 
00594         $oDB = oxDb::getDb(true);
00595 
00596         $rs = $oDB->execute( $sSelect);
00597         if ($rs != false && $rs->recordCount() > 0) {
00598             $blRet = true;
00599             $this->assign( $rs->fields);
00600         }
00601 
00602         return $blRet;
00603     }
00604 
00612     public function getFieldData( $sFieldName)
00613     {
00614         $sLongFieldName = $this->_getFieldLongName($sFieldName);
00615         return $this->$sLongFieldName->value;
00616     }
00617 
00623     public function getSelectFields()
00624     {
00625         $aSelectFields = array();
00626 
00627         foreach ( $this->_aFieldNames as $sKey => $sField ) {
00628             $sRealField = $this->getSqlFieldName($sKey);
00629             $sSelectSufix = "";
00630             if ( $sRealField !== $sKey ) {
00631                 $sSelectSufix = " as $sKey";
00632             }
00633             $aSelectFields[] = $this->_sViewTable . '.' . $sRealField . $sSelectSufix;
00634         }
00635 
00636         $sSelectFields = join( ", ", $aSelectFields );
00637         return $sSelectFields;
00638     }
00639 
00647     public function delete( $sOXID = null)
00648     {
00649         if ( !$sOXID ) {
00650             $sOXID = $this->getId();
00651 
00652             //do not allow derived deletion
00653             if ( !$this->allowDerivedDelete() ) {
00654                 return false;
00655             }
00656         }
00657 
00658         if ( !$sOXID ) {
00659             return false;
00660         }
00661 
00662 
00663         $oDB = oxDb::getDb(true);
00664         $sDelete = "delete from $this->_sCoreTable where oxid = ".$oDB->quote( $sOXID );
00665         $rs = $oDB->execute( $sDelete );
00666         if ( $blDelete = ( bool ) $oDB->affected_Rows() ) {
00667             $this->onChange(ACTION_DELETE, $sOXID);
00668         }
00669 
00670         return $blDelete;
00671     }
00672 
00673 
00679     public function save()
00680     {
00681         if ( !is_array( $this->_aFieldNames ) ) {
00682             return false;
00683         }
00684 
00685         $blRet = false;
00686 
00687         // #739A - should be executed here because of date/time formatting feature
00688         if ( $this->isAdmin() && !$this->getConfig()->getConfigParam( 'blSkipFormatConversion' ) ) {
00689             foreach ($this->_aFieldNames as $sName => $sVal) {
00690                 $sLongName = $this->_getFieldLongName($sName);
00691                 if ( isset($this->$sLongName->fldtype) && $this->$sLongName->fldtype == "datetime" ) {
00692                     oxDb::getInstance()->convertDBDateTime( $this->$sLongName, true );
00693                 } elseif ( isset($this->$sLongName->fldtype) && $this->$sLongName->fldtype == "timestamp" ) {
00694                     oxDb::getInstance()->convertDBTimestamp( $this->$sLongName, true);
00695                 } elseif ( isset($this->$sLongName->fldtype) && $this->$sLongName->fldtype == "date" ) {
00696                     oxDb::getInstance()->convertDBDate( $this->$sLongName, true);
00697                 }
00698             }
00699         }
00700 
00701         if ( $this->exists() ) {
00702 
00703             //do not allow derived update
00704             if ( !$this->allowDerivedUpdate() ) {
00705                 return false;
00706             }
00707 
00708             $blRet = $this->_update();
00709             $sAction = ACTION_UPDATE;
00710         } else {
00711             $blRet = $this->_insert();
00712             $sAction = ACTION_INSERT;
00713         }
00714 
00715         $this->onChange($sAction);
00716 
00717         if ( $blRet ) {
00718             return $this->getId();
00719         } else {
00720             return false;
00721         }
00722     }
00723 
00729     public function allowDerivedUpdate()
00730     {
00731         return !$this->isDerived();
00732     }
00733 
00739     public function allowDerivedDelete()
00740     {
00741         return !$this->isDerived();
00742     }
00743 
00751     public function exists( $sOXID = null)
00752     {
00753         if ( !$sOXID ) {
00754             $sOXID = $this->getId();
00755         }
00756         if ( !$sOXID ) {
00757             return false;
00758         }
00759 
00760         $oDB    = oxDb::getDb(true);
00761         $sSelect= "select oxid from {$this->getViewName()} where {$this->_sExistKey} = ".$oDB->quote($sOXID);
00762         return ( bool ) $oDB->getOne( $sSelect );
00763     }
00764 
00772     public function getSqlFieldName($sField)
00773     {
00774         return $sField;
00775     }
00776 
00784     public function getSqlActiveSnippet( $blForceCoreTable = false )
00785     {
00786         $sQ = '';
00787             $sTable = $this->getCoreTableName();
00788 
00789         // has 'active' field ?
00790         if ( isset( $this->_aFieldNames['oxactive'] ) ) {
00791             $sQ = " $sTable.oxactive = 1 ";
00792         }
00793 
00794         // has 'activefrom'/'activeto' fields ?
00795         if ( isset( $this->_aFieldNames['oxactivefrom'] ) && isset( $this->_aFieldNames['oxactiveto'] ) ) {
00796 
00797             $sDate = date( 'Y-m-d H:i:s', oxUtilsDate::getInstance()->getTime() );
00798 
00799             $sQ = $sQ?" $sQ or ":'';
00800             $sQ = " ( $sQ ( $sTable.oxactivefrom < '$sDate' and $sTable.oxactiveto > '$sDate' ) ) ";
00801         }
00802 
00803         return $sQ;
00804     }
00805 
00812     public function validate()
00813     {
00814         $this->_aErrors = array();
00815         foreach ($this->_aFieldNames as $fName => $iVal) {
00816 
00817             $fName = $this->_getFieldLongName($fName);
00818 
00819             if ( method_exists ( $this, "validate_$fName")) {
00820                 $validatorMethod = "validate_$fName";
00821                 if ( $error = $this->$validatorMethod()) {
00822                     $this->_aErrors[$fName] = $error;
00823                 }
00824             }
00825         }
00826         return !$this->hasErrors();
00827     }
00828 
00837     public function beforeUpdate( $sOXID = null )
00838     {
00839     }
00840 
00851     public function onChange( $iAction = null, $sOXID = null)
00852     {
00853     }
00854 
00855 
00861     public function hasErrors()
00862     {
00863         return count($this->_aErrors) > 0;
00864     }
00865 
00871     public function getErrors()
00872     {
00873         return $this->_aErrors;
00874     }
00875 
00883     public function getError( $sField)
00884     {
00885         if (isset($this->_aErrors[$sField])) {
00886             return $this->_aErrors[$sField];
00887         }
00888 
00889         //T2007-10-19
00890         //return array();
00891         return null;
00892     }
00893 
00901     public function getHtmlError( $sField)
00902     {
00903         if ( $error = $this->getError($sField) ) {
00904             return $error;
00905         }
00906     }
00907 
00913     public function setInList()
00914     {
00915         $this->_blIsInList = true;
00916     }
00917 
00923     protected function _isInList()
00924     {
00925         return $this->_blIsInList;
00926     }
00927 
00936     protected function _getObjectViewName( $sTable, $sShopID = null)
00937     {
00938             return $sTable;
00939 
00940     }
00941 
00942 
00952     protected function _getAllFields($blReturnSimple = false)
00953     {
00954         $myUtils = oxUtils::getInstance();
00955 
00956         //T2008-09-04
00957         //The cache here saves almost 7% of execution time
00958         //But it seems that oxeec_tbdsc_oxarticles.txt and oxeec_oxarticles_allfields.txt tmp files are identical.
00959         //TODO: Check what's up with that.
00960         $sCacheKey   = $this->_sCoreTable . "_allfields_" . $blReturnSimple;
00961         $aMetaFields = $myUtils->fromFileCache( $sCacheKey );
00962 
00963         if ($aMetaFields) {
00964             return $aMetaFields;
00965         }
00966 
00967         $aMetaFields = oxDb::getInstance()->getTableDescription( $this->_sCoreTable );
00968 
00969         if ( !$blReturnSimple ) {
00970             $myUtils->toFileCache( $sCacheKey, $aMetaFields );
00971             return $aMetaFields;
00972         }
00973 
00974         //returning simple array
00975         $aRet = array();
00976         foreach ( $aMetaFields as $oVal ) {
00977             $aRet[strtolower( $oVal->name )] = 0;
00978         }
00979 
00980         $myUtils->toFileCache( $sCacheKey, $aRet);
00981 
00982         return $aRet;
00983     }
00984 
00993     protected function _initDataStructure($blForceFullStructure = false)
00994     {
00995         $myUtils = oxUtils::getInstance();
00996 
00997         //get field names from cache
00998         $aFieldNames = null;
00999         $sFullCacheKey = 'fieldnames_' .$this->_sCoreTable . "_" . $this->_sCacheKey;
01000         if ($this->_sCacheKey && !$this->_isDisabledFieldCache()) {
01001             $aFieldNames = $myUtils->fromFileCache($sFullCacheKey);
01002         }
01003 
01004         if (!$aFieldNames) {
01005             $aFieldNames = $this->_getNonCachedFieldNames($blForceFullStructure);
01006             if ($this->_sCacheKey && !$this->_isDisabledFieldCache()) {
01007                 $myUtils->toFileCache($sFullCacheKey, $aFieldNames);
01008             }
01009         }
01010 
01011         if ( $aFieldNames !== false ) {
01012             foreach ( $aFieldNames as $sField => $sStatus ) {
01013                 $this->_addField($sField, $sStatus);
01014             }
01015         }
01016     }
01017 
01029     protected function _getNonCachedFieldNames($blForceFullStructure = false)
01030     {
01031         //T2008-02-22
01032         //so if this method is executed on cached version we see it when profiling
01033         startProfile("!__CACHABLE__!");
01034 
01035         //case 1. (admin)
01036         if ($this->isAdmin()) {
01037             $aMetaFields = $this->_getAllFields();
01038             foreach ( $aMetaFields as $oField ) {
01039                 if ( $oField->max_length == -1 ) {
01040                     $oField->max_length = 10;      // double or float
01041                 }
01042 
01043                 if ( $oField->type == "datetime" ) {
01044                     $oField->max_length = 20;
01045                 }
01046 
01047                 $this->_addField( $oField->name, $this->_getFieldStatus($oField->name), $oField->type, $oField->max_length );
01048             }
01049             stopProfile("!__CACHABLE__!");
01050             return false;
01051         }
01052 
01053         //case 2. (just get all fields)
01054         if ( $blForceFullStructure || !$this->_blUseLazyLoading ) {
01055             $aMetaFields = $this->_getAllFields(true);
01056             /*
01057             foreach ( $aMetaFields as $sFieldName => $sVal) {
01058                 $this->_addField( $sFieldName, $this->_getFieldStatus($sFieldName));
01059             }*/
01060             stopProfile("!__CACHABLE__!");
01061             return $aMetaFields;
01062         }
01063 
01064         //case 3. (get only oxid field, so we can fetch the rest of the fields over lazy loading mechanism)
01065         stopProfile("!__CACHABLE__!");
01066         return array("oxid" => 0);
01067     }
01068 
01077     protected function _getFieldStatus($sFieldName)
01078     {
01079         return 0;
01080     }
01081 
01092     protected function _addField($sName, $iStatus, $sType = null, $sLength = null)
01093     {
01094         //preparation
01095         $sName = strtolower($sName);
01096 
01097         //TODO: remove this
01098         //this is left only for debug as this should never happen
01099         if ($sName == "oxnid") {
01100             throw new Exception("oxnid added");
01101         }
01102 
01103         //TODO: remove this
01104         //this is left only for debug as this should never happen
01105         if (!is_int($iStatus )) {
01106             throw new Exception('Non int status!');
01107         }
01108 
01109         //adding field names element
01110         $this->_aFieldNames[$sName] = $iStatus;
01111 
01112         //allready set?
01113         $sLongName = $this->_getFieldLongName($sName);
01114         if ( isset($this->$sLongName) ) {
01115             return;
01116         }
01117 
01118         //defining the field
01119         $oField = false;
01120 
01121         if (isset($sType)) {
01122             $oField = new oxField();
01123             $oField->fldtype = $sType;
01124             //T2008-01-29
01125             //can't clone as the fields are objects and are not fully cloned
01126             $this->_blIsSimplyClonable = false;
01127         }
01128 
01129         if (isset($sLength)) {
01130             if (!$oField) {
01131                 $oField = new oxField();
01132             }
01133             $oField->fldmax_length = $sLength;
01134             $this->_blIsSimplyClonable = false;
01135         }
01136 
01137         $this->$sLongName = $oField;
01138     }
01139 
01147     protected function _getFieldLongName( $sFieldName)
01148     {
01149         //trying to avoid strpos call as often as possible
01150         if ($sFieldName[2] == $this->_sCoreTable[2] && strpos($sFieldName, $this->_sCoreTable."__") === 0) {
01151             return $sFieldName;
01152         }
01153 
01154         $sLongName = $this->_sCoreTable."__".strtolower( $sFieldName);
01155         return $sLongName;
01156     }
01157 
01167     protected function _setFieldData( $sFieldName, $sValue, $iDataType = oxField::T_TEXT)
01168     {
01169 
01170 
01171         $sLongFieldName = $this->_getFieldLongName( $sFieldName);
01172         //$sLongFieldName = $this->_sCoreTable . "__" . strtolower($sFieldName);
01173 
01174         //T2008-03-14
01175         //doing this because in lazy loaded lists on first load it is harmful to have initilised fields but not yet set
01176         //situation: only first article is loaded fully for "select oxid from oxarticles"
01177         /*
01178         if ($this->_blUseLazyLoading && !isset($this->$sLongFieldName))
01179             return;*/
01180 
01181         //in non lazy loading case we just add a field and do not care about it more
01182         if (!$this->_blUseLazyLoading && !isset($this->$sLongFieldName)) {
01183             $aFields = $this->_getAllFields(true);
01184             if ( isset( $aFields[strtolower($sFieldName)] ) ) {
01185                 $this->_addField($sFieldName, $this->_getFieldStatus($sFieldName));
01186             }
01187         }
01188         // if we have a double field we replace "," with "." in case somebody enters it in european format
01189         if (isset($this->$sLongFieldName) && isset($this->$sLongFieldName->fldtype) && $this->$sLongFieldName->fldtype == "double") {
01190             $sValue = str_replace( ",", ".", $sValue );
01191         }
01192 
01193         // isset is REQUIRED here not to use getter
01194         if (isset($this->$sLongFieldName) && is_object($this->$sLongFieldName)) {
01195             $this->$sLongFieldName->setValue($sValue, $iDataType);
01196         } else {
01197             $this->$sLongFieldName = new oxField($sValue, $iDataType);
01198         }
01199 
01200     }
01201 
01210     protected function _getUpdateFieldValue($sFieldName, $oField)
01211     {
01212         $blPassNullValue = false;
01213         if ($oField instanceof oxField) {
01214             $value = $oField->getRawValue();
01215         } else {
01216             $value = $oField->value;
01217         }
01218         // R. check if this field value is null AND it can be null according to table description
01219         if (null === $value) {
01220             $aMetaData = $this->_getAllFields();
01221             foreach ($aMetaData as $oMetaInfo) {
01222                 if (!strcasecmp($oMetaInfo->name, $sFieldName)) {
01223                     $blPassNullValue = !$oMetaInfo->not_null;
01224                     break;
01225                 }
01226             }
01227         }
01228         if ($blPassNullValue) {
01229             return 'null';
01230         } else {
01231             return oxDb::getDb()->quote( $value );
01232         }
01233     }
01234 
01243     protected function _getUpdateFields( $blUseSkipSaveFields = true )
01244     {
01245         $sSql = '';
01246         $blSep  = false;
01247         foreach (array_keys($this->_aFieldNames) as $sKey) {
01248             $sLongName = $this->_getFieldLongName($sKey);
01249             $oField = $this->$sLongName;
01250 
01251 
01252             if ( !$blUseSkipSaveFields || ($blUseSkipSaveFields && !in_array(strtolower($sKey), $this->_aSkipSaveFields)) ) {
01253                 $sKey = $this->getSqlFieldName($sKey);
01254                 $sSql .= (( $blSep) ? ',':'' ).$sKey." = ".$this->_getUpdateFieldValue($sKey, $oField);
01255                 $blSep = true;
01256             }
01257         }
01258 
01259         return $sSql;
01260     }
01261 
01271     protected function _update()
01272     {
01273         //do not allow derived item update
01274         if ( !$this->allowDerivedUpdate() ) {
01275             return false;
01276         }
01277 
01278 
01279         if ( !$this->getId() ) {
01280             $oEx = oxNew( 'oxObjectException' );
01281             $oEx->setMessage( 'EXCEPTION_OBJECT_OXIDNOTSET' );
01282             $oEx->setObject($this);
01283             throw $oEx;
01284         }
01285 
01286         $sIDKey = oxUtils::getInstance()->getArrFldName( $this->_sCoreTable.".oxid");
01287         $this->$sIDKey = new oxField($this->getId(), oxField::T_RAW);
01288 
01289         $sUpdate= "update {$this->_sCoreTable} set ".$this->_getUpdateFields()
01290                  ." where {$this->_sCoreTable}.oxid = '".$this->getId()."' ";
01291 
01292         //echo         $sUpdate."\n\n\n";
01293 
01294         //trigger event
01295         $this->beforeUpdate();
01296 
01297         $blRet = (bool) oxDB::getDb()->execute( $sUpdate);
01298         $this->_rebuildCache();
01299 
01300         return $blRet;
01301     }
01302 
01310     protected function _insert()
01311     {
01312 
01313         $oDB      = oxDb::getDb(true);
01314         $myConfig = $this->getConfig();
01315         $myUtils  = oxUtils::getInstance();
01316 
01317         // let's get a new ID
01318         if ( !$this->getId()) {
01319             $this->setId();
01320         }
01321 
01322         $sIDKey = $myUtils->getArrFldName( $this->_sCoreTable.".oxid");
01323         $this->$sIDKey = new oxField($this->getId(), oxField::T_RAW);
01324         $sInsert= "Insert into {$this->_sCoreTable} set ";
01325 
01326         //setting oxshopid
01327         $sShopField = $myUtils->getArrFldName($this->_sCoreTable.".oxshopid");
01328         if (isset($this->$sShopField) && !$this->$sShopField->value)
01329             $this->$sShopField = new oxField($myConfig->getShopId(), oxField::T_RAW);
01330         $sInsert .= $this->_getUpdateFields( false );
01331 
01332         $blRet = (bool) $oDB->execute( $sInsert);
01333 
01334         $this->_rebuildCache();
01335 
01336         return $blRet;
01337 
01338     }
01339 
01345     protected function _rebuildCache()
01346     {
01347         if ( !$this->_blIsNewCache) {
01348             oxUtils::getInstance()->rebuildCache();
01349             $this->_blIsNewCache = true;
01350         }
01351     }
01352 
01362     protected function _setRecordNumber( $sMaxField, $aWhere = null, $iMaxTryCnt = 5 )
01363     {
01364         // filtering
01365         $sWhere = "";
01366         if ( is_array( $aWhere ) && count( $aWhere ) > 0) {
01367             $sWhere = implode(" and ", $aWhere).' and ';
01368         }
01369 
01370         // SQL to set record number
01371         $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()."'";
01372 
01373         // SQL to check record number dublicates
01374         //this should not happen normally but we prefer to take extra care in this case due to parallel script execution etc.
01375         $sMaxSelect = "select $sMaxField from ".$this->getViewName()." where oxid='".$this->getId()."' ";
01376         $sCheck = "select count(oxid) from ".$this->getViewName()." where $sMaxField = ($sMaxSelect) and $sWhere 1 ";
01377 
01378         do {
01379             if ( oxDb::getDb(true)->Execute( $sUpdate ) === false ) {
01380                 return false;
01381             }
01382 
01383             $iChkCnt = oxDb::getDb(true)->GetOne( $sCheck );
01384         } while ( ( $iChkCnt > 1 ) && $iMaxTryCnt-- );
01385 
01386         $sFieldName = $this->getViewName().'__'.$sMaxField;
01387         $this->$sFieldName = new oxField(oxDb::getDb(true)->GetOne( $sMaxSelect ), oxField::T_RAW);//int value
01388 
01389         return ( $iChkCnt == 1 );
01390     }
01391 
01398     protected function _isDisabledFieldCache()
01399     {
01400         $sClass = get_class($this);
01401         if (isset(self::$_blDisableFieldCaching[$sClass]) && self::$_blDisableFieldCaching[$sClass]) {
01402             return true;
01403         }
01404 
01405         return false;
01406     }
01407 
01413     public function isOx()
01414     {
01415         $sOxId = $this->getId();
01416         if ( $sOxId[0] == 'o' && $sOxId[1] == 'x' ) {
01417             return true;
01418         }
01419         return false;
01420     }
01421 
01427     public function isReadOnly()
01428     {
01429         return $this->_blReadOnly;
01430     }
01431 
01439     public function setReadOnly( $blReadOnly )
01440     {
01441         $this->_blReadOnly = $blReadOnly;
01442     }
01443 
01444 }

Generated on Tue Aug 18 09:21:05 2009 for OXID eShop CE by  doxygen 1.5.5